OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 #include "chrome/browser/ui/views/browser_action_view.h" | 5 #include "chrome/browser/ui/views/browser_action_view.h" |
6 | 6 |
7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
8 #include "chrome/browser/extensions/api/commands/command_service.h" | 8 #include "chrome/browser/extensions/api/commands/command_service.h" |
9 #include "chrome/browser/extensions/api/commands/command_service_factory.h" | 9 #include "chrome/browser/extensions/api/commands/command_service_factory.h" |
10 #include "chrome/browser/extensions/extension_context_menu_model.h" | 10 #include "chrome/browser/extensions/extension_context_menu_model.h" |
11 #include "chrome/browser/ui/browser.h" | 11 #include "chrome/browser/ui/browser.h" |
12 #include "chrome/browser/ui/views/browser_actions_container.h" | 12 #include "chrome/browser/ui/views/browser_actions_container.h" |
13 #include "chrome/browser/ui/views/toolbar_view.h" | 13 #include "chrome/browser/ui/views/toolbar_view.h" |
14 #include "chrome/common/chrome_notification_types.h" | 14 #include "chrome/common/chrome_notification_types.h" |
15 #include "chrome/common/extensions/extension.h" | 15 #include "chrome/common/extensions/extension.h" |
| 16 #include "chrome/common/extensions/extension_manifest_constants.h" |
16 #include "grit/generated_resources.h" | 17 #include "grit/generated_resources.h" |
17 #include "grit/theme_resources.h" | 18 #include "grit/theme_resources.h" |
18 #include "grit/theme_resources_standard.h" | 19 #include "grit/theme_resources_standard.h" |
19 #include "ui/base/accessibility/accessible_view_state.h" | 20 #include "ui/base/accessibility/accessible_view_state.h" |
20 #include "ui/base/l10n/l10n_util.h" | 21 #include "ui/base/l10n/l10n_util.h" |
21 #include "ui/base/resource/resource_bundle.h" | 22 #include "ui/base/resource/resource_bundle.h" |
22 #include "ui/gfx/canvas.h" | 23 #include "ui/gfx/canvas.h" |
23 #include "ui/views/controls/menu/menu_model_adapter.h" | 24 #include "ui/views/controls/menu/menu_model_adapter.h" |
24 #include "ui/views/controls/menu/menu_runner.h" | 25 #include "ui/views/controls/menu/menu_runner.h" |
25 | 26 |
(...skipping 12 matching lines...) Expand all Loading... |
38 panel_(panel), | 39 panel_(panel), |
39 context_menu_(NULL) { | 40 context_menu_(NULL) { |
40 set_border(NULL); | 41 set_border(NULL); |
41 set_alignment(TextButton::ALIGN_CENTER); | 42 set_alignment(TextButton::ALIGN_CENTER); |
42 | 43 |
43 // No UpdateState() here because View hierarchy not setup yet. Our parent | 44 // No UpdateState() here because View hierarchy not setup yet. Our parent |
44 // should call UpdateState() after creation. | 45 // should call UpdateState() after creation. |
45 | 46 |
46 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED, | 47 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED, |
47 content::Source<ExtensionAction>(browser_action_)); | 48 content::Source<ExtensionAction>(browser_action_)); |
| 49 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED, |
| 50 content::Source<Profile>( |
| 51 panel_->profile()->GetOriginalProfile())); |
| 52 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED, |
| 53 content::Source<Profile>( |
| 54 panel_->profile()->GetOriginalProfile())); |
48 } | 55 } |
49 | 56 |
50 void BrowserActionButton::Destroy() { | 57 void BrowserActionButton::Destroy() { |
51 if (keybinding_.get() && panel_->GetFocusManager()) | 58 MaybeUnregisterExtensionCommand(false); |
52 panel_->GetFocusManager()->UnregisterAccelerator(*keybinding_.get(), this); | |
53 | 59 |
54 if (context_menu_) { | 60 if (context_menu_) { |
55 context_menu_->Cancel(); | 61 context_menu_->Cancel(); |
56 MessageLoop::current()->DeleteSoon(FROM_HERE, this); | 62 MessageLoop::current()->DeleteSoon(FROM_HERE, this); |
57 } else { | 63 } else { |
58 delete this; | 64 delete this; |
59 } | 65 } |
60 } | 66 } |
61 | 67 |
62 void BrowserActionButton::ViewHierarchyChanged( | 68 void BrowserActionButton::ViewHierarchyChanged( |
(...skipping 11 matching lines...) Expand all Loading... |
74 gfx::Size(Extension::kBrowserActionIconMaxSize, | 80 gfx::Size(Extension::kBrowserActionIconMaxSize, |
75 Extension::kBrowserActionIconMaxSize), | 81 Extension::kBrowserActionIconMaxSize), |
76 ImageLoadingTracker::DONT_CACHE); | 82 ImageLoadingTracker::DONT_CACHE); |
77 } else { | 83 } else { |
78 // Set the icon to be the default extensions icon. | 84 // Set the icon to be the default extensions icon. |
79 default_icon_ = *ui::ResourceBundle::GetSharedInstance().GetImageNamed( | 85 default_icon_ = *ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
80 IDR_EXTENSIONS_FAVICON).ToSkBitmap(); | 86 IDR_EXTENSIONS_FAVICON).ToSkBitmap(); |
81 UpdateState(); | 87 UpdateState(); |
82 } | 88 } |
83 | 89 |
84 extensions::CommandService* command_service = | 90 MaybeRegisterExtensionCommand(); |
85 extensions::CommandServiceFactory::GetForProfile( | |
86 panel_->browser()->profile()); | |
87 extensions::Command browser_action_command; | |
88 if (command_service->GetBrowserActionCommand( | |
89 extension_->id(), | |
90 extensions::CommandService::ACTIVE_ONLY, | |
91 &browser_action_command, | |
92 NULL)) { | |
93 keybinding_.reset(new ui::Accelerator( | |
94 browser_action_command.accelerator())); | |
95 panel_->GetFocusManager()->RegisterAccelerator( | |
96 *keybinding_.get(), ui::AcceleratorManager::kHighPriority, this); | |
97 } | |
98 } | 91 } |
99 | 92 |
100 MenuButton::ViewHierarchyChanged(is_add, parent, child); | 93 MenuButton::ViewHierarchyChanged(is_add, parent, child); |
101 } | 94 } |
102 | 95 |
103 bool BrowserActionButton::CanHandleAccelerators() const { | 96 bool BrowserActionButton::CanHandleAccelerators() const { |
104 // View::CanHandleAccelerators() checks to see if the view is visible before | 97 // View::CanHandleAccelerators() checks to see if the view is visible before |
105 // allowing it to process accelerators. This is not appropriate for browser | 98 // allowing it to process accelerators. This is not appropriate for browser |
106 // actions buttons, which can be hidden inside the overflow area. | 99 // actions buttons, which can be hidden inside the overflow area. |
107 return true; | 100 return true; |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 } | 171 } |
179 | 172 |
180 GURL BrowserActionButton::GetPopupUrl() { | 173 GURL BrowserActionButton::GetPopupUrl() { |
181 int tab_id = panel_->GetCurrentTabId(); | 174 int tab_id = panel_->GetCurrentTabId(); |
182 return (tab_id < 0) ? GURL() : browser_action_->GetPopupUrl(tab_id); | 175 return (tab_id < 0) ? GURL() : browser_action_->GetPopupUrl(tab_id); |
183 } | 176 } |
184 | 177 |
185 void BrowserActionButton::Observe(int type, | 178 void BrowserActionButton::Observe(int type, |
186 const content::NotificationSource& source, | 179 const content::NotificationSource& source, |
187 const content::NotificationDetails& details) { | 180 const content::NotificationDetails& details) { |
188 DCHECK(type == chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED); | 181 switch (type) { |
189 UpdateState(); | 182 case chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED: |
190 // The browser action may have become visible/hidden so we need to make | 183 UpdateState(); |
191 // sure the state gets updated. | 184 // The browser action may have become visible/hidden so we need to make |
192 panel_->OnBrowserActionVisibilityChanged(); | 185 // sure the state gets updated. |
| 186 panel_->OnBrowserActionVisibilityChanged(); |
| 187 break; |
| 188 case chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED: |
| 189 case chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED: { |
| 190 std::pair<const std::string, const std::string>* payload = |
| 191 content::Details<std::pair<const std::string, const std::string> >( |
| 192 details).ptr(); |
| 193 if (extension_->id() == payload->first && |
| 194 payload->second == |
| 195 extension_manifest_values::kBrowserActionKeybindingEvent) { |
| 196 if (type == chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED) |
| 197 MaybeRegisterExtensionCommand(); |
| 198 else |
| 199 MaybeUnregisterExtensionCommand(true); |
| 200 } |
| 201 break; |
| 202 } |
| 203 default: |
| 204 NOTREACHED(); |
| 205 break; |
| 206 } |
193 } | 207 } |
194 | 208 |
195 bool BrowserActionButton::Activate() { | 209 bool BrowserActionButton::Activate() { |
196 if (!IsPopup()) | 210 if (!IsPopup()) |
197 return true; | 211 return true; |
198 | 212 |
199 panel_->OnBrowserActionExecuted(this); | 213 panel_->OnBrowserActionExecuted(this); |
200 | 214 |
201 // TODO(erikkay): Run a nested modal loop while the mouse is down to | 215 // TODO(erikkay): Run a nested modal loop while the mouse is down to |
202 // enable menu-like drag-select behavior. | 216 // enable menu-like drag-select behavior. |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 } | 294 } |
281 | 295 |
282 void BrowserActionButton::SetButtonNotPushed() { | 296 void BrowserActionButton::SetButtonNotPushed() { |
283 SetState(views::CustomButton::BS_NORMAL); | 297 SetState(views::CustomButton::BS_NORMAL); |
284 menu_visible_ = false; | 298 menu_visible_ = false; |
285 } | 299 } |
286 | 300 |
287 BrowserActionButton::~BrowserActionButton() { | 301 BrowserActionButton::~BrowserActionButton() { |
288 } | 302 } |
289 | 303 |
| 304 void BrowserActionButton::MaybeRegisterExtensionCommand() { |
| 305 extensions::CommandService* command_service = |
| 306 extensions::CommandServiceFactory::GetForProfile( |
| 307 panel_->browser()->profile()); |
| 308 extensions::Command browser_action_command; |
| 309 if (command_service->GetBrowserActionCommand( |
| 310 extension_->id(), |
| 311 extensions::CommandService::ACTIVE_ONLY, |
| 312 &browser_action_command, |
| 313 NULL)) { |
| 314 keybinding_.reset(new ui::Accelerator( |
| 315 browser_action_command.accelerator())); |
| 316 panel_->GetFocusManager()->RegisterAccelerator( |
| 317 *keybinding_.get(), ui::AcceleratorManager::kHighPriority, this); |
| 318 } |
| 319 } |
| 320 |
| 321 void BrowserActionButton::MaybeUnregisterExtensionCommand(bool only_if_active) { |
| 322 if (!keybinding_.get() || !panel_->GetFocusManager()) |
| 323 return; |
| 324 |
| 325 extensions::CommandService* command_service = |
| 326 extensions::CommandServiceFactory::GetForProfile( |
| 327 panel_->browser()->profile()); |
| 328 |
| 329 extensions::Command browser_action_command; |
| 330 if (!only_if_active || !command_service->GetBrowserActionCommand( |
| 331 extension_->id(), |
| 332 extensions::CommandService::ACTIVE_ONLY, |
| 333 &browser_action_command, |
| 334 NULL)) { |
| 335 panel_->GetFocusManager()->UnregisterAccelerator(*keybinding_.get(), this); |
| 336 } |
| 337 } |
| 338 |
| 339 |
290 //////////////////////////////////////////////////////////////////////////////// | 340 //////////////////////////////////////////////////////////////////////////////// |
291 // BrowserActionView | 341 // BrowserActionView |
292 | 342 |
293 BrowserActionView::BrowserActionView(const Extension* extension, | 343 BrowserActionView::BrowserActionView(const Extension* extension, |
294 BrowserActionsContainer* panel) | 344 BrowserActionsContainer* panel) |
295 : panel_(panel) { | 345 : panel_(panel) { |
296 button_ = new BrowserActionButton(extension, panel); | 346 button_ = new BrowserActionButton(extension, panel); |
297 button_->set_drag_controller(panel_); | 347 button_->set_drag_controller(panel_); |
298 AddChildView(button_); | 348 AddChildView(button_); |
299 button_->UpdateState(); | 349 button_->UpdateState(); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
339 state->role = ui::AccessibilityTypes::ROLE_GROUPING; | 389 state->role = ui::AccessibilityTypes::ROLE_GROUPING; |
340 } | 390 } |
341 | 391 |
342 void BrowserActionView::PaintChildren(gfx::Canvas* canvas) { | 392 void BrowserActionView::PaintChildren(gfx::Canvas* canvas) { |
343 View::PaintChildren(canvas); | 393 View::PaintChildren(canvas); |
344 ExtensionAction* action = button()->browser_action(); | 394 ExtensionAction* action = button()->browser_action(); |
345 int tab_id = panel_->GetCurrentTabId(); | 395 int tab_id = panel_->GetCurrentTabId(); |
346 if (tab_id >= 0) | 396 if (tab_id >= 0) |
347 action->PaintBadge(canvas, gfx::Rect(width(), height()), tab_id); | 397 action->PaintBadge(canvas, gfx::Rect(width(), height()), tab_id); |
348 } | 398 } |
OLD | NEW |