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