OLD | NEW |
| (Empty) |
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. | |
4 | |
5 // This module implements the public-facing API functions for the <webview> tag. | |
6 | |
7 var WebViewInternal = require('webViewInternal').WebViewInternal; | |
8 var WebViewImpl = require('webView').WebViewImpl; | |
9 | |
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 // Navigates to the previous history entry. | |
15 'back', | |
16 | |
17 // Returns whether there is a previous history entry to navigate to. | |
18 'canGoBack', | |
19 | |
20 // Returns whether there is a subsequent history entry to navigate to. | |
21 'canGoForward', | |
22 | |
23 // Clears browsing data for the WebView partition. | |
24 'clearData', | |
25 | |
26 // Injects JavaScript code into the guest page. | |
27 'executeScript', | |
28 | |
29 // Initiates a find-in-page request. | |
30 'find', | |
31 | |
32 // Navigates to the subsequent history entry. | |
33 'forward', | |
34 | |
35 // Returns Chrome's internal process ID for the guest web page's current | |
36 // process. | |
37 'getProcessId', | |
38 | |
39 // Returns the user agent string used by the webview for guest page requests. | |
40 'getUserAgent', | |
41 | |
42 // Gets the current zoom factor. | |
43 'getZoom', | |
44 | |
45 // Navigates to a history entry using a history index relative to the current | |
46 // navigation. | |
47 'go', | |
48 | |
49 // Injects CSS into the guest page. | |
50 'insertCSS', | |
51 | |
52 // Indicates whether or not the webview's user agent string has been | |
53 // overridden. | |
54 'isUserAgentOverridden', | |
55 | |
56 // Loads a data URL with a specified base URL used for relative links. | |
57 // Optionally, a virtual URL can be provided to be shown to the user instead | |
58 // of the data URL. | |
59 'loadDataWithBaseUrl', | |
60 | |
61 // Prints the contents of the webview. | |
62 'print', | |
63 | |
64 // Reloads the current top-level page. | |
65 'reload', | |
66 | |
67 // Override the user agent string used by the webview for guest page requests. | |
68 'setUserAgentOverride', | |
69 | |
70 // Changes the zoom factor of the page. | |
71 'setZoom', | |
72 | |
73 // Stops loading the current navigation if one is in progress. | |
74 'stop', | |
75 | |
76 // Ends the current find session. | |
77 'stopFinding', | |
78 | |
79 // Forcibly kills the guest web page's renderer process. | |
80 'terminate' | |
81 ]; | |
82 | |
83 // ----------------------------------------------------------------------------- | |
84 // Custom API method implementations. | |
85 | |
86 WebViewImpl.prototype.back = function(callback) { | |
87 return this.go(-1, callback); | |
88 }; | |
89 | |
90 WebViewImpl.prototype.canGoBack = function() { | |
91 return this.entryCount > 1 && this.currentEntryIndex > 0; | |
92 }; | |
93 | |
94 WebViewImpl.prototype.canGoForward = function() { | |
95 return this.currentEntryIndex >= 0 && | |
96 this.currentEntryIndex < (this.entryCount - 1); | |
97 }; | |
98 | |
99 WebViewImpl.prototype.executeScript = function(var_args) { | |
100 return this.executeCode(WebViewInternal.executeScript, | |
101 $Array.slice(arguments)); | |
102 }; | |
103 | |
104 WebViewImpl.prototype.forward = function(callback) { | |
105 return this.go(1, callback); | |
106 }; | |
107 | |
108 WebViewImpl.prototype.getProcessId = function() { | |
109 return this.processId; | |
110 }; | |
111 | |
112 WebViewImpl.prototype.getUserAgent = function() { | |
113 return this.userAgentOverride || navigator.userAgent; | |
114 }; | |
115 | |
116 WebViewImpl.prototype.insertCSS = function(var_args) { | |
117 return this.executeCode(WebViewInternal.insertCSS, $Array.slice(arguments)); | |
118 }; | |
119 | |
120 WebViewImpl.prototype.isUserAgentOverridden = function() { | |
121 return !!this.userAgentOverride && | |
122 this.userAgentOverride != navigator.userAgent; | |
123 }; | |
124 | |
125 WebViewImpl.prototype.loadDataWithBaseUrl = function( | |
126 dataUrl, baseUrl, virtualUrl) { | |
127 if (!this.guest.getId()) { | |
128 return; | |
129 } | |
130 WebViewInternal.loadDataWithBaseUrl( | |
131 this.guest.getId(), dataUrl, baseUrl, virtualUrl, function() { | |
132 // Report any errors. | |
133 if (chrome.runtime.lastError != undefined) { | |
134 window.console.error( | |
135 'Error while running webview.loadDataWithBaseUrl: ' + | |
136 chrome.runtime.lastError.message); | |
137 } | |
138 }); | |
139 }; | |
140 | |
141 WebViewImpl.prototype.print = function() { | |
142 return this.executeScript({code: 'window.print();'}); | |
143 }; | |
144 | |
145 WebViewImpl.prototype.setUserAgentOverride = function(userAgentOverride) { | |
146 this.userAgentOverride = userAgentOverride; | |
147 if (!this.guest.getId()) { | |
148 // If we are not attached yet, then we will pick up the user agent on | |
149 // attachment. | |
150 return false; | |
151 } | |
152 WebViewInternal.overrideUserAgent(this.guest.getId(), userAgentOverride); | |
153 return true; | |
154 }; | |
155 | |
156 // ----------------------------------------------------------------------------- | |
157 | |
158 WebViewImpl.getApiMethods = function() { | |
159 return WEB_VIEW_API_METHODS; | |
160 }; | |
OLD | NEW |