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.itemToHostText
= "";
40 remoting
.Clipboard
.prototype.itemFromHostText
= "";
46 remoting
.Clipboard
.prototype.itemFromHostTextPending
= false;
52 remoting
.Clipboard
.prototype.blockOneClipboardSend_
= false;
55 * Notifies this object that a session has started.
57 * @return {void} Nothing.
59 remoting
.Clipboard
.prototype.startSession = function() {
60 // Clear the store of items sent and received. Those items now relate to a
62 this.itemToHostText
= "";
63 this.itemFromHostText
= "";
64 this.itemFromHostTextPending
= false;
66 // Do a paste operation, but make sure the resulting clipboard data isn't sent
67 // to the host. This stops the host seeing items that were placed on the
68 // clipboard before the session began. The user may not have intended such
69 // items to be sent to the host.
70 this.blockOneClipboardSend_
= true;
71 this.initiateToHost();
75 * Accepts a clipboard from the OS, and sends any changed clipboard items to
78 * Currently only text items are supported.
80 * @param {remoting.ClipboardData} clipboardData
81 * @return {void} Nothing.
83 remoting
.Clipboard
.prototype.toHost = function(clipboardData
) {
84 if (!clipboardData
|| !clipboardData
.types
|| !clipboardData
.getData
) {
87 if (!remoting
.clientSession
|| !remoting
.clientSession
.plugin
) {
90 var plugin
= remoting
.clientSession
.plugin
;
91 for (var i
= 0; i
< clipboardData
.types
.length
; i
++) {
92 var type
= clipboardData
.types
[i
];
93 // The browser presents text clipboard items as 'text/plain'.
94 if (type
== this.ItemTypes
.TEXT_TYPE
) {
95 var item
= clipboardData
.getData(type
);
99 // Don't send an item that's already been sent to the host.
100 // And don't send an item that we received from the host: if we do, we
101 // may send the same item to and fro indefinitely.
102 if ((item
!= this.itemToHostText
) && (item
!= this.itemFromHostText
)) {
103 if (!this.blockOneClipboardSend_
) {
104 // The plugin's JSON reader emits UTF-8.
105 plugin
.sendClipboardItem(this.ItemTypes
.TEXT_UTF8_TYPE
, item
);
107 this.itemToHostText
= item
;
111 this.blockOneClipboardSend_
= false;
115 * Accepts a clipboard item from the host, and stores it so that toOs() will
116 * subsequently send it to the OS clipboard.
118 * @param {string} mimeType The MIME type of the clipboard item.
119 * @param {string} item The clipboard item.
120 * @return {void} Nothing.
122 remoting
.Clipboard
.prototype.fromHost = function(mimeType
, item
) {
123 // The plugin's JSON layer will correctly convert only UTF-8 data sent from
125 if (mimeType
!= this.ItemTypes
.TEXT_UTF8_TYPE
) {
128 if (item
== this.itemToHostText
) {
131 this.itemFromHostText
= item
;
132 this.itemFromHostTextPending
= true;
137 * Moves any pending clipboard items to a ClipboardData object.
139 * @param {remoting.ClipboardData} clipboardData
140 * @return {boolean} Whether any clipboard items were moved to the ClipboardData
143 remoting
.Clipboard
.prototype.toOs = function(clipboardData
) {
144 if (!this.itemFromHostTextPending
) {
147 // The JSON layer between the plugin and this webapp converts UTF-8 to the
148 // JS string encoding. The browser will convert JS strings to the correct
149 // encoding, per OS and locale conventions, provided the data type is
151 clipboardData
.setData(this.ItemTypes
.TEXT_TYPE
, this.itemFromHostText
);
152 this.itemFromHostTextPending
= false;
157 * Initiates the process of sending any fresh items on the OS clipboard, to the
160 * This method makes the browser fire a paste event, which provides access to
161 * the OS clipboard. That event will be caught by a handler in the document,
162 * which will call toHost().
164 remoting
.Clipboard
.prototype.initiateToHost = function() {
165 // It would be cleaner to send a paste command to the plugin element,
166 // but that's not supported.
167 document
.execCommand("paste");
171 * Initiates the process of sending any items freshly received from the host,
172 * to the OS clipboard.
174 * This method makes the browser fire a copy event, which provides access to
175 * the OS clipboard. That event will be caught by a handler in the document,
176 * which will call toOs().
178 remoting
.Clipboard
.prototype.initiateToOs = function() {
179 // It would be cleaner to send a paste command to the plugin element,
180 // but that's not supported.
181 document
.execCommand("copy");
184 /** @type {remoting.Clipboard} */
185 remoting
.clipboard
= null;