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 for use with UIWebView.
7 goog.provide('__crWeb.messageDynamic');
9 goog.require('__crWeb.common');
12 * Namespace for this module.
14 __gCrWeb.message_dynamic = {};
16 /* Beginning of anonymous object. */
19 * Returns true if sending the message queue should be delayed.
21 function pageRequiresDelayedSend() {
22 // The Apple mobile password recovery page does not interact well with
23 // iframes, so on pages with the 'iforgot.apple.com' domain, delay sending
25 return window.location.origin === "https://iforgot.apple.com";
29 * Sends queued commands to the Objective-C side.
30 * @param {Object} queueObject Queue object containing messages to send.
32 __gCrWeb.message_dynamic.sendQueue = function(queueObject) {
33 var send = function() {
34 // The technique requires the document to be present. If it is not, the
35 // messages will be sent once the document object is created.
36 if (!document || !document.body) {
37 // This happens in rare occasions early in the page cycle. It is
38 // possible to create a body element indirectly here, but it has side
39 // effects such as blocking page redirection. The safest solution is to
40 // try again in 1/10th of a second.
41 window.setTimeout(__gCrWeb.message.invokeQueues, 100);
45 var frame = document.createElement('iframe');
46 frame.style.display = 'none';
47 frame.src = queueObject.scheme + '://' + __gCrWeb.windowId + '#' +
48 encodeURIComponent(__gCrWeb.common.JSONStringify(queueObject.queue));
50 document.body.appendChild(frame);
51 document.body.removeChild(frame);
54 // Some pages do not interact well with iframes. On those pages, delay
56 if (pageRequiresDelayedSend()) {
57 window.requestAnimationFrame(send);