Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(192)

Side by Side Diff: chrome/browser/guestview/webview/webview_guest.cc

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

Powered by Google App Engine
This is Rietveld 408576698