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