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

Unified 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: bug reference 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ios/web/web_state/js/resources/common.js ('k') | ios/web/web_state/js/resources/message_dynamic_ui.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ios/web/web_state/js/resources/message.js
diff --git a/ios/web/web_state/js/resources/message.js b/ios/web/web_state/js/resources/message.js
new file mode 100644
index 0000000000000000000000000000000000000000..40ebda46a74686a7725959f28a4c394cb6cc12b0
--- /dev/null
+++ b/ios/web/web_state/js/resources/message.js
@@ -0,0 +1,112 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Scripts for the message handler.
+
+/**
+ * Namespace for this module.
+ */
+__gCrWeb.message = {};
+
+/* Beginning of anonymous object. */
+new function() {
+ /**
+ * Object to manage queue of messages waiting to be sent to the main
+ * application for immediate processing.
+ * @type {Object}
+ * @private
+ */
+ var immediateMessageQueue_ = {
+ scheme: 'crwebinvokeimmediate',
+ reset: function() {
+ immediateMessageQueue_.queue = [];
+ // Since the array will be JSON serialized, protect against non-standard
+ // custom versions of Array.prototype.toJSON.
+ immediateMessageQueue_.queue.toJSON = null;
+ }
+ };
+ immediateMessageQueue_.reset();
+
+ /**
+ * Object to manage queue of messages waiting to be sent to the main
+ * application for asynchronous processing.
+ * @type {Object}
+ * @private
+ */
+ var messageQueue_ = {
+ scheme: 'crwebinvoke',
+ reset: function() {
+ messageQueue_.queue = [];
+ // Since the array will be JSON serialized, protect against non-standard
+ // custom versions of Array.prototype.toJSON.
+ messageQueue_.queue.toJSON = null
+ }
+ };
+ messageQueue_.reset();
+
+ /**
+ * Invokes a command immediately on the Objective-C side.
+ * An immediate command is a special class of command that must be handled at
+ * the earliest opportunity. See the notes in CRWWebController for
+ * restrictions and precautions.
+ * @param {Object} command The command in a JavaScript object.
+ * @private
+ */
+ __gCrWeb.message.invokeOnHostImmediate = function(command) {
+ // If there is no document or body, the command will be silently dropped.
+ if (!document || !document.body)
+ return;
+ immediateMessageQueue_.queue.push(command);
+ sendQueue_(immediateMessageQueue_);
+ };
+
+ /**
+ * Invokes a command on the Objective-C side.
+ * @param {Object} command The command in a JavaScript object.
+ * @private
+ */
+ __gCrWeb.message.invokeOnHost = function(command) {
+ messageQueue_.queue.push(command);
+ sendQueue_(messageQueue_);
+ };
+
+ /**
+ * Returns the message queue as a string.
+ * @return {!Object} The current message queue.
+ */
+ __gCrWeb.message.getMessageQueue = function() {
+ var messageQueueString = __gCrWeb.common.JSONStringify(messageQueue_.queue);
+ messageQueue_.reset()
+ return messageQueueString;
+ };
+
+ /**
+ * Sends both queues if they contain messages.
+ */
+ __gCrWeb.message.invokeQueues = function() {
+ if (immediateMessageQueue_.queue.length > 0)
+ sendQueue_(immediateMessageQueue_);
+ if (messageQueue_.queue.length > 0)
+ sendQueue_(messageQueue_);
+ };
+
+ function sendQueue_(queueObject) {
+ // Do nothing if windowId has not been set.
+ if (!__gCrWeb.windowIdObject || typeof __gCrWeb.windowId != 'string') {
+ return;
+ }
+ // Some pages/plugins implement Object.prototype.toJSON, which can result
+ // in serializing messageQueue_ to an invalid format.
+ var originalObjectToJSON = Object.prototype.toJSON;
+ if (originalObjectToJSON)
+ delete Object.prototype.toJSON;
+ __gCrWeb.message_dynamic.sendQueue(queueObject);
+
+ if (originalObjectToJSON) {
+ // Restore Object.prototype.toJSON to prevent from breaking any
+ // functionality on the page that depends on its custom implementation.
+ Object.prototype.toJSON = originalObjectToJSON;
+ }
+ };
+}
« no previous file with comments | « ios/web/web_state/js/resources/common.js ('k') | ios/web/web_state/js/resources/message_dynamic_ui.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698