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/toolbar/action_box_menu_model.h" | 5 #include "chrome/browser/ui/toolbar/action_box_menu_model.h" |
6 | 6 |
7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
8 #include "chrome/app/chrome_command_ids.h" | 8 #include "chrome/app/chrome_command_ids.h" |
9 #include "chrome/browser/chrome_to_mobile_service.h" | |
10 #include "chrome/browser/chrome_to_mobile_service_factory.h" | |
11 #include "chrome/browser/command_updater.h" | |
9 #include "chrome/browser/extensions/extension_toolbar_model.h" | 12 #include "chrome/browser/extensions/extension_toolbar_model.h" |
13 #include "chrome/browser/profiles/profile.h" | |
10 #include "chrome/browser/ui/bookmarks/bookmark_tab_helper.h" | 14 #include "chrome/browser/ui/bookmarks/bookmark_tab_helper.h" |
15 #include "chrome/browser/ui/browser.h" | |
11 #include "chrome/browser/ui/browser_tabstrip.h" | 16 #include "chrome/browser/ui/browser_tabstrip.h" |
12 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 17 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
13 #include "grit/generated_resources.h" | 18 #include "grit/generated_resources.h" |
14 #include "grit/theme_resources.h" | 19 #include "grit/theme_resources.h" |
15 #include "ui/base/resource/resource_bundle.h" | 20 #include "ui/base/resource/resource_bundle.h" |
16 | 21 |
17 namespace { | 22 namespace { |
18 | 23 |
19 // Extensions get command IDs that are beyond the maximal valid extension ID | 24 // Extensions get command IDs that are beyond the maximal valid extension ID |
20 // (0xDFFF) so that they are not confused with actual commands that appear in | 25 // (0xDFFF) so that they are not confused with actual commands that appear in |
21 // the menu. For more details see: chrome/app/chrome_command_ids.h | 26 // the menu. For more details see: chrome/app/chrome_command_ids.h |
22 // | 27 // |
23 const int kFirstExtensionCommandId = 0xE000; | 28 const int kFirstExtensionCommandId = 0xE000; |
24 | 29 |
25 } // namespace | 30 } // namespace |
26 | 31 |
27 //////////////////////////////////////////////////////////////////////////////// | 32 //////////////////////////////////////////////////////////////////////////////// |
28 // ActionBoxMenuModel | 33 // ActionBoxMenuModel |
29 | 34 |
30 ActionBoxMenuModel::ActionBoxMenuModel(Browser* browser, | 35 ActionBoxMenuModel::ActionBoxMenuModel(Browser* browser, |
31 ExtensionService* extension_service) | 36 ExtensionService* extension_service, |
37 CommandUpdater* command_updater) | |
32 : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)), | 38 : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)), |
33 browser_(browser), | 39 browser_(browser), |
34 extension_service_(extension_service) { | 40 extension_service_(extension_service), |
35 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 41 command_updater_(command_updater) { |
36 InsertItemWithStringIdAt(0, IDC_CHROME_TO_MOBILE_PAGE, | 42 // Disable Chrome To Mobile for off-the-record and non-synced profiles, |
37 IDS_CHROME_TO_MOBILE_BUBBLE_TOOLTIP); | 43 // or if the feature is disabled by a command line flag or chrome://flags. |
38 SetIcon(0, rb.GetNativeImageNamed(IDR_MOBILE)); | 44 if (!browser_->profile()->IsOffTheRecord() && |
39 | 45 browser_->profile()->IsSyncAccessible() && |
msw
2012/08/29 19:27:04
nit: I think we should drop the IsSyncAccessible c
Cait (Slow)
2012/08/29 20:26:14
Done.
| |
40 TabContents* current_tab_contents = chrome::GetActiveTabContents(browser); | 46 ChromeToMobileService::IsChromeToMobileEnabled()) { |
41 bool starred = current_tab_contents->bookmark_tab_helper()->is_starred(); | 47 command_updater_->AddCommandObserver(IDC_CHROME_TO_MOBILE_PAGE, this); |
42 InsertItemWithStringIdAt(1, IDC_BOOKMARK_PAGE, | |
43 starred ? IDS_TOOLTIP_STARRED : IDS_TOOLTIP_STAR); | |
44 SetIcon(1, rb.GetNativeImageNamed(starred ? IDR_STAR_LIT : IDR_STAR)); | |
45 | |
46 // Adds extensions to the model. | |
47 int command_id = kFirstExtensionCommandId; | |
48 const extensions::ExtensionList& action_box_items = action_box_menu_items(); | |
49 if (!action_box_items.empty()) { | |
50 AddSeparator(ui::NORMAL_SEPARATOR); | |
51 for (size_t i = 0; i < action_box_items.size(); ++i) { | |
52 const extensions::Extension* extension = action_box_items[i]; | |
53 AddItem(command_id, UTF8ToUTF16(extension->name())); | |
54 id_to_extension_id_map_[command_id++] = extension->id(); | |
55 } | |
56 } | 48 } |
49 UpdateMenu(); | |
57 } | 50 } |
58 | 51 |
59 ActionBoxMenuModel::~ActionBoxMenuModel() { | 52 ActionBoxMenuModel::~ActionBoxMenuModel() { |
60 // Ensures parent destructor does not use a partially destroyed delegate. | 53 // Ensures parent destructor does not use a partially destroyed delegate. |
61 set_delegate(NULL); | 54 set_delegate(NULL); |
55 // Remove command observer. | |
56 command_updater_->RemoveCommandObserver(this); | |
msw
2012/08/29 19:27:04
Will this work if the ctor skips adding the observ
Cait (Slow)
2012/08/29 20:26:14
Yeah, I think it is just a no-op in that case.
| |
62 } | 57 } |
63 | 58 |
64 bool ActionBoxMenuModel::IsItemExtension(int index) { | 59 bool ActionBoxMenuModel::IsItemExtension(int index) { |
65 return GetCommandIdAt(index) >= kFirstExtensionCommandId; | 60 return GetCommandIdAt(index) >= kFirstExtensionCommandId; |
66 } | 61 } |
67 | 62 |
68 const extensions::Extension* ActionBoxMenuModel::GetExtensionAt(int index) { | 63 const extensions::Extension* ActionBoxMenuModel::GetExtensionAt(int index) { |
69 if (!IsItemExtension(index)) | 64 if (!IsItemExtension(index)) |
70 return NULL; | 65 return NULL; |
71 | 66 |
(...skipping 23 matching lines...) Expand all Loading... | |
95 | 90 |
96 void ActionBoxMenuModel::ExecuteCommand(int command_id) { | 91 void ActionBoxMenuModel::ExecuteCommand(int command_id) { |
97 if (command_id < kFirstExtensionCommandId) | 92 if (command_id < kFirstExtensionCommandId) |
98 chrome::ExecuteCommand(browser_, command_id); | 93 chrome::ExecuteCommand(browser_, command_id); |
99 } | 94 } |
100 | 95 |
101 void ActionBoxMenuModel::Observe(int type, | 96 void ActionBoxMenuModel::Observe(int type, |
102 const content::NotificationSource& source, | 97 const content::NotificationSource& source, |
103 const content::NotificationDetails& details) { | 98 const content::NotificationDetails& details) { |
104 } | 99 } |
100 | |
101 void ActionBoxMenuModel::EnabledStateChangedForCommand(int id, bool enabled) { | |
102 DCHECK_EQ(id, IDC_CHROME_TO_MOBILE_PAGE); | |
103 UpdateMenu(); | |
104 command_updater_->UpdateCommandEnabled(IDC_CHROME_TO_MOBILE_PAGE, enabled); | |
msw
2012/08/29 19:27:04
This seems unnecessary.
Cait (Slow)
2012/08/29 20:26:14
Done.
| |
105 } | |
106 | |
107 void ActionBoxMenuModel::UpdateMenu() { | |
108 int next_item_index = 0; | |
109 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
110 | |
111 bool c2m_enabled = ChromeToMobileService::IsChromeToMobileEnabled() && | |
msw
2012/08/29 19:27:04
nit: nix |c2m_enabled|, inline conditionals into t
Cait (Slow)
2012/08/29 20:26:14
Done.
| |
112 ChromeToMobileServiceFactory::GetForProfile(browser_->profile())-> | |
113 HasMobiles(); | |
114 if (c2m_enabled) { | |
115 InsertItemWithStringIdAt(next_item_index, IDC_CHROME_TO_MOBILE_PAGE, | |
msw
2012/08/29 19:27:04
nit: perhaps use AddItem instead and nix next_item
Cait (Slow)
2012/08/29 20:26:14
Done.
| |
116 IDS_CHROME_TO_MOBILE_BUBBLE_TOOLTIP); | |
msw
2012/08/29 19:27:04
nit: indent
Cait (Slow)
2012/08/29 20:26:14
Done.
| |
117 SetIcon(next_item_index, rb.GetNativeImageNamed(IDR_MOBILE)); | |
118 next_item_index++; | |
119 } | |
120 TabContents* current_tab_contents = chrome::GetActiveTabContents(browser_); | |
121 bool starred = current_tab_contents->bookmark_tab_helper()->is_starred(); | |
122 InsertItemWithStringIdAt(next_item_index, IDC_BOOKMARK_PAGE, | |
123 starred ? IDS_TOOLTIP_STARRED : IDS_TOOLTIP_STAR); | |
124 SetIcon(next_item_index, | |
125 rb.GetNativeImageNamed(starred ? IDR_STAR_LIT : IDR_STAR)); | |
126 | |
127 // Adds extensions to the model. | |
128 int command_id = kFirstExtensionCommandId; | |
129 const extensions::ExtensionList& action_box_items = action_box_menu_items(); | |
130 if (!action_box_items.empty()) { | |
131 AddSeparator(ui::NORMAL_SEPARATOR); | |
132 for (size_t i = 0; i < action_box_items.size(); ++i) { | |
133 const extensions::Extension* extension = action_box_items[i]; | |
134 AddItem(command_id, UTF8ToUTF16(extension->name())); | |
135 id_to_extension_id_map_[command_id++] = extension->id(); | |
136 } | |
137 } | |
138 } | |
OLD | NEW |