1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 * A class for moving clipboard items between the plugin and the OS.
12 /** @suppress {duplicate} */
13 var remoting = remoting || {};
18 remoting.Clipboard = function() {
25 remoting.Clipboard.prototype.ItemTypes = {
26 TEXT_TYPE: 'text/plain',
27 TEXT_UTF8_TYPE: 'text/plain; charset=UTF-8'
34 remoting.Clipboard.prototype.previousContent = "";
40 remoting.Clipboard.prototype.itemFromHostTextPending = false;
46 remoting.Clipboard.prototype.blockOneClipboardSend_ = false;
49 * Notifies this object that a session has started.
51 * @return {void} Nothing.
53 remoting.Clipboard.prototype.startSession = function() {
54 // Clear the store of items sent and received. Those items now relate to a
56 this.previousContent = "";
57 this.itemFromHostTextPending = false;
59 // Do a paste operation, but make sure the resulting clipboard data isn't sent
60 // to the host. This stops the host seeing items that were placed on the
61 // clipboard before the session began. The user may not have intended such
62 // items to be sent to the host.
63 this.blockOneClipboardSend_ = true;
64 this.initiateToHost();
68 * Accepts a clipboard from the OS, and sends any changed clipboard items to
71 * Currently only text items are supported.
73 * @param {remoting.ClipboardData} clipboardData
74 * @return {void} Nothing.
76 remoting.Clipboard.prototype.toHost = function(clipboardData) {
77 if (!clipboardData || !clipboardData.types || !clipboardData.getData) {
80 if (!remoting.clientSession || !remoting.clientSession.plugin) {
83 var plugin = remoting.clientSession.plugin;
84 for (var i = 0; i < clipboardData.types.length; i++) {
85 var type = clipboardData.types[i];
86 // The browser presents text clipboard items as 'text/plain'.
87 if (type == this.ItemTypes.TEXT_TYPE) {
88 var item = clipboardData.getData(type);
92 // Don't send the same item more than once. Otherwise the item may be
93 // sent to and fro indefinitely.
94 if (item != this.previousContent) {
95 if (!this.blockOneClipboardSend_) {
96 // The plugin's JSON reader emits UTF-8.
97 plugin.sendClipboardItem(this.ItemTypes.TEXT_UTF8_TYPE, item);
99 this.previousContent = item;
103 this.blockOneClipboardSend_ = false;
107 * Accepts a clipboard item from the host, and stores it so that toOs() will
108 * subsequently send it to the OS clipboard.
110 * @param {string} mimeType The MIME type of the clipboard item.
111 * @param {string} item The clipboard item.
112 * @return {void} Nothing.
114 remoting.Clipboard.prototype.fromHost = function(mimeType, item) {
115 // The plugin's JSON layer will correctly convert only UTF-8 data sent from
117 if (mimeType != this.ItemTypes.TEXT_UTF8_TYPE) {
120 if (item == this.previousContent) {
123 this.previousContent = item;
124 this.itemFromHostTextPending = true;
129 * Moves any pending clipboard items to a ClipboardData object.
131 * @param {remoting.ClipboardData} clipboardData
132 * @return {boolean} Whether any clipboard items were moved to the ClipboardData
135 remoting.Clipboard.prototype.toOs = function(clipboardData) {
136 if (!this.itemFromHostTextPending) {
139 // The JSON layer between the plugin and this webapp converts UTF-8 to the
140 // JS string encoding. The browser will convert JS strings to the correct
141 // encoding, per OS and locale conventions, provided the data type is
143 clipboardData.setData(this.ItemTypes.TEXT_TYPE, this.previousContent);
144 this.itemFromHostTextPending = false;
149 * Initiates the process of sending any fresh items on the OS clipboard, to the
152 * This method makes the browser fire a paste event, which provides access to
153 * the OS clipboard. That event will be caught by a handler in the document,
154 * which will call toHost().
156 remoting.Clipboard.prototype.initiateToHost = function() {
157 // It would be cleaner to send a paste command to the plugin element,
158 // but that's not supported.
159 document.execCommand("paste");
163 * Initiates the process of sending any items freshly received from the host,
164 * to the OS clipboard.
166 * This method makes the browser fire a copy event, which provides access to
167 * the OS clipboard. That event will be caught by a handler in the document,
168 * which will call toOs().
170 remoting.Clipboard.prototype.initiateToOs = function() {
171 // It would be cleaner to send a paste command to the plugin element,
172 // but that's not supported.
173 document.execCommand("copy");
176 /** @type {remoting.Clipboard} */
177 remoting.clipboard = null;