Chromium Code Reviews| 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 /* Beginning of anonymous object. */ | 16 /* Beginning of anonymous object. */ |
| 17 (function() { | 17 (function() { |
| 18 /** | 18 /** |
| 19 * 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 |
| 20 * application for immediate processing. | |
| 21 * @type {Object} | |
| 22 * @private | |
| 23 */ | |
| 24 var immediateMessageQueue_ = { | |
| 25 scheme: 'crwebinvokeimmediate', | |
| 26 reset: function() { | |
| 27 immediateMessageQueue_.queue = []; | |
| 28 // Since the array will be JSON serialized, protect against non-standard | |
| 29 // custom versions of Array.prototype.toJSON. | |
| 30 immediateMessageQueue_.queue.toJSON = null; | |
| 31 } | |
| 32 }; | |
| 33 immediateMessageQueue_.reset(); | |
| 34 | |
| 35 /** | |
| 36 * Object to manage queue of messages waiting to be sent to the main | |
| 37 * application for asynchronous processing. | 20 * application for asynchronous processing. |
| 38 * @type {Object} | 21 * @type {Object} |
| 39 * @private | 22 * @private |
| 40 */ | 23 */ |
| 41 var messageQueue_ = { | 24 var messageQueue_ = { |
| 42 scheme: 'crwebinvoke', | 25 scheme: 'crwebinvoke', |
| 43 reset: function() { | 26 reset: function() { |
| 44 messageQueue_.queue = []; | 27 messageQueue_.queue = []; |
| 45 // Since the array will be JSON serialized, protect against non-standard | 28 // Since the array will be JSON serialized, protect against non-standard |
| 46 // custom versions of Array.prototype.toJSON. | 29 // custom versions of Array.prototype.toJSON. |
| 47 messageQueue_.queue.toJSON = null | 30 messageQueue_.queue.toJSON = null |
| 48 } | 31 } |
| 49 }; | 32 }; |
| 50 messageQueue_.reset(); | 33 messageQueue_.reset(); |
| 51 | 34 |
| 52 /** | 35 /** |
| 53 * Invokes a command immediately on the Objective-C side. | |
| 54 * An immediate command is a special class of command that must be handled at | |
| 55 * the earliest opportunity. See the notes in CRWWebController for | |
| 56 * restrictions and precautions. | |
| 57 * @param {Object} command The command in a JavaScript object. | |
| 58 * @private | |
| 59 */ | |
| 60 __gCrWeb.message.invokeOnHostImmediate = function(command) { | |
| 61 // If there is no document or body, the command will be silently dropped. | |
| 62 if (!document || !document.body) | |
| 63 return; | |
| 64 immediateMessageQueue_.queue.push(command); | |
| 65 sendQueue_(immediateMessageQueue_); | |
| 66 }; | |
| 67 | |
| 68 /** | |
| 69 * Invokes a command on the Objective-C side. | 36 * Invokes a command on the Objective-C side. |
| 70 * @param {Object} command The command in a JavaScript object. | 37 * @param {Object} command The command in a JavaScript object. |
| 71 * @private | 38 * @private |
| 72 */ | 39 */ |
| 73 __gCrWeb.message.invokeOnHost = function(command) { | 40 __gCrWeb.message.invokeOnHost = function(command) { |
| 74 // Avoid infinite loops in sites that send messages as a side effect | 41 // If there is no document or body, the command will be silently dropped. |
|
Eugene But (OOO till 7-30)
2016/09/19 23:20:40
Should we still enqueue the message, even if we ar
Eugene But (OOO till 7-30)
2016/09/19 23:20:40
Could you please clarify what do you mean by |will
michaeldo
2016/09/20 18:26:17
It is dropped here by the early return.
This logi
| |
| 75 // of URL verification (e.g., due to logging in an XHR override). | 42 if (!document || !document.body) |
| 76 if (window.__gCrWeb_Verifying) { | |
| 77 return; | 43 return; |
| 78 } | |
| 79 messageQueue_.queue.push(command); | 44 messageQueue_.queue.push(command); |
| 80 sendQueue_(messageQueue_); | 45 sendQueue_(messageQueue_); |
| 81 }; | 46 }; |
| 82 | 47 |
| 83 /** | 48 /** |
| 84 * Returns the message queue as a string. | 49 * Returns the message queue as a string. |
| 85 * @return {string} The current message queue as a JSON string. | 50 * @return {string} The current message queue as a JSON string. |
| 86 */ | 51 */ |
| 87 __gCrWeb.message.getMessageQueue = function() { | 52 __gCrWeb.message.getMessageQueue = function() { |
| 88 var messageQueueString = __gCrWeb.common.JSONStringify(messageQueue_.queue); | 53 var messageQueueString = __gCrWeb.common.JSONStringify(messageQueue_.queue); |
| 89 messageQueue_.reset() | 54 messageQueue_.reset() |
| 90 return messageQueueString; | 55 return messageQueueString; |
| 91 }; | 56 }; |
| 92 | 57 |
| 93 /** | 58 /** |
| 94 * Sends both queues if they contain messages. | 59 * Sends both queues if they contain messages. |
| 95 */ | 60 */ |
| 96 __gCrWeb.message.invokeQueues = function() { | 61 __gCrWeb.message.invokeQueues = function() { |
| 97 if (immediateMessageQueue_.queue.length > 0) | |
| 98 sendQueue_(immediateMessageQueue_); | |
| 99 if (messageQueue_.queue.length > 0) | 62 if (messageQueue_.queue.length > 0) |
| 100 sendQueue_(messageQueue_); | 63 sendQueue_(messageQueue_); |
| 101 }; | 64 }; |
| 102 | 65 |
| 103 function sendQueue_(queueObject) { | 66 function sendQueue_(queueObject) { |
| 104 // Do nothing if windowId has not been set. | 67 // Do nothing if windowId has not been set. |
| 105 if (!__gCrWeb.windowIdObject || typeof __gCrWeb.windowId != 'string') { | 68 if (!__gCrWeb.windowIdObject || typeof __gCrWeb.windowId != 'string') { |
| 106 return; | 69 return; |
| 107 } | 70 } |
| 108 // Some pages/plugins implement Object.prototype.toJSON, which can result | 71 // Some pages/plugins implement Object.prototype.toJSON, which can result |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 127 }); | 90 }); |
| 128 queueObject.reset(); | 91 queueObject.reset(); |
| 129 | 92 |
| 130 if (originalObjectToJSON) { | 93 if (originalObjectToJSON) { |
| 131 // Restore Object.prototype.toJSON to prevent from breaking any | 94 // Restore Object.prototype.toJSON to prevent from breaking any |
| 132 // functionality on the page that depends on its custom implementation. | 95 // functionality on the page that depends on its custom implementation. |
| 133 Object.prototype.toJSON = originalObjectToJSON; | 96 Object.prototype.toJSON = originalObjectToJSON; |
| 134 } | 97 } |
| 135 }; | 98 }; |
| 136 }()); | 99 }()); |
| OLD | NEW |