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 (ChromeToMobileServiceFactory::GetForProfile(browser_->profile())-> |
37 IDS_CHROME_TO_MOBILE_BUBBLE_TOOLTIP); | 43 HasMobiles()) { |
msw
2012/08/31 17:15:36
nit: indent 4 more spaces.
| |
38 SetIcon(0, rb.GetNativeImageNamed(IDR_MOBILE)); | 44 AddItemWithStringId(IDC_CHROME_TO_MOBILE_PAGE, |
39 | 45 IDS_CHROME_TO_MOBILE_BUBBLE_TOOLTIP); |
40 TabContents* current_tab_contents = chrome::GetActiveTabContents(browser); | 46 SetIcon(GetIndexOfCommandId(IDC_CHROME_TO_MOBILE_PAGE), |
47 rb.GetNativeImageNamed(IDR_MOBILE)); | |
48 } | |
49 TabContents* current_tab_contents = chrome::GetActiveTabContents(browser_); | |
41 bool starred = current_tab_contents->bookmark_tab_helper()->is_starred(); | 50 bool starred = current_tab_contents->bookmark_tab_helper()->is_starred(); |
42 InsertItemWithStringIdAt(1, IDC_BOOKMARK_PAGE, | 51 AddItemWithStringId(IDC_BOOKMARK_PAGE, |
43 starred ? IDS_TOOLTIP_STARRED : IDS_TOOLTIP_STAR); | 52 starred ? IDS_TOOLTIP_STARRED : IDS_TOOLTIP_STAR); |
44 SetIcon(1, rb.GetNativeImageNamed(starred ? IDR_STAR_LIT : IDR_STAR)); | 53 SetIcon(GetIndexOfCommandId(IDC_BOOKMARK_PAGE), |
54 rb.GetNativeImageNamed(starred ? IDR_STAR_LIT : IDR_STAR)); | |
45 | 55 |
46 // Adds extensions to the model. | 56 // Adds extensions to the model. |
47 int command_id = kFirstExtensionCommandId; | 57 int command_id = kFirstExtensionCommandId; |
48 const extensions::ExtensionList& action_box_items = action_box_menu_items(); | 58 const extensions::ExtensionList& action_box_items = GetActionBoxMenuItems(); |
49 if (!action_box_items.empty()) { | 59 if (!action_box_items.empty()) { |
50 AddSeparator(ui::NORMAL_SEPARATOR); | 60 AddSeparator(ui::NORMAL_SEPARATOR); |
51 for (size_t i = 0; i < action_box_items.size(); ++i) { | 61 for (size_t i = 0; i < action_box_items.size(); ++i) { |
52 const extensions::Extension* extension = action_box_items[i]; | 62 const extensions::Extension* extension = action_box_items[i]; |
53 AddItem(command_id, UTF8ToUTF16(extension->name())); | 63 AddItem(command_id, UTF8ToUTF16(extension->name())); |
54 id_to_extension_id_map_[command_id++] = extension->id(); | 64 id_to_extension_id_map_[command_id++] = extension->id(); |
55 } | 65 } |
56 } | 66 } |
57 } | 67 } |
58 | 68 |
59 ActionBoxMenuModel::~ActionBoxMenuModel() { | 69 ActionBoxMenuModel::~ActionBoxMenuModel() { |
60 // Ensures parent destructor does not use a partially destroyed delegate. | 70 // Ensures parent destructor does not use a partially destroyed delegate. |
61 set_delegate(NULL); | 71 set_delegate(NULL); |
62 } | 72 } |
63 | 73 |
64 bool ActionBoxMenuModel::IsItemExtension(int index) { | 74 bool ActionBoxMenuModel::IsItemExtension(int index) { |
65 return GetCommandIdAt(index) >= kFirstExtensionCommandId; | 75 return GetCommandIdAt(index) >= kFirstExtensionCommandId; |
66 } | 76 } |
67 | 77 |
68 const extensions::Extension* ActionBoxMenuModel::GetExtensionAt(int index) { | 78 const extensions::Extension* ActionBoxMenuModel::GetExtensionAt(int index) { |
69 if (!IsItemExtension(index)) | 79 if (!IsItemExtension(index)) |
70 return NULL; | 80 return NULL; |
71 | 81 |
72 // ExtensionList is mutable, so need to get up-to-date extension. | 82 // ExtensionList is mutable, so need to get up-to-date extension. |
73 int command_id = GetCommandIdAt(index); | 83 int command_id = GetCommandIdAt(index); |
74 IdToEntensionIdMap::const_iterator it = | 84 IdToEntensionIdMap::const_iterator it = |
75 id_to_extension_id_map_.find(command_id); | 85 id_to_extension_id_map_.find(command_id); |
76 if (it == id_to_extension_id_map_.end()) | 86 if (it == id_to_extension_id_map_.end()) |
77 return NULL; | 87 return NULL; |
78 | 88 ExtensionService* extension_service = |
79 return extension_service_->GetExtensionById(it->second, false); | 89 extensions::ExtensionSystem::Get(browser_->profile())-> |
90 extension_service(); | |
91 return extension_service->GetExtensionById(it->second, false); | |
80 } | 92 } |
81 | 93 |
82 bool ActionBoxMenuModel::IsCommandIdChecked(int command_id) const { | 94 bool ActionBoxMenuModel::IsCommandIdChecked(int command_id) const { |
83 return false; | 95 return false; |
84 } | 96 } |
85 | 97 |
86 bool ActionBoxMenuModel::IsCommandIdEnabled(int command_id) const { | 98 bool ActionBoxMenuModel::IsCommandIdEnabled(int command_id) const { |
87 return true; | 99 return true; |
88 } | 100 } |
89 | 101 |
90 bool ActionBoxMenuModel::GetAcceleratorForCommandId( | 102 bool ActionBoxMenuModel::GetAcceleratorForCommandId( |
91 int command_id, | 103 int command_id, |
92 ui::Accelerator* accelerator) { | 104 ui::Accelerator* accelerator) { |
93 return false; | 105 return false; |
94 } | 106 } |
95 | 107 |
96 void ActionBoxMenuModel::ExecuteCommand(int command_id) { | 108 void ActionBoxMenuModel::ExecuteCommand(int command_id) { |
97 if (command_id < kFirstExtensionCommandId) | 109 if (command_id < kFirstExtensionCommandId) |
98 chrome::ExecuteCommand(browser_, command_id); | 110 chrome::ExecuteCommand(browser_, command_id); |
99 } | 111 } |
100 | 112 |
113 const extensions::ExtensionList& ActionBoxMenuModel::GetActionBoxMenuItems() { | |
114 ExtensionService* extension_service = | |
115 extensions::ExtensionSystem::Get(browser_->profile())-> | |
116 extension_service(); | |
117 return extension_service->toolbar_model()->action_box_menu_items(); | |
118 } | |
119 | |
101 void ActionBoxMenuModel::Observe(int type, | 120 void ActionBoxMenuModel::Observe(int type, |
102 const content::NotificationSource& source, | 121 const content::NotificationSource& source, |
103 const content::NotificationDetails& details) { | 122 const content::NotificationDetails& details) { |
104 } | 123 } |
OLD | NEW |