browser_action_view.cc revision effb81e5f8246d0db0270817048dc992db66e9fb
1// Copyright 2013 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#include "chrome/browser/ui/views/toolbar/browser_action_view.h" 6 7#include "base/strings/utf_string_conversions.h" 8#include "chrome/browser/chrome_notification_types.h" 9#include "chrome/browser/extensions/api/commands/command_service.h" 10#include "chrome/browser/extensions/extension_action.h" 11#include "chrome/browser/extensions/extension_action_manager.h" 12#include "chrome/browser/extensions/extension_context_menu_model.h" 13#include "chrome/browser/extensions/extension_service.h" 14#include "chrome/browser/profiles/profile.h" 15#include "chrome/browser/themes/theme_service.h" 16#include "chrome/browser/themes/theme_service_factory.h" 17#include "chrome/browser/ui/browser.h" 18#include "chrome/browser/ui/views/toolbar/browser_actions_container.h" 19#include "chrome/browser/ui/views/toolbar/toolbar_view.h" 20#include "extensions/common/extension.h" 21#include "extensions/common/manifest_constants.h" 22#include "grit/generated_resources.h" 23#include "grit/theme_resources.h" 24#include "ui/accessibility/ax_view_state.h" 25#include "ui/base/l10n/l10n_util.h" 26#include "ui/base/resource/resource_bundle.h" 27#include "ui/events/event.h" 28#include "ui/gfx/image/image_skia.h" 29#include "ui/gfx/image/image_skia_operations.h" 30#include "ui/gfx/image/image_skia_source.h" 31#include "ui/views/controls/menu/menu_runner.h" 32 33using extensions::Extension; 34 35//////////////////////////////////////////////////////////////////////////////// 36// BrowserActionView 37 38bool BrowserActionView::Delegate::NeedToShowMultipleIconStates() const { 39 return true; 40} 41 42bool BrowserActionView::Delegate::NeedToShowTooltip() const { 43 return true; 44} 45 46BrowserActionView::BrowserActionView(const Extension* extension, 47 Browser* browser, 48 BrowserActionView::Delegate* delegate) 49 : browser_(browser), 50 delegate_(delegate), 51 button_(NULL), 52 extension_(extension) { 53 button_ = new BrowserActionButton(extension_, browser_, delegate_); 54 button_->set_drag_controller(delegate_); 55 button_->set_owned_by_client(); 56 AddChildView(button_); 57 button_->UpdateState(); 58} 59 60BrowserActionView::~BrowserActionView() { 61 button_->Destroy(); 62} 63 64gfx::ImageSkia BrowserActionView::GetIconWithBadge() { 65 return button_->GetIconWithBadge(); 66} 67 68void BrowserActionView::Layout() { 69 button_->SetBounds(0, y(), width(), height()); 70} 71 72void BrowserActionView::GetAccessibleState(ui::AXViewState* state) { 73 state->name = l10n_util::GetStringUTF16( 74 IDS_ACCNAME_EXTENSIONS_BROWSER_ACTION); 75 state->role = ui::AX_ROLE_GROUP; 76} 77 78gfx::Size BrowserActionView::GetPreferredSize() { 79 return gfx::Size(BrowserActionsContainer::IconWidth(false), 80 BrowserActionsContainer::IconHeight()); 81} 82 83void BrowserActionView::PaintChildren(gfx::Canvas* canvas) { 84 View::PaintChildren(canvas); 85 ExtensionAction* action = button()->browser_action(); 86 int tab_id = delegate_->GetCurrentTabId(); 87 if (tab_id >= 0) 88 action->PaintBadge(canvas, GetLocalBounds(), tab_id); 89} 90 91//////////////////////////////////////////////////////////////////////////////// 92// BrowserActionButton 93 94BrowserActionButton::BrowserActionButton(const Extension* extension, 95 Browser* browser, 96 BrowserActionView::Delegate* delegate) 97 : MenuButton(this, base::string16(), NULL, false), 98 browser_(browser), 99 browser_action_( 100 extensions::ExtensionActionManager::Get(browser->profile())-> 101 GetBrowserAction(*extension)), 102 extension_(extension), 103 icon_factory_(browser->profile(), extension, browser_action_, this), 104 delegate_(delegate), 105 context_menu_(NULL), 106 called_registered_extension_command_(false), 107 icon_observer_(NULL) { 108 SetBorder(views::Border::NullBorder()); 109 set_alignment(TextButton::ALIGN_CENTER); 110 set_context_menu_controller(this); 111 112 // No UpdateState() here because View hierarchy not setup yet. Our parent 113 // should call UpdateState() after creation. 114 115 content::NotificationSource notification_source = 116 content::Source<Profile>(browser_->profile()->GetOriginalProfile()); 117 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED, 118 content::Source<ExtensionAction>(browser_action_)); 119 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED, 120 notification_source); 121 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED, 122 notification_source); 123 124 // We also listen for browser theme changes on linux because a switch from or 125 // to GTK requires that we regrab our browser action images. 126 registrar_.Add( 127 this, 128 chrome::NOTIFICATION_BROWSER_THEME_CHANGED, 129 content::Source<ThemeService>( 130 ThemeServiceFactory::GetForProfile(browser->profile()))); 131} 132 133void BrowserActionButton::Destroy() { 134 MaybeUnregisterExtensionCommand(false); 135 136 if (context_menu_) { 137 context_menu_->Cancel(); 138 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); 139 } else { 140 delete this; 141 } 142} 143 144void BrowserActionButton::ViewHierarchyChanged( 145 const ViewHierarchyChangedDetails& details) { 146 if (details.is_add && !called_registered_extension_command_ && 147 GetFocusManager()) { 148 MaybeRegisterExtensionCommand(); 149 called_registered_extension_command_ = true; 150 } 151 152 MenuButton::ViewHierarchyChanged(details); 153} 154 155bool BrowserActionButton::CanHandleAccelerators() const { 156 // View::CanHandleAccelerators() checks to see if the view is visible before 157 // allowing it to process accelerators. This is not appropriate for browser 158 // actions buttons, which can be hidden inside the overflow area. 159 return true; 160} 161 162void BrowserActionButton::GetAccessibleState(ui::AXViewState* state) { 163 views::MenuButton::GetAccessibleState(state); 164 state->role = ui::AX_ROLE_BUTTON; 165} 166 167void BrowserActionButton::ButtonPressed(views::Button* sender, 168 const ui::Event& event) { 169 delegate_->OnBrowserActionExecuted(this); 170} 171 172void BrowserActionButton::ShowContextMenuForView( 173 View* source, 174 const gfx::Point& point, 175 ui::MenuSourceType source_type) { 176 if (!extension()->ShowConfigureContextMenus()) 177 return; 178 179 SetButtonPushed(); 180 181 // Reconstructs the menu every time because the menu's contents are dynamic. 182 scoped_refptr<ExtensionContextMenuModel> context_menu_contents_( 183 new ExtensionContextMenuModel(extension(), browser_, delegate_)); 184 menu_runner_.reset(new views::MenuRunner(context_menu_contents_.get())); 185 186 context_menu_ = menu_runner_->GetMenu(); 187 gfx::Point screen_loc; 188 views::View::ConvertPointToScreen(this, &screen_loc); 189 if (menu_runner_->RunMenuAt(GetWidget(), NULL, gfx::Rect(screen_loc, size()), 190 views::MenuItemView::TOPLEFT, source_type, 191 views::MenuRunner::HAS_MNEMONICS | views::MenuRunner::CONTEXT_MENU) == 192 views::MenuRunner::MENU_DELETED) { 193 return; 194 } 195 196 menu_runner_.reset(); 197 SetButtonNotPushed(); 198 context_menu_ = NULL; 199} 200 201void BrowserActionButton::UpdateState() { 202 int tab_id = delegate_->GetCurrentTabId(); 203 if (tab_id < 0) 204 return; 205 206 SetShowMultipleIconStates(delegate_->NeedToShowMultipleIconStates()); 207 208 if (!IsEnabled(tab_id)) { 209 SetState(views::CustomButton::STATE_DISABLED); 210 } else { 211 SetState(menu_visible_ ? 212 views::CustomButton::STATE_PRESSED : 213 views::CustomButton::STATE_NORMAL); 214 } 215 216 gfx::ImageSkia icon = *icon_factory_.GetIcon(tab_id).ToImageSkia(); 217 218 if (!icon.isNull()) { 219 if (!browser_action()->GetIsVisible(tab_id)) 220 icon = gfx::ImageSkiaOperations::CreateTransparentImage(icon, .25); 221 222 ThemeService* theme = 223 ThemeServiceFactory::GetForProfile(browser_->profile()); 224 225 gfx::ImageSkia bg = *theme->GetImageSkiaNamed(IDR_BROWSER_ACTION); 226 SetIcon(gfx::ImageSkiaOperations::CreateSuperimposedImage(bg, icon)); 227 228 gfx::ImageSkia bg_h = *theme->GetImageSkiaNamed(IDR_BROWSER_ACTION_H); 229 SetHoverIcon(gfx::ImageSkiaOperations::CreateSuperimposedImage(bg_h, icon)); 230 231 gfx::ImageSkia bg_p = *theme->GetImageSkiaNamed(IDR_BROWSER_ACTION_P); 232 SetPushedIcon( 233 gfx::ImageSkiaOperations::CreateSuperimposedImage(bg_p, icon)); 234 } 235 236 // If the browser action name is empty, show the extension name instead. 237 std::string title = browser_action()->GetTitle(tab_id); 238 base::string16 name = 239 base::UTF8ToUTF16(title.empty() ? extension()->name() : title); 240 SetTooltipText(delegate_->NeedToShowTooltip() ? name : base::string16()); 241 SetAccessibleName(name); 242 243 parent()->SchedulePaint(); 244} 245 246bool BrowserActionButton::IsPopup() { 247 int tab_id = delegate_->GetCurrentTabId(); 248 return (tab_id < 0) ? false : browser_action_->HasPopup(tab_id); 249} 250 251GURL BrowserActionButton::GetPopupUrl() { 252 int tab_id = delegate_->GetCurrentTabId(); 253 return (tab_id < 0) ? GURL() : browser_action_->GetPopupUrl(tab_id); 254} 255 256void BrowserActionButton::Observe(int type, 257 const content::NotificationSource& source, 258 const content::NotificationDetails& details) { 259 switch (type) { 260 case chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED: 261 UpdateState(); 262 // The browser action may have become visible/hidden so we need to make 263 // sure the state gets updated. 264 delegate_->OnBrowserActionVisibilityChanged(); 265 break; 266 case chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED: 267 case chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED: { 268 std::pair<const std::string, const std::string>* payload = 269 content::Details<std::pair<const std::string, const std::string> >( 270 details).ptr(); 271 if (extension_->id() == payload->first && 272 payload->second == 273 extensions::manifest_values::kBrowserActionCommandEvent) { 274 if (type == chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED) 275 MaybeRegisterExtensionCommand(); 276 else 277 MaybeUnregisterExtensionCommand(true); 278 } 279 break; 280 } 281 case chrome::NOTIFICATION_BROWSER_THEME_CHANGED: 282 UpdateState(); 283 break; 284 default: 285 NOTREACHED(); 286 break; 287 } 288} 289 290void BrowserActionButton::OnIconUpdated() { 291 UpdateState(); 292 if (icon_observer_) 293 icon_observer_->OnIconUpdated(GetIconWithBadge()); 294} 295 296bool BrowserActionButton::Activate() { 297 if (!IsPopup()) 298 return true; 299 300 delegate_->OnBrowserActionExecuted(this); 301 302 // TODO(erikkay): Run a nested modal loop while the mouse is down to 303 // enable menu-like drag-select behavior. 304 305 // The return value of this method is returned via OnMousePressed. 306 // We need to return false here since we're handing off focus to another 307 // widget/view, and true will grab it right back and try to send events 308 // to us. 309 return false; 310} 311 312bool BrowserActionButton::OnMousePressed(const ui::MouseEvent& event) { 313 if (!event.IsRightMouseButton()) { 314 return IsPopup() ? MenuButton::OnMousePressed(event) : 315 TextButton::OnMousePressed(event); 316 } 317 318 if (!views::View::ShouldShowContextMenuOnMousePress()) { 319 // See comments in MenuButton::Activate() as to why this is needed. 320 SetMouseHandler(NULL); 321 322 ShowContextMenu(gfx::Point(), ui::MENU_SOURCE_MOUSE); 323 } 324 return false; 325} 326 327void BrowserActionButton::OnMouseReleased(const ui::MouseEvent& event) { 328 if (IsPopup() || context_menu_) { 329 // TODO(erikkay) this never actually gets called (probably because of the 330 // loss of focus). 331 MenuButton::OnMouseReleased(event); 332 } else { 333 TextButton::OnMouseReleased(event); 334 } 335} 336 337void BrowserActionButton::OnMouseExited(const ui::MouseEvent& event) { 338 if (IsPopup() || context_menu_) 339 MenuButton::OnMouseExited(event); 340 else 341 TextButton::OnMouseExited(event); 342} 343 344bool BrowserActionButton::OnKeyReleased(const ui::KeyEvent& event) { 345 return IsPopup() ? MenuButton::OnKeyReleased(event) : 346 TextButton::OnKeyReleased(event); 347} 348 349void BrowserActionButton::OnGestureEvent(ui::GestureEvent* event) { 350 if (IsPopup()) 351 MenuButton::OnGestureEvent(event); 352 else 353 TextButton::OnGestureEvent(event); 354} 355 356bool BrowserActionButton::AcceleratorPressed( 357 const ui::Accelerator& accelerator) { 358 delegate_->OnBrowserActionExecuted(this); 359 return true; 360} 361 362void BrowserActionButton::SetButtonPushed() { 363 SetState(views::CustomButton::STATE_PRESSED); 364 menu_visible_ = true; 365} 366 367void BrowserActionButton::SetButtonNotPushed() { 368 SetState(views::CustomButton::STATE_NORMAL); 369 menu_visible_ = false; 370} 371 372bool BrowserActionButton::IsEnabled(int tab_id) const { 373 return browser_action_->GetIsVisible(tab_id); 374} 375 376gfx::ImageSkia BrowserActionButton::GetIconWithBadge() { 377 int tab_id = delegate_->GetCurrentTabId(); 378 gfx::Size spacing(0, ToolbarView::kVertSpacing); 379 gfx::ImageSkia icon = *icon_factory_.GetIcon(tab_id).ToImageSkia(); 380 if (!IsEnabled(tab_id)) 381 icon = gfx::ImageSkiaOperations::CreateTransparentImage(icon, .25); 382 return browser_action_->GetIconWithBadge(icon, tab_id, spacing); 383} 384 385gfx::ImageSkia BrowserActionButton::GetIconForTest() { 386 return icon(); 387} 388 389BrowserActionButton::~BrowserActionButton() { 390} 391 392void BrowserActionButton::MaybeRegisterExtensionCommand() { 393 extensions::CommandService* command_service = 394 extensions::CommandService::Get(browser_->profile()); 395 extensions::Command browser_action_command; 396 if (command_service->GetBrowserActionCommand( 397 extension_->id(), 398 extensions::CommandService::ACTIVE_ONLY, 399 &browser_action_command, 400 NULL)) { 401 keybinding_.reset(new ui::Accelerator( 402 browser_action_command.accelerator())); 403 GetFocusManager()->RegisterAccelerator( 404 *keybinding_.get(), ui::AcceleratorManager::kHighPriority, this); 405 } 406} 407 408void BrowserActionButton::MaybeUnregisterExtensionCommand(bool only_if_active) { 409 if (!keybinding_.get() || !GetFocusManager()) 410 return; 411 412 extensions::CommandService* command_service = 413 extensions::CommandService::Get(browser_->profile()); 414 415 extensions::Command browser_action_command; 416 if (!only_if_active || !command_service->GetBrowserActionCommand( 417 extension_->id(), 418 extensions::CommandService::ACTIVE_ONLY, 419 &browser_action_command, 420 NULL)) { 421 GetFocusManager()->UnregisterAccelerator(*keybinding_.get(), this); 422 keybinding_.reset(NULL); 423 } 424} 425