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
3 // found in the LICENSE file.
5 // Scripts for the message handler.
7 goog.provide('__crWeb.message');
9 goog.require('__crWeb.common');
10 goog.require('__crWeb.messageDynamic');
13 * Namespace for this module.
15 __gCrWeb.message = {};
17 /* Beginning of anonymous object. */
20 * Object to manage queue of messages waiting to be sent to the main
21 * application for immediate processing.
25 var immediateMessageQueue_ = {
26 scheme: 'crwebinvokeimmediate',
28 immediateMessageQueue_.queue = [];
29 // Since the array will be JSON serialized, protect against non-standard
30 // custom versions of Array.prototype.toJSON.
31 immediateMessageQueue_.queue.toJSON = null;
34 immediateMessageQueue_.reset();
37 * Object to manage queue of messages waiting to be sent to the main
38 * application for asynchronous processing.
43 scheme: 'crwebinvoke',
45 messageQueue_.queue = [];
46 // Since the array will be JSON serialized, protect against non-standard
47 // custom versions of Array.prototype.toJSON.
48 messageQueue_.queue.toJSON = null
51 messageQueue_.reset();
54 * Invokes a command immediately on the Objective-C side.
55 * An immediate command is a special class of command that must be handled at
56 * the earliest opportunity. See the notes in CRWWebController for
57 * restrictions and precautions.
58 * @param {Object} command The command in a JavaScript object.
61 __gCrWeb.message.invokeOnHostImmediate = function(command) {
62 // If there is no document or body, the command will be silently dropped.
63 if (!document || !document.body)
65 immediateMessageQueue_.queue.push(command);
66 sendQueue_(immediateMessageQueue_);
70 * Invokes a command on the Objective-C side.
71 * @param {Object} command The command in a JavaScript object.
74 __gCrWeb.message.invokeOnHost = function(command) {
75 // Avoid infinite loops in sites that send messages as a side effect
76 // of URL verification (e.g., due to logging in an XHR override).
77 if (window.__gCrWeb_Verifying) {
80 messageQueue_.queue.push(command);
81 sendQueue_(messageQueue_);
85 * Returns the message queue as a string.
86 * @return {string} The current message queue as a JSON string.
88 __gCrWeb.message.getMessageQueue = function() {
89 var messageQueueString = __gCrWeb.common.JSONStringify(messageQueue_.queue);
91 return messageQueueString;
95 * Sends both queues if they contain messages.
97 __gCrWeb.message.invokeQueues = function() {
98 if (immediateMessageQueue_.queue.length > 0)
99 sendQueue_(immediateMessageQueue_);
100 if (messageQueue_.queue.length > 0)
101 sendQueue_(messageQueue_);
104 function sendQueue_(queueObject) {
105 // Do nothing if windowId has not been set.
106 if (!__gCrWeb.windowIdObject || typeof __gCrWeb.windowId != 'string') {
109 // Some pages/plugins implement Object.prototype.toJSON, which can result
110 // in serializing messageQueue_ to an invalid format.
111 var originalObjectToJSON = Object.prototype.toJSON;
112 if (originalObjectToJSON)
113 delete Object.prototype.toJSON;
114 __gCrWeb.message_dynamic.sendQueue(queueObject);
116 if (originalObjectToJSON) {
117 // Restore Object.prototype.toJSON to prevent from breaking any
118 // functionality on the page that depends on its custom implementation.
119 Object.prototype.toJSON = originalObjectToJSON;