Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 | |
| 6 #include "chrome/browser/guest_view/web_view/chrome_web_view_guest_delegate.h" | |
| 7 | |
| 8 #include "chrome/browser/extensions/chrome_extension_web_contents_observer.h" | |
| 9 #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" | |
| 12 #include "chrome/browser/ui/pdf/pdf_tab_helper.h" | |
| 13 #include "chrome/browser/ui/zoom/zoom_controller.h" | |
| 14 #include "chrome/common/chrome_version_info.h" | |
| 15 #include "components/renderer_context_menu/context_menu_delegate.h" | |
| 16 #include "content/public/common/page_zoom.h" | |
| 17 | |
| 18 #if defined(ENABLE_PRINTING) | |
| 19 #if defined(ENABLE_FULL_PRINTING) | |
| 20 #include "chrome/browser/printing/print_preview_message_handler.h" | |
| 21 #include "chrome/browser/printing/print_view_manager.h" | |
| 22 #else | |
| 23 #include "chrome/browser/printing/print_view_manager_basic.h" | |
| 24 #endif // defined(ENABLE_FULL_PRINTING) | |
| 25 #endif // defined(ENABLE_PRINTING) | |
| 26 | |
| 27 ChromeWebViewGuestDelegate::ChromeWebViewGuestDelegate( | |
| 28 extensions::WebViewGuest* web_view_guest) | |
| 29 : WebViewGuestDelegate(), | |
| 30 pending_context_menu_request_id_(0), | |
| 31 chromevox_injected_(false), | |
| 32 current_zoom_factor_(1.0), | |
| 33 web_view_guest_(web_view_guest) { | |
| 34 } | |
| 35 | |
| 36 ChromeWebViewGuestDelegate::~ChromeWebViewGuestDelegate() { | |
| 37 } | |
| 38 | |
| 39 double ChromeWebViewGuestDelegate::GetZoom() { | |
| 40 return current_zoom_factor_; | |
| 41 } | |
| 42 | |
| 43 void ChromeWebViewGuestDelegate::InjectChromeVoxIfNeeded( | |
| 44 content::RenderViewHost* render_view_host) { | |
| 45 #if defined(OS_CHROMEOS) | |
| 46 if (!chromevox_injected_) { | |
| 47 chromeos::AccessibilityManager* manager = | |
| 48 chromeos::AccessibilityManager::Get(); | |
| 49 if (manager && manager->IsSpokenFeedbackEnabled()) { | |
| 50 manager->InjectChromeVox(render_view_host); | |
| 51 chromevox_injected_ = true; | |
| 52 } | |
| 53 } | |
| 54 #endif | |
| 55 } | |
| 56 | |
| 57 // TODO(hanxi) Investigate which of these observers should move to the | |
| 58 // extension module in the future. | |
| 59 void ChromeWebViewGuestDelegate::OnAttachWebViewHelpers( | |
| 60 content::WebContents* contents) { | |
| 61 // Create a zoom controller for the guest contents give it access to | |
| 62 // GetZoomLevel() and and SetZoomLevel() in WebViewGuest. | |
| 63 // TODO(wjmaclean) This currently uses the same HostZoomMap as the browser | |
| 64 // context, but we eventually want to isolate the guest contents from zoom | |
| 65 // changes outside the guest (e.g. in the main browser), so we should | |
| 66 // create a separate HostZoomMap for the guest. | |
| 67 ZoomController::CreateForWebContents(contents); | |
| 68 | |
| 69 FaviconTabHelper::CreateForWebContents(contents); | |
| 70 extensions::ChromeExtensionWebContentsObserver:: | |
| 71 CreateForWebContents(contents); | |
| 72 #if defined(ENABLE_PRINTING) | |
| 73 #if defined(ENABLE_FULL_PRINTING) | |
| 74 printing::PrintViewManager::CreateForWebContents(contents); | |
| 75 printing::PrintPreviewMessageHandler::CreateForWebContents(contents); | |
| 76 #else | |
| 77 printing::PrintViewManagerBasic::CreateForWebContents(contents); | |
| 78 #endif // defined(ENABLE_FULL_PRINTING) | |
| 79 #endif // defined(ENABLE_PRINTING) | |
| 80 PDFTabHelper::CreateForWebContents(contents); | |
| 81 } | |
| 82 | |
| 83 void ChromeWebViewGuestDelegate::OnDidCommitProvisionalLoadForFrame( | |
| 84 bool is_main_frame) { | |
| 85 // Update the current zoom factor for the new page. | |
| 86 ZoomController* zoom_controller = | |
| 87 ZoomController::FromWebContents(guest_web_contents()); | |
| 88 DCHECK(zoom_controller); | |
| 89 current_zoom_factor_ = zoom_controller->GetZoomLevel(); | |
| 90 if (is_main_frame) | |
| 91 chromevox_injected_ = false; | |
| 92 } | |
| 93 | |
| 94 void ChromeWebViewGuestDelegate::OnDidInitialize() { | |
| 95 #if defined(OS_CHROMEOS) | |
| 96 chromeos::AccessibilityManager* accessibility_manager = | |
| 97 chromeos::AccessibilityManager::Get(); | |
| 98 CHECK(accessibility_manager); | |
| 99 accessibility_subscription_ = accessibility_manager->RegisterCallback( | |
| 100 base::Bind(&ChromeWebViewGuestDelegate::OnAccessibilityStatusChanged, | |
| 101 base::Unretained(this))); | |
| 102 #endif | |
| 103 } | |
| 104 | |
| 105 void ChromeWebViewGuestDelegate::OnGuestDestroyed() { | |
| 106 // Clean up custom context menu items for this guest. | |
| 107 extensions::MenuManager* menu_manager = extensions::MenuManager::Get( | |
| 108 Profile::FromBrowserContext(web_view_guest_->browser_context())); | |
| 109 menu_manager->RemoveAllContextItems(extensions::MenuItem::ExtensionKey( | |
| 110 web_view_guest_->embedder_extension_id(), | |
| 111 web_view_guest_->view_instance_id())); | |
| 112 } | |
| 113 | |
| 114 // static | |
| 115 scoped_ptr<base::ListValue> ChromeWebViewGuestDelegate::MenuModelToValue( | |
| 116 const ui::SimpleMenuModel& menu_model) { | |
| 117 scoped_ptr<base::ListValue> items(new base::ListValue()); | |
| 118 for (int i = 0; i < menu_model.GetItemCount(); ++i) { | |
| 119 base::DictionaryValue* item_value = new base::DictionaryValue(); | |
| 120 // TODO(lazyboy): We need to expose some kind of enum equivalent of | |
| 121 // |command_id| instead of plain integers. | |
| 122 item_value->SetInteger(webview::kMenuItemCommandId, | |
| 123 menu_model.GetCommandIdAt(i)); | |
| 124 item_value->SetString(webview::kMenuItemLabel, menu_model.GetLabelAt(i)); | |
| 125 items->Append(item_value); | |
| 126 } | |
| 127 return items.Pass(); | |
| 128 } | |
| 129 | |
| 130 extensions::GuestViewBase::Event* ChromeWebViewGuestDelegate:: | |
| 131 OnHandleContextMenu(const content::ContextMenuParams& params) { | |
|
Fady Samuel
2014/08/20 14:45:09
HandleContextMenu
Xi Han
2014/08/20 15:46:01
Done.
| |
| 132 ContextMenuDelegate* menu_delegate = | |
| 133 ContextMenuDelegate::FromWebContents(guest_web_contents()); | |
| 134 DCHECK(menu_delegate); | |
| 135 | |
| 136 pending_menu_ = menu_delegate->BuildMenu(guest_web_contents(), params); | |
| 137 | |
| 138 // Pass it to embedder. | |
| 139 int request_id = ++pending_context_menu_request_id_; | |
| 140 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); | |
| 141 scoped_ptr<base::ListValue> items = | |
| 142 MenuModelToValue(pending_menu_->menu_model()); | |
| 143 args->Set(webview::kContextMenuItems, items.release()); | |
| 144 args->SetInteger(webview::kRequestId, request_id); | |
| 145 return new extensions::GuestViewBase::Event( | |
| 146 webview::kEventContextMenu, args.Pass()); | |
| 147 } | |
| 148 | |
| 149 extensions::GuestViewBase::Event* ChromeWebViewGuestDelegate::OnSetZoom( | |
| 150 double zoom_factor, | |
| 151 const std::string& old_zoom_factor, | |
| 152 const std::string& new_zoom_factor, | |
| 153 const std::string& event_zoom_change) { | |
|
Fady Samuel
2014/08/20 14:45:09
This seems weird to me. Extensions shouldn't know
Xi Han
2014/08/20 15:46:01
Done.
| |
| 154 ZoomController* zoom_controller = | |
| 155 ZoomController::FromWebContents(guest_web_contents()); | |
| 156 DCHECK(zoom_controller); | |
| 157 double zoom_level = content::ZoomFactorToZoomLevel(zoom_factor); | |
| 158 zoom_controller->SetZoomLevel(zoom_level); | |
| 159 | |
| 160 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); | |
| 161 args->SetDouble(old_zoom_factor, current_zoom_factor_); | |
| 162 args->SetDouble(new_zoom_factor, zoom_factor); | |
| 163 return new extensions::GuestViewBase::Event(event_zoom_change, args.Pass()); | |
| 164 } | |
| 165 | |
| 166 void ChromeWebViewGuestDelegate::SetCurrentZoomFactor(double zoom_factor) { | |
| 167 current_zoom_factor_ = zoom_factor; | |
| 168 } | |
| 169 | |
| 170 void ChromeWebViewGuestDelegate::OnShowContextMenu( | |
| 171 int request_id, | |
| 172 const MenuItemVector* items) { | |
| 173 if (!pending_menu_.get()) | |
| 174 return; | |
| 175 | |
| 176 // Make sure this was the correct request. | |
| 177 if (request_id != pending_context_menu_request_id_) | |
| 178 return; | |
| 179 | |
| 180 // TODO(lazyboy): Implement. | |
| 181 DCHECK(!items); | |
| 182 | |
| 183 ContextMenuDelegate* menu_delegate = | |
| 184 ContextMenuDelegate::FromWebContents(guest_web_contents()); | |
| 185 menu_delegate->ShowMenu(pending_menu_.Pass()); | |
| 186 } | |
| 187 | |
| 188 #if defined(OS_CHROMEOS) | |
| 189 void ChromeWebViewGuestDelegate::OnAccessibilityStatusChanged( | |
| 190 const chromeos::AccessibilityStatusEventDetails& details) { | |
| 191 if (details.notification_type == chromeos::ACCESSIBILITY_MANAGER_SHUTDOWN) { | |
| 192 accessibility_subscription_.reset(); | |
| 193 } else if (details.notification_type == | |
| 194 chromeos::ACCESSIBILITY_TOGGLE_SPOKEN_FEEDBACK) { | |
| 195 if (details.enabled) | |
| 196 InjectChromeVoxIfNeeded(guest_web_contents()->GetRenderViewHost()); | |
| 197 else | |
| 198 chromevox_injected_ = false; | |
| 199 } | |
| 200 } | |
| 201 #endif | |
| OLD | NEW |