Commit bbef79b0 authored by Olivier Robin's avatar Olivier Robin Committed by Commit Bot

Lint and format javascript.

Running glint and clang-format on JavaScript files.

Bug: 487804, 802170
Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Change-Id: Ic13a1a141b74b761b83d5aee20c0f4bff384b57b
Reviewed-on: https://chromium-review.googlesource.com/919263Reviewed-by: default avatarMoe Ahmadi <mahmadi@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Commit-Queue: Olivier Robin <olivierrobin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#537295}
parent 45a12805
......@@ -24,25 +24,23 @@ __gCrWeb.suggestion = {};
// minification.
__gCrWeb['suggestion'] = __gCrWeb.suggestion;
/**
* Returns the element with the specified name that is a child of the
* specified parent element.
* @param {Element} parent The parent of the desired element.
* @param {string} name The name of the desired element.
* @return {Element} The element if found, otherwise null;
*/
var getElementByNameWithParent_ = function(parent, name) {
if (parent.name === name)
return parent;
/**
* Returns the element with the specified name that is a child of the
* specified parent element.
* @param {Element} parent The parent of the desired element.
* @param {string} name The name of the desired element.
* @return {Element} The element if found, otherwise null;
*/
var getElementByNameWithParent_ = function(parent, name) {
if (parent.name === name) return parent;
var el;
for (var i = 0; i < parent.children.length; i++) {
el = getElementByNameWithParent_(parent.children[i], name);
if (el)
return el;
}
return null;
};
var el;
for (var i = 0; i < parent.children.length; i++) {
el = getElementByNameWithParent_(parent.children[i], name);
if (el) return el;
}
return null;
};
/**
* Returns the first element in |elements| that is later than |elementToCompare|
......@@ -57,8 +55,8 @@ __gCrWeb['suggestion'] = __gCrWeb.suggestion;
* @return {Element} the first element in |elements| that is later than
* |elementToCompare| in tab order if there is one; null otherwise.
*/
__gCrWeb.suggestion.getNextElementInTabOrder =
function(elementToCompare, elementList) {
__gCrWeb.suggestion.getNextElementInTabOrder = function(
elementToCompare, elementList) {
var elements = [];
for (var i = 0; i < elementList.length; ++i) {
elements[i] = elementList[i];
......@@ -87,10 +85,10 @@ __gCrWeb.suggestion.getNextElementInTabOrder =
// |element2| that has DOM tree position |index2| in tab order. It is assumed
// |index1 !== index2|.
var comparator = function(element1, index1, element2, index2) {
var tabOrder1 = __gCrWeb.suggestion.getTabOrder(element1);
var tabOrder2 = __gCrWeb.suggestion.getTabOrder(element2);
return tabOrder1 > tabOrder2 ||
(tabOrder1 === tabOrder2 && index1 > index2);
var tabOrder1 = __gCrWeb.suggestion.getTabOrder(element1);
var tabOrder2 = __gCrWeb.suggestion.getTabOrder(element2);
return tabOrder1 > tabOrder2 ||
(tabOrder1 === tabOrder2 && index1 > index2);
};
return __gCrWeb.suggestion.getFormElementAfter(
elementToCompare, elements, comparator);
......@@ -109,8 +107,8 @@ __gCrWeb.suggestion.getNextElementInTabOrder =
* @return {Element} the last element in |elements| that is earlier than
* |elementToCompare| in tab order if there is one; null otherwise.
*/
__gCrWeb.suggestion.getPreviousElementInTabOrder =
function(elementToCompare, elementList) {
__gCrWeb.suggestion.getPreviousElementInTabOrder = function(
elementToCompare, elementList) {
var elements = [];
for (var i = 0; i < elementList.length; ++i) {
elements[i] = elementList[i];
......@@ -136,10 +134,10 @@ __gCrWeb.suggestion.getPreviousElementInTabOrder =
// |element2| that has DOM tree position |index2| in tab order. It is assumed
// |index1 !== index2|.
var comparator = function(element1, index1, element2, index2) {
var tabOrder1 = __gCrWeb.suggestion.getTabOrder(element1);
var tabOrder2 = __gCrWeb.suggestion.getTabOrder(element2);
return tabOrder1 < tabOrder2 ||
(tabOrder1 === tabOrder2 && index1 < index2);
var tabOrder1 = __gCrWeb.suggestion.getTabOrder(element1);
var tabOrder2 = __gCrWeb.suggestion.getTabOrder(element2);
return tabOrder1 < tabOrder2 ||
(tabOrder1 === tabOrder2 && index1 < index2);
};
return __gCrWeb.suggestion.getFormElementAfter(
......@@ -165,8 +163,8 @@ __gCrWeb.suggestion.getPreviousElementInTabOrder =
* tree order.
* @return {Element} The element that satisfies the conditions given above.
*/
__gCrWeb.suggestion.getFormElementAfter =
function(elementToCompare, elements, comparator) {
__gCrWeb.suggestion.getFormElementAfter = function(
elementToCompare, elements, comparator) {
// Computes the index |indexToCompare| of |elementToCompare| in |element|.
var indexToCompare = elements.indexOf(elementToCompare);
if (indexToCompare === -1) {
......@@ -234,8 +232,7 @@ __gCrWeb.suggestion.isSequentiallyReachable = function(element) {
// on this condition, false is returned for an iframe (as Mobile Safari does
// not navigate to elements in an iframe, there is no need to recursively
// check if there is a reachable element in an iframe).
if (element.tagName !== 'INPUT' &&
element.tagName !== 'SELECT' &&
if (element.tagName !== 'INPUT' && element.tagName !== 'SELECT' &&
element.tagName !== 'TEXTAREA') {
return false;
}
......@@ -243,12 +240,9 @@ __gCrWeb.suggestion.isSequentiallyReachable = function(element) {
// The following elements are skipped when navigating using 'Prev' and "Next'
// buttons in Mobile Safari.
if (element.tagName === 'INPUT' &&
(element.type === 'submit' ||
element.type === 'reset' ||
element.type === 'image' ||
element.type === 'button' ||
element.type === 'range' ||
element.type === 'radio' ||
(element.type === 'submit' || element.type === 'reset' ||
element.type === 'image' || element.type === 'button' ||
element.type === 'range' || element.type === 'radio' ||
element.type === 'checkbox')) {
return false;
}
......@@ -289,8 +283,7 @@ __gCrWeb.suggestion.getTabOrder = function(element) {
*/
__gCrWeb.suggestion.getFormElement = function(formName, fieldName) {
var form = __gCrWeb.common.getFormElementFromIdentifier(formName);
if (!form)
return null;
if (!form) return null;
return getElementByNameWithParent_(form, fieldName);
};
......@@ -299,11 +292,11 @@ __gCrWeb.suggestion.getFormElement = function(formName, fieldName) {
* if there is no such element.
*/
__gCrWeb.suggestion['selectNextElement'] = function(formName, fieldName) {
var currentElement =
formName ? __gCrWeb.suggestion.getFormElement(formName, fieldName) :
document.activeElement;
var nextElement = __gCrWeb.suggestion.getNextElementInTabOrder(currentElement,
document.all);
var currentElement = formName ?
__gCrWeb.suggestion.getFormElement(formName, fieldName) :
document.activeElement;
var nextElement = __gCrWeb.suggestion.getNextElementInTabOrder(
currentElement, document.all);
if (nextElement) {
nextElement.focus();
}
......@@ -314,9 +307,9 @@ __gCrWeb.suggestion['selectNextElement'] = function(formName, fieldName) {
* operation if there is no such element.
*/
__gCrWeb.suggestion['selectPreviousElement'] = function(formName, fieldName) {
var currentElement =
formName ? __gCrWeb.suggestion.getFormElement(formName, fieldName) :
document.activeElement;
var currentElement = formName ?
__gCrWeb.suggestion.getFormElement(formName, fieldName) :
document.activeElement;
var prevElement = __gCrWeb.suggestion.getPreviousElementInTabOrder(
currentElement, document.all);
if (prevElement) {
......@@ -325,25 +318,29 @@ __gCrWeb.suggestion['selectPreviousElement'] = function(formName, fieldName) {
};
/**
* @param {string} formName The name of the form containing the element.
* @param {string} fieldName The name of the field containing the element.
* @return {boolean} Whether there is an element in the sequential navigation
* after the currently active element.
*/
__gCrWeb.suggestion['hasNextElement'] = function(formName, fieldName) {
var currentElement =
formName ? __gCrWeb.suggestion.getFormElement(formName, fieldName) :
document.activeElement;
return __gCrWeb.suggestion.getNextElementInTabOrder(currentElement,
document.all) !== null;
var currentElement = formName ?
__gCrWeb.suggestion.getFormElement(formName, fieldName) :
document.activeElement;
return __gCrWeb.suggestion.getNextElementInTabOrder(
currentElement, document.all) !== null;
};
/**
* @param {string} formName The name of the form containing the element.
* @param {string} fieldName The name of the field containing the element.
* @return {boolean} Whether there is an element in the sequential navigation
* before the currently active element.
*/
__gCrWeb.suggestion['hasPreviousElement'] = function(formName, fieldName) {
var currentElement =
formName ? __gCrWeb.suggestion.getFormElement(formName, fieldName) :
document.activeElement;
var currentElement = formName ?
__gCrWeb.suggestion.getFormElement(formName, fieldName) :
document.activeElement;
return __gCrWeb.suggestion.getPreviousElementInTabOrder(
currentElement, document.all) !== null;
};
......
......@@ -184,9 +184,7 @@ source_set("eg_tests") {
]
}
# TODO(crbug.com/487804): use js_compile_checked instead once the errors have
# been fixed.
js_compile_unchecked("injected_js") {
js_compile_checked("injected_js") {
visibility = [ ":passwords" ]
sources = [
"resources/password_controller.js",
......
// Copyright 2017 The Chromium Authors. All rights reserved.
// Copyright 2017 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.
// The set of scripts to be injected into the web view as early as possible.
goog.provide('__crWeb.chromeBundle');
goog.require('__crWeb.print');
goog.require('__crWeb.autofill');
goog.require('__crWeb.print');
......@@ -5,4 +5,4 @@
// Set of scripts required by web layer backed up by WKWebView.
goog.provide('__crWeb.allFramesWebBundle');
goog.require('__crWeb.form');
\ No newline at end of file
goog.require('__crWeb.form');
......@@ -16,4 +16,3 @@ var __gCrWeb = {};
// Store __gCrWeb global namespace object referenced by a string, so it does not
// get renamed by closure compiler during the minification.
window['__gCrWeb'] = __gCrWeb;
This diff is collapsed.
......@@ -16,36 +16,37 @@ __gCrWeb.console = {};
/* Beginning of anonymous object. */
(function() {
function sendConsoleMessage(method, originalArgs) {
var message, slicedArgs = Array.prototype.slice.call(originalArgs);
try {
message = slicedArgs.join(' ');
} catch (err) {
}
__gCrWeb.message.invokeOnHost({'command': 'console',
'method': method,
'message': message,
'origin': document.location.origin});
function sendConsoleMessage(method, originalArgs) {
var message, slicedArgs = Array.prototype.slice.call(originalArgs);
try {
message = slicedArgs.join(' ');
} catch (err) {
}
console.log = function() {
sendConsoleMessage('log', arguments);
};
console.debug = function() {
sendConsoleMessage('debug', arguments);
};
console.info = function() {
sendConsoleMessage('info', arguments);
};
console.warn = function() {
sendConsoleMessage('warn', arguments);
};
console.error = function() {
sendConsoleMessage('error', arguments);
};
__gCrWeb.message.invokeOnHost({
'command': 'console',
'method': method,
'message': message,
'origin': document.location.origin
});
}
console.log = function() {
sendConsoleMessage('log', arguments);
};
console.debug = function() {
sendConsoleMessage('debug', arguments);
};
console.info = function() {
sendConsoleMessage('info', arguments);
};
console.warn = function() {
sendConsoleMessage('warn', arguments);
};
console.error = function() {
sendConsoleMessage('error', arguments);
};
}());
......@@ -13,22 +13,22 @@ goog.provide('__crWeb.error');
/** Beginning of anonymouse object */
(function() {
/**
* JavaScript errors are logged on the main application side. The handler is
* added ASAP to catch any errors in startup.
*/
window.addEventListener('error', function(event) {
// Sadly, event.filename and event.lineno are always 'undefined' and '0'
// with UIWebView.
// TODO(crbug.com/711359): the aforementioned APIs may be working now in
// WKWebView. Evaluate any cleanup / improvement we can do here.
__gCrWeb.message.invokeOnHost(
{'command': 'window.error', 'message': event.message.toString()});
});
/**
* JavaScript errors are logged on the main application side. The handler is
* added ASAP to catch any errors in startup.
*/
window.addEventListener('error', function(event) {
// Sadly, event.filename and event.lineno are always 'undefined' and '0'
// with UIWebView.
// TODO(crbug.com/711359): the aforementioned APIs may be working now in
// WKWebView. Evaluate any cleanup / improvement we can do here.
__gCrWeb.message.invokeOnHost(
{'command': 'window.error', 'message': event.message.toString()});
});
// Flush the message queue.
if (__gCrWeb.message) {
__gCrWeb.message.invokeQueues();
}
// Flush the message queue.
if (__gCrWeb.message) {
__gCrWeb.message.invokeQueues();
}
}()); // End of anonymous object
......@@ -14,91 +14,91 @@ goog.require('__crWeb.message');
/** Beginning of anonymous object */
(function() {
// Skip iframes that have different origins from the main frame. For such
// frames no form related actions (eg. filling, saving) are supported.
try {
// The following line generates exception for iframes that have different
// origin that.
// TODO(crbug.com/792642): implement sending messages instead of using
// window.top, when messaging framework is ready.
if (!window.top.document)
return;
}
catch(error) {
return;
}
// Skip iframes that have different origins from the main frame. For such
// frames no form related actions (eg. filling, saving) are supported.
try {
// The following line generates exception for iframes that have different
// origin that.
// TODO(crbug.com/792642): implement sending messages instead of using
// window.top, when messaging framework is ready.
if (!window.top.document) return;
} catch (error) {
return;
}
/**
* Focus and input events for form elements are messaged to the main
* application for broadcast to WebStateObservers.
* This is done with a single event handler for each type being added to the
* main document element which checks the source element of the event; this
* is much easier to manage than adding handlers to individual elements.
* @private
*/
var formActivity_ = function(evt) {
var srcElement = evt.srcElement;
var value = srcElement.value || '';
var fieldType = srcElement.type || '';
/**
* Focus and input events for form elements are messaged to the main
* application for broadcast to WebStateObservers.
* This is done with a single event handler for each type being added to the
* main document element which checks the source element of the event; this
* is much easier to manage than adding handlers to individual elements.
* @private
*/
var formActivity_ = function(evt) {
var srcElement = evt.srcElement;
var value = srcElement.value || '';
var fieldType = srcElement.type || '';
var msg = {
'command': 'form.activity',
'formName': __gCrWeb.common.getFormIdentifier(evt.srcElement.form),
'fieldName': __gCrWeb.common.getFieldIdentifier(srcElement),
'fieldType': fieldType,
'type': evt.type,
'value': value
};
__gCrWeb.message.invokeOnHost(msg);
var msg = {
'command': 'form.activity',
'formName': __gCrWeb.common.getFormIdentifier(evt.srcElement.form),
'fieldName': __gCrWeb.common.getFieldIdentifier(srcElement),
'fieldType': fieldType,
'type': evt.type,
'value': value
};
__gCrWeb.message.invokeOnHost(msg);
};
/**
* Focus events performed on the 'capture' phase otherwise they are often
* not received.
*/
document.addEventListener('focus', formActivity_, true);
document.addEventListener('blur', formActivity_, true);
document.addEventListener('change', formActivity_, true);
/**
* Focus events performed on the 'capture' phase otherwise they are often
* not received.
*/
document.addEventListener('focus', formActivity_, true);
document.addEventListener('blur', formActivity_, true);
document.addEventListener('change', formActivity_, true);
/**
* Text input is watched at the bubbling phase as this seems adequate in
* practice and it is less obtrusive to page scripts than capture phase.
*/
document.addEventListener('input', formActivity_, false);
document.addEventListener('keyup', formActivity_, false);
/**
* Text input is watched at the bubbling phase as this seems adequate in
* practice and it is less obtrusive to page scripts than capture phase.
*/
document.addEventListener('input', formActivity_, false);
document.addEventListener('keyup', formActivity_, false);
/**
* Capture form submit actions.
*/
document.addEventListener('submit', function(evt) {
var action;
if (evt['defaultPrevented'])
return;
action = evt.target.getAttribute('action');
// Default action is to re-submit to same page.
if (!action) {
action = document.location.href;
}
__gCrWeb.message.invokeOnHost({
'command': 'document.submit',
'formName': __gCrWeb.common.getFormIdentifier(evt.srcElement),
'href': getFullyQualifiedUrl_(action)
});
}, false);
/**
* Capture form submit actions.
*/
document.addEventListener('submit', function(evt) {
var action;
if (evt['defaultPrevented']) return;
action = evt.target.getAttribute('action');
// Default action is to re-submit to same page.
if (!action) {
action = document.location.href;
}
__gCrWeb.message.invokeOnHost({
'command': 'document.submit',
'formName': __gCrWeb.common.getFormIdentifier(evt.srcElement),
'href': getFullyQualifiedUrl_(action)
});
}, false);
/** @private */
var getFullyQualifiedUrl_ = function(originalURL) {
// A dummy anchor (never added to the document) is used to obtain the
// fully-qualified URL of |originalURL|.
var anchor = document.createElement('a');
anchor.href = originalURL;
return anchor.href;
};
/** @private
* @param {string} originalURL
* @return {string}
*/
var getFullyQualifiedUrl_ = function(originalURL) {
// A dummy anchor (never added to the document) is used to obtain the
// fully-qualified URL of |originalURL|.
var anchor = document.createElement('a');
anchor.href = originalURL;
return anchor.href;
};
/** Flush the message queue. */
if (__gCrWeb.message) {
__gCrWeb.message.invokeQueues();
}
/** Flush the message queue. */
if (__gCrWeb.message) {
__gCrWeb.message.invokeQueues();
}
}()); // End of anonymous object
......@@ -14,18 +14,20 @@ goog.provide('__crWeb.legacy');
/** Beginning of anonymouse object */
(function() {
/**
* Handles document load completion tasks. Invoked from
* [WKNavigationDelegate webView:didFinishNavigation:], when document load is
* complete.
* TODO(crbug.com/546350): Investigate using
* WKUserScriptInjectionTimeAtDocumentEnd to inject this material at the
* appropriate time so that this API will not be needed.
*/
__gCrWeb.didFinishNavigation = function() {
// Send the favicons to the browser.
__gCrWeb.message.invokeOnHost({'command': 'document.favicons',
'favicons': __gCrWeb.common.getFavicons()});
}
/**
* Handles document load completion tasks. Invoked from
* [WKNavigationDelegate webView:didFinishNavigation:], when document load is
* complete.
* TODO(crbug.com/546350): Investigate using
* WKUserScriptInjectionTimeAtDocumentEnd to inject this material at the
* appropriate time so that this API will not be needed.
*/
__gCrWeb.didFinishNavigation = function() {
// Send the favicons to the browser.
__gCrWeb.message.invokeOnHost({
'command': 'document.favicons',
'favicons': __gCrWeb.common.getFavicons()
});
};
}()); // End of anonymouse object
......@@ -7,30 +7,30 @@
* switching to WKBasedNavigationManager.
*/
goog.provide('__crWeb.legacynavigation');
goog.provide('__crWeb.legacynavigation');
/** Beginning of anonymouse object */
(function() {
/**
* Intercept window.history methods to call back/forward natively.
*/
window.history.back = function() {
__gCrWeb.message.invokeOnHost({'command': 'window.history.back'});
};
window.history.forward = function() {
__gCrWeb.message.invokeOnHost({'command': 'window.history.forward'});
};
window.history.go = function(delta) {
__gCrWeb.message.invokeOnHost(
{'command': 'window.history.go', 'value': delta | 0});
};
/** Flush the message queue. */
if (__gCrWeb.message) {
__gCrWeb.message.invokeQueues();
}
/**
* Intercept window.history methods to call back/forward natively.
*/
window.history.back = function() {
__gCrWeb.message.invokeOnHost({'command': 'window.history.back'});
};
window.history.forward = function() {
__gCrWeb.message.invokeOnHost({'command': 'window.history.forward'});
};
window.history.go = function(delta) {
__gCrWeb.message.invokeOnHost(
{'command': 'window.history.go', 'value': delta | 0});
};
/** Flush the message queue. */
if (__gCrWeb.message) {
__gCrWeb.message.invokeQueues();
}
}()); // End of anonymouse object
......@@ -11,5 +11,5 @@ goog.require('__crWeb.console');
goog.require('__crWeb.contextMenu');
goog.require('__crWeb.error');
goog.require('__crWeb.legacy');
goog.require('__crWeb.scrollWorkaround');
goog.require('__crWeb.navigation');
goog.require('__crWeb.scrollWorkaround');
......@@ -20,74 +20,72 @@ __gCrWeb['message'] = __gCrWeb.message;
/* Beginning of anonymous object. */
(function() {
/**
* 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.
delete messageQueue_.queue.toJSON;
}
};
messageQueue_.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.
delete messageQueue_.queue.toJSON;
}
};
messageQueue_.reset();
/**
* Invokes a command on the Objective-C side.
* @param {Object} command The command in a JavaScript object.
* @public
*/
__gCrWeb.message.invokeOnHost = function(command) {
messageQueue_.queue.push(command);
sendQueue_(messageQueue_);
};
/**
* Invokes a command on the Objective-C side.
* @param {Object} command The command in a JavaScript object.
* @public
*/
__gCrWeb.message.invokeOnHost = function(command) {
messageQueue_.queue.push(command);
sendQueue_(messageQueue_);
};
/**
* Returns the message queue as a string.
* @return {string} The current message queue as a JSON string.
*/
__gCrWeb.message.getMessageQueue = function() {
var messageQueueString = __gCrWeb.common.JSONStringify(messageQueue_.queue);
messageQueue_.reset()
return messageQueueString;
};
/**
* Returns the message queue as a string.
* @return {string} The current message queue as a JSON string.
*/
__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 (messageQueue_.queue.length > 0)
sendQueue_(messageQueue_);
};
/**
* Sends both queues if they contain messages.
*/
__gCrWeb.message.invokeQueues = function() {
if (messageQueue_.queue.length > 0) sendQueue_(messageQueue_);
};
function sendQueue_(queueObject) {
// Do nothing if windowId has not been set.
if (typeof window.top.__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;
function sendQueue_(queueObject) {
// Do nothing if windowId has not been set.
if (typeof window.top.__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;
queueObject.queue.forEach(function(command) {
__gCrWeb.common.sendWebKitMessage(queueObject.scheme, {
"crwCommand": command,
"crwWindowId": window.top.__gCrWeb['windowId']
});
queueObject.queue.forEach(function(command) {
__gCrWeb.common.sendWebKitMessage(queueObject.scheme, {
'crwCommand': command,
'crwWindowId': window.top.__gCrWeb['windowId']
});
queueObject.reset();
});
queueObject.reset();
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;
}
};
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;
}
}
}());
......@@ -11,113 +11,111 @@ goog.provide('__crWeb.navigation');
/** Beginning of anonymouse object */
(function() {
/**
* A popstate event needs to be fired anytime the active history entry
* changes without an associated document change. Either via back, forward, go
* navigation or by loading the URL, clicking on a link, etc.
*/
__gCrWeb['dispatchPopstateEvent'] = function(stateObject) {
var popstateEvent = window.document.createEvent('HTMLEvents');
popstateEvent.initEvent('popstate', true, false);
if (stateObject)
popstateEvent.state = JSON.parse(stateObject);
// setTimeout() is used in order to return immediately. Otherwise the
// dispatchEvent call waits for all event handlers to return, which could
// cause a ReentryGuard failure.
window.setTimeout(function() {
window.dispatchEvent(popstateEvent);
}, 0);
};
/**
* A hashchange event needs to be fired after a same-document history
* navigation between two URLs that are equivalent except for their fragments.
*/
__gCrWeb['dispatchHashchangeEvent'] = function(oldURL, newURL) {
var hashchangeEvent = window.document.createEvent('HTMLEvents');
hashchangeEvent.initEvent('hashchange', true, false);
if (oldURL)
hashchangeEvent.oldURL = oldURL;
if (newURL)
hashchangeEvent.newURL = newURL
// setTimeout() is used in order to return immediately. Otherwise the
// dispatchEvent call waits for all event handlers to return, which could
// cause a ReentryGuard failure.
window.setTimeout(function() {
window.dispatchEvent(hashchangeEvent);
}, 0);
};
/**
* Keep the original pushState() and replaceState() methods. It's needed to
* update the web view's URL and window.history.state property during history
* navigations that don't cause a page load.
* @private
*/
var originalWindowHistoryPushState = window.history.pushState;
var originalWindowHistoryReplaceState = window.history.replaceState;
__gCrWeb['replaceWebViewURL'] = function(url, stateObject) {
originalWindowHistoryReplaceState.call(history, stateObject, '', url);
};
/**
* Intercepts window.history methods so native code can differentiate between
* same-document navigation that are state navigations vs. hash navigations.
* This is needed for backward compatibility of DidStartLoading, which is
* triggered for fragment navigation but not state navigation.
* TODO(crbug.com/783382): Remove this once DidStartLoading is no longer
* called for same-document navigation.
*/
window.history.pushState = function(stateObject, pageTitle, pageUrl) {
__gCrWeb.message.invokeOnHost(
{'command': 'window.history.willChangeState'});
// Calling stringify() on undefined causes a JSON parse error.
var serializedState =
typeof(stateObject) == 'undefined' ? '' :
__gCrWeb.common.JSONStringify(stateObject);
pageUrl = pageUrl || window.location.href;
originalWindowHistoryPushState.call(history, stateObject,
pageTitle, pageUrl);
__gCrWeb.message.invokeOnHost(
{'command': 'window.history.didPushState',
'stateObject': serializedState,
'baseUrl': document.baseURI,
'pageUrl': pageUrl.toString()});
};
window.history.replaceState = function(stateObject, pageTitle, pageUrl) {
__gCrWeb.message.invokeOnHost(
{'command': 'window.history.willChangeState'});
// Calling stringify() on undefined causes a JSON parse error.
var serializedState =
typeof(stateObject) == 'undefined' ? '' :
__gCrWeb.common.JSONStringify(stateObject);
pageUrl = pageUrl || window.location.href;
originalWindowHistoryReplaceState.call(history, stateObject,
pageTitle, pageUrl);
__gCrWeb.message.invokeOnHost(
{'command': 'window.history.didReplaceState',
'stateObject': serializedState,
'baseUrl': document.baseURI,
'pageUrl': pageUrl.toString()});
};
window.addEventListener('hashchange', function(evt) {
// Because hash changes don't trigger __gCrWeb.didFinishNavigation, so fetch
// favicons for the new page manually.
__gCrWeb.message.invokeOnHost({'command': 'document.favicons',
'favicons': __gCrWeb.common.getFavicons()});
__gCrWeb.message.invokeOnHost({'command': 'window.hashchange'});
/**
* A popstate event needs to be fired anytime the active history entry
* changes without an associated document change. Either via back, forward, go
* navigation or by loading the URL, clicking on a link, etc.
*/
__gCrWeb['dispatchPopstateEvent'] = function(stateObject) {
var popstateEvent = window.document.createEvent('HTMLEvents');
popstateEvent.initEvent('popstate', true, false);
if (stateObject) popstateEvent.state = JSON.parse(stateObject);
// setTimeout() is used in order to return immediately. Otherwise the
// dispatchEvent call waits for all event handlers to return, which could
// cause a ReentryGuard failure.
window.setTimeout(function() {
window.dispatchEvent(popstateEvent);
}, 0);
};
/**
* A hashchange event needs to be fired after a same-document history
* navigation between two URLs that are equivalent except for their fragments.
*/
__gCrWeb['dispatchHashchangeEvent'] = function(oldURL, newURL) {
var hashchangeEvent = window.document.createEvent('HTMLEvents');
hashchangeEvent.initEvent('hashchange', true, false);
if (oldURL) hashchangeEvent.oldURL = oldURL;
if (newURL) hashchangeEvent.newURL = newURL;
// setTimeout() is used in order to return immediately. Otherwise the
// dispatchEvent call waits for all event handlers to return, which could
// cause a ReentryGuard failure.
window.setTimeout(function() {
window.dispatchEvent(hashchangeEvent);
}, 0);
};
/**
* Keep the original pushState() and replaceState() methods. It's needed to
* update the web view's URL and window.history.state property during history
* navigations that don't cause a page load.
* @private
*/
var originalWindowHistoryPushState = window.history.pushState;
var originalWindowHistoryReplaceState = window.history.replaceState;
__gCrWeb['replaceWebViewURL'] = function(url, stateObject) {
originalWindowHistoryReplaceState.call(history, stateObject, '', url);
};
/**
* Intercepts window.history methods so native code can differentiate between
* same-document navigation that are state navigations vs. hash navigations.
* This is needed for backward compatibility of DidStartLoading, which is
* triggered for fragment navigation but not state navigation.
* TODO(crbug.com/783382): Remove this once DidStartLoading is no longer
* called for same-document navigation.
*/
window.history.pushState = function(stateObject, pageTitle, pageUrl) {
__gCrWeb.message.invokeOnHost({'command': 'window.history.willChangeState'});
// Calling stringify() on undefined causes a JSON parse error.
var serializedState = typeof(stateObject) == 'undefined' ?
'' :
__gCrWeb.common.JSONStringify(stateObject);
pageUrl = pageUrl || window.location.href;
originalWindowHistoryPushState.call(history, stateObject, pageTitle, pageUrl);
__gCrWeb.message.invokeOnHost({
'command': 'window.history.didPushState',
'stateObject': serializedState,
'baseUrl': document.baseURI,
'pageUrl': pageUrl.toString()
});
};
window.history.replaceState = function(stateObject, pageTitle, pageUrl) {
__gCrWeb.message.invokeOnHost({'command': 'window.history.willChangeState'});
// Calling stringify() on undefined causes a JSON parse error.
var serializedState = typeof(stateObject) == 'undefined' ?
'' :
__gCrWeb.common.JSONStringify(stateObject);
pageUrl = pageUrl || window.location.href;
originalWindowHistoryReplaceState.call(
history, stateObject, pageTitle, pageUrl);
__gCrWeb.message.invokeOnHost({
'command': 'window.history.didReplaceState',
'stateObject': serializedState,
'baseUrl': document.baseURI,
'pageUrl': pageUrl.toString()
});
};
window.addEventListener('hashchange', function(evt) {
// Because hash changes don't trigger __gCrWeb.didFinishNavigation, so fetch
// favicons for the new page manually.
__gCrWeb.message.invokeOnHost({
'command': 'document.favicons',
'favicons': __gCrWeb.common.getFavicons()
});
__gCrWeb.message.invokeOnHost({'command': 'window.hashchange'});
});
/** Flush the message queue. */
if (__gCrWeb.message) {
__gCrWeb.message.invokeQueues();
}
/** Flush the message queue. */
if (__gCrWeb.message) {
__gCrWeb.message.invokeQueues();
}
}()); // End of anonymouse object
......@@ -48,7 +48,7 @@ __crPostRequestWorkaround.runPostRequest = function(
byteArrays.push(byteArray);
}
return new Blob(byteArrays, {type: contentType});
}
};
/**
* Creates and executes a POST request.
......@@ -57,6 +57,7 @@ __crPostRequestWorkaround.runPostRequest = function(
* Each header value must be expressed as a key-value pair in this object.
* @param {string} body Request body encoded with Base64.
* @param {string} contentType Content-Type header value.
* @return {number | string}
*/
var createAndSendPostRequest = function(url, headers, body, contentType) {
var request = new XMLHttpRequest();
......@@ -72,14 +73,15 @@ __crPostRequestWorkaround.runPostRequest = function(
throw request.status;
}
return request.responseText;
}
};
document.open();
try {
document.write(createAndSendPostRequest(url, headers, body, contentType));
window.webkit.messageHandlers['POSTSuccessHandler'].postMessage("");
} catch(error) {
document.write(/** @type {string} */ (
createAndSendPostRequest(url, headers, body, contentType)));
window.webkit.messageHandlers['POSTSuccessHandler'].postMessage('');
} catch (error) {
window.webkit.messageHandlers['POSTErrorHandler'].postMessage(error);
}
document.close();
}
};
......@@ -11,28 +11,27 @@ goog.provide('__crWeb.scrollWorkaround');
/** Beginning of anonymous object */
(function() {
/** @private */
var webViewScrollViewIsDragging_ = false;
/**
* Tracks whether user is in the middle of scrolling/dragging. If user is
* scrolling, ignore window.scrollTo() until user stops scrolling.
*/
__gCrWeb['setWebViewScrollViewIsDragging'] = function(state) {
webViewScrollViewIsDragging_ = state;
};
/** @private */
var originalWindowScrollTo_ = window.scrollTo;
/**
* Wraps the original window.scrollTo() to suppress it as long as
* webViewScrollViewIsDragging is true.
*/
window.scrollTo = function(x, y) {
if (webViewScrollViewIsDragging_)
return;
originalWindowScrollTo_(x, y);
};
}()) // End of anonymouse object
/** @private */
var webViewScrollViewIsDragging_ = false;
/**
* Tracks whether user is in the middle of scrolling/dragging. If user is
* scrolling, ignore window.scrollTo() until user stops scrolling.
*/
__gCrWeb['setWebViewScrollViewIsDragging'] = function(state) {
webViewScrollViewIsDragging_ = state;
};
/** @private */
var originalWindowScrollTo_ = window.scrollTo;
/**
* Wraps the original window.scrollTo() to suppress it as long as
* webViewScrollViewIsDragging is true.
*/
window.scrollTo = function(x, y) {
if (webViewScrollViewIsDragging_) return;
originalWindowScrollTo_(x, y);
};
}()); // End of anonymous object
......@@ -7,10 +7,10 @@
// Script to set windowId.
(function() {
// CRWJSWindowIDManager replaces $(WINDOW_ID) with appropriate string upon
// injection.
__gCrWeb['windowId'] = '$(WINDOW_ID)';
// CRWJSWindowIDManager replaces $(WINDOW_ID) with appropriate string upon
// injection.
__gCrWeb['windowId'] = '$(WINDOW_ID)';
// Send messages queued since message.js injection.
__gCrWeb.message.invokeQueues();
// Send messages queued since message.js injection.
__gCrWeb.message.invokeQueues();
}());
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment