| 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 |
| 16 /* Beginning of anonymous object. */ | 21 /* Beginning of anonymous object. */ |
| 17 (function() { | 22 (function() { |
| 18 /** | 23 /** |
| 19 * Object to manage queue of messages waiting to be sent to the main | 24 * Object to manage queue of messages waiting to be sent to the main |
| 20 * application for asynchronous processing. | 25 * application for asynchronous processing. |
| 21 * @type {Object} | 26 * @type {Object} |
| 22 * @private | 27 * @private |
| 23 */ | 28 */ |
| 24 var messageQueue_ = { | 29 var messageQueue_ = { |
| 25 scheme: 'crwebinvoke', | 30 scheme: 'crwebinvoke', |
| 26 reset: function() { | 31 reset: function() { |
| 27 messageQueue_.queue = []; | 32 messageQueue_.queue = []; |
| 28 // Since the array will be JSON serialized, protect against non-standard | 33 // Since the array will be JSON serialized, protect against non-standard |
| 29 // custom versions of Array.prototype.toJSON. | 34 // custom versions of Array.prototype.toJSON. |
| 30 delete messageQueue_.queue.toJSON; | 35 delete messageQueue_.queue.toJSON; |
| 31 } | 36 } |
| 32 }; | 37 }; |
| 33 messageQueue_.reset(); | 38 messageQueue_.reset(); |
| 34 | 39 |
| 35 /** | 40 /** |
| 36 * Invokes a command on the Objective-C side. | 41 * Invokes a command on the Objective-C side. |
| 37 * @param {Object} command The command in a JavaScript object. | 42 * @param {Object} command The command in a JavaScript object. |
| 38 * @private | 43 * @public |
| 39 */ | 44 */ |
| 40 __gCrWeb.message.invokeOnHost = function(command) { | 45 __gCrWeb.message.invokeOnHost = function(command) { |
| 41 messageQueue_.queue.push(command); | 46 messageQueue_.queue.push(command); |
| 42 sendQueue_(messageQueue_); | 47 sendQueue_(messageQueue_); |
| 43 }; | 48 }; |
| 44 | 49 |
| 45 /** | 50 /** |
| 46 * Returns the message queue as a string. | 51 * Returns the message queue as a string. |
| 47 * @return {string} The current message queue as a JSON string. | 52 * @return {string} The current message queue as a JSON string. |
| 48 */ | 53 */ |
| (...skipping 18 matching lines...) Expand all Loading... |
| 67 } | 72 } |
| 68 // Some pages/plugins implement Object.prototype.toJSON, which can result | 73 // Some pages/plugins implement Object.prototype.toJSON, which can result |
| 69 // in serializing messageQueue_ to an invalid format. | 74 // in serializing messageQueue_ to an invalid format. |
| 70 var originalObjectToJSON = Object.prototype.toJSON; | 75 var originalObjectToJSON = Object.prototype.toJSON; |
| 71 if (originalObjectToJSON) | 76 if (originalObjectToJSON) |
| 72 delete Object.prototype.toJSON; | 77 delete Object.prototype.toJSON; |
| 73 | 78 |
| 74 queueObject.queue.forEach(function(command) { | 79 queueObject.queue.forEach(function(command) { |
| 75 var stringifiedMessage = __gCrWeb.common.JSONStringify({ | 80 var stringifiedMessage = __gCrWeb.common.JSONStringify({ |
| 76 "crwCommand": command, | 81 "crwCommand": command, |
| 77 "crwWindowId": __gCrWeb.windowId | 82 "crwWindowId": __gCrWeb['windowId'] |
| 78 }); | 83 }); |
| 79 // A web page can override |window.webkit| with any value. Deleting the | 84 // A web page can override |window.webkit| with any value. Deleting the |
| 80 // object ensures that original and working implementation of | 85 // object ensures that original and working implementation of |
| 81 // window.webkit is restored. | 86 // window.webkit is restored. |
| 82 var oldWebkit = window.webkit; | 87 var oldWebkit = window.webkit; |
| 83 delete window['webkit']; | 88 delete window['webkit']; |
| 84 window.webkit.messageHandlers[queueObject.scheme].postMessage( | 89 window.webkit.messageHandlers[queueObject.scheme].postMessage( |
| 85 stringifiedMessage); | 90 stringifiedMessage); |
| 86 window.webkit = oldWebkit; | 91 window.webkit = oldWebkit; |
| 87 }); | 92 }); |
| 88 queueObject.reset(); | 93 queueObject.reset(); |
| 89 | 94 |
| 90 if (originalObjectToJSON) { | 95 if (originalObjectToJSON) { |
| 91 // Restore Object.prototype.toJSON to prevent from breaking any | 96 // Restore Object.prototype.toJSON to prevent from breaking any |
| 92 // functionality on the page that depends on its custom implementation. | 97 // functionality on the page that depends on its custom implementation. |
| 93 Object.prototype.toJSON = originalObjectToJSON; | 98 Object.prototype.toJSON = originalObjectToJSON; |
| 94 } | 99 } |
| 95 }; | 100 }; |
| 96 }()); | 101 }()); |
| OLD | NEW |