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" | |
12 #include "chrome/browser/extensions/extension_service.h" | |
13 #include "chrome/browser/extensions/extension_system.h" | |
9 #include "chrome/browser/extensions/extension_toolbar_model.h" | 14 #include "chrome/browser/extensions/extension_toolbar_model.h" |
15 #include "chrome/browser/profiles/profile.h" | |
10 #include "chrome/browser/ui/bookmarks/bookmark_tab_helper.h" | 16 #include "chrome/browser/ui/bookmarks/bookmark_tab_helper.h" |
17 #include "chrome/browser/ui/browser.h" | |
18 #include "chrome/browser/ui/browser_commands.h" | |
11 #include "chrome/browser/ui/browser_tabstrip.h" | 19 #include "chrome/browser/ui/browser_tabstrip.h" |
12 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 20 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
13 #include "grit/generated_resources.h" | 21 #include "grit/generated_resources.h" |
14 #include "grit/theme_resources.h" | 22 #include "grit/theme_resources.h" |
15 #include "ui/base/resource/resource_bundle.h" | 23 #include "ui/base/resource/resource_bundle.h" |
16 | 24 |
17 namespace { | 25 namespace { |
18 | 26 |
19 // Extensions get command IDs that are beyond the maximal valid extension ID | 27 // 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 | 28 // (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 | 29 // the menu. For more details see: chrome/app/chrome_command_ids.h |
22 // | 30 // |
23 const int kFirstExtensionCommandId = 0xE000; | 31 const int kFirstExtensionCommandId = 0xE000; |
24 | 32 |
25 } // namespace | 33 } // namespace |
26 | 34 |
27 //////////////////////////////////////////////////////////////////////////////// | 35 //////////////////////////////////////////////////////////////////////////////// |
28 // ActionBoxMenuModel | 36 // ActionBoxMenuModel |
29 | 37 |
30 ActionBoxMenuModel::ActionBoxMenuModel(Browser* browser, | 38 ActionBoxMenuModel::ActionBoxMenuModel(Browser* browser) |
31 ExtensionService* extension_service) | |
32 : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)), | 39 : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)), |
33 browser_(browser), | 40 browser_(browser) { |
34 extension_service_(extension_service) { | |
35 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 41 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
36 InsertItemWithStringIdAt(0, IDC_CHROME_TO_MOBILE_PAGE, | 42 if (ChromeToMobileService::IsChromeToMobileEnabled() && |
msw
2012/08/30 22:21:09
nit: You can nix IsChromeToMobileEnabled, it's 1:1
Cait (Slow)
2012/08/31 15:38:12
Done.
| |
37 IDS_CHROME_TO_MOBILE_BUBBLE_TOOLTIP); | 43 ChromeToMobileServiceFactory::GetForProfile(browser_->profile())-> |
38 SetIcon(0, rb.GetNativeImageNamed(IDR_MOBILE)); | 44 HasMobiles()) { |
39 | 45 AddItemWithStringId(IDC_CHROME_TO_MOBILE_PAGE, |
40 TabContents* current_tab_contents = chrome::GetActiveTabContents(browser); | 46 IDS_CHROME_TO_MOBILE_BUBBLE_TOOLTIP); |
47 SetIcon(GetIndexOfCommandId(IDC_CHROME_TO_MOBILE_PAGE), | |
48 rb.GetNativeImageNamed(IDR_MOBILE)); | |
49 } | |
50 TabContents* current_tab_contents = chrome::GetActiveTabContents(browser_); | |
41 bool starred = current_tab_contents->bookmark_tab_helper()->is_starred(); | 51 bool starred = current_tab_contents->bookmark_tab_helper()->is_starred(); |
42 InsertItemWithStringIdAt(1, IDC_BOOKMARK_PAGE, | 52 AddItemWithStringId(IDC_BOOKMARK_PAGE, |
43 starred ? IDS_TOOLTIP_STARRED : IDS_TOOLTIP_STAR); | 53 starred ? IDS_TOOLTIP_STARRED : IDS_TOOLTIP_STAR); |
44 SetIcon(1, rb.GetNativeImageNamed(starred ? IDR_STAR_LIT : IDR_STAR)); | 54 SetIcon(GetIndexOfCommandId(IDC_BOOKMARK_PAGE), |
55 rb.GetNativeImageNamed(starred ? IDR_STAR_LIT : IDR_STAR)); | |
45 | 56 |
46 // Adds extensions to the model. | 57 // Adds extensions to the model. |
47 int command_id = kFirstExtensionCommandId; | 58 int command_id = kFirstExtensionCommandId; |
48 const extensions::ExtensionList& action_box_items = action_box_menu_items(); | 59 const extensions::ExtensionList& action_box_items = GetActionBoxMenuItems(); |
49 if (!action_box_items.empty()) { | 60 if (!action_box_items.empty()) { |
50 AddSeparator(ui::NORMAL_SEPARATOR); | 61 AddSeparator(ui::NORMAL_SEPARATOR); |
51 for (size_t i = 0; i < action_box_items.size(); ++i) { | 62 for (size_t i = 0; i < action_box_items.size(); ++i) { |
52 const extensions::Extension* extension = action_box_items[i]; | 63 const extensions::Extension* extension = action_box_items[i]; |
53 AddItem(command_id, UTF8ToUTF16(extension->name())); | 64 AddItem(command_id, UTF8ToUTF16(extension->name())); |
54 id_to_extension_id_map_[command_id++] = extension->id(); | 65 id_to_extension_id_map_[command_id++] = extension->id(); |
55 } | 66 } |
56 } | 67 } |
57 } | 68 } |
58 | 69 |
59 ActionBoxMenuModel::~ActionBoxMenuModel() { | 70 ActionBoxMenuModel::~ActionBoxMenuModel() { |
60 // Ensures parent destructor does not use a partially destroyed delegate. | 71 // Ensures parent destructor does not use a partially destroyed delegate. |
61 set_delegate(NULL); | 72 set_delegate(NULL); |
62 } | 73 } |
63 | 74 |
64 bool ActionBoxMenuModel::IsItemExtension(int index) { | 75 bool ActionBoxMenuModel::IsItemExtension(int index) { |
65 return GetCommandIdAt(index) >= kFirstExtensionCommandId; | 76 return GetCommandIdAt(index) >= kFirstExtensionCommandId; |
66 } | 77 } |
67 | 78 |
68 const extensions::Extension* ActionBoxMenuModel::GetExtensionAt(int index) { | 79 const extensions::Extension* ActionBoxMenuModel::GetExtensionAt(int index) { |
69 if (!IsItemExtension(index)) | 80 if (!IsItemExtension(index)) |
70 return NULL; | 81 return NULL; |
71 | 82 |
72 // ExtensionList is mutable, so need to get up-to-date extension. | 83 // ExtensionList is mutable, so need to get up-to-date extension. |
73 int command_id = GetCommandIdAt(index); | 84 int command_id = GetCommandIdAt(index); |
74 IdToEntensionIdMap::const_iterator it = | 85 IdToEntensionIdMap::const_iterator it = |
75 id_to_extension_id_map_.find(command_id); | 86 id_to_extension_id_map_.find(command_id); |
76 if (it == id_to_extension_id_map_.end()) | 87 if (it == id_to_extension_id_map_.end()) |
77 return NULL; | 88 return NULL; |
78 | 89 ExtensionService* extension_service = |
79 return extension_service_->GetExtensionById(it->second, false); | 90 extensions::ExtensionSystem::Get(browser_->profile())-> |
91 extension_service(); | |
92 return extension_service->GetExtensionById(it->second, false); | |
80 } | 93 } |
81 | 94 |
82 bool ActionBoxMenuModel::IsCommandIdChecked(int command_id) const { | 95 bool ActionBoxMenuModel::IsCommandIdChecked(int command_id) const { |
83 return false; | 96 return false; |
84 } | 97 } |
85 | 98 |
86 bool ActionBoxMenuModel::IsCommandIdEnabled(int command_id) const { | 99 bool ActionBoxMenuModel::IsCommandIdEnabled(int command_id) const { |
87 return true; | 100 return true; |
88 } | 101 } |
89 | 102 |
90 bool ActionBoxMenuModel::GetAcceleratorForCommandId( | 103 bool ActionBoxMenuModel::GetAcceleratorForCommandId( |
91 int command_id, | 104 int command_id, |
92 ui::Accelerator* accelerator) { | 105 ui::Accelerator* accelerator) { |
93 return false; | 106 return false; |
94 } | 107 } |
95 | 108 |
96 void ActionBoxMenuModel::ExecuteCommand(int command_id) { | 109 void ActionBoxMenuModel::ExecuteCommand(int command_id) { |
97 if (command_id < kFirstExtensionCommandId) | 110 if (command_id < kFirstExtensionCommandId) |
98 chrome::ExecuteCommand(browser_, command_id); | 111 chrome::ExecuteCommand(browser_, command_id); |
99 } | 112 } |
100 | 113 |
114 const extensions::ExtensionList& ActionBoxMenuModel::GetActionBoxMenuItems() { | |
115 ExtensionService* extension_service = | |
116 extensions::ExtensionSystem::Get(browser_->profile())-> | |
117 extension_service(); | |
118 return extension_service->toolbar_model()->action_box_menu_items(); | |
119 } | |
120 | |
101 void ActionBoxMenuModel::Observe(int type, | 121 void ActionBoxMenuModel::Observe(int type, |
102 const content::NotificationSource& source, | 122 const content::NotificationSource& source, |
103 const content::NotificationDetails& details) { | 123 const content::NotificationDetails& details) { |
104 } | 124 } |
OLD | NEW |