OLD | NEW |
| (Empty) |
1 // Copyright 2013 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 #ifndef CHROME_BROWSER_GUESTVIEW_WEBVIEW_WEBVIEW_GUEST_H_ | |
6 #define CHROME_BROWSER_GUESTVIEW_WEBVIEW_WEBVIEW_GUEST_H_ | |
7 | |
8 #include "base/observer_list.h" | |
9 #include "chrome/browser/extensions/tab_helper.h" | |
10 #include "chrome/browser/guestview/guestview.h" | |
11 #include "chrome/browser/guestview/webview/javascript_dialog_helper.h" | |
12 #include "chrome/browser/guestview/webview/webview_find_helper.h" | |
13 #include "content/public/browser/javascript_dialog_manager.h" | |
14 #include "content/public/browser/notification_registrar.h" | |
15 #include "content/public/browser/web_contents_observer.h" | |
16 #include "third_party/WebKit/public/web/WebFindOptions.h" | |
17 | |
18 #if defined(OS_CHROMEOS) | |
19 #include "chrome/browser/chromeos/accessibility/accessibility_manager.h" | |
20 #endif | |
21 | |
22 namespace extensions { | |
23 class ScriptExecutor; | |
24 class WebviewFindFunction; | |
25 } // namespace extensions | |
26 | |
27 // A WebViewGuest is a WebContentsObserver on the guest WebContents of a | |
28 // <webview> tag. It provides the browser-side implementation of the <webview> | |
29 // API and manages the lifetime of <webview> extension events. WebViewGuest is | |
30 // created on attachment. That is, when a guest WebContents is associated with | |
31 // a particular embedder WebContents. This happens on either initial navigation | |
32 // or through the use of the New Window API, when a new window is attached to | |
33 // a particular <webview>. | |
34 class WebViewGuest : public GuestView, | |
35 public content::NotificationObserver, | |
36 public content::WebContentsObserver { | |
37 public: | |
38 WebViewGuest(content::WebContents* guest_web_contents, | |
39 const std::string& embedder_extension_id); | |
40 | |
41 static WebViewGuest* From(int embedder_process_id, int instance_id); | |
42 static WebViewGuest* FromWebContents(content::WebContents* contents); | |
43 // Returns guestview::kInstanceIDNone if |contents| does not correspond to a | |
44 // WebViewGuest. | |
45 static int GetViewInstanceId(content::WebContents* contents); | |
46 | |
47 // GuestView implementation. | |
48 virtual void Attach(content::WebContents* embedder_web_contents, | |
49 const base::DictionaryValue& args) OVERRIDE; | |
50 virtual GuestView::Type GetViewType() const OVERRIDE; | |
51 virtual WebViewGuest* AsWebView() OVERRIDE; | |
52 virtual AdViewGuest* AsAdView() OVERRIDE; | |
53 | |
54 // GuestDelegate implementation. | |
55 virtual void AddMessageToConsole(int32 level, | |
56 const base::string16& message, | |
57 int32 line_no, | |
58 const base::string16& source_id) OVERRIDE; | |
59 virtual void LoadProgressed(double progress) OVERRIDE; | |
60 virtual void Close() OVERRIDE; | |
61 virtual void DidAttach() OVERRIDE; | |
62 virtual void EmbedderDestroyed() OVERRIDE; | |
63 virtual void FindReply(int request_id, | |
64 int number_of_matches, | |
65 const gfx::Rect& selection_rect, | |
66 int active_match_ordinal, | |
67 bool final_update) OVERRIDE; | |
68 virtual void GuestProcessGone(base::TerminationStatus status) OVERRIDE; | |
69 virtual bool HandleKeyboardEvent( | |
70 const content::NativeWebKeyboardEvent& event) OVERRIDE; | |
71 virtual bool IsDragAndDropEnabled() OVERRIDE; | |
72 virtual bool IsOverridingUserAgent() const OVERRIDE; | |
73 virtual void LoadAbort(bool is_top_level, | |
74 const GURL& url, | |
75 const std::string& error_type) OVERRIDE; | |
76 virtual void RendererResponsive() OVERRIDE; | |
77 virtual void RendererUnresponsive() OVERRIDE; | |
78 virtual void RequestPermission( | |
79 BrowserPluginPermissionType permission_type, | |
80 const base::DictionaryValue& request_info, | |
81 const PermissionResponseCallback& callback, | |
82 bool allowed_by_default) OVERRIDE; | |
83 virtual GURL ResolveURL(const std::string& src) OVERRIDE; | |
84 virtual void SizeChanged(const gfx::Size& old_size, const gfx::Size& new_size) | |
85 OVERRIDE; | |
86 virtual void RequestMediaAccessPermission( | |
87 const content::MediaStreamRequest& request, | |
88 const content::MediaResponseCallback& callback) OVERRIDE; | |
89 virtual void CanDownload(const std::string& request_method, | |
90 const GURL& url, | |
91 const base::Callback<void(bool)>& callback) OVERRIDE; | |
92 virtual void RequestPointerLockPermission( | |
93 bool user_gesture, | |
94 bool last_unlocked_by_target, | |
95 const base::Callback<void(bool)>& callback) OVERRIDE; | |
96 virtual content::JavaScriptDialogManager* | |
97 GetJavaScriptDialogManager() OVERRIDE; | |
98 | |
99 // NotificationObserver implementation. | |
100 virtual void Observe(int type, | |
101 const content::NotificationSource& source, | |
102 const content::NotificationDetails& details) OVERRIDE; | |
103 | |
104 // Set the zoom factor. | |
105 virtual void SetZoom(double zoom_factor) OVERRIDE; | |
106 | |
107 // Returns the current zoom factor. | |
108 double GetZoom(); | |
109 | |
110 // Begin or continue a find request. | |
111 void Find(const base::string16& search_text, | |
112 const blink::WebFindOptions& options, | |
113 scoped_refptr<extensions::WebviewFindFunction> find_function); | |
114 | |
115 // Conclude a find request to clear highlighting. | |
116 void StopFinding(content::StopFindAction); | |
117 | |
118 // If possible, navigate the guest to |relative_index| entries away from the | |
119 // current navigation entry. | |
120 void Go(int relative_index); | |
121 | |
122 // Reload the guest. | |
123 void Reload(); | |
124 | |
125 // Requests Geolocation Permission from the embedder. | |
126 void RequestGeolocationPermission(int bridge_id, | |
127 const GURL& requesting_frame, | |
128 bool user_gesture, | |
129 const base::Callback<void(bool)>& callback); | |
130 | |
131 void OnWebViewGeolocationPermissionResponse( | |
132 int bridge_id, | |
133 bool user_gesture, | |
134 const base::Callback<void(bool)>& callback, | |
135 bool allow, | |
136 const std::string& user_input); | |
137 | |
138 void CancelGeolocationPermissionRequest(int bridge_id); | |
139 | |
140 void OnWebViewMediaPermissionResponse( | |
141 const content::MediaStreamRequest& request, | |
142 const content::MediaResponseCallback& callback, | |
143 bool allow, | |
144 const std::string& user_input); | |
145 | |
146 void OnWebViewDownloadPermissionResponse( | |
147 const base::Callback<void(bool)>& callback, | |
148 bool allow, | |
149 const std::string& user_input); | |
150 | |
151 void OnWebViewPointerLockPermissionResponse( | |
152 const base::Callback<void(bool)>& callback, | |
153 bool allow, | |
154 const std::string& user_input); | |
155 | |
156 enum PermissionResponseAction { | |
157 DENY, | |
158 ALLOW, | |
159 DEFAULT | |
160 }; | |
161 | |
162 enum SetPermissionResult { | |
163 SET_PERMISSION_INVALID, | |
164 SET_PERMISSION_ALLOWED, | |
165 SET_PERMISSION_DENIED | |
166 }; | |
167 | |
168 // Responds to the permission request |request_id| with |action| and | |
169 // |user_input|. Returns whether there was a pending request for the provided | |
170 // |request_id|. | |
171 SetPermissionResult SetPermission(int request_id, | |
172 PermissionResponseAction action, | |
173 const std::string& user_input); | |
174 | |
175 // Overrides the user agent for this guest. | |
176 // This affects subsequent guest navigations. | |
177 void SetUserAgentOverride(const std::string& user_agent_override); | |
178 | |
179 // Stop loading the guest. | |
180 void Stop(); | |
181 | |
182 // Kill the guest process. | |
183 void Terminate(); | |
184 | |
185 // Clears data in the storage partition of this guest. | |
186 // | |
187 // Partition data that are newer than |removal_since| will be removed. | |
188 // |removal_mask| corresponds to bitmask in StoragePartition::RemoveDataMask. | |
189 bool ClearData(const base::Time remove_since, | |
190 uint32 removal_mask, | |
191 const base::Closure& callback); | |
192 | |
193 extensions::ScriptExecutor* script_executor() { | |
194 return script_executor_.get(); | |
195 } | |
196 | |
197 private: | |
198 virtual ~WebViewGuest(); | |
199 | |
200 // A map to store the callback for a request keyed by the request's id. | |
201 struct PermissionResponseInfo { | |
202 PermissionResponseCallback callback; | |
203 BrowserPluginPermissionType permission_type; | |
204 bool allowed_by_default; | |
205 PermissionResponseInfo(); | |
206 PermissionResponseInfo(const PermissionResponseCallback& callback, | |
207 BrowserPluginPermissionType permission_type, | |
208 bool allowed_by_default); | |
209 ~PermissionResponseInfo(); | |
210 }; | |
211 | |
212 static void RecordUserInitiatedUMA(const PermissionResponseInfo& info, | |
213 bool allow); | |
214 // WebContentsObserver implementation. | |
215 virtual void DidCommitProvisionalLoadForFrame( | |
216 int64 frame_id, | |
217 const base::string16& frame_unique_name, | |
218 bool is_main_frame, | |
219 const GURL& url, | |
220 content::PageTransition transition_type, | |
221 content::RenderViewHost* render_view_host) OVERRIDE; | |
222 virtual void DidFailProvisionalLoad( | |
223 int64 frame_id, | |
224 const base::string16& frame_unique_name, | |
225 bool is_main_frame, | |
226 const GURL& validated_url, | |
227 int error_code, | |
228 const base::string16& error_description, | |
229 content::RenderViewHost* render_view_host) OVERRIDE; | |
230 virtual void DidStartProvisionalLoadForFrame( | |
231 int64 frame_id, | |
232 int64 parent_frame_id, | |
233 bool is_main_frame, | |
234 const GURL& validated_url, | |
235 bool is_error_page, | |
236 bool is_iframe_srcdoc, | |
237 content::RenderViewHost* render_view_host) OVERRIDE; | |
238 virtual void DocumentLoadedInFrame( | |
239 int64 frame_id, | |
240 content::RenderViewHost* render_view_host) OVERRIDE; | |
241 virtual void DidStopLoading( | |
242 content::RenderViewHost* render_view_host) OVERRIDE; | |
243 virtual void WebContentsDestroyed( | |
244 content::WebContents* web_contents) OVERRIDE; | |
245 virtual void UserAgentOverrideSet(const std::string& user_agent) OVERRIDE; | |
246 | |
247 // Called after the load handler is called in the guest's main frame. | |
248 void LoadHandlerCalled(); | |
249 | |
250 // Called when a redirect notification occurs. | |
251 void LoadRedirect(const GURL& old_url, | |
252 const GURL& new_url, | |
253 bool is_top_level); | |
254 | |
255 void AddWebViewToExtensionRendererState(); | |
256 static void RemoveWebViewFromExtensionRendererState( | |
257 content::WebContents* web_contents); | |
258 | |
259 #if defined(OS_CHROMEOS) | |
260 // Notification of a change in the state of an accessibility setting. | |
261 void OnAccessibilityStatusChanged( | |
262 const chromeos::AccessibilityStatusEventDetails& details); | |
263 #endif | |
264 | |
265 void InjectChromeVoxIfNeeded(content::RenderViewHost* render_view_host); | |
266 | |
267 // Bridge IDs correspond to a geolocation request. This method will remove | |
268 // the bookkeeping for a particular geolocation request associated with the | |
269 // provided |bridge_id|. It returns the request ID of the geolocation request. | |
270 int RemoveBridgeID(int bridge_id); | |
271 | |
272 int RequestPermissionInternal( | |
273 BrowserPluginPermissionType permission_type, | |
274 const base::DictionaryValue& request_info, | |
275 const PermissionResponseCallback& callback, | |
276 bool allowed_by_default); | |
277 | |
278 ObserverList<extensions::TabHelper::ScriptExecutionObserver> | |
279 script_observers_; | |
280 scoped_ptr<extensions::ScriptExecutor> script_executor_; | |
281 | |
282 content::NotificationRegistrar notification_registrar_; | |
283 | |
284 // A counter to generate a unique request id for a permission request. | |
285 // We only need the ids to be unique for a given WebViewGuest. | |
286 int next_permission_request_id_; | |
287 | |
288 typedef std::map<int, PermissionResponseInfo> RequestMap; | |
289 RequestMap pending_permission_requests_; | |
290 | |
291 // True if the user agent is overridden. | |
292 bool is_overriding_user_agent_; | |
293 | |
294 // Indicates that the page needs to be reloaded once it has been attached to | |
295 // an embedder. | |
296 bool pending_reload_on_attachment_; | |
297 | |
298 // Main frame ID of last committed page. | |
299 int64 main_frame_id_; | |
300 | |
301 // Set to |true| if ChromeVox was already injected in main frame. | |
302 bool chromevox_injected_; | |
303 | |
304 // Stores the current zoom factor. | |
305 double current_zoom_factor_; | |
306 | |
307 // Handles find requests and replies for the webview find API. | |
308 WebviewFindHelper find_helper_; | |
309 | |
310 // Handles the JavaScript dialog requests. | |
311 JavaScriptDialogHelper javascript_dialog_helper_; | |
312 | |
313 friend void WebviewFindHelper::DispatchFindUpdateEvent(bool canceled, | |
314 bool final_update); | |
315 | |
316 #if defined(OS_CHROMEOS) | |
317 // Subscription to receive notifications on changes to a11y settings. | |
318 scoped_ptr<chromeos::AccessibilityStatusSubscription> | |
319 accessibility_subscription_; | |
320 #endif | |
321 | |
322 std::map<int, int> bridge_id_to_request_id_map_; | |
323 | |
324 DISALLOW_COPY_AND_ASSIGN(WebViewGuest); | |
325 }; | |
326 | |
327 #endif // CHROME_BROWSER_GUESTVIEW_WEBVIEW_WEBVIEW_GUEST_H_ | |
OLD | NEW |