Revert 168224 - Update V8 to version 3.15.4.
[chromium-blink-merge.git] / remoting / webapp / clipboard.js
blob90759bd5bfd8e9ef881d01199f8a15c6a98a5df3
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.
5 /**
6 * @fileoverview
7 * A class for moving clipboard items between the plugin and the OS.
8 */
10 'use strict';
12 /** @suppress {duplicate} */
13 var remoting = remoting || {};
15 /**
16 * @constructor
18 remoting.Clipboard = function() {
21 /**
22 * @private
23 * @enum {string}
25 remoting.Clipboard.prototype.ItemTypes = {
26 TEXT_TYPE: 'text/plain',
27 TEXT_UTF8_TYPE: 'text/plain; charset=UTF-8'
30 /**
31 * @private
32 * @type {string}
34 remoting.Clipboard.prototype.itemToHostText = "";
36 /**
37 * @private
38 * @type {string}
40 remoting.Clipboard.prototype.itemFromHostText = "";
42 /**
43 * @private
44 * @type {boolean}
46 remoting.Clipboard.prototype.itemFromHostTextPending = false;
48 /**
49 * @private
50 * @type {boolean}
52 remoting.Clipboard.prototype.blockOneClipboardSend_ = false;
54 /**
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
61 // previous session.
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();
74 /**
75 * Accepts a clipboard from the OS, and sends any changed clipboard items to
76 * the host.
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) {
85 return;
87 if (!remoting.clientSession || !remoting.clientSession.plugin) {
88 return;
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);
96 if (!item) {
97 item = "";
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
124 // the host.
125 if (mimeType != this.ItemTypes.TEXT_UTF8_TYPE) {
126 return;
128 if (item == this.itemToHostText) {
129 return;
131 this.itemFromHostText = item;
132 this.itemFromHostTextPending = true;
133 this.initiateToOs();
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
141 * object.
143 remoting.Clipboard.prototype.toOs = function(clipboardData) {
144 if (!this.itemFromHostTextPending) {
145 return false;
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
150 // 'text/plain'.
151 clipboardData.setData(this.ItemTypes.TEXT_TYPE, this.itemFromHostText);
152 this.itemFromHostTextPending = false;
153 return true;
157 * Initiates the process of sending any fresh items on the OS clipboard, to the
158 * host.
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;