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.
6 #include "chrome/browser/guest_view/web_view/chrome_web_view_guest_delegate.h"
8 #include "chrome/browser/extensions/chrome_extension_web_contents_observer.h"
9 #include "chrome/browser/favicon/favicon_utils.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h"
12 #include "chrome/browser/ui/pdf/chrome_pdf_web_contents_helper_client.h"
13 #include "components/browsing_data/storage_partition_http_cache_data_remover.h"
14 #include "components/guest_view/browser/guest_view_event.h"
15 #include "components/renderer_context_menu/context_menu_delegate.h"
16 #include "content/public/browser/render_process_host.h"
17 #include "extensions/browser/api/web_request/web_request_api.h"
18 #include "extensions/browser/guest_view/web_view/web_view_constants.h"
20 using guest_view::GuestViewEvent
;
22 namespace extensions
{
24 ChromeWebViewGuestDelegate::ChromeWebViewGuestDelegate(
25 WebViewGuest
* web_view_guest
)
26 : pending_context_menu_request_id_(0),
27 chromevox_injected_(false),
28 web_view_guest_(web_view_guest
),
29 weak_ptr_factory_(this) {
32 ChromeWebViewGuestDelegate::~ChromeWebViewGuestDelegate() {
35 bool ChromeWebViewGuestDelegate::HandleContextMenu(
36 const content::ContextMenuParams
& params
) {
37 ContextMenuDelegate
* menu_delegate
=
38 ContextMenuDelegate::FromWebContents(guest_web_contents());
39 DCHECK(menu_delegate
);
41 pending_menu_
= menu_delegate
->BuildMenu(guest_web_contents(), params
);
42 // It's possible for the returned menu to be null, so early out to avoid
43 // a crash. TODO(wjmaclean): find out why it's possible for this to happen
44 // in the first place, and if it's an error.
48 // Pass it to embedder.
49 int request_id
= ++pending_context_menu_request_id_
;
50 scoped_ptr
<base::DictionaryValue
> args(new base::DictionaryValue());
51 scoped_ptr
<base::ListValue
> items
=
52 MenuModelToValue(pending_menu_
->menu_model());
53 args
->Set(webview::kContextMenuItems
, items
.release());
54 args
->SetInteger(webview::kRequestId
, request_id
);
55 web_view_guest()->DispatchEventToView(
56 new GuestViewEvent(webview::kEventContextMenuShow
, args
.Pass()));
60 void ChromeWebViewGuestDelegate::OnDidInitialize() {
61 #if defined(OS_CHROMEOS)
62 chromeos::AccessibilityManager
* accessibility_manager
=
63 chromeos::AccessibilityManager::Get();
64 CHECK(accessibility_manager
);
65 accessibility_subscription_
= accessibility_manager
->RegisterCallback(
66 base::Bind(&ChromeWebViewGuestDelegate::OnAccessibilityStatusChanged
,
67 weak_ptr_factory_
.GetWeakPtr()));
72 scoped_ptr
<base::ListValue
> ChromeWebViewGuestDelegate::MenuModelToValue(
73 const ui::SimpleMenuModel
& menu_model
) {
74 scoped_ptr
<base::ListValue
> items(new base::ListValue());
75 for (int i
= 0; i
< menu_model
.GetItemCount(); ++i
) {
76 base::DictionaryValue
* item_value
= new base::DictionaryValue();
77 // TODO(lazyboy): We need to expose some kind of enum equivalent of
78 // |command_id| instead of plain integers.
79 item_value
->SetInteger(webview::kMenuItemCommandId
,
80 menu_model
.GetCommandIdAt(i
));
81 item_value
->SetString(webview::kMenuItemLabel
, menu_model
.GetLabelAt(i
));
82 items
->Append(item_value
);
87 void ChromeWebViewGuestDelegate::OnShowContextMenu(
89 const MenuItemVector
* items
) {
90 if (!pending_menu_
.get())
93 // Make sure this was the correct request.
94 if (request_id
!= pending_context_menu_request_id_
)
97 // TODO(lazyboy): Implement.
100 ContextMenuDelegate
* menu_delegate
=
101 ContextMenuDelegate::FromWebContents(guest_web_contents());
102 menu_delegate
->ShowMenu(pending_menu_
.Pass());
105 void ChromeWebViewGuestDelegate::InjectChromeVoxIfNeeded(
106 content::RenderViewHost
* render_view_host
) {
107 #if defined(OS_CHROMEOS)
108 if (!chromevox_injected_
) {
109 chromeos::AccessibilityManager
* manager
=
110 chromeos::AccessibilityManager::Get();
111 if (manager
&& manager
->IsSpokenFeedbackEnabled()) {
112 manager
->InjectChromeVox(render_view_host
);
113 chromevox_injected_
= true;
119 #if defined(OS_CHROMEOS)
120 void ChromeWebViewGuestDelegate::OnAccessibilityStatusChanged(
121 const chromeos::AccessibilityStatusEventDetails
& details
) {
122 if (details
.notification_type
== chromeos::ACCESSIBILITY_MANAGER_SHUTDOWN
) {
123 accessibility_subscription_
.reset();
124 } else if (details
.notification_type
==
125 chromeos::ACCESSIBILITY_TOGGLE_SPOKEN_FEEDBACK
) {
127 InjectChromeVoxIfNeeded(guest_web_contents()->GetRenderViewHost());
129 chromevox_injected_
= false;
134 } // namespace extensions