| 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/chrome_extension_web_contents_observer.h" | 8 #include "chrome/browser/extensions/chrome_extension_web_contents_observer.h" |
| 9 #include "chrome/browser/favicon/favicon_tab_helper.h" | 9 #include "chrome/browser/favicon/favicon_tab_helper.h" |
| 10 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h" | 10 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h" |
| 11 #include "chrome/browser/ui/pdf/chrome_pdf_web_contents_helper_client.h" | 11 #include "chrome/browser/ui/pdf/chrome_pdf_web_contents_helper_client.h" |
| 12 #include "chrome/common/chrome_version_info.h" | 12 #include "chrome/common/chrome_version_info.h" |
| 13 #include "components/pdf/browser/pdf_web_contents_helper.h" | 13 #include "components/pdf/browser/pdf_web_contents_helper.h" |
| 14 #include "components/renderer_context_menu/context_menu_delegate.h" | 14 #include "components/renderer_context_menu/context_menu_delegate.h" |
| 15 #include "components/ui/zoom/zoom_controller.h" | |
| 16 #include "content/public/common/page_zoom.h" | |
| 17 #include "extensions/browser/api/web_request/web_request_api.h" | 15 #include "extensions/browser/api/web_request/web_request_api.h" |
| 18 #include "extensions/browser/guest_view/web_view/web_view_constants.h" | 16 #include "extensions/browser/guest_view/web_view/web_view_constants.h" |
| 19 | 17 |
| 20 #if defined(ENABLE_PRINTING) | 18 #if defined(ENABLE_PRINTING) |
| 21 #if defined(ENABLE_PRINT_PREVIEW) | 19 #if defined(ENABLE_PRINT_PREVIEW) |
| 22 #include "chrome/browser/printing/print_preview_message_handler.h" | 20 #include "chrome/browser/printing/print_preview_message_handler.h" |
| 23 #include "chrome/browser/printing/print_view_manager.h" | 21 #include "chrome/browser/printing/print_view_manager.h" |
| 24 #else | 22 #else |
| 25 #include "chrome/browser/printing/print_view_manager_basic.h" | 23 #include "chrome/browser/printing/print_view_manager_basic.h" |
| 26 #endif // defined(ENABLE_PRINT_PREVIEW) | 24 #endif // defined(ENABLE_PRINT_PREVIEW) |
| 27 #endif // defined(ENABLE_PRINTING) | 25 #endif // defined(ENABLE_PRINTING) |
| 28 | 26 |
| 29 namespace extensions { | 27 namespace extensions { |
| 30 | 28 |
| 31 using ui_zoom::ZoomController; | |
| 32 | |
| 33 ChromeWebViewGuestDelegate::ChromeWebViewGuestDelegate( | 29 ChromeWebViewGuestDelegate::ChromeWebViewGuestDelegate( |
| 34 WebViewGuest* web_view_guest) | 30 WebViewGuest* web_view_guest) |
| 35 : pending_context_menu_request_id_(0), | 31 : pending_context_menu_request_id_(0), |
| 36 chromevox_injected_(false), | 32 chromevox_injected_(false), |
| 37 current_zoom_factor_(1.0), | |
| 38 web_view_guest_(web_view_guest), | 33 web_view_guest_(web_view_guest), |
| 39 weak_ptr_factory_(this) { | 34 weak_ptr_factory_(this) { |
| 40 } | 35 } |
| 41 | 36 |
| 42 ChromeWebViewGuestDelegate::~ChromeWebViewGuestDelegate() { | 37 ChromeWebViewGuestDelegate::~ChromeWebViewGuestDelegate() { |
| 43 } | 38 } |
| 44 | 39 |
| 45 double ChromeWebViewGuestDelegate::GetZoom() { | |
| 46 return current_zoom_factor_; | |
| 47 } | |
| 48 | |
| 49 bool ChromeWebViewGuestDelegate::HandleContextMenu( | 40 bool ChromeWebViewGuestDelegate::HandleContextMenu( |
| 50 const content::ContextMenuParams& params) { | 41 const content::ContextMenuParams& params) { |
| 51 ContextMenuDelegate* menu_delegate = | 42 ContextMenuDelegate* menu_delegate = |
| 52 ContextMenuDelegate::FromWebContents(guest_web_contents()); | 43 ContextMenuDelegate::FromWebContents(guest_web_contents()); |
| 53 DCHECK(menu_delegate); | 44 DCHECK(menu_delegate); |
| 54 | 45 |
| 55 pending_menu_ = menu_delegate->BuildMenu(guest_web_contents(), params); | 46 pending_menu_ = menu_delegate->BuildMenu(guest_web_contents(), params); |
| 56 | 47 |
| 57 // Pass it to embedder. | 48 // Pass it to embedder. |
| 58 int request_id = ++pending_context_menu_request_id_; | 49 int request_id = ++pending_context_menu_request_id_; |
| 59 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); | 50 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); |
| 60 scoped_ptr<base::ListValue> items = | 51 scoped_ptr<base::ListValue> items = |
| 61 MenuModelToValue(pending_menu_->menu_model()); | 52 MenuModelToValue(pending_menu_->menu_model()); |
| 62 args->Set(webview::kContextMenuItems, items.release()); | 53 args->Set(webview::kContextMenuItems, items.release()); |
| 63 args->SetInteger(webview::kRequestId, request_id); | 54 args->SetInteger(webview::kRequestId, request_id); |
| 64 web_view_guest()->DispatchEventToEmbedder( | 55 web_view_guest()->DispatchEventToEmbedder( |
| 65 new GuestViewBase::Event(webview::kEventContextMenu, args.Pass())); | 56 new GuestViewBase::Event(webview::kEventContextMenu, args.Pass())); |
| 66 return true; | 57 return true; |
| 67 } | 58 } |
| 68 | 59 |
| 69 // TODO(hanxi) Investigate which of these observers should move to the | 60 // TODO(hanxi) Investigate which of these observers should move to the |
| 70 // extension module in the future. | 61 // extension module in the future. |
| 71 void ChromeWebViewGuestDelegate::OnAttachWebViewHelpers( | 62 void ChromeWebViewGuestDelegate::OnAttachWebViewHelpers( |
| 72 content::WebContents* contents) { | 63 content::WebContents* contents) { |
| 73 // Create a zoom controller for the guest contents give it access to | |
| 74 // GetZoomLevel() and and SetZoomLevel() in WebViewGuest. | |
| 75 // TODO(wjmaclean) This currently uses the same HostZoomMap as the browser | |
| 76 // context, but we eventually want to isolate the guest contents from zoom | |
| 77 // changes outside the guest (e.g. in the main browser), so we should | |
| 78 // create a separate HostZoomMap for the guest. | |
| 79 ZoomController::CreateForWebContents(contents); | |
| 80 | |
| 81 FaviconTabHelper::CreateForWebContents(contents); | 64 FaviconTabHelper::CreateForWebContents(contents); |
| 82 ChromeExtensionWebContentsObserver::CreateForWebContents(contents); | 65 ChromeExtensionWebContentsObserver::CreateForWebContents(contents); |
| 83 #if defined(ENABLE_PRINTING) | 66 #if defined(ENABLE_PRINTING) |
| 84 #if defined(ENABLE_PRINT_PREVIEW) | 67 #if defined(ENABLE_PRINT_PREVIEW) |
| 85 printing::PrintViewManager::CreateForWebContents(contents); | 68 printing::PrintViewManager::CreateForWebContents(contents); |
| 86 printing::PrintPreviewMessageHandler::CreateForWebContents(contents); | 69 printing::PrintPreviewMessageHandler::CreateForWebContents(contents); |
| 87 #else | 70 #else |
| 88 printing::PrintViewManagerBasic::CreateForWebContents(contents); | 71 printing::PrintViewManagerBasic::CreateForWebContents(contents); |
| 89 #endif // defined(ENABLE_PRINT_PREVIEW) | 72 #endif // defined(ENABLE_PRINT_PREVIEW) |
| 90 #endif // defined(ENABLE_PRINTING) | 73 #endif // defined(ENABLE_PRINTING) |
| 91 pdf::PDFWebContentsHelper::CreateForWebContentsWithClient( | 74 pdf::PDFWebContentsHelper::CreateForWebContentsWithClient( |
| 92 contents, | 75 contents, |
| 93 scoped_ptr<pdf::PDFWebContentsHelperClient>( | 76 scoped_ptr<pdf::PDFWebContentsHelperClient>( |
| 94 new ChromePDFWebContentsHelperClient())); | 77 new ChromePDFWebContentsHelperClient())); |
| 95 } | 78 } |
| 96 | 79 |
| 97 void ChromeWebViewGuestDelegate::OnDidAttachToEmbedder() { | |
| 98 // TODO(fsamuel): This code should be implemented in GuestViewBase once the | |
| 99 // ZoomController moves to the extensions module. | |
| 100 ZoomController* zoom_controller = ZoomController::FromWebContents( | |
| 101 web_view_guest()->embedder_web_contents()); | |
| 102 if (!zoom_controller) | |
| 103 return; | |
| 104 // Listen to the embedder's zoom changes. | |
| 105 zoom_controller->AddObserver(this); | |
| 106 // Set the guest's initial zoom level to be equal to the embedder's. | |
| 107 ZoomController::FromWebContents(guest_web_contents())-> | |
| 108 SetZoomLevel(zoom_controller->GetZoomLevel()); | |
| 109 } | |
| 110 | |
| 111 void ChromeWebViewGuestDelegate::OnDidCommitProvisionalLoadForFrame( | 80 void ChromeWebViewGuestDelegate::OnDidCommitProvisionalLoadForFrame( |
| 112 bool is_main_frame) { | 81 bool is_main_frame) { |
| 113 // Update the current zoom factor for the new page. | |
| 114 ZoomController* zoom_controller = | |
| 115 ZoomController::FromWebContents(guest_web_contents()); | |
| 116 DCHECK(zoom_controller); | |
| 117 current_zoom_factor_ = | |
| 118 content::ZoomLevelToZoomFactor(zoom_controller->GetZoomLevel()); | |
| 119 if (is_main_frame) | 82 if (is_main_frame) |
| 120 chromevox_injected_ = false; | 83 chromevox_injected_ = false; |
| 121 } | 84 } |
| 122 | 85 |
| 123 void ChromeWebViewGuestDelegate::OnDidInitialize() { | 86 void ChromeWebViewGuestDelegate::OnDidInitialize() { |
| 124 #if defined(OS_CHROMEOS) | 87 #if defined(OS_CHROMEOS) |
| 125 chromeos::AccessibilityManager* accessibility_manager = | 88 chromeos::AccessibilityManager* accessibility_manager = |
| 126 chromeos::AccessibilityManager::Get(); | 89 chromeos::AccessibilityManager::Get(); |
| 127 CHECK(accessibility_manager); | 90 CHECK(accessibility_manager); |
| 128 accessibility_subscription_ = accessibility_manager->RegisterCallback( | 91 accessibility_subscription_ = accessibility_manager->RegisterCallback( |
| 129 base::Bind(&ChromeWebViewGuestDelegate::OnAccessibilityStatusChanged, | 92 base::Bind(&ChromeWebViewGuestDelegate::OnAccessibilityStatusChanged, |
| 130 weak_ptr_factory_.GetWeakPtr())); | 93 weak_ptr_factory_.GetWeakPtr())); |
| 131 #endif | 94 #endif |
| 132 } | 95 } |
| 133 | 96 |
| 134 void ChromeWebViewGuestDelegate::OnDocumentLoadedInFrame( | 97 void ChromeWebViewGuestDelegate::OnDocumentLoadedInFrame( |
| 135 content::RenderFrameHost* render_frame_host) { | 98 content::RenderFrameHost* render_frame_host) { |
| 136 if (!render_frame_host->GetParent()) | 99 if (!render_frame_host->GetParent()) |
| 137 InjectChromeVoxIfNeeded(render_frame_host->GetRenderViewHost()); | 100 InjectChromeVoxIfNeeded(render_frame_host->GetRenderViewHost()); |
| 138 } | 101 } |
| 139 | 102 |
| 140 void ChromeWebViewGuestDelegate::OnEmbedderWillBeDestroyed() { | |
| 141 content::WebContents* embedder_web_contents = | |
| 142 web_view_guest()->embedder_web_contents(); | |
| 143 if (!embedder_web_contents) | |
| 144 return; | |
| 145 | |
| 146 ZoomController* zoom_controller = | |
| 147 ZoomController::FromWebContents(embedder_web_contents); | |
| 148 if (zoom_controller) | |
| 149 zoom_controller->RemoveObserver(this); | |
| 150 } | |
| 151 | |
| 152 void ChromeWebViewGuestDelegate::OnGuestDestroyed() { | 103 void ChromeWebViewGuestDelegate::OnGuestDestroyed() { |
| 153 // Clean up custom context menu items for this guest. | 104 // Clean up custom context menu items for this guest. |
| 154 MenuManager* menu_manager = MenuManager::Get( | 105 MenuManager* menu_manager = MenuManager::Get( |
| 155 Profile::FromBrowserContext(web_view_guest()->browser_context())); | 106 Profile::FromBrowserContext(web_view_guest()->browser_context())); |
| 156 menu_manager->RemoveAllContextItems(MenuItem::ExtensionKey( | 107 menu_manager->RemoveAllContextItems(MenuItem::ExtensionKey( |
| 157 web_view_guest()->owner_extension_id(), | 108 web_view_guest()->owner_extension_id(), |
| 158 web_view_guest()->view_instance_id())); | 109 web_view_guest()->view_instance_id())); |
| 159 } | 110 } |
| 160 | 111 |
| 161 // static | 112 // static |
| 162 scoped_ptr<base::ListValue> ChromeWebViewGuestDelegate::MenuModelToValue( | 113 scoped_ptr<base::ListValue> ChromeWebViewGuestDelegate::MenuModelToValue( |
| 163 const ui::SimpleMenuModel& menu_model) { | 114 const ui::SimpleMenuModel& menu_model) { |
| 164 scoped_ptr<base::ListValue> items(new base::ListValue()); | 115 scoped_ptr<base::ListValue> items(new base::ListValue()); |
| 165 for (int i = 0; i < menu_model.GetItemCount(); ++i) { | 116 for (int i = 0; i < menu_model.GetItemCount(); ++i) { |
| 166 base::DictionaryValue* item_value = new base::DictionaryValue(); | 117 base::DictionaryValue* item_value = new base::DictionaryValue(); |
| 167 // TODO(lazyboy): We need to expose some kind of enum equivalent of | 118 // TODO(lazyboy): We need to expose some kind of enum equivalent of |
| 168 // |command_id| instead of plain integers. | 119 // |command_id| instead of plain integers. |
| 169 item_value->SetInteger(webview::kMenuItemCommandId, | 120 item_value->SetInteger(webview::kMenuItemCommandId, |
| 170 menu_model.GetCommandIdAt(i)); | 121 menu_model.GetCommandIdAt(i)); |
| 171 item_value->SetString(webview::kMenuItemLabel, menu_model.GetLabelAt(i)); | 122 item_value->SetString(webview::kMenuItemLabel, menu_model.GetLabelAt(i)); |
| 172 items->Append(item_value); | 123 items->Append(item_value); |
| 173 } | 124 } |
| 174 return items.Pass(); | 125 return items.Pass(); |
| 175 } | 126 } |
| 176 | 127 |
| 177 void ChromeWebViewGuestDelegate::OnSetZoom(double zoom_factor) { | |
| 178 ZoomController* zoom_controller = | |
| 179 ZoomController::FromWebContents(guest_web_contents()); | |
| 180 DCHECK(zoom_controller); | |
| 181 double zoom_level = content::ZoomFactorToZoomLevel(zoom_factor); | |
| 182 zoom_controller->SetZoomLevel(zoom_level); | |
| 183 | |
| 184 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); | |
| 185 args->SetDouble(webview::kOldZoomFactor, current_zoom_factor_); | |
| 186 args->SetDouble(webview::kNewZoomFactor, zoom_factor); | |
| 187 web_view_guest()->DispatchEventToEmbedder( | |
| 188 new GuestViewBase::Event(webview::kEventZoomChange, args.Pass())); | |
| 189 current_zoom_factor_ = zoom_factor; | |
| 190 } | |
| 191 | |
| 192 void ChromeWebViewGuestDelegate::OnShowContextMenu( | 128 void ChromeWebViewGuestDelegate::OnShowContextMenu( |
| 193 int request_id, | 129 int request_id, |
| 194 const MenuItemVector* items) { | 130 const MenuItemVector* items) { |
| 195 if (!pending_menu_.get()) | 131 if (!pending_menu_.get()) |
| 196 return; | 132 return; |
| 197 | 133 |
| 198 // Make sure this was the correct request. | 134 // Make sure this was the correct request. |
| 199 if (request_id != pending_context_menu_request_id_) | 135 if (request_id != pending_context_menu_request_id_) |
| 200 return; | 136 return; |
| 201 | 137 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 229 } else if (details.notification_type == | 165 } else if (details.notification_type == |
| 230 chromeos::ACCESSIBILITY_TOGGLE_SPOKEN_FEEDBACK) { | 166 chromeos::ACCESSIBILITY_TOGGLE_SPOKEN_FEEDBACK) { |
| 231 if (details.enabled) | 167 if (details.enabled) |
| 232 InjectChromeVoxIfNeeded(guest_web_contents()->GetRenderViewHost()); | 168 InjectChromeVoxIfNeeded(guest_web_contents()->GetRenderViewHost()); |
| 233 else | 169 else |
| 234 chromevox_injected_ = false; | 170 chromevox_injected_ = false; |
| 235 } | 171 } |
| 236 } | 172 } |
| 237 #endif | 173 #endif |
| 238 | 174 |
| 239 void ChromeWebViewGuestDelegate::OnZoomChanged( | |
| 240 const ZoomController::ZoomChangedEventData& data) { | |
| 241 ZoomController::FromWebContents(guest_web_contents())-> | |
| 242 SetZoomLevel(data.new_zoom_level); | |
| 243 } | |
| 244 | |
| 245 } // namespace extensions | 175 } // namespace extensions |
| OLD | NEW |