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), |
| 44 find_helper_(web_view_guest), |
30 pending_context_menu_request_id_(0), | 45 pending_context_menu_request_id_(0), |
31 chromevox_injected_(false), | 46 chromevox_injected_(false), |
32 current_zoom_factor_(1.0), | 47 current_zoom_factor_(1.0) { |
33 web_view_guest_(web_view_guest) { | |
34 } | 48 } |
35 | 49 |
36 ChromeWebViewGuestDelegate::~ChromeWebViewGuestDelegate() { | 50 ChromeWebViewGuestDelegate::~ChromeWebViewGuestDelegate() { |
37 } | 51 } |
38 | 52 |
| 53 void ChromeWebViewGuestDelegate::Find( |
| 54 const base::string16& search_text, |
| 55 const blink::WebFindOptions& options, |
| 56 extensions::WebViewInternalFindFunction* find_function) { |
| 57 find_helper_.Find(guest_web_contents(), search_text, options, find_function); |
| 58 } |
| 59 |
| 60 void ChromeWebViewGuestDelegate::FindReply(content::WebContents* source, |
| 61 int request_id, |
| 62 int number_of_matches, |
| 63 const gfx::Rect& selection_rect, |
| 64 int active_match_ordinal, |
| 65 bool final_update) { |
| 66 find_helper_.FindReply(request_id, number_of_matches, selection_rect, |
| 67 active_match_ordinal, final_update); |
| 68 } |
| 69 |
| 70 void ChromeWebViewGuestDelegate::StopFinding(content::StopFindAction action) { |
| 71 find_helper_.CancelAllFindSessions(); |
| 72 guest_web_contents()->StopFinding(action); |
| 73 } |
| 74 |
39 double ChromeWebViewGuestDelegate::GetZoom() { | 75 double ChromeWebViewGuestDelegate::GetZoom() { |
40 return current_zoom_factor_; | 76 return current_zoom_factor_; |
41 } | 77 } |
42 | 78 |
43 bool ChromeWebViewGuestDelegate::HandleContextMenu( | 79 bool ChromeWebViewGuestDelegate::HandleContextMenu( |
44 const content::ContextMenuParams& params) { | 80 const content::ContextMenuParams& params) { |
45 ContextMenuDelegate* menu_delegate = | 81 ContextMenuDelegate* menu_delegate = |
46 ContextMenuDelegate::FromWebContents(guest_web_contents()); | 82 ContextMenuDelegate::FromWebContents(guest_web_contents()); |
47 DCHECK(menu_delegate); | 83 DCHECK(menu_delegate); |
48 | 84 |
49 pending_menu_ = menu_delegate->BuildMenu(guest_web_contents(), params); | 85 pending_menu_ = menu_delegate->BuildMenu(guest_web_contents(), params); |
50 | 86 |
51 // Pass it to embedder. | 87 // Pass it to embedder. |
52 int request_id = ++pending_context_menu_request_id_; | 88 int request_id = ++pending_context_menu_request_id_; |
53 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); | 89 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); |
54 scoped_ptr<base::ListValue> items = | 90 scoped_ptr<base::ListValue> items = |
55 MenuModelToValue(pending_menu_->menu_model()); | 91 MenuModelToValue(pending_menu_->menu_model()); |
56 args->Set(webview::kContextMenuItems, items.release()); | 92 args->Set(webview::kContextMenuItems, items.release()); |
57 args->SetInteger(webview::kRequestId, request_id); | 93 args->SetInteger(webview::kRequestId, request_id); |
58 web_view_guest_->DispatchEventToEmbedder( | 94 web_view_guest()->DispatchEventToEmbedder( |
59 new extensions::GuestViewBase::Event( | 95 new extensions::GuestViewBase::Event( |
60 webview::kEventContextMenu, args.Pass())); | 96 webview::kEventContextMenu, args.Pass())); |
61 return true; | 97 return true; |
62 } | 98 } |
63 | 99 |
64 // TODO(hanxi) Investigate which of these observers should move to the | 100 // TODO(hanxi) Investigate which of these observers should move to the |
65 // extension module in the future. | 101 // extension module in the future. |
66 void ChromeWebViewGuestDelegate::OnAttachWebViewHelpers( | 102 void ChromeWebViewGuestDelegate::OnAttachWebViewHelpers( |
67 content::WebContents* contents) { | 103 content::WebContents* contents) { |
68 // Create a zoom controller for the guest contents give it access to | 104 // 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) | 116 #if defined(ENABLE_FULL_PRINTING) |
81 printing::PrintViewManager::CreateForWebContents(contents); | 117 printing::PrintViewManager::CreateForWebContents(contents); |
82 printing::PrintPreviewMessageHandler::CreateForWebContents(contents); | 118 printing::PrintPreviewMessageHandler::CreateForWebContents(contents); |
83 #else | 119 #else |
84 printing::PrintViewManagerBasic::CreateForWebContents(contents); | 120 printing::PrintViewManagerBasic::CreateForWebContents(contents); |
85 #endif // defined(ENABLE_FULL_PRINTING) | 121 #endif // defined(ENABLE_FULL_PRINTING) |
86 #endif // defined(ENABLE_PRINTING) | 122 #endif // defined(ENABLE_PRINTING) |
87 PDFTabHelper::CreateForWebContents(contents); | 123 PDFTabHelper::CreateForWebContents(contents); |
88 } | 124 } |
89 | 125 |
| 126 void ChromeWebViewGuestDelegate::OnEmbedderDestroyed() { |
| 127 // TODO(fsamuel): WebRequest event listeners for <webview> should survive |
| 128 // reparenting of a <webview> within a single embedder. Right now, we keep |
| 129 // around the browser state for the listener for the lifetime of the embedder. |
| 130 // Ideally, the lifetime of the listeners should match the lifetime of the |
| 131 // <webview> DOM node. Once http://crbug.com/156219 is resolved we can move |
| 132 // the call to RemoveWebViewEventListenersOnIOThread back to |
| 133 // WebViewGuest::WebContentsDestroyed. |
| 134 content::BrowserThread::PostTask( |
| 135 content::BrowserThread::IO, |
| 136 FROM_HERE, |
| 137 base::Bind( |
| 138 &RemoveWebViewEventListenersOnIOThread, |
| 139 web_view_guest()->browser_context(), |
| 140 web_view_guest()->embedder_extension_id(), |
| 141 web_view_guest()->embedder_render_process_id(), |
| 142 web_view_guest()->view_instance_id())); |
| 143 } |
| 144 |
90 void ChromeWebViewGuestDelegate::OnDidCommitProvisionalLoadForFrame( | 145 void ChromeWebViewGuestDelegate::OnDidCommitProvisionalLoadForFrame( |
91 bool is_main_frame) { | 146 bool is_main_frame) { |
| 147 find_helper_.CancelAllFindSessions(); |
| 148 |
92 // Update the current zoom factor for the new page. | 149 // Update the current zoom factor for the new page. |
93 ZoomController* zoom_controller = | 150 ZoomController* zoom_controller = |
94 ZoomController::FromWebContents(guest_web_contents()); | 151 ZoomController::FromWebContents(guest_web_contents()); |
95 DCHECK(zoom_controller); | 152 DCHECK(zoom_controller); |
96 current_zoom_factor_ = zoom_controller->GetZoomLevel(); | 153 current_zoom_factor_ = zoom_controller->GetZoomLevel(); |
97 if (is_main_frame) | 154 if (is_main_frame) |
98 chromevox_injected_ = false; | 155 chromevox_injected_ = false; |
99 } | 156 } |
100 | 157 |
101 void ChromeWebViewGuestDelegate::OnDidInitialize() { | 158 void ChromeWebViewGuestDelegate::OnDidInitialize() { |
102 #if defined(OS_CHROMEOS) | 159 #if defined(OS_CHROMEOS) |
103 chromeos::AccessibilityManager* accessibility_manager = | 160 chromeos::AccessibilityManager* accessibility_manager = |
104 chromeos::AccessibilityManager::Get(); | 161 chromeos::AccessibilityManager::Get(); |
105 CHECK(accessibility_manager); | 162 CHECK(accessibility_manager); |
106 accessibility_subscription_ = accessibility_manager->RegisterCallback( | 163 accessibility_subscription_ = accessibility_manager->RegisterCallback( |
107 base::Bind(&ChromeWebViewGuestDelegate::OnAccessibilityStatusChanged, | 164 base::Bind(&ChromeWebViewGuestDelegate::OnAccessibilityStatusChanged, |
108 base::Unretained(this))); | 165 base::Unretained(this))); |
109 #endif | 166 #endif |
110 } | 167 } |
111 | 168 |
112 void ChromeWebViewGuestDelegate::OnDocumentLoadedInFrame( | 169 void ChromeWebViewGuestDelegate::OnDocumentLoadedInFrame( |
113 content::RenderFrameHost* render_frame_host) { | 170 content::RenderFrameHost* render_frame_host) { |
114 if (!render_frame_host->GetParent()) | 171 if (!render_frame_host->GetParent()) |
115 InjectChromeVoxIfNeeded(render_frame_host->GetRenderViewHost()); | 172 InjectChromeVoxIfNeeded(render_frame_host->GetRenderViewHost()); |
116 } | 173 } |
117 | 174 |
118 void ChromeWebViewGuestDelegate::OnGuestDestroyed() { | 175 void ChromeWebViewGuestDelegate::OnGuestDestroyed() { |
119 // Clean up custom context menu items for this guest. | 176 // Clean up custom context menu items for this guest. |
120 extensions::MenuManager* menu_manager = extensions::MenuManager::Get( | 177 extensions::MenuManager* menu_manager = extensions::MenuManager::Get( |
121 Profile::FromBrowserContext(web_view_guest_->browser_context())); | 178 Profile::FromBrowserContext(web_view_guest()->browser_context())); |
122 menu_manager->RemoveAllContextItems(extensions::MenuItem::ExtensionKey( | 179 menu_manager->RemoveAllContextItems(extensions::MenuItem::ExtensionKey( |
123 web_view_guest_->embedder_extension_id(), | 180 web_view_guest()->embedder_extension_id(), |
124 web_view_guest_->view_instance_id())); | 181 web_view_guest()->view_instance_id())); |
125 } | 182 } |
126 | 183 |
127 // static | 184 // static |
128 scoped_ptr<base::ListValue> ChromeWebViewGuestDelegate::MenuModelToValue( | 185 scoped_ptr<base::ListValue> ChromeWebViewGuestDelegate::MenuModelToValue( |
129 const ui::SimpleMenuModel& menu_model) { | 186 const ui::SimpleMenuModel& menu_model) { |
130 scoped_ptr<base::ListValue> items(new base::ListValue()); | 187 scoped_ptr<base::ListValue> items(new base::ListValue()); |
131 for (int i = 0; i < menu_model.GetItemCount(); ++i) { | 188 for (int i = 0; i < menu_model.GetItemCount(); ++i) { |
132 base::DictionaryValue* item_value = new base::DictionaryValue(); | 189 base::DictionaryValue* item_value = new base::DictionaryValue(); |
133 // TODO(lazyboy): We need to expose some kind of enum equivalent of | 190 // TODO(lazyboy): We need to expose some kind of enum equivalent of |
134 // |command_id| instead of plain integers. | 191 // |command_id| instead of plain integers. |
135 item_value->SetInteger(webview::kMenuItemCommandId, | 192 item_value->SetInteger(webview::kMenuItemCommandId, |
136 menu_model.GetCommandIdAt(i)); | 193 menu_model.GetCommandIdAt(i)); |
137 item_value->SetString(webview::kMenuItemLabel, menu_model.GetLabelAt(i)); | 194 item_value->SetString(webview::kMenuItemLabel, menu_model.GetLabelAt(i)); |
138 items->Append(item_value); | 195 items->Append(item_value); |
139 } | 196 } |
140 return items.Pass(); | 197 return items.Pass(); |
141 } | 198 } |
142 | 199 |
| 200 void ChromeWebViewGuestDelegate::OnRenderProcessGone() { |
| 201 // Cancel all find sessions in progress. |
| 202 find_helper_.CancelAllFindSessions(); |
| 203 } |
| 204 |
143 void ChromeWebViewGuestDelegate::OnSetZoom(double zoom_factor) { | 205 void ChromeWebViewGuestDelegate::OnSetZoom(double zoom_factor) { |
144 ZoomController* zoom_controller = | 206 ZoomController* zoom_controller = |
145 ZoomController::FromWebContents(guest_web_contents()); | 207 ZoomController::FromWebContents(guest_web_contents()); |
146 DCHECK(zoom_controller); | 208 DCHECK(zoom_controller); |
147 double zoom_level = content::ZoomFactorToZoomLevel(zoom_factor); | 209 double zoom_level = content::ZoomFactorToZoomLevel(zoom_factor); |
148 zoom_controller->SetZoomLevel(zoom_level); | 210 zoom_controller->SetZoomLevel(zoom_level); |
149 | 211 |
150 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); | 212 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); |
151 args->SetDouble(webview::kOldZoomFactor, current_zoom_factor_); | 213 args->SetDouble(webview::kOldZoomFactor, current_zoom_factor_); |
152 args->SetDouble(webview::kNewZoomFactor, zoom_factor); | 214 args->SetDouble(webview::kNewZoomFactor, zoom_factor); |
153 web_view_guest_->DispatchEventToEmbedder( | 215 web_view_guest()->DispatchEventToEmbedder( |
154 new extensions::GuestViewBase::Event( | 216 new extensions::GuestViewBase::Event( |
155 webview::kEventZoomChange, args.Pass())); | 217 webview::kEventZoomChange, args.Pass())); |
156 current_zoom_factor_ = zoom_factor; | 218 current_zoom_factor_ = zoom_factor; |
157 } | 219 } |
158 | 220 |
159 void ChromeWebViewGuestDelegate::OnShowContextMenu( | 221 void ChromeWebViewGuestDelegate::OnShowContextMenu( |
160 int request_id, | 222 int request_id, |
161 const MenuItemVector* items) { | 223 const MenuItemVector* items) { |
162 if (!pending_menu_.get()) | 224 if (!pending_menu_.get()) |
163 return; | 225 return; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 accessibility_subscription_.reset(); | 257 accessibility_subscription_.reset(); |
196 } else if (details.notification_type == | 258 } else if (details.notification_type == |
197 chromeos::ACCESSIBILITY_TOGGLE_SPOKEN_FEEDBACK) { | 259 chromeos::ACCESSIBILITY_TOGGLE_SPOKEN_FEEDBACK) { |
198 if (details.enabled) | 260 if (details.enabled) |
199 InjectChromeVoxIfNeeded(guest_web_contents()->GetRenderViewHost()); | 261 InjectChromeVoxIfNeeded(guest_web_contents()->GetRenderViewHost()); |
200 else | 262 else |
201 chromevox_injected_ = false; | 263 chromevox_injected_ = false; |
202 } | 264 } |
203 } | 265 } |
204 #endif | 266 #endif |
OLD | NEW |