Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(297)

Side by Side Diff: ios/web/web_state/js/resources/message.js

Issue 788253002: Upstream injected JavaScript for the iOS web layer (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 // Scripts for the message handler.
6
7 /**
8 * Namespace for this module.
9 */
10 __gCrWeb.message = {};
11
12 /* Beginning of anonymous object. */
13 new function() {
14 /**
15 * Object to manage queue of messages waiting to be sent to the main
16 * application for immediate processing.
17 * @type {Object}
18 * @private
19 */
20 var immediateMessageQueue_ = {
21 scheme: 'crwebinvokeimmediate',
22 reset: function() {
23 immediateMessageQueue_.queue = [];
24 // Since the array will be JSON serialized, protect against non-standard
25 // custom versions of Array.prototype.toJSON.
26 immediateMessageQueue_.queue.toJSON = null;
27 }
28 };
29 immediateMessageQueue_.reset();
30
31 /**
32 * Object to manage queue of messages waiting to be sent to the main
33 * application for asynchronous processing.
34 * @type {Object}
35 * @private
36 */
37 var messageQueue_ = {
38 scheme: 'crwebinvoke',
39 reset: function() {
40 messageQueue_.queue = [];
41 // Since the array will be JSON serialized, protect against non-standard
42 // custom versions of Array.prototype.toJSON.
43 messageQueue_.queue.toJSON = null
44 }
45 };
46 messageQueue_.reset();
47
48 /**
49 * Invokes a command immediately on the Objective-C side.
50 * An immediate command is a special class of command that must be handled at
51 * the earliest opportunity. See the notes in CRWWebController for
52 * restrictions and precautions.
53 * @param {Object} command The command in a JavaScript object.
54 * @private
55 */
56 __gCrWeb.message.invokeOnHostImmediate = function(command) {
57 // If there is no document or body, the command will be silently dropped.
58 if (!document || !document.body)
59 return;
60 immediateMessageQueue_.queue.push(command);
61 sendQueue_(immediateMessageQueue_);
62 };
63
64 /**
65 * Invokes a command on the Objective-C side.
66 * @param {Object} command The command in a JavaScript object.
67 * @private
68 */
69 __gCrWeb.message.invokeOnHost = function(command) {
70 messageQueue_.queue.push(command);
71 sendQueue_(messageQueue_);
72 };
73
74 /**
75 * Returns the message queue as a string.
76 * @return {!Object} The current message queue.
77 */
78 __gCrWeb.message.getMessageQueue = function() {
79 var messageQueueString = __gCrWeb.common.JSONStringify(messageQueue_.queue);
80 messageQueue_.reset()
81 return messageQueueString;
82 };
83
84 /**
85 * Sends both queues if they contain messages.
86 */
87 __gCrWeb.message.invokeQueues = function() {
88 if (immediateMessageQueue_.queue.length > 0)
89 sendQueue_(immediateMessageQueue_);
90 if (messageQueue_.queue.length > 0)
91 sendQueue_(messageQueue_);
92 };
93
94 function sendQueue_(queueObject) {
95 // Do nothing if windowId has not been set.
96 if (!__gCrWeb.windowIdObject || typeof __gCrWeb.windowId != 'string') {
97 return;
98 }
99 // Some pages/plugins implement Object.prototype.toJSON, which can result
100 // in serializing messageQueue_ to an invalid format.
101 var originalObjectToJSON = Object.prototype.toJSON;
102 if (originalObjectToJSON)
103 delete Object.prototype.toJSON;
104 __gCrWeb.message_dynamic.sendQueue(queueObject);
105
106 if (originalObjectToJSON) {
107 // Restore Object.prototype.toJSON to prevent from breaking any
108 // functionality on the page that depends on its custom implementation.
109 Object.prototype.toJSON = originalObjectToJSON;
110 }
111 };
112 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698