Commit ac1216a9 authored by paulmeyer's avatar paulmeyer Committed by Commit bot

More cleaning up in web_view.js.

I put all the public-facing API methods together, put them in alphabetical order, and made their location more obvious in the file. I also removed some more code/functions that were unused/unneeded.

Review URL: https://codereview.chromium.org/637193005

Cr-Commit-Position: refs/heads/master@{#299119}
parent 07269613
...@@ -107,7 +107,7 @@ function WebViewInternal(webviewNode) { ...@@ -107,7 +107,7 @@ function WebViewInternal(webviewNode) {
var shadowRoot = this.webviewNode.createShadowRoot(); var shadowRoot = this.webviewNode.createShadowRoot();
this.partition = new Partition(); this.partition = new Partition();
this.setupWebviewNodeAttributes(); this.setupWebViewSrcAttributeMutationObserver();
this.setupFocusPropagation(); this.setupFocusPropagation();
this.setupWebviewNodeProperties(); this.setupWebviewNodeProperties();
...@@ -169,7 +169,6 @@ WebViewInternal.prototype.setupFocusPropagation = function() { ...@@ -169,7 +169,6 @@ WebViewInternal.prototype.setupFocusPropagation = function() {
// See http://crbug.com/231664. // See http://crbug.com/231664.
this.webviewNode.setAttribute('tabIndex', -1); this.webviewNode.setAttribute('tabIndex', -1);
} }
var self = this;
this.webviewNode.addEventListener('focus', function(e) { this.webviewNode.addEventListener('focus', function(e) {
// Focus the BrowserPlugin when the <webview> takes focus. // Focus the BrowserPlugin when the <webview> takes focus.
this.browserPluginNode.focus(); this.browserPluginNode.focus();
...@@ -180,110 +179,13 @@ WebViewInternal.prototype.setupFocusPropagation = function() { ...@@ -180,110 +179,13 @@ WebViewInternal.prototype.setupFocusPropagation = function() {
}.bind(this)); }.bind(this));
}; };
// Navigates to the previous history entry. // Validation helper function for executeScript() and insertCSS().
WebViewInternal.prototype.back = function(callback) {
return this.go(-1, callback);
};
// Navigates to the subsequent history entry.
WebViewInternal.prototype.forward = function(callback) {
return this.go(1, callback);
};
// Returns whether there is a previous history entry to navigate to.
WebViewInternal.prototype.canGoBack = function() {
return this.entryCount > 1 && this.currentEntryIndex > 0;
};
// Returns whether there is a subsequent history entry to navigate to.
WebViewInternal.prototype.canGoForward = function() {
return this.currentEntryIndex >= 0 &&
this.currentEntryIndex < (this.entryCount - 1);
};
// Clears browsing data for the WebView partition.
WebViewInternal.prototype.clearData = function() {
if (!this.guestInstanceId) {
return;
}
var args = $Array.concat([this.guestInstanceId], $Array.slice(arguments));
$Function.apply(WebView.clearData, null, args);
};
// Returns Chrome's internal process ID for the guest web page's current
// process.
WebViewInternal.prototype.getProcessId = function() {
return this.processId;
};
// Navigates to a history entry using a history index relative to the current
// navigation.
WebViewInternal.prototype.go = function(relativeIndex, callback) {
if (!this.guestInstanceId) {
return;
}
WebView.go(this.guestInstanceId, relativeIndex, callback);
};
// Prints the contents of the webview.
WebViewInternal.prototype.print = function() {
this.executeScript({code: 'window.print();'});
};
// Reloads the current top-level page.
WebViewInternal.prototype.reload = function() {
if (!this.guestInstanceId) {
return;
}
WebView.reload(this.guestInstanceId);
};
// Stops loading the current navigation if one is in progress.
WebViewInternal.prototype.stop = function() {
if (!this.guestInstanceId) {
return;
}
WebView.stop(this.guestInstanceId);
};
// Forcibly kills the guest web page's renderer process.
WebViewInternal.prototype.terminate = function() {
if (!this.guestInstanceId) {
return;
}
WebView.terminate(this.guestInstanceId);
};
WebViewInternal.prototype.validateExecuteCodeCall = function() { WebViewInternal.prototype.validateExecuteCodeCall = function() {
if (!this.guestInstanceId) { if (!this.guestInstanceId) {
throw new Error(ERROR_MSG_CANNOT_INJECT_SCRIPT); throw new Error(ERROR_MSG_CANNOT_INJECT_SCRIPT);
} }
}; };
// Injects JavaScript code into the guest page.
WebViewInternal.prototype.executeScript = function(var_args) {
this.validateExecuteCodeCall();
var webview_src = this.src;
if (this.baseUrlForDataUrl != '') {
webview_src = this.baseUrlForDataUrl;
}
var args = $Array.concat([this.guestInstanceId, webview_src],
$Array.slice(arguments));
$Function.apply(WebView.executeScript, null, args);
};
// Injects CSS into the guest page.
WebViewInternal.prototype.insertCSS = function(var_args) {
this.validateExecuteCodeCall();
var webview_src = this.src;
if (this.baseUrlForDataUrl != '') {
webview_src = this.baseUrlForDataUrl;
}
var args = $Array.concat([this.guestInstanceId, webview_src],
$Array.slice(arguments));
$Function.apply(WebView.insertCSS, null, args);
};
WebViewInternal.prototype.setupAutoSizeProperties = function() { WebViewInternal.prototype.setupAutoSizeProperties = function() {
$Array.forEach(AUTO_SIZE_ATTRIBUTES, function(attributeName) { $Array.forEach(AUTO_SIZE_ATTRIBUTES, function(attributeName) {
this[attributeName] = this.webviewNode.getAttribute(attributeName); this[attributeName] = this.webviewNode.getAttribute(attributeName);
...@@ -364,10 +266,6 @@ WebViewInternal.prototype.setupWebviewNodeProperties = function() { ...@@ -364,10 +266,6 @@ WebViewInternal.prototype.setupWebviewNodeProperties = function() {
}); });
}; };
WebViewInternal.prototype.setupWebviewNodeAttributes = function() {
this.setupWebViewSrcAttributeMutationObserver();
};
// The purpose of this mutation observer is to catch assignment to the src // The purpose of this mutation observer is to catch assignment to the src
// attribute without any changes to its value. This is useful in the case // attribute without any changes to its value. This is useful in the case
// where the webview guest has crashed and navigating to the same address // where the webview guest has crashed and navigating to the same address
...@@ -592,7 +490,7 @@ WebViewInternal.prototype.parseSrcAttribute = function(result) { ...@@ -592,7 +490,7 @@ WebViewInternal.prototype.parseSrcAttribute = function(result) {
return; return;
} }
if (!this.hasGuestInstanceID()) { if (this.guestInstanceId == undefined) {
if (this.beforeFirstNavigation) { if (this.beforeFirstNavigation) {
this.beforeFirstNavigation = false; this.beforeFirstNavigation = false;
this.createGuest(); this.createGuest();
...@@ -602,7 +500,6 @@ WebViewInternal.prototype.parseSrcAttribute = function(result) { ...@@ -602,7 +500,6 @@ WebViewInternal.prototype.parseSrcAttribute = function(result) {
// Navigate to |this.src|. // Navigate to |this.src|.
WebView.navigate(this.guestInstanceId, this.src); WebView.navigate(this.guestInstanceId, this.src);
return true;
}; };
WebViewInternal.prototype.parseAttributes = function() { WebViewInternal.prototype.parseAttributes = function() {
...@@ -615,10 +512,6 @@ WebViewInternal.prototype.parseAttributes = function() { ...@@ -615,10 +512,6 @@ WebViewInternal.prototype.parseAttributes = function() {
this.parseSrcAttribute(result); this.parseSrcAttribute(result);
}; };
WebViewInternal.prototype.hasGuestInstanceID = function() {
return this.guestInstanceId != undefined;
};
WebViewInternal.prototype.createGuest = function() { WebViewInternal.prototype.createGuest = function() {
if (this.pendingGuestCreation) { if (this.pendingGuestCreation) {
return; return;
...@@ -700,17 +593,159 @@ WebViewInternal.prototype.onAttach = function(storagePartitionId) { ...@@ -700,17 +593,159 @@ WebViewInternal.prototype.onAttach = function(storagePartitionId) {
this.partition.fromAttribute(storagePartitionId, this.hasNavigated()); this.partition.fromAttribute(storagePartitionId, this.hasNavigated());
}; };
WebViewInternal.prototype.buildAttachParams = function(isNewWindow) {
var params = {
'allowtransparency': this.allowtransparency || false,
'autosize': this.webviewNode.hasAttribute(WEB_VIEW_ATTRIBUTE_AUTOSIZE),
'instanceId': this.viewInstanceId,
'maxheight': parseInt(this.maxheight || 0),
'maxwidth': parseInt(this.maxwidth || 0),
'minheight': parseInt(this.minheight || 0),
'minwidth': parseInt(this.minwidth || 0),
'name': this.name,
// We don't need to navigate new window from here.
'src': isNewWindow ? undefined : this.src,
// If we have a partition from the opener, that will also be already
// set via this.onAttach().
'storagePartitionId': this.partition.toAttribute(),
'userAgentOverride': this.userAgentOverride
};
return params;
};
WebViewInternal.prototype.attachWindow = function(guestInstanceId,
isNewWindow) {
this.guestInstanceId = guestInstanceId;
var params = this.buildAttachParams(isNewWindow);
if (!this.isPluginInRenderTree()) {
this.deferredAttachState = {isNewWindow: isNewWindow};
return true;
}
this.deferredAttachState = null;
return guestViewInternalNatives.AttachGuest(
this.internalInstanceId,
this.guestInstanceId,
params, function(w) {
this.contentWindow = w;
}.bind(this)
);
};
// -----------------------------------------------------------------------------
// Public-facing API methods.
// Navigates to the previous history entry.
WebViewInternal.prototype.back = function(callback) {
return this.go(-1, callback);
};
// Returns whether there is a previous history entry to navigate to.
WebViewInternal.prototype.canGoBack = function() {
return this.entryCount > 1 && this.currentEntryIndex > 0;
};
// Returns whether there is a subsequent history entry to navigate to.
WebViewInternal.prototype.canGoForward = function() {
return this.currentEntryIndex >= 0 &&
this.currentEntryIndex < (this.entryCount - 1);
};
// Clears browsing data for the WebView partition.
WebViewInternal.prototype.clearData = function() {
if (!this.guestInstanceId) {
return;
}
var args = $Array.concat([this.guestInstanceId], $Array.slice(arguments));
$Function.apply(WebView.clearData, null, args);
};
// Injects JavaScript code into the guest page.
WebViewInternal.prototype.executeScript = function(var_args) {
this.validateExecuteCodeCall();
var webviewSrc = this.src;
if (this.baseUrlForDataUrl != '') {
webviewSrc = this.baseUrlForDataUrl;
}
var args = $Array.concat([this.guestInstanceId, webviewSrc],
$Array.slice(arguments));
$Function.apply(WebView.executeScript, null, args);
};
// Initiates a find-in-page request.
WebViewInternal.prototype.find = function(search_text, options, callback) {
if (!this.guestInstanceId) {
return;
}
WebView.find(this.guestInstanceId, search_text, options, callback);
};
// Navigates to the subsequent history entry.
WebViewInternal.prototype.forward = function(callback) {
return this.go(1, callback);
};
// Returns Chrome's internal process ID for the guest web page's current
// process.
WebViewInternal.prototype.getProcessId = function() {
return this.processId;
};
// Returns the user agent string used by the webview for guest page requests. // Returns the user agent string used by the webview for guest page requests.
WebViewInternal.prototype.getUserAgent = function() { WebViewInternal.prototype.getUserAgent = function() {
return this.userAgentOverride || navigator.userAgent; return this.userAgentOverride || navigator.userAgent;
}; };
// Gets the current zoom factor.
WebViewInternal.prototype.getZoom = function(callback) {
if (!this.guestInstanceId) {
return;
}
WebView.getZoom(this.guestInstanceId, callback);
};
// Navigates to a history entry using a history index relative to the current
// navigation.
WebViewInternal.prototype.go = function(relativeIndex, callback) {
if (!this.guestInstanceId) {
return;
}
WebView.go(this.guestInstanceId, relativeIndex, callback);
};
// Injects CSS into the guest page.
WebViewInternal.prototype.insertCSS = function(var_args) {
this.validateExecuteCodeCall();
var webviewSrc = this.src;
if (this.baseUrlForDataUrl != '') {
webviewSrc = this.baseUrlForDataUrl;
}
var args = $Array.concat([this.guestInstanceId, webviewSrc],
$Array.slice(arguments));
$Function.apply(WebView.insertCSS, null, args);
};
// Indicates whether or not the webview's user agent string has been overridden. // Indicates whether or not the webview's user agent string has been overridden.
WebViewInternal.prototype.isUserAgentOverridden = function() { WebViewInternal.prototype.isUserAgentOverridden = function() {
return !!this.userAgentOverride && return !!this.userAgentOverride &&
this.userAgentOverride != navigator.userAgent; this.userAgentOverride != navigator.userAgent;
}; };
// Prints the contents of the webview.
WebViewInternal.prototype.print = function() {
this.executeScript({code: 'window.print();'});
};
// Reloads the current top-level page.
WebViewInternal.prototype.reload = function() {
if (!this.guestInstanceId) {
return;
}
WebView.reload(this.guestInstanceId);
};
// Override the user agent string used by the webview for guest page requests. // Override the user agent string used by the webview for guest page requests.
WebViewInternal.prototype.setUserAgentOverride = function(userAgentOverride) { WebViewInternal.prototype.setUserAgentOverride = function(userAgentOverride) {
this.userAgentOverride = userAgentOverride; this.userAgentOverride = userAgentOverride;
...@@ -722,77 +757,39 @@ WebViewInternal.prototype.setUserAgentOverride = function(userAgentOverride) { ...@@ -722,77 +757,39 @@ WebViewInternal.prototype.setUserAgentOverride = function(userAgentOverride) {
WebView.overrideUserAgent(this.guestInstanceId, userAgentOverride); WebView.overrideUserAgent(this.guestInstanceId, userAgentOverride);
}; };
// Initiates a find-in-page request. // Changes the zoom factor of the page.
WebViewInternal.prototype.find = function(search_text, options, callback) { WebViewInternal.prototype.setZoom = function(zoomFactor, callback) {
if (!this.guestInstanceId) { if (!this.guestInstanceId) {
return; return;
} }
WebView.find(this.guestInstanceId, search_text, options, callback); WebView.setZoom(this.guestInstanceId, zoomFactor, callback);
}; };
// Ends the current find session. // Stops loading the current navigation if one is in progress.
WebViewInternal.prototype.stopFinding = function(action) { WebViewInternal.prototype.stop = function() {
if (!this.guestInstanceId) { if (!this.guestInstanceId) {
return; return;
} }
WebView.stopFinding(this.guestInstanceId, action); WebView.stop(this.guestInstanceId);
}; };
// Changes the zoom factor of the page. // Ends the current find session.
WebViewInternal.prototype.setZoom = function(zoomFactor, callback) { WebViewInternal.prototype.stopFinding = function(action) {
if (!this.guestInstanceId) { if (!this.guestInstanceId) {
return; return;
} }
WebView.setZoom(this.guestInstanceId, zoomFactor, callback); WebView.stopFinding(this.guestInstanceId, action);
}; };
// Gets the current zoom factor. // Forcibly kills the guest web page's renderer process.
WebViewInternal.prototype.getZoom = function(callback) { WebViewInternal.prototype.terminate = function() {
if (!this.guestInstanceId) { if (!this.guestInstanceId) {
return; return;
} }
WebView.getZoom(this.guestInstanceId, callback); WebView.terminate(this.guestInstanceId);
};
WebViewInternal.prototype.buildAttachParams = function(isNewWindow) {
var params = {
'allowtransparency': this.allowtransparency || false,
'autosize': this.webviewNode.hasAttribute(WEB_VIEW_ATTRIBUTE_AUTOSIZE),
'instanceId': this.viewInstanceId,
'maxheight': parseInt(this.maxheight || 0),
'maxwidth': parseInt(this.maxwidth || 0),
'minheight': parseInt(this.minheight || 0),
'minwidth': parseInt(this.minwidth || 0),
'name': this.name,
// We don't need to navigate new window from here.
'src': isNewWindow ? undefined : this.src,
// If we have a partition from the opener, that will also be already
// set via this.onAttach().
'storagePartitionId': this.partition.toAttribute(),
'userAgentOverride': this.userAgentOverride
};
return params;
}; };
WebViewInternal.prototype.attachWindow = function(guestInstanceId, // -----------------------------------------------------------------------------
isNewWindow) {
this.guestInstanceId = guestInstanceId;
var params = this.buildAttachParams(isNewWindow);
if (!this.isPluginInRenderTree()) {
this.deferredAttachState = {isNewWindow: isNewWindow};
return true;
}
this.deferredAttachState = null;
return guestViewInternalNatives.AttachGuest(
this.internalInstanceId,
this.guestInstanceId,
params, function(w) {
this.contentWindow = w;
}.bind(this)
);
};
// Registers browser plugin <object> custom element. // Registers browser plugin <object> custom element.
function registerBrowserPluginElement() { function registerBrowserPluginElement() {
...@@ -868,25 +865,25 @@ function registerWebViewElement() { ...@@ -868,25 +865,25 @@ function registerWebViewElement() {
// Public-facing API methods. // Public-facing API methods.
var methods = [ var methods = [
'back', 'back',
'find',
'forward',
'canGoBack', 'canGoBack',
'canGoForward', 'canGoForward',
'clearData', 'clearData',
'executeScript',
'find',
'forward',
'getProcessId', 'getProcessId',
'getUserAgent',
'getZoom', 'getZoom',
'go', 'go',
'insertCSS',
'isUserAgentOverridden',
'print', 'print',
'reload', 'reload',
'setUserAgentOverride',
'setZoom', 'setZoom',
'stop', 'stop',
'stopFinding', 'stopFinding',
'terminate', 'terminate'
'executeScript',
'insertCSS',
'getUserAgent',
'isUserAgentOverridden',
'setUserAgentOverride'
]; ];
// Add the experimental API methods, if available. // Add the experimental API methods, if available.
......
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