OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 | 5 |
6 #include "chrome/browser/guest_view/web_view/chrome_web_view_guest_delegate.h" | 6 #include "chrome/browser/guest_view/web_view/chrome_web_view_guest_delegate.h" |
7 | 7 |
| 8 #include "chrome/browser/extensions/api/web_request/web_request_api.h" |
8 #include "chrome/browser/extensions/chrome_extension_web_contents_observer.h" | 9 #include "chrome/browser/extensions/chrome_extension_web_contents_observer.h" |
9 #include "chrome/browser/favicon/favicon_tab_helper.h" | 10 #include "chrome/browser/favicon/favicon_tab_helper.h" |
10 #include "chrome/browser/guest_view/web_view/web_view_constants.h" | |
11 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h" | 11 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h" |
12 #include "chrome/browser/ui/pdf/pdf_tab_helper.h" | 12 #include "chrome/browser/ui/pdf/pdf_tab_helper.h" |
13 #include "chrome/browser/ui/zoom/zoom_controller.h" | 13 #include "chrome/browser/ui/zoom/zoom_controller.h" |
14 #include "chrome/common/chrome_version_info.h" | 14 #include "chrome/common/chrome_version_info.h" |
15 #include "components/renderer_context_menu/context_menu_delegate.h" | 15 #include "components/renderer_context_menu/context_menu_delegate.h" |
16 #include "content/public/common/page_zoom.h" | 16 #include "content/public/common/page_zoom.h" |
| 17 #include "extensions/browser/guest_view/web_view/web_view_constants.h" |
17 | 18 |
18 #if defined(ENABLE_PRINTING) | 19 #if defined(ENABLE_PRINTING) |
19 #if defined(ENABLE_FULL_PRINTING) | 20 #if defined(ENABLE_FULL_PRINTING) |
20 #include "chrome/browser/printing/print_preview_message_handler.h" | 21 #include "chrome/browser/printing/print_preview_message_handler.h" |
21 #include "chrome/browser/printing/print_view_manager.h" | 22 #include "chrome/browser/printing/print_view_manager.h" |
22 #else | 23 #else |
23 #include "chrome/browser/printing/print_view_manager_basic.h" | 24 #include "chrome/browser/printing/print_view_manager_basic.h" |
24 #endif // defined(ENABLE_FULL_PRINTING) | 25 #endif // defined(ENABLE_FULL_PRINTING) |
25 #endif // defined(ENABLE_PRINTING) | 26 #endif // defined(ENABLE_PRINTING) |
26 | 27 |
| 28 void RemoveWebViewEventListenersOnIOThread( |
| 29 void* profile, |
| 30 const std::string& extension_id, |
| 31 int embedder_process_id, |
| 32 int view_instance_id) { |
| 33 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 34 ExtensionWebRequestEventRouter::GetInstance()->RemoveWebViewEventListeners( |
| 35 profile, |
| 36 extension_id, |
| 37 embedder_process_id, |
| 38 view_instance_id); |
| 39 } |
| 40 |
27 ChromeWebViewGuestDelegate::ChromeWebViewGuestDelegate( | 41 ChromeWebViewGuestDelegate::ChromeWebViewGuestDelegate( |
28 extensions::WebViewGuest* web_view_guest) | 42 extensions::WebViewGuest* web_view_guest) |
29 : WebViewGuestDelegate(), | 43 : WebViewGuestDelegate(web_view_guest), |
30 pending_context_menu_request_id_(0), | 44 pending_context_menu_request_id_(0), |
31 chromevox_injected_(false), | 45 chromevox_injected_(false), |
32 current_zoom_factor_(1.0), | 46 current_zoom_factor_(1.0) { |
33 web_view_guest_(web_view_guest) { | |
34 } | 47 } |
35 | 48 |
36 ChromeWebViewGuestDelegate::~ChromeWebViewGuestDelegate() { | 49 ChromeWebViewGuestDelegate::~ChromeWebViewGuestDelegate() { |
37 } | 50 } |
38 | 51 |
39 double ChromeWebViewGuestDelegate::GetZoom() { | 52 double ChromeWebViewGuestDelegate::GetZoom() { |
40 return current_zoom_factor_; | 53 return current_zoom_factor_; |
41 } | 54 } |
42 | 55 |
43 bool ChromeWebViewGuestDelegate::HandleContextMenu( | 56 bool ChromeWebViewGuestDelegate::HandleContextMenu( |
44 const content::ContextMenuParams& params) { | 57 const content::ContextMenuParams& params) { |
45 ContextMenuDelegate* menu_delegate = | 58 ContextMenuDelegate* menu_delegate = |
46 ContextMenuDelegate::FromWebContents(guest_web_contents()); | 59 ContextMenuDelegate::FromWebContents(guest_web_contents()); |
47 DCHECK(menu_delegate); | 60 DCHECK(menu_delegate); |
48 | 61 |
49 pending_menu_ = menu_delegate->BuildMenu(guest_web_contents(), params); | 62 pending_menu_ = menu_delegate->BuildMenu(guest_web_contents(), params); |
50 | 63 |
51 // Pass it to embedder. | 64 // Pass it to embedder. |
52 int request_id = ++pending_context_menu_request_id_; | 65 int request_id = ++pending_context_menu_request_id_; |
53 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); | 66 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); |
54 scoped_ptr<base::ListValue> items = | 67 scoped_ptr<base::ListValue> items = |
55 MenuModelToValue(pending_menu_->menu_model()); | 68 MenuModelToValue(pending_menu_->menu_model()); |
56 args->Set(webview::kContextMenuItems, items.release()); | 69 args->Set(webview::kContextMenuItems, items.release()); |
57 args->SetInteger(webview::kRequestId, request_id); | 70 args->SetInteger(webview::kRequestId, request_id); |
58 web_view_guest_->DispatchEventToEmbedder( | 71 web_view_guest()->DispatchEventToEmbedder( |
59 new extensions::GuestViewBase::Event( | 72 new extensions::GuestViewBase::Event( |
60 webview::kEventContextMenu, args.Pass())); | 73 webview::kEventContextMenu, args.Pass())); |
61 return true; | 74 return true; |
62 } | 75 } |
63 | 76 |
64 // TODO(hanxi) Investigate which of these observers should move to the | 77 // TODO(hanxi) Investigate which of these observers should move to the |
65 // extension module in the future. | 78 // extension module in the future. |
66 void ChromeWebViewGuestDelegate::OnAttachWebViewHelpers( | 79 void ChromeWebViewGuestDelegate::OnAttachWebViewHelpers( |
67 content::WebContents* contents) { | 80 content::WebContents* contents) { |
68 // Create a zoom controller for the guest contents give it access to | 81 // Create a zoom controller for the guest contents give it access to |
(...skipping 11 matching lines...) Expand all Loading... |
80 #if defined(ENABLE_FULL_PRINTING) | 93 #if defined(ENABLE_FULL_PRINTING) |
81 printing::PrintViewManager::CreateForWebContents(contents); | 94 printing::PrintViewManager::CreateForWebContents(contents); |
82 printing::PrintPreviewMessageHandler::CreateForWebContents(contents); | 95 printing::PrintPreviewMessageHandler::CreateForWebContents(contents); |
83 #else | 96 #else |
84 printing::PrintViewManagerBasic::CreateForWebContents(contents); | 97 printing::PrintViewManagerBasic::CreateForWebContents(contents); |
85 #endif // defined(ENABLE_FULL_PRINTING) | 98 #endif // defined(ENABLE_FULL_PRINTING) |
86 #endif // defined(ENABLE_PRINTING) | 99 #endif // defined(ENABLE_PRINTING) |
87 PDFTabHelper::CreateForWebContents(contents); | 100 PDFTabHelper::CreateForWebContents(contents); |
88 } | 101 } |
89 | 102 |
| 103 void ChromeWebViewGuestDelegate::OnEmbedderDestroyed() { |
| 104 // TODO(fsamuel): WebRequest event listeners for <webview> should survive |
| 105 // reparenting of a <webview> within a single embedder. Right now, we keep |
| 106 // around the browser state for the listener for the lifetime of the embedder. |
| 107 // Ideally, the lifetime of the listeners should match the lifetime of the |
| 108 // <webview> DOM node. Once http://crbug.com/156219 is resolved we can move |
| 109 // the call to RemoveWebViewEventListenersOnIOThread back to |
| 110 // WebViewGuest::WebContentsDestroyed. |
| 111 content::BrowserThread::PostTask( |
| 112 content::BrowserThread::IO, |
| 113 FROM_HERE, |
| 114 base::Bind( |
| 115 &RemoveWebViewEventListenersOnIOThread, |
| 116 web_view_guest()->browser_context(), |
| 117 web_view_guest()->embedder_extension_id(), |
| 118 web_view_guest()->embedder_render_process_id(), |
| 119 web_view_guest()->view_instance_id())); |
| 120 } |
| 121 |
90 void ChromeWebViewGuestDelegate::OnDidCommitProvisionalLoadForFrame( | 122 void ChromeWebViewGuestDelegate::OnDidCommitProvisionalLoadForFrame( |
91 bool is_main_frame) { | 123 bool is_main_frame) { |
92 // Update the current zoom factor for the new page. | 124 // Update the current zoom factor for the new page. |
93 ZoomController* zoom_controller = | 125 ZoomController* zoom_controller = |
94 ZoomController::FromWebContents(guest_web_contents()); | 126 ZoomController::FromWebContents(guest_web_contents()); |
95 DCHECK(zoom_controller); | 127 DCHECK(zoom_controller); |
96 current_zoom_factor_ = zoom_controller->GetZoomLevel(); | 128 current_zoom_factor_ = zoom_controller->GetZoomLevel(); |
97 if (is_main_frame) | 129 if (is_main_frame) |
98 chromevox_injected_ = false; | 130 chromevox_injected_ = false; |
99 } | 131 } |
(...skipping 11 matching lines...) Expand all Loading... |
111 | 143 |
112 void ChromeWebViewGuestDelegate::OnDocumentLoadedInFrame( | 144 void ChromeWebViewGuestDelegate::OnDocumentLoadedInFrame( |
113 content::RenderFrameHost* render_frame_host) { | 145 content::RenderFrameHost* render_frame_host) { |
114 if (!render_frame_host->GetParent()) | 146 if (!render_frame_host->GetParent()) |
115 InjectChromeVoxIfNeeded(render_frame_host->GetRenderViewHost()); | 147 InjectChromeVoxIfNeeded(render_frame_host->GetRenderViewHost()); |
116 } | 148 } |
117 | 149 |
118 void ChromeWebViewGuestDelegate::OnGuestDestroyed() { | 150 void ChromeWebViewGuestDelegate::OnGuestDestroyed() { |
119 // Clean up custom context menu items for this guest. | 151 // Clean up custom context menu items for this guest. |
120 extensions::MenuManager* menu_manager = extensions::MenuManager::Get( | 152 extensions::MenuManager* menu_manager = extensions::MenuManager::Get( |
121 Profile::FromBrowserContext(web_view_guest_->browser_context())); | 153 Profile::FromBrowserContext(web_view_guest()->browser_context())); |
122 menu_manager->RemoveAllContextItems(extensions::MenuItem::ExtensionKey( | 154 menu_manager->RemoveAllContextItems(extensions::MenuItem::ExtensionKey( |
123 web_view_guest_->embedder_extension_id(), | 155 web_view_guest()->embedder_extension_id(), |
124 web_view_guest_->view_instance_id())); | 156 web_view_guest()->view_instance_id())); |
125 } | 157 } |
126 | 158 |
127 // static | 159 // static |
128 scoped_ptr<base::ListValue> ChromeWebViewGuestDelegate::MenuModelToValue( | 160 scoped_ptr<base::ListValue> ChromeWebViewGuestDelegate::MenuModelToValue( |
129 const ui::SimpleMenuModel& menu_model) { | 161 const ui::SimpleMenuModel& menu_model) { |
130 scoped_ptr<base::ListValue> items(new base::ListValue()); | 162 scoped_ptr<base::ListValue> items(new base::ListValue()); |
131 for (int i = 0; i < menu_model.GetItemCount(); ++i) { | 163 for (int i = 0; i < menu_model.GetItemCount(); ++i) { |
132 base::DictionaryValue* item_value = new base::DictionaryValue(); | 164 base::DictionaryValue* item_value = new base::DictionaryValue(); |
133 // TODO(lazyboy): We need to expose some kind of enum equivalent of | 165 // TODO(lazyboy): We need to expose some kind of enum equivalent of |
134 // |command_id| instead of plain integers. | 166 // |command_id| instead of plain integers. |
135 item_value->SetInteger(webview::kMenuItemCommandId, | 167 item_value->SetInteger(webview::kMenuItemCommandId, |
136 menu_model.GetCommandIdAt(i)); | 168 menu_model.GetCommandIdAt(i)); |
137 item_value->SetString(webview::kMenuItemLabel, menu_model.GetLabelAt(i)); | 169 item_value->SetString(webview::kMenuItemLabel, menu_model.GetLabelAt(i)); |
138 items->Append(item_value); | 170 items->Append(item_value); |
139 } | 171 } |
140 return items.Pass(); | 172 return items.Pass(); |
141 } | 173 } |
142 | 174 |
143 void ChromeWebViewGuestDelegate::OnSetZoom(double zoom_factor) { | 175 void ChromeWebViewGuestDelegate::OnSetZoom(double zoom_factor) { |
144 ZoomController* zoom_controller = | 176 ZoomController* zoom_controller = |
145 ZoomController::FromWebContents(guest_web_contents()); | 177 ZoomController::FromWebContents(guest_web_contents()); |
146 DCHECK(zoom_controller); | 178 DCHECK(zoom_controller); |
147 double zoom_level = content::ZoomFactorToZoomLevel(zoom_factor); | 179 double zoom_level = content::ZoomFactorToZoomLevel(zoom_factor); |
148 zoom_controller->SetZoomLevel(zoom_level); | 180 zoom_controller->SetZoomLevel(zoom_level); |
149 | 181 |
150 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); | 182 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); |
151 args->SetDouble(webview::kOldZoomFactor, current_zoom_factor_); | 183 args->SetDouble(webview::kOldZoomFactor, current_zoom_factor_); |
152 args->SetDouble(webview::kNewZoomFactor, zoom_factor); | 184 args->SetDouble(webview::kNewZoomFactor, zoom_factor); |
153 web_view_guest_->DispatchEventToEmbedder( | 185 web_view_guest()->DispatchEventToEmbedder( |
154 new extensions::GuestViewBase::Event( | 186 new extensions::GuestViewBase::Event( |
155 webview::kEventZoomChange, args.Pass())); | 187 webview::kEventZoomChange, args.Pass())); |
156 current_zoom_factor_ = zoom_factor; | 188 current_zoom_factor_ = zoom_factor; |
157 } | 189 } |
158 | 190 |
159 void ChromeWebViewGuestDelegate::OnShowContextMenu( | 191 void ChromeWebViewGuestDelegate::OnShowContextMenu( |
160 int request_id, | 192 int request_id, |
161 const MenuItemVector* items) { | 193 const MenuItemVector* items) { |
162 if (!pending_menu_.get()) | 194 if (!pending_menu_.get()) |
163 return; | 195 return; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 accessibility_subscription_.reset(); | 227 accessibility_subscription_.reset(); |
196 } else if (details.notification_type == | 228 } else if (details.notification_type == |
197 chromeos::ACCESSIBILITY_TOGGLE_SPOKEN_FEEDBACK) { | 229 chromeos::ACCESSIBILITY_TOGGLE_SPOKEN_FEEDBACK) { |
198 if (details.enabled) | 230 if (details.enabled) |
199 InjectChromeVoxIfNeeded(guest_web_contents()->GetRenderViewHost()); | 231 InjectChromeVoxIfNeeded(guest_web_contents()->GetRenderViewHost()); |
200 else | 232 else |
201 chromevox_injected_ = false; | 233 chromevox_injected_ = false; |
202 } | 234 } |
203 } | 235 } |
204 #endif | 236 #endif |
OLD | NEW |