diff options
author | paulmeyer <paulmeyer@chromium.org> | 2014-10-10 08:35:29 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2014-10-10 15:36:00 +0000 |
commit | ac1216a904719ebe0a7ba6ab3936571f6bd74caa (patch) | |
tree | 928da1cf9cd8099f7a12b253b9e63ea9a4d7e58d /extensions | |
parent | 0726961302dc0cbfb91cd64f6b361477fbad924a (diff) | |
download | chromium_src-ac1216a904719ebe0a7ba6ab3936571f6bd74caa.zip chromium_src-ac1216a904719ebe0a7ba6ab3936571f6bd74caa.tar.gz chromium_src-ac1216a904719ebe0a7ba6ab3936571f6bd74caa.tar.bz2 |
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}
Diffstat (limited to 'extensions')
-rw-r--r-- | extensions/renderer/resources/web_view.js | 335 |
1 files changed, 166 insertions, 169 deletions
diff --git a/extensions/renderer/resources/web_view.js b/extensions/renderer/resources/web_view.js index 88540fb..3965e61 100644 --- a/extensions/renderer/resources/web_view.js +++ b/extensions/renderer/resources/web_view.js @@ -107,7 +107,7 @@ function WebViewInternal(webviewNode) { var shadowRoot = this.webviewNode.createShadowRoot(); this.partition = new Partition(); - this.setupWebviewNodeAttributes(); + this.setupWebViewSrcAttributeMutationObserver(); this.setupFocusPropagation(); this.setupWebviewNodeProperties(); @@ -169,7 +169,6 @@ WebViewInternal.prototype.setupFocusPropagation = function() { // See http://crbug.com/231664. this.webviewNode.setAttribute('tabIndex', -1); } - var self = this; this.webviewNode.addEventListener('focus', function(e) { // Focus the BrowserPlugin when the <webview> takes focus. this.browserPluginNode.focus(); @@ -180,110 +179,13 @@ WebViewInternal.prototype.setupFocusPropagation = function() { }.bind(this)); }; -// Navigates to the previous history entry. -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); -}; - +// Validation helper function for executeScript() and insertCSS(). WebViewInternal.prototype.validateExecuteCodeCall = function() { if (!this.guestInstanceId) { 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() { $Array.forEach(AUTO_SIZE_ATTRIBUTES, function(attributeName) { this[attributeName] = this.webviewNode.getAttribute(attributeName); @@ -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 // 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 @@ -592,7 +490,7 @@ WebViewInternal.prototype.parseSrcAttribute = function(result) { return; } - if (!this.hasGuestInstanceID()) { + if (this.guestInstanceId == undefined) { if (this.beforeFirstNavigation) { this.beforeFirstNavigation = false; this.createGuest(); @@ -602,7 +500,6 @@ WebViewInternal.prototype.parseSrcAttribute = function(result) { // Navigate to |this.src|. WebView.navigate(this.guestInstanceId, this.src); - return true; }; WebViewInternal.prototype.parseAttributes = function() { @@ -615,10 +512,6 @@ WebViewInternal.prototype.parseAttributes = function() { this.parseSrcAttribute(result); }; -WebViewInternal.prototype.hasGuestInstanceID = function() { - return this.guestInstanceId != undefined; -}; - WebViewInternal.prototype.createGuest = function() { if (this.pendingGuestCreation) { return; @@ -700,17 +593,159 @@ WebViewInternal.prototype.onAttach = function(storagePartitionId) { 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. WebViewInternal.prototype.getUserAgent = function() { 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. WebViewInternal.prototype.isUserAgentOverridden = function() { return !!this.userAgentOverride && 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. WebViewInternal.prototype.setUserAgentOverride = function(userAgentOverride) { this.userAgentOverride = userAgentOverride; @@ -722,77 +757,39 @@ WebViewInternal.prototype.setUserAgentOverride = function(userAgentOverride) { WebView.overrideUserAgent(this.guestInstanceId, userAgentOverride); }; -// Initiates a find-in-page request. -WebViewInternal.prototype.find = function(search_text, options, callback) { +// Changes the zoom factor of the page. +WebViewInternal.prototype.setZoom = function(zoomFactor, callback) { if (!this.guestInstanceId) { return; } - WebView.find(this.guestInstanceId, search_text, options, callback); + WebView.setZoom(this.guestInstanceId, zoomFactor, callback); }; -// Ends the current find session. -WebViewInternal.prototype.stopFinding = function(action) { +// Stops loading the current navigation if one is in progress. +WebViewInternal.prototype.stop = function() { if (!this.guestInstanceId) { return; } - WebView.stopFinding(this.guestInstanceId, action); + WebView.stop(this.guestInstanceId); }; -// Changes the zoom factor of the page. -WebViewInternal.prototype.setZoom = function(zoomFactor, callback) { +// Ends the current find session. +WebViewInternal.prototype.stopFinding = function(action) { if (!this.guestInstanceId) { return; } - WebView.setZoom(this.guestInstanceId, zoomFactor, callback); + WebView.stopFinding(this.guestInstanceId, action); }; -// Gets the current zoom factor. -WebViewInternal.prototype.getZoom = function(callback) { +// Forcibly kills the guest web page's renderer process. +WebViewInternal.prototype.terminate = function() { if (!this.guestInstanceId) { return; } - WebView.getZoom(this.guestInstanceId, callback); -}; - -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; + WebView.terminate(this.guestInstanceId); }; -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. function registerBrowserPluginElement() { @@ -868,25 +865,25 @@ function registerWebViewElement() { // Public-facing API methods. var methods = [ 'back', - 'find', - 'forward', 'canGoBack', 'canGoForward', 'clearData', + 'executeScript', + 'find', + 'forward', 'getProcessId', + 'getUserAgent', 'getZoom', 'go', + 'insertCSS', + 'isUserAgentOverridden', 'print', 'reload', + 'setUserAgentOverride', 'setZoom', 'stop', 'stopFinding', - 'terminate', - 'executeScript', - 'insertCSS', - 'getUserAgent', - 'isUserAgentOverridden', - 'setUserAgentOverride' + 'terminate' ]; // Add the experimental API methods, if available. |