| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Scripts for the message handler. | 5 // Scripts for the message handler. |
| 6 | 6 |
| 7 goog.provide('__crWeb.message'); | 7 goog.provide('__crWeb.message'); |
| 8 | 8 |
| 9 goog.require('__crWeb.common'); | 9 goog.require('__crWeb.common'); |
| 10 | 10 |
| 11 /** | 11 /** |
| 12 * Namespace for this module. | 12 * Namespace for this module. |
| 13 */ | 13 */ |
| 14 __gCrWeb.message = {}; | 14 __gCrWeb.message = {}; |
| 15 | 15 |
| 16 // Store message namespace object in a global __gCrWeb object referenced by a | |
| 17 // string, so it does not get renamed by closure compiler during the | |
| 18 // minification. | |
| 19 __gCrWeb['message'] = __gCrWeb.message; | |
| 20 | |
| 21 /* Beginning of anonymous object. */ | 16 /* Beginning of anonymous object. */ |
| 22 (function() { | 17 (function() { |
| 23 /** | 18 /** |
| 24 * Object to manage queue of messages waiting to be sent to the main | 19 * Object to manage queue of messages waiting to be sent to the main |
| 25 * application for asynchronous processing. | 20 * application for asynchronous processing. |
| 26 * @type {Object} | 21 * @type {Object} |
| 27 * @private | 22 * @private |
| 28 */ | 23 */ |
| 29 var messageQueue_ = { | 24 var messageQueue_ = { |
| 30 scheme: 'crwebinvoke', | 25 scheme: 'crwebinvoke', |
| 31 reset: function() { | 26 reset: function() { |
| 32 messageQueue_.queue = []; | 27 messageQueue_.queue = []; |
| 33 // Since the array will be JSON serialized, protect against non-standard | 28 // Since the array will be JSON serialized, protect against non-standard |
| 34 // custom versions of Array.prototype.toJSON. | 29 // custom versions of Array.prototype.toJSON. |
| 35 delete messageQueue_.queue.toJSON; | 30 delete messageQueue_.queue.toJSON; |
| 36 } | 31 } |
| 37 }; | 32 }; |
| 38 messageQueue_.reset(); | 33 messageQueue_.reset(); |
| 39 | 34 |
| 40 /** | 35 /** |
| 41 * Invokes a command on the Objective-C side. | 36 * Invokes a command on the Objective-C side. |
| 42 * @param {Object} command The command in a JavaScript object. | 37 * @param {Object} command The command in a JavaScript object. |
| 43 * @public | 38 * @private |
| 44 */ | 39 */ |
| 45 __gCrWeb.message.invokeOnHost = function(command) { | 40 __gCrWeb.message.invokeOnHost = function(command) { |
| 46 messageQueue_.queue.push(command); | 41 messageQueue_.queue.push(command); |
| 47 sendQueue_(messageQueue_); | 42 sendQueue_(messageQueue_); |
| 48 }; | 43 }; |
| 49 | 44 |
| 50 /** | 45 /** |
| 51 * Returns the message queue as a string. | 46 * Returns the message queue as a string. |
| 52 * @return {string} The current message queue as a JSON string. | 47 * @return {string} The current message queue as a JSON string. |
| 53 */ | 48 */ |
| (...skipping 18 matching lines...) Expand all Loading... |
| 72 } | 67 } |
| 73 // Some pages/plugins implement Object.prototype.toJSON, which can result | 68 // Some pages/plugins implement Object.prototype.toJSON, which can result |
| 74 // in serializing messageQueue_ to an invalid format. | 69 // in serializing messageQueue_ to an invalid format. |
| 75 var originalObjectToJSON = Object.prototype.toJSON; | 70 var originalObjectToJSON = Object.prototype.toJSON; |
| 76 if (originalObjectToJSON) | 71 if (originalObjectToJSON) |
| 77 delete Object.prototype.toJSON; | 72 delete Object.prototype.toJSON; |
| 78 | 73 |
| 79 queueObject.queue.forEach(function(command) { | 74 queueObject.queue.forEach(function(command) { |
| 80 var stringifiedMessage = __gCrWeb.common.JSONStringify({ | 75 var stringifiedMessage = __gCrWeb.common.JSONStringify({ |
| 81 "crwCommand": command, | 76 "crwCommand": command, |
| 82 "crwWindowId": __gCrWeb['windowId'] | 77 "crwWindowId": __gCrWeb.windowId |
| 83 }); | 78 }); |
| 84 // A web page can override |window.webkit| with any value. Deleting the | 79 // A web page can override |window.webkit| with any value. Deleting the |
| 85 // object ensures that original and working implementation of | 80 // object ensures that original and working implementation of |
| 86 // window.webkit is restored. | 81 // window.webkit is restored. |
| 87 var oldWebkit = window.webkit; | 82 var oldWebkit = window.webkit; |
| 88 delete window['webkit']; | 83 delete window['webkit']; |
| 89 window.webkit.messageHandlers[queueObject.scheme].postMessage( | 84 window.webkit.messageHandlers[queueObject.scheme].postMessage( |
| 90 stringifiedMessage); | 85 stringifiedMessage); |
| 91 window.webkit = oldWebkit; | 86 window.webkit = oldWebkit; |
| 92 }); | 87 }); |
| 93 queueObject.reset(); | 88 queueObject.reset(); |
| 94 | 89 |
| 95 if (originalObjectToJSON) { | 90 if (originalObjectToJSON) { |
| 96 // Restore Object.prototype.toJSON to prevent from breaking any | 91 // Restore Object.prototype.toJSON to prevent from breaking any |
| 97 // functionality on the page that depends on its custom implementation. | 92 // functionality on the page that depends on its custom implementation. |
| 98 Object.prototype.toJSON = originalObjectToJSON; | 93 Object.prototype.toJSON = originalObjectToJSON; |
| 99 } | 94 } |
| 100 }; | 95 }; |
| 101 }()); | 96 }()); |
| OLD | NEW |