OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/toolbar/browser_action_view.h" | 5 #include "chrome/browser/ui/views/toolbar/browser_action_view.h" |
6 | 6 |
7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "chrome/browser/chrome_notification_types.h" | 8 #include "chrome/browser/chrome_notification_types.h" |
9 #include "chrome/browser/extensions/api/commands/command_service.h" | 9 #include "chrome/browser/extensions/api/commands/command_service.h" |
10 #include "chrome/browser/extensions/extension_action.h" | 10 #include "chrome/browser/extensions/extension_action.h" |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 Browser* browser, | 103 Browser* browser, |
104 BrowserActionView::Delegate* delegate) | 104 BrowserActionView::Delegate* delegate) |
105 : MenuButton(this, base::string16(), NULL, false), | 105 : MenuButton(this, base::string16(), NULL, false), |
106 browser_(browser), | 106 browser_(browser), |
107 browser_action_( | 107 browser_action_( |
108 extensions::ExtensionActionManager::Get(browser->profile())-> | 108 extensions::ExtensionActionManager::Get(browser->profile())-> |
109 GetBrowserAction(*extension)), | 109 GetBrowserAction(*extension)), |
110 extension_(extension), | 110 extension_(extension), |
111 icon_factory_(browser->profile(), extension, browser_action_, this), | 111 icon_factory_(browser->profile(), extension, browser_action_, this), |
112 delegate_(delegate), | 112 delegate_(delegate), |
113 context_menu_(NULL), | |
114 called_registered_extension_command_(false), | 113 called_registered_extension_command_(false), |
115 icon_observer_(NULL) { | 114 icon_observer_(NULL) { |
116 SetHorizontalAlignment(gfx::ALIGN_CENTER); | 115 SetHorizontalAlignment(gfx::ALIGN_CENTER); |
117 set_context_menu_controller(this); | 116 set_context_menu_controller(this); |
118 | 117 |
119 // No UpdateState() here because View hierarchy not setup yet. Our parent | 118 // No UpdateState() here because View hierarchy not setup yet. Our parent |
120 // should call UpdateState() after creation. | 119 // should call UpdateState() after creation. |
121 | 120 |
122 content::NotificationSource notification_source = | 121 content::NotificationSource notification_source = |
123 content::Source<Profile>(browser_->profile()->GetOriginalProfile()); | 122 content::Source<Profile>(browser_->profile()->GetOriginalProfile()); |
124 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED, | 123 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED, |
125 content::Source<ExtensionAction>(browser_action_)); | 124 content::Source<ExtensionAction>(browser_action_)); |
126 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED, | 125 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED, |
127 notification_source); | 126 notification_source); |
128 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED, | 127 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED, |
129 notification_source); | 128 notification_source); |
130 | 129 |
131 // We also listen for browser theme changes on linux because a switch from or | 130 // We also listen for browser theme changes on linux because a switch from or |
132 // to GTK requires that we regrab our browser action images. | 131 // to GTK requires that we regrab our browser action images. |
133 registrar_.Add( | 132 registrar_.Add( |
134 this, | 133 this, |
135 chrome::NOTIFICATION_BROWSER_THEME_CHANGED, | 134 chrome::NOTIFICATION_BROWSER_THEME_CHANGED, |
136 content::Source<ThemeService>( | 135 content::Source<ThemeService>( |
137 ThemeServiceFactory::GetForProfile(browser->profile()))); | 136 ThemeServiceFactory::GetForProfile(browser->profile()))); |
138 } | 137 } |
139 | 138 |
140 void BrowserActionButton::Destroy() { | 139 void BrowserActionButton::Destroy() { |
141 MaybeUnregisterExtensionCommand(false); | 140 MaybeUnregisterExtensionCommand(false); |
142 | 141 |
143 if (context_menu_) { | 142 if (menu_runner_) { |
144 context_menu_->Cancel(); | 143 menu_runner_->Cancel(); |
145 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); | 144 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); |
146 } else { | 145 } else { |
147 delete this; | 146 delete this; |
148 } | 147 } |
149 } | 148 } |
150 | 149 |
151 void BrowserActionButton::ViewHierarchyChanged( | 150 void BrowserActionButton::ViewHierarchyChanged( |
152 const ViewHierarchyChangedDetails& details) { | 151 const ViewHierarchyChangedDetails& details) { |
153 if (details.is_add && !called_registered_extension_command_ && | 152 if (details.is_add && !called_registered_extension_command_ && |
154 GetFocusManager()) { | 153 GetFocusManager()) { |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 // already visible menu, in this case the Chrome menu. | 199 // already visible menu, in this case the Chrome menu. |
201 parent = BrowserView::GetBrowserViewForBrowser(browser_)->toolbar() | 200 parent = BrowserView::GetBrowserViewForBrowser(browser_)->toolbar() |
202 ->app_menu() | 201 ->app_menu() |
203 ->GetWidget(); | 202 ->GetWidget(); |
204 } else { | 203 } else { |
205 parent = GetWidget(); | 204 parent = GetWidget(); |
206 } | 205 } |
207 | 206 |
208 menu_runner_.reset( | 207 menu_runner_.reset( |
209 new views::MenuRunner(context_menu_contents.get(), run_types)); | 208 new views::MenuRunner(context_menu_contents.get(), run_types)); |
210 context_menu_ = menu_runner_->GetMenu(); | |
211 | 209 |
212 if (menu_runner_->RunMenuAt(parent, | 210 if (menu_runner_->RunMenuAt(parent, |
213 NULL, | 211 NULL, |
214 gfx::Rect(screen_loc, size()), | 212 gfx::Rect(screen_loc, size()), |
215 views::MENU_ANCHOR_TOPLEFT, | 213 views::MENU_ANCHOR_TOPLEFT, |
216 source_type) == | 214 source_type) == |
217 views::MenuRunner::MENU_DELETED) { | 215 views::MenuRunner::MENU_DELETED) { |
218 return; | 216 return; |
219 } | 217 } |
220 | 218 |
221 menu_runner_.reset(); | 219 menu_runner_.reset(); |
222 SetButtonNotPushed(); | 220 SetButtonNotPushed(); |
223 context_menu_ = NULL; | |
224 } | 221 } |
225 | 222 |
226 void BrowserActionButton::UpdateState() { | 223 void BrowserActionButton::UpdateState() { |
227 int tab_id = delegate_->GetCurrentTabId(); | 224 int tab_id = delegate_->GetCurrentTabId(); |
228 if (tab_id < 0) | 225 if (tab_id < 0) |
229 return; | 226 return; |
230 | 227 |
231 if (!IsEnabled(tab_id)) { | 228 if (!IsEnabled(tab_id)) { |
232 SetState(views::CustomButton::STATE_DISABLED); | 229 SetState(views::CustomButton::STATE_DISABLED); |
233 } else { | 230 } else { |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
335 if (!views::View::ShouldShowContextMenuOnMousePress()) { | 332 if (!views::View::ShouldShowContextMenuOnMousePress()) { |
336 // See comments in MenuButton::Activate() as to why this is needed. | 333 // See comments in MenuButton::Activate() as to why this is needed. |
337 SetMouseHandler(NULL); | 334 SetMouseHandler(NULL); |
338 | 335 |
339 ShowContextMenu(gfx::Point(), ui::MENU_SOURCE_MOUSE); | 336 ShowContextMenu(gfx::Point(), ui::MENU_SOURCE_MOUSE); |
340 } | 337 } |
341 return false; | 338 return false; |
342 } | 339 } |
343 | 340 |
344 void BrowserActionButton::OnMouseReleased(const ui::MouseEvent& event) { | 341 void BrowserActionButton::OnMouseReleased(const ui::MouseEvent& event) { |
345 if (IsPopup() || context_menu_) { | 342 if (IsPopup() || menu_runner_) { |
346 // TODO(erikkay) this never actually gets called (probably because of the | 343 // TODO(erikkay) this never actually gets called (probably because of the |
347 // loss of focus). | 344 // loss of focus). |
348 MenuButton::OnMouseReleased(event); | 345 MenuButton::OnMouseReleased(event); |
349 } else { | 346 } else { |
350 LabelButton::OnMouseReleased(event); | 347 LabelButton::OnMouseReleased(event); |
351 } | 348 } |
352 } | 349 } |
353 | 350 |
354 void BrowserActionButton::OnMouseExited(const ui::MouseEvent& event) { | 351 void BrowserActionButton::OnMouseExited(const ui::MouseEvent& event) { |
355 if (IsPopup() || context_menu_) | 352 if (IsPopup() || menu_runner_) |
356 MenuButton::OnMouseExited(event); | 353 MenuButton::OnMouseExited(event); |
357 else | 354 else |
358 LabelButton::OnMouseExited(event); | 355 LabelButton::OnMouseExited(event); |
359 } | 356 } |
360 | 357 |
361 bool BrowserActionButton::OnKeyReleased(const ui::KeyEvent& event) { | 358 bool BrowserActionButton::OnKeyReleased(const ui::KeyEvent& event) { |
362 return IsPopup() ? MenuButton::OnKeyReleased(event) : | 359 return IsPopup() ? MenuButton::OnKeyReleased(event) : |
363 LabelButton::OnKeyReleased(event); | 360 LabelButton::OnKeyReleased(event); |
364 } | 361 } |
365 | 362 |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
448 extensions::Command browser_action_command; | 445 extensions::Command browser_action_command; |
449 if (!only_if_active || !command_service->GetBrowserActionCommand( | 446 if (!only_if_active || !command_service->GetBrowserActionCommand( |
450 extension_->id(), | 447 extension_->id(), |
451 extensions::CommandService::ACTIVE_ONLY, | 448 extensions::CommandService::ACTIVE_ONLY, |
452 &browser_action_command, | 449 &browser_action_command, |
453 NULL)) { | 450 NULL)) { |
454 GetFocusManager()->UnregisterAccelerator(*keybinding_.get(), this); | 451 GetFocusManager()->UnregisterAccelerator(*keybinding_.get(), this); |
455 keybinding_.reset(NULL); | 452 keybinding_.reset(NULL); |
456 } | 453 } |
457 } | 454 } |
OLD | NEW |