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. Default
12 // implementations come from createDefaultApiMethod() in web_view.js.
13 var WEB_VIEW_API_METHODS = [
14 // Add content scripts for the guest page.
17 // Navigates to the previous history entry.
20 // Returns whether there is a previous history entry to navigate to.
23 // Returns whether there is a subsequent history entry to navigate to.
26 // Clears browsing data for the WebView partition.
29 // Injects JavaScript code into the guest page.
32 // Initiates a find-in-page request.
35 // Navigates to the subsequent history entry.
38 // Returns Chrome's internal process ID for the guest web page's current
42 // Returns the user agent string used by the webview for guest page requests.
45 // Gets the current zoom factor.
48 // Gets the current zoom mode of the webview.
51 // Navigates to a history entry using a history index relative to the current
55 // Injects CSS into the guest page.
58 // Indicates whether or not the webview's user agent string has been
60 'isUserAgentOverridden',
62 // Loads a data URL with a specified base URL used for relative links.
63 // Optionally, a virtual URL can be provided to be shown to the user instead
65 'loadDataWithBaseUrl',
67 // Prints the contents of the webview.
70 // Removes content scripts for the guest page.
71 'removeContentScripts',
73 // Reloads the current top-level page.
76 // Override the user agent string used by the webview for guest page requests.
77 'setUserAgentOverride',
79 // Changes the zoom factor of the page.
82 // Changes the zoom mode of the webview.
85 // Stops loading the current navigation if one is in progress.
88 // Ends the current find session.
91 // Forcibly kills the guest web page's renderer process.
95 // -----------------------------------------------------------------------------
96 // Custom API method implementations.
98 WebViewImpl.prototype.addContentScripts = function(rules) {
99 return WebViewInternal.addContentScripts(this.viewInstanceId, rules);
102 WebViewImpl.prototype.back = function(callback) {
103 return this.go(-1, callback);
106 WebViewImpl.prototype.canGoBack = function() {
107 return this.entryCount > 1 && this.currentEntryIndex > 0;
110 WebViewImpl.prototype.canGoForward = function() {
111 return this.currentEntryIndex >= 0 &&
112 this.currentEntryIndex < (this.entryCount - 1);
115 WebViewImpl.prototype.executeScript = function(var_args) {
116 return this.executeCode(WebViewInternal.executeScript,
117 $Array.slice(arguments));
120 WebViewImpl.prototype.forward = function(callback) {
121 return this.go(1, callback);
124 WebViewImpl.prototype.getProcessId = function() {
125 return this.processId;
128 WebViewImpl.prototype.getUserAgent = function() {
129 return this.userAgentOverride || navigator.userAgent;
132 WebViewImpl.prototype.insertCSS = function(var_args) {
133 return this.executeCode(WebViewInternal.insertCSS, $Array.slice(arguments));
136 WebViewImpl.prototype.isUserAgentOverridden = function() {
137 return !!this.userAgentOverride &&
138 this.userAgentOverride != navigator.userAgent;
141 WebViewImpl.prototype.loadDataWithBaseUrl = function(
142 dataUrl, baseUrl, virtualUrl) {
143 if (!this.guest.getId()) {
146 WebViewInternal.loadDataWithBaseUrl(
147 this.guest.getId(), dataUrl, baseUrl, virtualUrl, function() {
148 // Report any errors.
149 if (chrome.runtime.lastError != undefined) {
150 window.console.error(
151 'Error while running webview.loadDataWithBaseUrl: ' +
152 chrome.runtime.lastError.message);
157 WebViewImpl.prototype.print = function() {
158 return this.executeScript({code: 'window.print();'});
161 WebViewImpl.prototype.removeContentScripts = function(names) {
162 return WebViewInternal.removeContentScripts(this.viewInstanceId, names);
165 WebViewImpl.prototype.setUserAgentOverride = function(userAgentOverride) {
166 this.userAgentOverride = userAgentOverride;
167 if (!this.guest.getId()) {
168 // If we are not attached yet, then we will pick up the user agent on
172 WebViewInternal.overrideUserAgent(this.guest.getId(), userAgentOverride);
176 WebViewImpl.prototype.setZoom = function(zoomFactor, callback) {
177 if (!this.guest.getId()) {
178 this.cachedZoomFactor = zoomFactor;
181 this.cachedZoomFactor = 1;
182 WebViewInternal.setZoom(this.guest.getId(), zoomFactor, callback);
186 // -----------------------------------------------------------------------------
188 WebViewImpl.getApiMethods = function() {
189 return WEB_VIEW_API_METHODS;