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