Chromium Code Reviews| Index: remoting/webapp/crd/js/clipboard.js |
| diff --git a/remoting/webapp/crd/js/clipboard.js b/remoting/webapp/crd/js/clipboard.js |
| index 0d116779e34f6ea19489c4e0e6e6b6ca863f73bc..a12daaf5f428cea0d10353d3c2ff2904605198d1 100644 |
| --- a/remoting/webapp/crd/js/clipboard.js |
| +++ b/remoting/webapp/crd/js/clipboard.js |
| @@ -1,4 +1,4 @@ |
| -// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| @@ -7,52 +7,60 @@ |
| * A class for moving clipboard items between the plugin and the OS. |
| */ |
| -'use strict'; |
| - |
| /** @suppress {duplicate} */ |
| var remoting = remoting || {}; |
| -/** |
| - * @constructor |
| - */ |
| -remoting.Clipboard = function() { |
| -}; |
| +(function() { |
| + |
| +'use strict'; |
| /** |
| * @private |
| * @enum {string} |
| */ |
| -remoting.Clipboard.prototype.ItemTypes = { |
| +var ItemTypes = { |
| TEXT_TYPE: 'text/plain', |
| TEXT_UTF8_TYPE: 'text/plain; charset=UTF-8' |
| }; |
| -/** @private {string} */ |
| -remoting.Clipboard.prototype.previousContent = ""; |
| +/** |
| + * @constructor |
| + * @param {remoting.ClientPlugin} plugin |
| + * @implements {base.Disposable} |
| + */ |
| +remoting.Clipboard = function(plugin) { |
| + /** @private {string} */ |
| + this.previousContent_ = ''; |
| -/** @private {boolean} */ |
| -remoting.Clipboard.prototype.itemFromHostTextPending = false; |
| + /** @private {boolean} */ |
| + this.itemFromHostTextPending_ = false; |
| -/** @private {boolean} */ |
| -remoting.Clipboard.prototype.blockOneClipboardSend_ = false; |
| + /** @private {boolean} */ |
| + this.blockOneClipboardSend_ = true; |
| -/** |
| - * Notifies this object that a session has started. |
| - * |
| - * @return {void} Nothing. |
| - */ |
| -remoting.Clipboard.prototype.startSession = function() { |
| - // Clear the store of items sent and received. Those items now relate to a |
| - // previous session. |
| - this.previousContent = ""; |
| - this.itemFromHostTextPending = false; |
| + /** @private */ |
| + this.plugin_ = plugin; |
| + |
| + /** @private */ |
| + this.eventHooks_ = new base.Disposables( |
| + new base.DomEventHook(plugin.element(), 'focus', |
|
kelvinp
2015/04/09 00:28:26
moved from connectedView.js
|
| + this.initiateToHost_.bind(this), false), |
| + new base.DomEventHook(window, 'paste', this.onPaste_.bind(this), false), |
|
kelvinp
2015/04/09 00:28:26
moved from remoting.js
|
| + new base.DomEventHook(window, 'copy', this.onCopy_.bind(this), false)); |
| // Do a paste operation, but make sure the resulting clipboard data isn't sent |
| // to the host. This stops the host seeing items that were placed on the |
| // clipboard before the session began. The user may not have intended such |
| // items to be sent to the host. |
| - this.blockOneClipboardSend_ = true; |
| - this.initiateToHost(); |
| + this.initiateToHost_(); |
| + this.plugin_.setClipboardHandler(this.fromHost_.bind(this)); |
| +}; |
| + |
| +remoting.Clipboard.prototype.dispose = function() { |
| + this.plugin_.setClipboardHandler(base.doNothing); |
| + this.plugin_ = null; |
| + base.dispose(this.eventHooks_); |
| + this.eventHooks_ = null; |
| }; |
| /** |
| @@ -63,37 +71,35 @@ remoting.Clipboard.prototype.startSession = function() { |
| * |
| * @param {ClipboardData} clipboardData |
| * @return {void} Nothing. |
| + * @private |
| */ |
| -remoting.Clipboard.prototype.toHost = function(clipboardData) { |
| +remoting.Clipboard.prototype.toHost_ = function(clipboardData) { |
| if (!clipboardData || !clipboardData.types || !clipboardData.getData) { |
| console.log('Got invalid clipboardData.'); |
| return; |
| } |
| - if (!remoting.clientSession) { |
| - return; |
| - } |
| for (var i = 0; i < clipboardData.types.length; i++) { |
| var type = clipboardData.types[i]; |
| var item = clipboardData.getData(type); |
| if (!item) { |
| - item = ""; |
| + item = ''; |
| } |
| console.log('Got clipboard from OS, type: ' + type + |
| ' length: ' + item.length + ' new: ' + |
| - (item != this.previousContent) + ' blocking-send: ' + |
| + (item != this.previousContent_) + ' blocking-send: ' + |
| this.blockOneClipboardSend_); |
| // The browser presents text clipboard items as 'text/plain'. |
| - if (type == this.ItemTypes.TEXT_TYPE) { |
| + if (type == ItemTypes.TEXT_TYPE) { |
| // Don't send the same item more than once. Otherwise the item may be |
| // sent to and fro indefinitely. |
| - if (item != this.previousContent) { |
| + if (item != this.previousContent_) { |
| if (!this.blockOneClipboardSend_) { |
| // The plugin's JSON reader emits UTF-8. |
| console.log('Sending clipboard to host.'); |
| remoting.clientSession.sendClipboardItem( |
| - this.ItemTypes.TEXT_UTF8_TYPE, item); |
| + ItemTypes.TEXT_UTF8_TYPE, item); |
| } |
| - this.previousContent = item; |
| + this.previousContent_ = item; |
| } |
| } |
| } |
| @@ -108,21 +114,21 @@ remoting.Clipboard.prototype.toHost = function(clipboardData) { |
| * @param {string} item The clipboard item. |
| * @return {void} Nothing. |
| */ |
| -remoting.Clipboard.prototype.fromHost = function(mimeType, item) { |
| +remoting.Clipboard.prototype.fromHost_ = function(mimeType, item) { |
| // The plugin's JSON layer will correctly convert only UTF-8 data sent from |
| // the host. |
| console.log('Got clipboard from host, type: ' + mimeType + |
| ' length: ' + item.length + ' new: ' + |
| - (item != this.previousContent)); |
| - if (mimeType != this.ItemTypes.TEXT_UTF8_TYPE) { |
| + (item != this.previousContent_)); |
| + if (mimeType != ItemTypes.TEXT_UTF8_TYPE) { |
| return; |
| } |
| - if (item == this.previousContent) { |
| + if (item == this.previousContent_) { |
| return; |
| } |
| - this.previousContent = item; |
| - this.itemFromHostTextPending = true; |
| - this.initiateToOs(); |
| + this.previousContent_ = item; |
| + this.itemFromHostTextPending_ = true; |
| + this.initiateToOs_(); |
| }; |
| /** |
| @@ -131,9 +137,10 @@ remoting.Clipboard.prototype.fromHost = function(mimeType, item) { |
| * @param {ClipboardData} clipboardData |
| * @return {boolean} Whether any clipboard items were moved to the ClipboardData |
| * object. |
| + * @private |
| */ |
| -remoting.Clipboard.prototype.toOs = function(clipboardData) { |
| - if (!this.itemFromHostTextPending) { |
| +remoting.Clipboard.prototype.toOs_ = function(clipboardData) { |
| + if (!this.itemFromHostTextPending_) { |
| console.log('Got unexpected clipboard copy event.'); |
| return false; |
| } |
| @@ -141,9 +148,9 @@ remoting.Clipboard.prototype.toOs = function(clipboardData) { |
| // JS string encoding. The browser will convert JS strings to the correct |
| // encoding, per OS and locale conventions, provided the data type is |
| // 'text/plain'. |
| - console.log('Setting OS clipboard, length: ' + this.previousContent.length); |
| - clipboardData.setData(this.ItemTypes.TEXT_TYPE, this.previousContent); |
| - this.itemFromHostTextPending = false; |
| + console.log('Setting OS clipboard, length: ' + this.previousContent_.length); |
| + clipboardData.setData(ItemTypes.TEXT_TYPE, this.previousContent_); |
| + this.itemFromHostTextPending_ = false; |
| return true; |
| }; |
| @@ -154,12 +161,13 @@ remoting.Clipboard.prototype.toOs = function(clipboardData) { |
| * This method makes the browser fire a paste event, which provides access to |
| * the OS clipboard. That event will be caught by a handler in the document, |
| * which will call toHost(). |
| + * @private |
| */ |
| -remoting.Clipboard.prototype.initiateToHost = function() { |
| +remoting.Clipboard.prototype.initiateToHost_ = function() { |
| // It would be cleaner to send a paste command to the plugin element, |
| // but that's not supported. |
| //console.log('Initiating clipboard paste.'); |
| - document.execCommand("paste"); |
| + document.execCommand('paste'); |
| }; |
| /** |
| @@ -169,13 +177,40 @@ remoting.Clipboard.prototype.initiateToHost = function() { |
| * This method makes the browser fire a copy event, which provides access to |
| * the OS clipboard. That event will be caught by a handler in the document, |
| * which will call toOs(). |
| + * @private |
| */ |
| -remoting.Clipboard.prototype.initiateToOs = function() { |
| +remoting.Clipboard.prototype.initiateToOs_ = function() { |
| // It would be cleaner to send a paste command to the plugin element, |
| // but that's not supported. |
| console.log('Initiating clipboard copy.'); |
| - document.execCommand("copy"); |
| + document.execCommand('copy'); |
| +}; |
| + |
| +/** |
| + * Callback function called when the browser window gets a paste operation. |
| + * |
| + * @param {Event} event |
| + * @return {void} Nothing. |
| + * @private |
| + */ |
| +remoting.Clipboard.prototype.onPaste_ = function(event) { |
| + if (event && event.clipboardData) { |
| + this.toHost_(event.clipboardData); |
| + } |
| +}; |
| + |
| +/** |
| + * Callback function called when the browser window gets a copy operation. |
| + * |
| + * @param {Event} event |
| + * @return {void} Nothing. |
| + * @private |
| + */ |
| +remoting.Clipboard.prototype.onCopy_ = function(event) { |
| + if (event && event.clipboardData && this.toOs_(event.clipboardData)) { |
| + // The default action may overwrite items that we added to clipboardData. |
| + event.preventDefault(); |
| + } |
| }; |
| -/** @type {remoting.Clipboard} */ |
| -remoting.clipboard = null; |
| +})(); |