Commit 8f204c40 authored by Kevin McNee's avatar Kevin McNee Committed by Commit Bot

Move <webview> public api implementation to the element's prototype

Currently, all of the <webview> public api methods forward to
WebViewImpl. We now implement methods that don't involve internal
details of WebViewImpl on the <webview> element itself. For methods
that do require details of WebViewImpl or WebViewInternal, we forward
the calls as needed.

Bug: 793935, 867831
Change-Id: Iad6a1a1b27c9351e7f7edeaa246b26e799be697d
Reviewed-on: https://chromium-review.googlesource.com/1188868
Commit-Queue: Kevin McNee <mcnee@chromium.org>
Reviewed-by: default avatarPaul Meyer <paulmeyer@chromium.org>
Cr-Commit-Position: refs/heads/master@{#589915}
parent d7c481fb
......@@ -12,6 +12,6 @@ var AppViewImpl = require('appView').AppViewImpl;
class AppViewElement extends GuestViewContainerElement {}
forwardApiMethods(AppViewElement, ['connect']);
forwardApiMethods(AppViewElement, AppViewImpl, null, ['connect']);
registerElement('AppView', AppViewElement, AppViewImpl);
......@@ -7,8 +7,6 @@
var GuestViewContainer = require('guestViewContainer').GuestViewContainer;
var ExtensionViewConstants =
require('extensionViewConstants').ExtensionViewConstants;
var EXTENSION_VIEW_API_METHODS =
require('extensionViewApiMethods').EXTENSION_VIEW_API_METHODS;
var ExtensionViewAttributes =
require('extensionViewAttributes').ExtensionViewAttributes;
var ExtensionViewEvents = require('extensionViewEvents').ExtensionViewEvents;
......
......@@ -15,6 +15,7 @@ var EXTENSION_VIEW_API_METHODS =
class ExtensionViewElement extends GuestViewContainerElement {}
// Forward ExtensionViewElement.foo* method calls to ExtensionViewImpl.foo*.
forwardApiMethods(ExtensionViewElement, EXTENSION_VIEW_API_METHODS);
forwardApiMethods(
ExtensionViewElement, ExtensionViewImpl, null, EXTENSION_VIEW_API_METHODS);
registerElement('ExtensionView', ExtensionViewElement, ExtensionViewImpl);
......@@ -8,6 +8,7 @@
var GuestViewContainer = require('guestViewContainer').GuestViewContainer;
var DocumentNatives = requireNative('document_natives');
var IdGenerator = requireNative('id_generator');
var logging = requireNative('logging');
// Registers the browserplugin and guestview as custom elements once the
// document has loaded.
......@@ -128,16 +129,40 @@ function registerGuestViewElement(
}
// Forward public API methods from |containerElementType|'s prototype to their
// internal implementations.
function forwardApiMethods(containerElementType, methodNames) {
var createProtoHandler = function(m) {
// internal implementations. If the method is defined on |containerType|, we
// forward to that. Otherwise, we forward to the method on |internalApi|.
function forwardApiMethods(
containerElementType, containerType, internalApi, methodNames) {
var createContainerImplHandler = function(m) {
return function(var_args) {
var internal = privates(this).internal;
return $Function.apply(internal[m], internal, arguments);
};
};
var createInternalApiHandler = function(m) {
return function(var_args) {
var internal = privates(this).internal;
var instanceId = internal.guest.getId();
if (!instanceId) {
return false;
}
var args = $Array.concat([instanceId], $Array.slice(arguments));
$Function.apply(internalApi[m], null, args);
return true;
};
};
for (var m of methodNames) {
containerElementType.prototype[m] = createProtoHandler(m);
if (!containerElementType.prototype[m]) {
if (containerType.prototype[m]) {
containerElementType.prototype[m] = createContainerImplHandler(m);
} else if (internalApi && internalApi[m]) {
containerElementType.prototype[m] = createInternalApiHandler(m);
} else {
logging.DCHECK(false, m + ' has no implementation.');
}
}
}
}
......
......@@ -10,7 +10,6 @@ var GuestView = require('guestView').GuestView;
var GuestViewContainer = require('guestViewContainer').GuestViewContainer;
var GuestViewInternalNatives = requireNative('guest_view_internal');
var WebViewConstants = require('webViewConstants').WebViewConstants;
var WEB_VIEW_API_METHODS = require('webViewApiMethods').WEB_VIEW_API_METHODS;
var WebViewAttributes = require('webViewAttributes').WebViewAttributes;
var WebViewEvents = require('webViewEvents').WebViewEvents;
var WebViewInternal = getInternalApi ?
......@@ -188,14 +187,6 @@ WebViewImpl.prototype.attachWindow$ = function(opt_guestInstanceId) {
return $Function.call(GuestViewContainer.prototype.attachWindow$, this);
};
WebViewImpl.prototype.addContentScripts = function(rules) {
return WebViewInternal.addContentScripts(this.viewInstanceId, rules);
};
WebViewImpl.prototype.removeContentScripts = function(names) {
return WebViewInternal.removeContentScripts(this.viewInstanceId, names);
};
// Shared implementation of executeScript() and insertCSS().
WebViewImpl.prototype.executeCode = function(func, args) {
if (!this.guest.getId()) {
......@@ -214,45 +205,6 @@ WebViewImpl.prototype.executeCode = function(func, args) {
return true;
};
WebViewImpl.prototype.executeScript = function(var_args) {
return this.executeCode(
WebViewInternal.executeScript, $Array.slice(arguments));
};
WebViewImpl.prototype.insertCSS = function(var_args) {
return this.executeCode(WebViewInternal.insertCSS, $Array.slice(arguments));
};
WebViewImpl.prototype.back = function(callback) {
return this.go(-1, callback);
};
WebViewImpl.prototype.canGoBack = function() {
return this.entryCount > 1 && this.currentEntryIndex > 0;
};
WebViewImpl.prototype.canGoForward = function() {
return this.currentEntryIndex >= 0 &&
this.currentEntryIndex < (this.entryCount - 1);
};
WebViewImpl.prototype.forward = function(callback) {
return this.go(1, callback);
};
WebViewImpl.prototype.getProcessId = function() {
return this.processId;
};
WebViewImpl.prototype.getUserAgent = function() {
return this.userAgentOverride || navigator.userAgent;
};
WebViewImpl.prototype.isUserAgentOverridden = function() {
return !!this.userAgentOverride &&
this.userAgentOverride != navigator.userAgent;
};
WebViewImpl.prototype.setUserAgentOverride = function(userAgentOverride) {
this.userAgentOverride = userAgentOverride;
if (!this.guest.getId()) {
......@@ -280,10 +232,6 @@ WebViewImpl.prototype.loadDataWithBaseUrl = function(
});
};
WebViewImpl.prototype.print = function() {
return this.executeScript({code: 'window.print();'});
};
WebViewImpl.prototype.setZoom = function(zoomFactor, callback) {
if (!this.guest.getId()) {
this.cachedZoomFactor = zoomFactor;
......@@ -304,26 +252,5 @@ WebViewImpl.prototype.makeElementFullscreen = function() {
// Implemented when the ChromeWebView API is available.
WebViewImpl.prototype.maybeSetupContextMenus = function() {};
(() => {
// For <webview> API methods which aren't explicitly defined on |WebViewImpl|,
// create default implementations which forward the call to |WebViewInternal|.
var createDefaultApiMethod = function(m) {
return function(var_args) {
if (!this.guest.getId()) {
return false;
}
var args = $Array.concat([this.guest.getId()], $Array.slice(arguments));
$Function.apply(WebViewInternal[m], null, args);
return true;
};
};
for (var methodName of WEB_VIEW_API_METHODS) {
if (WebViewImpl.prototype[methodName] == undefined) {
WebViewImpl.prototype[methodName] = createDefaultApiMethod(methodName);
}
}
})();
// Exports.
exports.$set('WebViewImpl', WebViewImpl);
......@@ -10,10 +10,84 @@ var GuestViewContainerElement =
require('guestViewContainerElement').GuestViewContainerElement;
var WebViewImpl = require('webView').WebViewImpl;
var WEB_VIEW_API_METHODS = require('webViewApiMethods').WEB_VIEW_API_METHODS;
var WebViewInternal = getInternalApi ?
getInternalApi('webViewInternal') :
require('webViewInternal').WebViewInternal;
class WebViewElement extends GuestViewContainerElement {}
// Forward WebViewElement.foo* method calls to WebViewImpl.foo*.
forwardApiMethods(WebViewElement, WEB_VIEW_API_METHODS);
WebViewElement.prototype.addContentScripts = function(rules) {
var internal = privates(this).internal;
return WebViewInternal.addContentScripts(internal.viewInstanceId, rules);
};
WebViewElement.prototype.removeContentScripts = function(names) {
var internal = privates(this).internal;
return WebViewInternal.removeContentScripts(internal.viewInstanceId, names);
};
WebViewElement.prototype.insertCSS = function(var_args) {
var internal = privates(this).internal;
return internal.executeCode(
WebViewInternal.insertCSS, $Array.slice(arguments));
};
WebViewElement.prototype.executeScript = function(var_args) {
var internal = privates(this).internal;
return internal.executeCode(
WebViewInternal.executeScript, $Array.slice(arguments));
};
WebViewElement.prototype.print = function() {
var internal = privates(this).internal;
return internal.executeCode(
WebViewInternal.executeScript, [{code: 'window.print();'}]);
};
WebViewElement.prototype.back = function(callback) {
return $Function.call(
privates(WebViewElement).originalGo, this, -1, callback);
};
WebViewElement.prototype.canGoBack = function() {
var internal = privates(this).internal;
return internal.entryCount > 1 && internal.currentEntryIndex > 0;
};
WebViewElement.prototype.canGoForward = function() {
var internal = privates(this).internal;
return internal.currentEntryIndex >= 0 &&
internal.currentEntryIndex < (internal.entryCount - 1);
};
WebViewElement.prototype.forward = function(callback) {
return $Function.call(privates(WebViewElement).originalGo, this, 1, callback);
};
WebViewElement.prototype.getProcessId = function() {
var internal = privates(this).internal;
return internal.processId;
};
WebViewElement.prototype.getUserAgent = function() {
var internal = privates(this).internal;
return internal.userAgentOverride || navigator.userAgent;
};
WebViewElement.prototype.isUserAgentOverridden = function() {
var internal = privates(this).internal;
return !!internal.userAgentOverride &&
internal.userAgentOverride != navigator.userAgent;
};
// Forward remaining WebViewElement.foo* method calls to WebViewImpl.foo* or
// WebViewInternal.foo*.
forwardApiMethods(
WebViewElement, WebViewImpl, WebViewInternal, WEB_VIEW_API_METHODS);
// Since |back| and |forward| are implemented in terms of |go|, we need to
// keep a reference to the real |go| function, since user code may override
// |WebViewElement.prototype.go|.
privates(WebViewElement).originalGo = WebViewElement.prototype.go;
registerElement('WebView', WebViewElement, WebViewImpl);
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