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 | |
75 // of URL verification (e.g., due to logging in an XHR override). | |
76 if (window.__gCrWeb_Verifying) { | |
77 return; | |
78 } | |
79 messageQueue_.queue.push(command); | 41 messageQueue_.queue.push(command); |
80 sendQueue_(messageQueue_); | 42 sendQueue_(messageQueue_); |
81 }; | 43 }; |
82 | 44 |
83 /** | 45 /** |
84 * Returns the message queue as a string. | 46 * Returns the message queue as a string. |
85 * @return {string} The current message queue as a JSON string. | 47 * @return {string} The current message queue as a JSON string. |
86 */ | 48 */ |
87 __gCrWeb.message.getMessageQueue = function() { | 49 __gCrWeb.message.getMessageQueue = function() { |
88 var messageQueueString = __gCrWeb.common.JSONStringify(messageQueue_.queue); | 50 var messageQueueString = __gCrWeb.common.JSONStringify(messageQueue_.queue); |
89 messageQueue_.reset() | 51 messageQueue_.reset() |
90 return messageQueueString; | 52 return messageQueueString; |
91 }; | 53 }; |
92 | 54 |
93 /** | 55 /** |
94 * Sends both queues if they contain messages. | 56 * Sends both queues if they contain messages. |
95 */ | 57 */ |
96 __gCrWeb.message.invokeQueues = function() { | 58 __gCrWeb.message.invokeQueues = function() { |
97 if (immediateMessageQueue_.queue.length > 0) | |
98 sendQueue_(immediateMessageQueue_); | |
99 if (messageQueue_.queue.length > 0) | 59 if (messageQueue_.queue.length > 0) |
100 sendQueue_(messageQueue_); | 60 sendQueue_(messageQueue_); |
101 }; | 61 }; |
102 | 62 |
103 function sendQueue_(queueObject) { | 63 function sendQueue_(queueObject) { |
104 // Do nothing if windowId has not been set. | 64 // Do nothing if windowId has not been set. |
105 if (!__gCrWeb.windowIdObject || typeof __gCrWeb.windowId != 'string') { | 65 if (!__gCrWeb.windowIdObject || typeof __gCrWeb.windowId != 'string') { |
106 return; | 66 return; |
107 } | 67 } |
108 // Some pages/plugins implement Object.prototype.toJSON, which can result | 68 // Some pages/plugins implement Object.prototype.toJSON, which can result |
(...skipping 18 matching lines...) Expand all Loading... |
127 }); | 87 }); |
128 queueObject.reset(); | 88 queueObject.reset(); |
129 | 89 |
130 if (originalObjectToJSON) { | 90 if (originalObjectToJSON) { |
131 // Restore Object.prototype.toJSON to prevent from breaking any | 91 // Restore Object.prototype.toJSON to prevent from breaking any |
132 // functionality on the page that depends on its custom implementation. | 92 // functionality on the page that depends on its custom implementation. |
133 Object.prototype.toJSON = originalObjectToJSON; | 93 Object.prototype.toJSON = originalObjectToJSON; |
134 } | 94 } |
135 }; | 95 }; |
136 }()); | 96 }()); |
OLD | NEW |