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 "chrome/common/url_constants.h"
14 #include "components/browsing_data/storage_partition_http_cache_data_remover.h"
15 #include "components/guest_view/browser/guest_view_event.h"
16 #include "components/renderer_context_menu/context_menu_delegate.h"
17 #include "content/public/browser/render_process_host.h"
18 #include "extensions/browser/api/web_request/web_request_api.h"
19 #include "extensions/browser/guest_view/web_view/web_view_constants.h"
21 using guest_view::GuestViewEvent
;
23 namespace extensions
{
25 ChromeWebViewGuestDelegate::ChromeWebViewGuestDelegate(
26 WebViewGuest
* web_view_guest
)
27 : pending_context_menu_request_id_(0),
28 chromevox_injected_(false),
29 web_view_guest_(web_view_guest
),
30 weak_ptr_factory_(this) {
33 ChromeWebViewGuestDelegate::~ChromeWebViewGuestDelegate() {
36 bool ChromeWebViewGuestDelegate::HandleContextMenu(
37 const content::ContextMenuParams
& params
) {
38 ContextMenuDelegate
* menu_delegate
=
39 ContextMenuDelegate::FromWebContents(guest_web_contents());
40 DCHECK(menu_delegate
);
42 content::ContextMenuParams new_params
= params
;
43 // The only case where |context_menu_position_| is not initialized is the case
44 // where the input event is directly sent to the guest WebContents without
45 // ever going throught the embedder and BrowserPlugin's
46 // RenderWidgetHostViewGuest. This only happens in some tests, e.g.,
47 // WebViewInteractiveTest.ContextMenuParamCoordinates.
48 if (context_menu_position_
) {
49 new_params
.x
= context_menu_position_
->x();
50 new_params
.y
= context_menu_position_
->y();
53 pending_menu_
= menu_delegate
->BuildMenu(guest_web_contents(), new_params
);
54 // It's possible for the returned menu to be null, so early out to avoid
55 // a crash. TODO(wjmaclean): find out why it's possible for this to happen
56 // in the first place, and if it's an error.
60 // Pass it to embedder.
61 int request_id
= ++pending_context_menu_request_id_
;
62 scoped_ptr
<base::DictionaryValue
> args(new base::DictionaryValue());
63 scoped_ptr
<base::ListValue
> items
=
64 MenuModelToValue(pending_menu_
->menu_model());
65 args
->Set(webview::kContextMenuItems
, items
.release());
66 args
->SetInteger(webview::kRequestId
, request_id
);
67 web_view_guest()->DispatchEventToView(
68 new GuestViewEvent(webview::kEventContextMenuShow
, args
.Pass()));
72 void ChromeWebViewGuestDelegate::OnDidInitialize() {
73 #if defined(OS_CHROMEOS)
74 chromeos::AccessibilityManager
* accessibility_manager
=
75 chromeos::AccessibilityManager::Get();
76 CHECK(accessibility_manager
);
77 accessibility_subscription_
= accessibility_manager
->RegisterCallback(
78 base::Bind(&ChromeWebViewGuestDelegate::OnAccessibilityStatusChanged
,
79 weak_ptr_factory_
.GetWeakPtr()));
84 scoped_ptr
<base::ListValue
> ChromeWebViewGuestDelegate::MenuModelToValue(
85 const ui::SimpleMenuModel
& menu_model
) {
86 scoped_ptr
<base::ListValue
> items(new base::ListValue());
87 for (int i
= 0; i
< menu_model
.GetItemCount(); ++i
) {
88 base::DictionaryValue
* item_value
= new base::DictionaryValue();
89 // TODO(lazyboy): We need to expose some kind of enum equivalent of
90 // |command_id| instead of plain integers.
91 item_value
->SetInteger(webview::kMenuItemCommandId
,
92 menu_model
.GetCommandIdAt(i
));
93 item_value
->SetString(webview::kMenuItemLabel
, menu_model
.GetLabelAt(i
));
94 items
->Append(item_value
);
99 void ChromeWebViewGuestDelegate::OnShowContextMenu(
101 const MenuItemVector
* items
) {
102 if (!pending_menu_
.get())
105 // Make sure this was the correct request.
106 if (request_id
!= pending_context_menu_request_id_
)
109 // TODO(lazyboy): Implement.
112 ContextMenuDelegate
* menu_delegate
=
113 ContextMenuDelegate::FromWebContents(guest_web_contents());
114 menu_delegate
->ShowMenu(pending_menu_
.Pass());
117 bool ChromeWebViewGuestDelegate::ShouldHandleFindRequestsForEmbedder() const {
118 // Find requests will be handled by the guest for the Chrome signin page.
119 return web_view_guest_
->owner_web_contents()->GetWebUI() != nullptr &&
120 web_view_guest_
->GetOwnerSiteURL().GetOrigin().spec() ==
121 chrome::kChromeUIChromeSigninURL
;
124 void ChromeWebViewGuestDelegate::InjectChromeVoxIfNeeded(
125 content::RenderViewHost
* render_view_host
) {
126 #if defined(OS_CHROMEOS)
127 if (!chromevox_injected_
) {
128 chromeos::AccessibilityManager
* manager
=
129 chromeos::AccessibilityManager::Get();
130 if (manager
&& manager
->IsSpokenFeedbackEnabled()) {
131 manager
->InjectChromeVox(render_view_host
);
132 chromevox_injected_
= true;
138 #if defined(OS_CHROMEOS)
139 void ChromeWebViewGuestDelegate::OnAccessibilityStatusChanged(
140 const chromeos::AccessibilityStatusEventDetails
& details
) {
141 if (details
.notification_type
== chromeos::ACCESSIBILITY_MANAGER_SHUTDOWN
) {
142 accessibility_subscription_
.reset();
143 } else if (details
.notification_type
==
144 chromeos::ACCESSIBILITY_TOGGLE_SPOKEN_FEEDBACK
) {
146 InjectChromeVoxIfNeeded(guest_web_contents()->GetRenderViewHost());
148 chromevox_injected_
= false;
153 void ChromeWebViewGuestDelegate::SetContextMenuPosition(
154 const gfx::Point
& position
) {
155 if (context_menu_position_
== nullptr)
156 context_menu_position_
.reset(new gfx::Point());
158 *context_menu_position_
= position
;
161 } // namespace extensions