1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // This module implements the public-facing API functions for the <webview> tag.
7 var WebViewInternal = require('webViewInternal').WebViewInternal;
8 var WebViewImpl = require('webView').WebViewImpl;
10 // An array of <webview>'s public-facing API methods. Methods without custom
11 // implementations will be given default implementations that call into the
12 // internal API method with the same name in |WebViewInternal|. For example, a
13 // method called 'someApiMethod' would be given the following default
16 // WebViewImpl.prototype.someApiMethod = function(var_args) {
17 // if (!this.guest.getId()) {
20 // var args = $Array.concat([this.guest.getId()], $Array.slice(arguments));
21 // $Function.apply(WebViewInternal.someApiMethod, null, args);
25 // These default implementations come from createDefaultApiMethod() in
27 var WEB_VIEW_API_METHODS = [
28 // Add content scripts for the guest page.
31 // Navigates to the previous history entry.
34 // Returns whether there is a previous history entry to navigate to.
37 // Returns whether there is a subsequent history entry to navigate to.
40 // Clears browsing data for the WebView partition.
43 // Injects JavaScript code into the guest page.
46 // Initiates a find-in-page request.
49 // Navigates to the subsequent history entry.
52 // Returns Chrome's internal process ID for the guest web page's current
56 // Returns the user agent string used by the webview for guest page requests.
59 // Gets the current zoom factor.
62 // Gets the current zoom mode of the webview.
65 // Navigates to a history entry using a history index relative to the current
69 // Injects CSS into the guest page.
72 // Indicates whether or not the webview's user agent string has been
74 'isUserAgentOverridden',
76 // Loads a data URL with a specified base URL used for relative links.
77 // Optionally, a virtual URL can be provided to be shown to the user instead
79 'loadDataWithBaseUrl',
81 // Prints the contents of the webview.
84 // Removes content scripts for the guest page.
85 'removeContentScripts',
87 // Reloads the current top-level page.
90 // Override the user agent string used by the webview for guest page requests.
91 'setUserAgentOverride',
93 // Changes the zoom factor of the page.
96 // Changes the zoom mode of the webview.
99 // Stops loading the current navigation if one is in progress.
102 // Ends the current find session.
105 // Forcibly kills the guest web page's renderer process.
109 // -----------------------------------------------------------------------------
110 // Custom API method implementations.
112 WebViewImpl.prototype.addContentScripts = function(rules) {
113 return WebViewInternal.addContentScripts(this.viewInstanceId, rules);
116 WebViewImpl.prototype.back = function(callback) {
117 return this.go(-1, callback);
120 WebViewImpl.prototype.canGoBack = function() {
121 return this.entryCount > 1 && this.currentEntryIndex > 0;
124 WebViewImpl.prototype.canGoForward = function() {
125 return this.currentEntryIndex >= 0 &&
126 this.currentEntryIndex < (this.entryCount - 1);
129 WebViewImpl.prototype.executeScript = function(var_args) {
130 return this.executeCode(WebViewInternal.executeScript,
131 $Array.slice(arguments));
134 WebViewImpl.prototype.forward = function(callback) {
135 return this.go(1, callback);
138 WebViewImpl.prototype.getProcessId = function() {
139 return this.processId;
142 WebViewImpl.prototype.getUserAgent = function() {
143 return this.userAgentOverride || navigator.userAgent;
146 WebViewImpl.prototype.insertCSS = function(var_args) {
147 return this.executeCode(WebViewInternal.insertCSS, $Array.slice(arguments));
150 WebViewImpl.prototype.isUserAgentOverridden = function() {
151 return !!this.userAgentOverride &&
152 this.userAgentOverride != navigator.userAgent;
155 WebViewImpl.prototype.loadDataWithBaseUrl = function(
156 dataUrl, baseUrl, virtualUrl) {
157 if (!this.guest.getId()) {
160 WebViewInternal.loadDataWithBaseUrl(
161 this.guest.getId(), dataUrl, baseUrl, virtualUrl, function() {
162 // Report any errors.
163 if (chrome.runtime.lastError != undefined) {
164 window.console.error(
165 'Error while running webview.loadDataWithBaseUrl: ' +
166 chrome.runtime.lastError.message);
171 WebViewImpl.prototype.print = function() {
172 return this.executeScript({code: 'window.print();'});
175 WebViewImpl.prototype.removeContentScripts = function(names) {
176 return WebViewInternal.removeContentScripts(this.viewInstanceId, names);
179 WebViewImpl.prototype.setUserAgentOverride = function(userAgentOverride) {
180 this.userAgentOverride = userAgentOverride;
181 if (!this.guest.getId()) {
182 // If we are not attached yet, then we will pick up the user agent on
186 WebViewInternal.overrideUserAgent(this.guest.getId(), userAgentOverride);
190 WebViewImpl.prototype.setZoom = function(zoomFactor, callback) {
191 if (!this.guest.getId()) {
192 this.cachedZoomFactor = zoomFactor;
195 this.cachedZoomFactor = 1;
196 WebViewInternal.setZoom(this.guest.getId(), zoomFactor, callback);
200 // -----------------------------------------------------------------------------
202 WebViewImpl.getApiMethods = function() {
203 return WEB_VIEW_API_METHODS;