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 #include "chrome/browser/guest_view/web_view/web_view_guest.h" | |
6 | |
7 #include "base/message_loop/message_loop.h" | |
8 #include "base/strings/stringprintf.h" | |
9 #include "chrome/browser/chrome_notification_types.h" | |
10 #include "chrome/browser/extensions/api/web_request/web_request_api.h" | |
11 #include "chrome/browser/extensions/api/webview/webview_api.h" | |
12 #include "chrome/browser/extensions/chrome_extension_web_contents_observer.h" | |
13 #include "chrome/browser/extensions/extension_renderer_state.h" | |
14 #include "chrome/browser/extensions/menu_manager.h" | |
15 #include "chrome/browser/extensions/script_executor.h" | |
16 #include "chrome/browser/favicon/favicon_tab_helper.h" | |
17 #include "chrome/browser/guest_view/guest_view_constants.h" | |
18 #include "chrome/browser/guest_view/web_view/web_view_constants.h" | |
19 #include "chrome/browser/guest_view/web_view/web_view_permission_types.h" | |
20 #include "chrome/common/chrome_version_info.h" | |
21 #include "content/public/browser/browser_thread.h" | |
22 #include "content/public/browser/geolocation_permission_context.h" | |
23 #include "content/public/browser/native_web_keyboard_event.h" | |
24 #include "content/public/browser/navigation_entry.h" | |
25 #include "content/public/browser/notification_details.h" | |
26 #include "content/public/browser/notification_service.h" | |
27 #include "content/public/browser/notification_source.h" | |
28 #include "content/public/browser/notification_types.h" | |
29 #include "content/public/browser/render_process_host.h" | |
30 #include "content/public/browser/resource_request_details.h" | |
31 #include "content/public/browser/site_instance.h" | |
32 #include "content/public/browser/storage_partition.h" | |
33 #include "content/public/browser/user_metrics.h" | |
34 #include "content/public/browser/web_contents.h" | |
35 #include "content/public/browser/web_contents_delegate.h" | |
36 #include "content/public/common/media_stream_request.h" | |
37 #include "content/public/common/page_zoom.h" | |
38 #include "content/public/common/result_codes.h" | |
39 #include "content/public/common/stop_find_action.h" | |
40 #include "extensions/common/constants.h" | |
41 #include "net/base/net_errors.h" | |
42 #include "third_party/WebKit/public/web/WebFindOptions.h" | |
43 | |
44 #if defined(ENABLE_PLUGINS) | |
45 #include "chrome/browser/guest_view/web_view/plugin_permission_helper.h" | |
46 #endif | |
47 | |
48 #if defined(OS_CHROMEOS) | |
49 #include "chrome/browser/chromeos/accessibility/accessibility_manager.h" | |
50 #endif | |
51 | |
52 using base::UserMetricsAction; | |
53 using content::WebContents; | |
54 | |
55 namespace { | |
56 | |
57 static std::string TerminationStatusToString(base::TerminationStatus status) { | |
58 switch (status) { | |
59 case base::TERMINATION_STATUS_NORMAL_TERMINATION: | |
60 return "normal"; | |
61 case base::TERMINATION_STATUS_ABNORMAL_TERMINATION: | |
62 case base::TERMINATION_STATUS_STILL_RUNNING: | |
63 return "abnormal"; | |
64 case base::TERMINATION_STATUS_PROCESS_WAS_KILLED: | |
65 return "killed"; | |
66 case base::TERMINATION_STATUS_PROCESS_CRASHED: | |
67 #if defined(OS_ANDROID) | |
68 case base::TERMINATION_STATUS_OOM_PROTECTED: | |
69 #endif | |
70 return "crashed"; | |
71 case base::TERMINATION_STATUS_MAX_ENUM: | |
72 break; | |
73 } | |
74 NOTREACHED() << "Unknown Termination Status."; | |
75 return "unknown"; | |
76 } | |
77 | |
78 static std::string PermissionTypeToString(BrowserPluginPermissionType type) { | |
79 switch (type) { | |
80 case BROWSER_PLUGIN_PERMISSION_TYPE_NEW_WINDOW: | |
81 return webview::kPermissionTypeNewWindow; | |
82 case BROWSER_PLUGIN_PERMISSION_TYPE_UNKNOWN: | |
83 NOTREACHED(); | |
84 break; | |
85 default: { | |
86 WebViewPermissionType webview = static_cast<WebViewPermissionType>(type); | |
87 switch (webview) { | |
88 case WEB_VIEW_PERMISSION_TYPE_DOWNLOAD: | |
89 return webview::kPermissionTypeDownload; | |
90 case WEB_VIEW_PERMISSION_TYPE_GEOLOCATION: | |
91 return webview::kPermissionTypeGeolocation; | |
92 case WEB_VIEW_PERMISSION_TYPE_JAVASCRIPT_DIALOG: | |
93 return webview::kPermissionTypeDialog; | |
94 case WEB_VIEW_PERMISSION_TYPE_LOAD_PLUGIN: | |
95 return webview::kPermissionTypeLoadPlugin; | |
96 case WEB_VIEW_PERMISSION_TYPE_MEDIA: | |
97 return webview::kPermissionTypeMedia; | |
98 case WEB_VIEW_PERMISSION_TYPE_POINTER_LOCK: | |
99 return webview::kPermissionTypePointerLock; | |
100 } | |
101 NOTREACHED(); | |
102 } | |
103 } | |
104 return std::string(); | |
105 } | |
106 | |
107 void RemoveWebViewEventListenersOnIOThread( | |
108 void* profile, | |
109 const std::string& extension_id, | |
110 int embedder_process_id, | |
111 int view_instance_id) { | |
112 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
113 ExtensionWebRequestEventRouter::GetInstance()->RemoveWebViewEventListeners( | |
114 profile, | |
115 extension_id, | |
116 embedder_process_id, | |
117 view_instance_id); | |
118 } | |
119 | |
120 void AttachWebViewHelpers(WebContents* contents) { | |
121 FaviconTabHelper::CreateForWebContents(contents); | |
122 extensions::ChromeExtensionWebContentsObserver::CreateForWebContents( | |
123 contents); | |
124 #if defined(ENABLE_PLUGINS) | |
125 PluginPermissionHelper::CreateForWebContents(contents); | |
126 #endif | |
127 } | |
128 | |
129 } // namespace | |
130 | |
131 WebViewGuest::WebViewGuest(WebContents* guest_web_contents, | |
132 const std::string& extension_id) | |
133 : GuestView<WebViewGuest>(guest_web_contents, extension_id), | |
134 WebContentsObserver(guest_web_contents), | |
135 script_executor_(new extensions::ScriptExecutor(guest_web_contents, | |
136 &script_observers_)), | |
137 next_permission_request_id_(0), | |
138 is_overriding_user_agent_(false), | |
139 pending_reload_on_attachment_(false), | |
140 main_frame_id_(0), | |
141 chromevox_injected_(false), | |
142 find_helper_(this), | |
143 javascript_dialog_helper_(this) { | |
144 notification_registrar_.Add( | |
145 this, content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME, | |
146 content::Source<WebContents>(guest_web_contents)); | |
147 | |
148 notification_registrar_.Add( | |
149 this, content::NOTIFICATION_RESOURCE_RECEIVED_REDIRECT, | |
150 content::Source<WebContents>(guest_web_contents)); | |
151 | |
152 #if defined(OS_CHROMEOS) | |
153 chromeos::AccessibilityManager* accessibility_manager = | |
154 chromeos::AccessibilityManager::Get(); | |
155 CHECK(accessibility_manager); | |
156 accessibility_subscription_ = accessibility_manager->RegisterCallback( | |
157 base::Bind(&WebViewGuest::OnAccessibilityStatusChanged, | |
158 base::Unretained(this))); | |
159 #endif | |
160 | |
161 AttachWebViewHelpers(guest_web_contents); | |
162 } | |
163 | |
164 // static | |
165 const std::string& WebViewGuest::Type = "webview"; | |
166 | |
167 // static. | |
168 int WebViewGuest::GetViewInstanceId(WebContents* contents) { | |
169 WebViewGuest* guest = FromWebContents(contents); | |
170 if (!guest) | |
171 return guestview::kInstanceIDNone; | |
172 | |
173 return guest->view_instance_id(); | |
174 } | |
175 | |
176 // static | |
177 void WebViewGuest::RecordUserInitiatedUMA(const PermissionResponseInfo& info, | |
178 bool allow) { | |
179 if (allow) { | |
180 // Note that |allow| == true means the embedder explicitly allowed the | |
181 // request. For some requests they might still fail. An example of such | |
182 // scenario would be: an embedder allows geolocation request but doesn't | |
183 // have geolocation access on its own. | |
184 switch (info.permission_type) { | |
185 case BROWSER_PLUGIN_PERMISSION_TYPE_NEW_WINDOW: | |
186 content::RecordAction( | |
187 UserMetricsAction("BrowserPlugin.PermissionAllow.NewWindow")); | |
188 break; | |
189 case BROWSER_PLUGIN_PERMISSION_TYPE_UNKNOWN: | |
190 break; | |
191 default: { | |
192 WebViewPermissionType webview_permission_type = | |
193 static_cast<WebViewPermissionType>(info.permission_type); | |
194 switch (webview_permission_type) { | |
195 case WEB_VIEW_PERMISSION_TYPE_DOWNLOAD: | |
196 content::RecordAction( | |
197 UserMetricsAction("WebView.PermissionAllow.Download")); | |
198 break; | |
199 case WEB_VIEW_PERMISSION_TYPE_GEOLOCATION: | |
200 content::RecordAction( | |
201 UserMetricsAction("WebView.PermissionAllow.Geolocation")); | |
202 break; | |
203 case WEB_VIEW_PERMISSION_TYPE_JAVASCRIPT_DIALOG: | |
204 content::RecordAction( | |
205 UserMetricsAction("WebView.PermissionAllow.JSDialog")); | |
206 break; | |
207 case WEB_VIEW_PERMISSION_TYPE_LOAD_PLUGIN: | |
208 content::RecordAction( | |
209 UserMetricsAction("WebView.Guest.PermissionAllow.PluginLoad")); | |
210 case WEB_VIEW_PERMISSION_TYPE_MEDIA: | |
211 content::RecordAction( | |
212 UserMetricsAction("WebView.PermissionAllow.Media")); | |
213 break; | |
214 case WEB_VIEW_PERMISSION_TYPE_POINTER_LOCK: | |
215 content::RecordAction( | |
216 UserMetricsAction("WebView.PermissionAllow.PointerLock")); | |
217 break; | |
218 default: | |
219 break; | |
220 } | |
221 } | |
222 } | |
223 } else { | |
224 switch (info.permission_type) { | |
225 case BROWSER_PLUGIN_PERMISSION_TYPE_NEW_WINDOW: | |
226 content::RecordAction( | |
227 UserMetricsAction("BrowserPlugin.PermissionDeny.NewWindow")); | |
228 break; | |
229 case BROWSER_PLUGIN_PERMISSION_TYPE_UNKNOWN: | |
230 break; | |
231 default: { | |
232 WebViewPermissionType webview_permission_type = | |
233 static_cast<WebViewPermissionType>(info.permission_type); | |
234 switch (webview_permission_type) { | |
235 case WEB_VIEW_PERMISSION_TYPE_DOWNLOAD: | |
236 content::RecordAction( | |
237 UserMetricsAction("WebView.PermissionDeny.Download")); | |
238 break; | |
239 case WEB_VIEW_PERMISSION_TYPE_GEOLOCATION: | |
240 content::RecordAction( | |
241 UserMetricsAction("WebView.PermissionDeny.Geolocation")); | |
242 break; | |
243 case WEB_VIEW_PERMISSION_TYPE_JAVASCRIPT_DIALOG: | |
244 content::RecordAction( | |
245 UserMetricsAction("WebView.PermissionDeny.JSDialog")); | |
246 break; | |
247 case WEB_VIEW_PERMISSION_TYPE_LOAD_PLUGIN: | |
248 content::RecordAction( | |
249 UserMetricsAction("WebView.Guest.PermissionDeny.PluginLoad")); | |
250 case WEB_VIEW_PERMISSION_TYPE_MEDIA: | |
251 content::RecordAction( | |
252 UserMetricsAction("WebView.PermissionDeny.Media")); | |
253 break; | |
254 case WEB_VIEW_PERMISSION_TYPE_POINTER_LOCK: | |
255 content::RecordAction( | |
256 UserMetricsAction("WebView.PermissionDeny.PointerLock")); | |
257 break; | |
258 default: | |
259 break; | |
260 } | |
261 } | |
262 } | |
263 } | |
264 } | |
265 | |
266 void WebViewGuest::Attach(WebContents* embedder_web_contents, | |
267 const base::DictionaryValue& args) { | |
268 std::string user_agent_override; | |
269 if (args.GetString(webview::kParameterUserAgentOverride, | |
270 &user_agent_override)) { | |
271 SetUserAgentOverride(user_agent_override); | |
272 } else { | |
273 SetUserAgentOverride(""); | |
274 } | |
275 | |
276 GuestViewBase::Attach(embedder_web_contents, args); | |
277 | |
278 AddWebViewToExtensionRendererState(); | |
279 } | |
280 | |
281 void WebViewGuest::AddMessageToConsole(int32 level, | |
282 const base::string16& message, | |
283 int32 line_no, | |
284 const base::string16& source_id) { | |
285 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); | |
286 // Log levels are from base/logging.h: LogSeverity. | |
287 args->SetInteger(webview::kLevel, level); | |
288 args->SetString(webview::kMessage, message); | |
289 args->SetInteger(webview::kLine, line_no); | |
290 args->SetString(webview::kSourceId, source_id); | |
291 DispatchEvent( | |
292 new GuestViewBase::Event(webview::kEventConsoleMessage, args.Pass())); | |
293 } | |
294 | |
295 void WebViewGuest::Close() { | |
296 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); | |
297 DispatchEvent(new GuestViewBase::Event(webview::kEventClose, args.Pass())); | |
298 } | |
299 | |
300 void WebViewGuest::DidAttach() { | |
301 if (pending_reload_on_attachment_) { | |
302 pending_reload_on_attachment_ = false; | |
303 guest_web_contents()->GetController().Reload(false); | |
304 } | |
305 } | |
306 | |
307 void WebViewGuest::EmbedderDestroyed() { | |
308 // TODO(fsamuel): WebRequest event listeners for <webview> should survive | |
309 // reparenting of a <webview> within a single embedder. Right now, we keep | |
310 // around the browser state for the listener for the lifetime of the embedder. | |
311 // Ideally, the lifetime of the listeners should match the lifetime of the | |
312 // <webview> DOM node. Once http://crbug.com/156219 is resolved we can move | |
313 // the call to RemoveWebViewEventListenersOnIOThread back to | |
314 // WebViewGuest::WebContentsDestroyed. | |
315 content::BrowserThread::PostTask( | |
316 content::BrowserThread::IO, | |
317 FROM_HERE, | |
318 base::Bind( | |
319 &RemoveWebViewEventListenersOnIOThread, | |
320 browser_context(), embedder_extension_id(), | |
321 embedder_render_process_id(), | |
322 view_instance_id())); | |
323 } | |
324 | |
325 void WebViewGuest::FindReply(int request_id, | |
326 int number_of_matches, | |
327 const gfx::Rect& selection_rect, | |
328 int active_match_ordinal, | |
329 bool final_update) { | |
330 find_helper_.FindReply(request_id, number_of_matches, selection_rect, | |
331 active_match_ordinal, final_update); | |
332 } | |
333 | |
334 void WebViewGuest::GuestProcessGone(base::TerminationStatus status) { | |
335 // Cancel all find sessions in progress. | |
336 find_helper_.CancelAllFindSessions(); | |
337 | |
338 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); | |
339 args->SetInteger(webview::kProcessId, | |
340 guest_web_contents()->GetRenderProcessHost()->GetID()); | |
341 args->SetString(webview::kReason, TerminationStatusToString(status)); | |
342 DispatchEvent(new GuestViewBase::Event(webview::kEventExit, args.Pass())); | |
343 } | |
344 | |
345 bool WebViewGuest::HandleKeyboardEvent( | |
346 const content::NativeWebKeyboardEvent& event) { | |
347 if (event.type != blink::WebInputEvent::RawKeyDown) | |
348 return false; | |
349 | |
350 #if defined(OS_MACOSX) | |
351 if (event.modifiers != blink::WebInputEvent::MetaKey) | |
352 return false; | |
353 | |
354 if (event.windowsKeyCode == ui::VKEY_OEM_4) { | |
355 Go(-1); | |
356 return true; | |
357 } | |
358 | |
359 if (event.windowsKeyCode == ui::VKEY_OEM_6) { | |
360 Go(1); | |
361 return true; | |
362 } | |
363 #else | |
364 if (event.windowsKeyCode == ui::VKEY_BROWSER_BACK) { | |
365 Go(-1); | |
366 return true; | |
367 } | |
368 | |
369 if (event.windowsKeyCode == ui::VKEY_BROWSER_FORWARD) { | |
370 Go(1); | |
371 return true; | |
372 } | |
373 #endif | |
374 return false; | |
375 } | |
376 | |
377 bool WebViewGuest::IsDragAndDropEnabled() { | |
378 return true; | |
379 } | |
380 | |
381 bool WebViewGuest::IsOverridingUserAgent() const { | |
382 return is_overriding_user_agent_; | |
383 } | |
384 | |
385 void WebViewGuest::LoadProgressed(double progress) { | |
386 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); | |
387 args->SetString(guestview::kUrl, guest_web_contents()->GetURL().spec()); | |
388 args->SetDouble(webview::kProgress, progress); | |
389 DispatchEvent( | |
390 new GuestViewBase::Event(webview::kEventLoadProgress, args.Pass())); | |
391 } | |
392 | |
393 void WebViewGuest::LoadAbort(bool is_top_level, | |
394 const GURL& url, | |
395 const std::string& error_type) { | |
396 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); | |
397 args->SetBoolean(guestview::kIsTopLevel, is_top_level); | |
398 args->SetString(guestview::kUrl, url.possibly_invalid_spec()); | |
399 args->SetString(guestview::kReason, error_type); | |
400 DispatchEvent( | |
401 new GuestViewBase::Event(webview::kEventLoadAbort, args.Pass())); | |
402 } | |
403 | |
404 // TODO(fsamuel): Find a reliable way to test the 'responsive' and | |
405 // 'unresponsive' events. | |
406 void WebViewGuest::RendererResponsive() { | |
407 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); | |
408 args->SetInteger(webview::kProcessId, | |
409 guest_web_contents()->GetRenderProcessHost()->GetID()); | |
410 DispatchEvent( | |
411 new GuestViewBase::Event(webview::kEventResponsive, args.Pass())); | |
412 } | |
413 | |
414 void WebViewGuest::RendererUnresponsive() { | |
415 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); | |
416 args->SetInteger(webview::kProcessId, | |
417 guest_web_contents()->GetRenderProcessHost()->GetID()); | |
418 DispatchEvent( | |
419 new GuestViewBase::Event(webview::kEventUnresponsive, args.Pass())); | |
420 } | |
421 | |
422 void WebViewGuest::RequestPermission( | |
423 BrowserPluginPermissionType permission_type, | |
424 const base::DictionaryValue& request_info, | |
425 const PermissionResponseCallback& callback, | |
426 bool allowed_by_default) { | |
427 RequestPermissionInternal(permission_type, | |
428 request_info, | |
429 callback, | |
430 allowed_by_default); | |
431 } | |
432 | |
433 void WebViewGuest::Observe(int type, | |
434 const content::NotificationSource& source, | |
435 const content::NotificationDetails& details) { | |
436 switch (type) { | |
437 case content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME: { | |
438 DCHECK_EQ(content::Source<WebContents>(source).ptr(), | |
439 guest_web_contents()); | |
440 if (content::Source<WebContents>(source).ptr() == guest_web_contents()) | |
441 LoadHandlerCalled(); | |
442 break; | |
443 } | |
444 case content::NOTIFICATION_RESOURCE_RECEIVED_REDIRECT: { | |
445 DCHECK_EQ(content::Source<WebContents>(source).ptr(), | |
446 guest_web_contents()); | |
447 content::ResourceRedirectDetails* resource_redirect_details = | |
448 content::Details<content::ResourceRedirectDetails>(details).ptr(); | |
449 bool is_top_level = | |
450 resource_redirect_details->resource_type == ResourceType::MAIN_FRAME; | |
451 LoadRedirect(resource_redirect_details->url, | |
452 resource_redirect_details->new_url, | |
453 is_top_level); | |
454 break; | |
455 } | |
456 default: | |
457 NOTREACHED() << "Unexpected notification sent."; | |
458 break; | |
459 } | |
460 } | |
461 | |
462 void WebViewGuest::SetZoom(double zoom_factor) { | |
463 double zoom_level = content::ZoomFactorToZoomLevel(zoom_factor); | |
464 guest_web_contents()->SetZoomLevel(zoom_level); | |
465 | |
466 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); | |
467 args->SetDouble(webview::kOldZoomFactor, current_zoom_factor_); | |
468 args->SetDouble(webview::kNewZoomFactor, zoom_factor); | |
469 DispatchEvent( | |
470 new GuestViewBase::Event(webview::kEventZoomChange, args.Pass())); | |
471 | |
472 current_zoom_factor_ = zoom_factor; | |
473 } | |
474 | |
475 double WebViewGuest::GetZoom() { | |
476 return current_zoom_factor_; | |
477 } | |
478 | |
479 void WebViewGuest::Find( | |
480 const base::string16& search_text, | |
481 const blink::WebFindOptions& options, | |
482 scoped_refptr<extensions::WebviewFindFunction> find_function) { | |
483 find_helper_.Find(guest_web_contents(), search_text, options, find_function); | |
484 } | |
485 | |
486 void WebViewGuest::StopFinding(content::StopFindAction action) { | |
487 find_helper_.CancelAllFindSessions(); | |
488 guest_web_contents()->StopFinding(action); | |
489 } | |
490 | |
491 void WebViewGuest::Go(int relative_index) { | |
492 guest_web_contents()->GetController().GoToOffset(relative_index); | |
493 } | |
494 | |
495 void WebViewGuest::Reload() { | |
496 // TODO(fsamuel): Don't check for repost because we don't want to show | |
497 // Chromium's repost warning. We might want to implement a separate API | |
498 // for registering a callback if a repost is about to happen. | |
499 guest_web_contents()->GetController().Reload(false); | |
500 } | |
501 | |
502 | |
503 void WebViewGuest::RequestGeolocationPermission( | |
504 int bridge_id, | |
505 const GURL& requesting_frame, | |
506 bool user_gesture, | |
507 const base::Callback<void(bool)>& callback) { | |
508 base::DictionaryValue request_info; | |
509 request_info.Set(guestview::kUrl, | |
510 base::Value::CreateStringValue(requesting_frame.spec())); | |
511 request_info.Set(guestview::kUserGesture, | |
512 base::Value::CreateBooleanValue(user_gesture)); | |
513 | |
514 // It is safe to hold an unretained pointer to WebViewGuest because this | |
515 // callback is called from WebViewGuest::SetPermission. | |
516 const PermissionResponseCallback permission_callback = | |
517 base::Bind(&WebViewGuest::OnWebViewGeolocationPermissionResponse, | |
518 base::Unretained(this), | |
519 bridge_id, | |
520 user_gesture, | |
521 callback); | |
522 int request_id = RequestPermissionInternal( | |
523 static_cast<BrowserPluginPermissionType>( | |
524 WEB_VIEW_PERMISSION_TYPE_GEOLOCATION), | |
525 request_info, | |
526 permission_callback, | |
527 false /* allowed_by_default */); | |
528 bridge_id_to_request_id_map_[bridge_id] = request_id; | |
529 } | |
530 | |
531 void WebViewGuest::OnWebViewGeolocationPermissionResponse( | |
532 int bridge_id, | |
533 bool user_gesture, | |
534 const base::Callback<void(bool)>& callback, | |
535 bool allow, | |
536 const std::string& user_input) { | |
537 // The <webview> embedder has allowed the permission. We now need to make sure | |
538 // that the embedder has geolocation permission. | |
539 RemoveBridgeID(bridge_id); | |
540 | |
541 if (!allow || !attached()) { | |
542 callback.Run(false); | |
543 return; | |
544 } | |
545 | |
546 content::GeolocationPermissionContext* geolocation_context = | |
547 browser_context()->GetGeolocationPermissionContext(); | |
548 | |
549 DCHECK(geolocation_context); | |
550 geolocation_context->RequestGeolocationPermission( | |
551 embedder_web_contents()->GetRenderProcessHost()->GetID(), | |
552 embedder_web_contents()->GetRoutingID(), | |
553 // The geolocation permission request here is not initiated | |
554 // through WebGeolocationPermissionRequest. We are only interested | |
555 // in the fact whether the embedder/app has geolocation | |
556 // permission. Therefore we use an invalid |bridge_id|. | |
557 -1 /* bridge_id */, | |
558 embedder_web_contents()->GetLastCommittedURL(), | |
559 user_gesture, | |
560 callback); | |
561 } | |
562 | |
563 void WebViewGuest::CancelGeolocationPermissionRequest(int bridge_id) { | |
564 int request_id = RemoveBridgeID(bridge_id); | |
565 RequestMap::iterator request_itr = | |
566 pending_permission_requests_.find(request_id); | |
567 | |
568 if (request_itr == pending_permission_requests_.end()) | |
569 return; | |
570 | |
571 pending_permission_requests_.erase(request_itr); | |
572 } | |
573 | |
574 void WebViewGuest::OnWebViewMediaPermissionResponse( | |
575 const content::MediaStreamRequest& request, | |
576 const content::MediaResponseCallback& callback, | |
577 bool allow, | |
578 const std::string& user_input) { | |
579 if (!allow || !attached()) { | |
580 // Deny the request. | |
581 callback.Run(content::MediaStreamDevices(), | |
582 content::MEDIA_DEVICE_INVALID_STATE, | |
583 scoped_ptr<content::MediaStreamUI>()); | |
584 return; | |
585 } | |
586 if (!embedder_web_contents()->GetDelegate()) | |
587 return; | |
588 | |
589 embedder_web_contents()->GetDelegate()-> | |
590 RequestMediaAccessPermission(embedder_web_contents(), request, callback); | |
591 } | |
592 | |
593 void WebViewGuest::OnWebViewDownloadPermissionResponse( | |
594 const base::Callback<void(bool)>& callback, | |
595 bool allow, | |
596 const std::string& user_input) { | |
597 callback.Run(allow && attached()); | |
598 } | |
599 | |
600 void WebViewGuest::OnWebViewPointerLockPermissionResponse( | |
601 const base::Callback<void(bool)>& callback, | |
602 bool allow, | |
603 const std::string& user_input) { | |
604 callback.Run(allow && attached()); | |
605 } | |
606 | |
607 WebViewGuest::SetPermissionResult WebViewGuest::SetPermission( | |
608 int request_id, | |
609 PermissionResponseAction action, | |
610 const std::string& user_input) { | |
611 RequestMap::iterator request_itr = | |
612 pending_permission_requests_.find(request_id); | |
613 | |
614 if (request_itr == pending_permission_requests_.end()) | |
615 return SET_PERMISSION_INVALID; | |
616 | |
617 const PermissionResponseInfo& info = request_itr->second; | |
618 bool allow = (action == ALLOW) || | |
619 ((action == DEFAULT) && info.allowed_by_default); | |
620 | |
621 info.callback.Run(allow, user_input); | |
622 | |
623 // Only record user initiated (i.e. non-default) actions. | |
624 if (action != DEFAULT) | |
625 RecordUserInitiatedUMA(info, allow); | |
626 | |
627 pending_permission_requests_.erase(request_itr); | |
628 | |
629 return allow ? SET_PERMISSION_ALLOWED : SET_PERMISSION_DENIED; | |
630 } | |
631 | |
632 void WebViewGuest::SetUserAgentOverride( | |
633 const std::string& user_agent_override) { | |
634 is_overriding_user_agent_ = !user_agent_override.empty(); | |
635 if (is_overriding_user_agent_) { | |
636 content::RecordAction(UserMetricsAction("WebView.Guest.OverrideUA")); | |
637 } | |
638 guest_web_contents()->SetUserAgentOverride(user_agent_override); | |
639 } | |
640 | |
641 void WebViewGuest::Stop() { | |
642 guest_web_contents()->Stop(); | |
643 } | |
644 | |
645 void WebViewGuest::Terminate() { | |
646 content::RecordAction(UserMetricsAction("WebView.Guest.Terminate")); | |
647 base::ProcessHandle process_handle = | |
648 guest_web_contents()->GetRenderProcessHost()->GetHandle(); | |
649 if (process_handle) | |
650 base::KillProcess(process_handle, content::RESULT_CODE_KILLED, false); | |
651 } | |
652 | |
653 bool WebViewGuest::ClearData(const base::Time remove_since, | |
654 uint32 removal_mask, | |
655 const base::Closure& callback) { | |
656 content::RecordAction(UserMetricsAction("WebView.Guest.ClearData")); | |
657 content::StoragePartition* partition = | |
658 content::BrowserContext::GetStoragePartition( | |
659 guest_web_contents()->GetBrowserContext(), | |
660 guest_web_contents()->GetSiteInstance()); | |
661 | |
662 if (!partition) | |
663 return false; | |
664 | |
665 partition->ClearData( | |
666 removal_mask, | |
667 content::StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL, | |
668 GURL(), | |
669 content::StoragePartition::OriginMatcherFunction(), | |
670 remove_since, | |
671 base::Time::Now(), | |
672 callback); | |
673 return true; | |
674 } | |
675 | |
676 WebViewGuest::~WebViewGuest() { | |
677 } | |
678 | |
679 void WebViewGuest::DidCommitProvisionalLoadForFrame( | |
680 int64 frame_id, | |
681 const base::string16& frame_unique_name, | |
682 bool is_main_frame, | |
683 const GURL& url, | |
684 content::PageTransition transition_type, | |
685 content::RenderViewHost* render_view_host) { | |
686 find_helper_.CancelAllFindSessions(); | |
687 | |
688 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); | |
689 args->SetString(guestview::kUrl, url.spec()); | |
690 args->SetBoolean(guestview::kIsTopLevel, is_main_frame); | |
691 args->SetInteger(webview::kInternalCurrentEntryIndex, | |
692 guest_web_contents()->GetController().GetCurrentEntryIndex()); | |
693 args->SetInteger(webview::kInternalEntryCount, | |
694 guest_web_contents()->GetController().GetEntryCount()); | |
695 args->SetInteger(webview::kInternalProcessId, | |
696 guest_web_contents()->GetRenderProcessHost()->GetID()); | |
697 DispatchEvent( | |
698 new GuestViewBase::Event(webview::kEventLoadCommit, args.Pass())); | |
699 | |
700 // Update the current zoom factor for the new page. | |
701 current_zoom_factor_ = content::ZoomLevelToZoomFactor( | |
702 guest_web_contents()->GetZoomLevel()); | |
703 | |
704 if (is_main_frame) { | |
705 chromevox_injected_ = false; | |
706 main_frame_id_ = frame_id; | |
707 } | |
708 } | |
709 | |
710 void WebViewGuest::DidFailProvisionalLoad( | |
711 int64 frame_id, | |
712 const base::string16& frame_unique_name, | |
713 bool is_main_frame, | |
714 const GURL& validated_url, | |
715 int error_code, | |
716 const base::string16& error_description, | |
717 content::RenderViewHost* render_view_host) { | |
718 // Translate the |error_code| into an error string. | |
719 std::string error_type; | |
720 base::RemoveChars(net::ErrorToString(error_code), "net::", &error_type); | |
721 LoadAbort(is_main_frame, validated_url, error_type); | |
722 } | |
723 | |
724 void WebViewGuest::DidStartProvisionalLoadForFrame( | |
725 int64 frame_id, | |
726 int64 parent_frame_id, | |
727 bool is_main_frame, | |
728 const GURL& validated_url, | |
729 bool is_error_page, | |
730 bool is_iframe_srcdoc, | |
731 content::RenderViewHost* render_view_host) { | |
732 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); | |
733 args->SetString(guestview::kUrl, validated_url.spec()); | |
734 args->SetBoolean(guestview::kIsTopLevel, is_main_frame); | |
735 DispatchEvent( | |
736 new GuestViewBase::Event(webview::kEventLoadStart, args.Pass())); | |
737 } | |
738 | |
739 void WebViewGuest::DocumentLoadedInFrame( | |
740 int64 frame_id, | |
741 content::RenderViewHost* render_view_host) { | |
742 if (frame_id == main_frame_id_) | |
743 InjectChromeVoxIfNeeded(render_view_host); | |
744 } | |
745 | |
746 void WebViewGuest::DidStopLoading(content::RenderViewHost* render_view_host) { | |
747 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); | |
748 DispatchEvent(new GuestViewBase::Event(webview::kEventLoadStop, args.Pass())); | |
749 } | |
750 | |
751 void WebViewGuest::WebContentsDestroyed(WebContents* web_contents) { | |
752 // Clean up custom context menu items for this guest. | |
753 extensions::MenuManager* menu_manager = extensions::MenuManager::Get( | |
754 Profile::FromBrowserContext(browser_context())); | |
755 menu_manager->RemoveAllContextItems(extensions::MenuItem::ExtensionKey( | |
756 embedder_extension_id(), view_instance_id())); | |
757 | |
758 RemoveWebViewFromExtensionRendererState(web_contents); | |
759 } | |
760 | |
761 void WebViewGuest::UserAgentOverrideSet(const std::string& user_agent) { | |
762 content::NavigationController& controller = | |
763 guest_web_contents()->GetController(); | |
764 content::NavigationEntry* entry = controller.GetVisibleEntry(); | |
765 if (!entry) | |
766 return; | |
767 entry->SetIsOverridingUserAgent(!user_agent.empty()); | |
768 if (!attached()) { | |
769 // We cannot reload now because all resource loads are suspended until | |
770 // attachment. | |
771 pending_reload_on_attachment_ = true; | |
772 return; | |
773 } | |
774 guest_web_contents()->GetController().Reload(false); | |
775 } | |
776 | |
777 void WebViewGuest::LoadHandlerCalled() { | |
778 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); | |
779 DispatchEvent( | |
780 new GuestViewBase::Event(webview::kEventContentLoad, args.Pass())); | |
781 } | |
782 | |
783 void WebViewGuest::LoadRedirect(const GURL& old_url, | |
784 const GURL& new_url, | |
785 bool is_top_level) { | |
786 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); | |
787 args->SetBoolean(guestview::kIsTopLevel, is_top_level); | |
788 args->SetString(webview::kNewURL, new_url.spec()); | |
789 args->SetString(webview::kOldURL, old_url.spec()); | |
790 DispatchEvent( | |
791 new GuestViewBase::Event(webview::kEventLoadRedirect, args.Pass())); | |
792 } | |
793 | |
794 void WebViewGuest::AddWebViewToExtensionRendererState() { | |
795 const GURL& site_url = guest_web_contents()->GetSiteInstance()->GetSiteURL(); | |
796 std::string partition_domain; | |
797 std::string partition_id; | |
798 bool in_memory; | |
799 if (!GetGuestPartitionConfigForSite( | |
800 site_url, &partition_domain, &partition_id, &in_memory)) { | |
801 NOTREACHED(); | |
802 return; | |
803 } | |
804 DCHECK(embedder_extension_id() == partition_domain); | |
805 | |
806 ExtensionRendererState::WebViewInfo webview_info; | |
807 webview_info.embedder_process_id = embedder_render_process_id(); | |
808 webview_info.instance_id = view_instance_id(); | |
809 webview_info.partition_id = partition_id; | |
810 webview_info.embedder_extension_id = embedder_extension_id(); | |
811 | |
812 content::BrowserThread::PostTask( | |
813 content::BrowserThread::IO, FROM_HERE, | |
814 base::Bind( | |
815 &ExtensionRendererState::AddWebView, | |
816 base::Unretained(ExtensionRendererState::GetInstance()), | |
817 guest_web_contents()->GetRenderProcessHost()->GetID(), | |
818 guest_web_contents()->GetRoutingID(), | |
819 webview_info)); | |
820 } | |
821 | |
822 // static | |
823 void WebViewGuest::RemoveWebViewFromExtensionRendererState( | |
824 WebContents* web_contents) { | |
825 content::BrowserThread::PostTask( | |
826 content::BrowserThread::IO, FROM_HERE, | |
827 base::Bind( | |
828 &ExtensionRendererState::RemoveWebView, | |
829 base::Unretained(ExtensionRendererState::GetInstance()), | |
830 web_contents->GetRenderProcessHost()->GetID(), | |
831 web_contents->GetRoutingID())); | |
832 } | |
833 | |
834 GURL WebViewGuest::ResolveURL(const std::string& src) { | |
835 if (!in_extension()) { | |
836 NOTREACHED(); | |
837 return GURL(src); | |
838 } | |
839 | |
840 GURL default_url(base::StringPrintf("%s://%s/", | |
841 extensions::kExtensionScheme, | |
842 embedder_extension_id().c_str())); | |
843 return default_url.Resolve(src); | |
844 } | |
845 | |
846 void WebViewGuest::SizeChanged(const gfx::Size& old_size, | |
847 const gfx::Size& new_size) { | |
848 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); | |
849 args->SetInteger(webview::kOldHeight, old_size.height()); | |
850 args->SetInteger(webview::kOldWidth, old_size.width()); | |
851 args->SetInteger(webview::kNewHeight, new_size.height()); | |
852 args->SetInteger(webview::kNewWidth, new_size.width()); | |
853 DispatchEvent( | |
854 new GuestViewBase::Event(webview::kEventSizeChanged, args.Pass())); | |
855 } | |
856 | |
857 void WebViewGuest::RequestMediaAccessPermission( | |
858 const content::MediaStreamRequest& request, | |
859 const content::MediaResponseCallback& callback) { | |
860 base::DictionaryValue request_info; | |
861 request_info.Set( | |
862 guestview::kUrl, | |
863 base::Value::CreateStringValue(request.security_origin.spec())); | |
864 RequestPermission(static_cast<BrowserPluginPermissionType>( | |
865 WEB_VIEW_PERMISSION_TYPE_MEDIA), | |
866 request_info, | |
867 base::Bind(&WebViewGuest::OnWebViewMediaPermissionResponse, | |
868 base::Unretained(this), | |
869 request, | |
870 callback), | |
871 false /* allowed_by_default */); | |
872 } | |
873 | |
874 void WebViewGuest::CanDownload( | |
875 const std::string& request_method, | |
876 const GURL& url, | |
877 const base::Callback<void(bool)>& callback) { | |
878 base::DictionaryValue request_info; | |
879 request_info.Set( | |
880 guestview::kUrl, | |
881 base::Value::CreateStringValue(url.spec())); | |
882 RequestPermission( | |
883 static_cast<BrowserPluginPermissionType>( | |
884 WEB_VIEW_PERMISSION_TYPE_DOWNLOAD), | |
885 request_info, | |
886 base::Bind(&WebViewGuest::OnWebViewDownloadPermissionResponse, | |
887 base::Unretained(this), | |
888 callback), | |
889 false /* allowed_by_default */); | |
890 } | |
891 | |
892 void WebViewGuest::RequestPointerLockPermission( | |
893 bool user_gesture, | |
894 bool last_unlocked_by_target, | |
895 const base::Callback<void(bool)>& callback) { | |
896 base::DictionaryValue request_info; | |
897 request_info.Set(guestview::kUserGesture, | |
898 base::Value::CreateBooleanValue(user_gesture)); | |
899 request_info.Set(webview::kLastUnlockedBySelf, | |
900 base::Value::CreateBooleanValue(last_unlocked_by_target)); | |
901 request_info.Set(guestview::kUrl, | |
902 base::Value::CreateStringValue( | |
903 guest_web_contents()->GetLastCommittedURL().spec())); | |
904 | |
905 RequestPermission( | |
906 static_cast<BrowserPluginPermissionType>( | |
907 WEB_VIEW_PERMISSION_TYPE_POINTER_LOCK), | |
908 request_info, | |
909 base::Bind(&WebViewGuest::OnWebViewPointerLockPermissionResponse, | |
910 base::Unretained(this), | |
911 callback), | |
912 false /* allowed_by_default */); | |
913 } | |
914 | |
915 content::JavaScriptDialogManager* | |
916 WebViewGuest::GetJavaScriptDialogManager() { | |
917 return &javascript_dialog_helper_; | |
918 } | |
919 | |
920 #if defined(OS_CHROMEOS) | |
921 void WebViewGuest::OnAccessibilityStatusChanged( | |
922 const chromeos::AccessibilityStatusEventDetails& details) { | |
923 if (details.notification_type == chromeos::ACCESSIBILITY_MANAGER_SHUTDOWN) { | |
924 accessibility_subscription_.reset(); | |
925 } else if (details.notification_type == | |
926 chromeos::ACCESSIBILITY_TOGGLE_SPOKEN_FEEDBACK) { | |
927 if (details.enabled) | |
928 InjectChromeVoxIfNeeded(guest_web_contents()->GetRenderViewHost()); | |
929 else | |
930 chromevox_injected_ = false; | |
931 } | |
932 } | |
933 #endif | |
934 | |
935 void WebViewGuest::InjectChromeVoxIfNeeded( | |
936 content::RenderViewHost* render_view_host) { | |
937 #if defined(OS_CHROMEOS) | |
938 if (!chromevox_injected_) { | |
939 chromeos::AccessibilityManager* manager = | |
940 chromeos::AccessibilityManager::Get(); | |
941 if (manager && manager->IsSpokenFeedbackEnabled()) { | |
942 manager->InjectChromeVox(render_view_host); | |
943 chromevox_injected_ = true; | |
944 } | |
945 } | |
946 #endif | |
947 } | |
948 | |
949 int WebViewGuest::RemoveBridgeID(int bridge_id) { | |
950 std::map<int, int>::iterator bridge_itr = | |
951 bridge_id_to_request_id_map_.find(bridge_id); | |
952 if (bridge_itr == bridge_id_to_request_id_map_.end()) | |
953 return webview::kInvalidPermissionRequestID; | |
954 | |
955 int request_id = bridge_itr->second; | |
956 bridge_id_to_request_id_map_.erase(bridge_itr); | |
957 return request_id; | |
958 } | |
959 | |
960 int WebViewGuest::RequestPermissionInternal( | |
961 BrowserPluginPermissionType permission_type, | |
962 const base::DictionaryValue& request_info, | |
963 const PermissionResponseCallback& callback, | |
964 bool allowed_by_default) { | |
965 // If there are too many pending permission requests then reject this request. | |
966 if (pending_permission_requests_.size() >= | |
967 webview::kMaxOutstandingPermissionRequests) { | |
968 // Let the stack unwind before we deny the permission request so that | |
969 // objects held by the permission request are not destroyed immediately | |
970 // after creation. This is to allow those same objects to be accessed again | |
971 // in the same scope without fear of use after freeing. | |
972 base::MessageLoop::current()->PostTask( | |
973 FROM_HERE, | |
974 base::Bind(&PermissionResponseCallback::Run, | |
975 base::Owned(new PermissionResponseCallback(callback)), | |
976 allowed_by_default, | |
977 std::string())); | |
978 return webview::kInvalidPermissionRequestID; | |
979 } | |
980 | |
981 int request_id = next_permission_request_id_++; | |
982 pending_permission_requests_[request_id] = | |
983 PermissionResponseInfo(callback, permission_type, allowed_by_default); | |
984 scoped_ptr<base::DictionaryValue> args(request_info.DeepCopy()); | |
985 args->SetInteger(webview::kRequestId, request_id); | |
986 switch (static_cast<int>(permission_type)) { | |
987 case BROWSER_PLUGIN_PERMISSION_TYPE_NEW_WINDOW: { | |
988 DispatchEvent( | |
989 new GuestViewBase::Event(webview::kEventNewWindow, args.Pass())); | |
990 break; | |
991 } | |
992 case WEB_VIEW_PERMISSION_TYPE_JAVASCRIPT_DIALOG: { | |
993 DispatchEvent( | |
994 new GuestViewBase::Event(webview::kEventDialog, args.Pass())); | |
995 break; | |
996 } | |
997 default: { | |
998 args->SetString(webview::kPermission, | |
999 PermissionTypeToString(permission_type)); | |
1000 DispatchEvent(new GuestViewBase::Event(webview::kEventPermissionRequest, | |
1001 args.Pass())); | |
1002 break; | |
1003 } | |
1004 } | |
1005 return request_id; | |
1006 } | |
1007 | |
1008 WebViewGuest::PermissionResponseInfo::PermissionResponseInfo() | |
1009 : permission_type(BROWSER_PLUGIN_PERMISSION_TYPE_UNKNOWN), | |
1010 allowed_by_default(false) { | |
1011 } | |
1012 | |
1013 WebViewGuest::PermissionResponseInfo::PermissionResponseInfo( | |
1014 const PermissionResponseCallback& callback, | |
1015 BrowserPluginPermissionType permission_type, | |
1016 bool allowed_by_default) | |
1017 : callback(callback), | |
1018 permission_type(permission_type), | |
1019 allowed_by_default(allowed_by_default) { | |
1020 } | |
1021 | |
1022 WebViewGuest::PermissionResponseInfo::~PermissionResponseInfo() { | |
1023 } | |
OLD | NEW |