Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(612)

Side by Side Diff: chrome/browser/ui/toolbar/action_box_menu_model.cc

Issue 10887029: Only show chrome2mobile in action box if it is enabled (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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() &&
msw 2012/08/29 22:05:42 I think this should just always observe; sorry for
Cait (Slow) 2012/08/29 23:03:47 Done.
39 45 ChromeToMobileService::IsChromeToMobileEnabled()) {
40 TabContents* current_tab_contents = chrome::GetActiveTabContents(browser); 46 command_updater_->AddCommandObserver(IDC_CHROME_TO_MOBILE_PAGE, this);
41 bool starred = current_tab_contents->bookmark_tab_helper()->is_starred();
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 } 47 }
48 UpdateMenu();
57 } 49 }
58 50
59 ActionBoxMenuModel::~ActionBoxMenuModel() { 51 ActionBoxMenuModel::~ActionBoxMenuModel() {
60 // Ensures parent destructor does not use a partially destroyed delegate. 52 // Ensures parent destructor does not use a partially destroyed delegate.
61 set_delegate(NULL); 53 set_delegate(NULL);
54 // Remove command observer.
55 command_updater_->RemoveCommandObserver(this);
62 } 56 }
63 57
64 bool ActionBoxMenuModel::IsItemExtension(int index) { 58 bool ActionBoxMenuModel::IsItemExtension(int index) {
65 return GetCommandIdAt(index) >= kFirstExtensionCommandId; 59 return GetCommandIdAt(index) >= kFirstExtensionCommandId;
66 } 60 }
67 61
68 const extensions::Extension* ActionBoxMenuModel::GetExtensionAt(int index) { 62 const extensions::Extension* ActionBoxMenuModel::GetExtensionAt(int index) {
69 if (!IsItemExtension(index)) 63 if (!IsItemExtension(index))
70 return NULL; 64 return NULL;
71 65
(...skipping 23 matching lines...) Expand all
95 89
96 void ActionBoxMenuModel::ExecuteCommand(int command_id) { 90 void ActionBoxMenuModel::ExecuteCommand(int command_id) {
97 if (command_id < kFirstExtensionCommandId) 91 if (command_id < kFirstExtensionCommandId)
98 chrome::ExecuteCommand(browser_, command_id); 92 chrome::ExecuteCommand(browser_, command_id);
99 } 93 }
100 94
101 void ActionBoxMenuModel::Observe(int type, 95 void ActionBoxMenuModel::Observe(int type,
102 const content::NotificationSource& source, 96 const content::NotificationSource& source,
103 const content::NotificationDetails& details) { 97 const content::NotificationDetails& details) {
104 } 98 }
99
100 void ActionBoxMenuModel::EnabledStateChangedForCommand(int id, bool enabled) {
101 DCHECK_EQ(id, IDC_CHROME_TO_MOBILE_PAGE);
102 UpdateMenu();
103 }
104
105 void ActionBoxMenuModel::UpdateMenu() {
106 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
107 if (ChromeToMobileService::IsChromeToMobileEnabled() &&
108 ChromeToMobileServiceFactory::GetForProfile(browser_->profile())->
109 HasMobiles()) {
110 AddItemWithStringId(IDC_CHROME_TO_MOBILE_PAGE,
111 IDS_CHROME_TO_MOBILE_BUBBLE_TOOLTIP);
112 SetIcon(GetIndexOfCommandId(IDC_CHROME_TO_MOBILE_PAGE),
msw 2012/08/29 22:05:42 Ah, sorry my advice necessitated this roundabout w
113 rb.GetNativeImageNamed(IDR_MOBILE));
114 }
115 TabContents* current_tab_contents = chrome::GetActiveTabContents(browser_);
116 bool starred = current_tab_contents->bookmark_tab_helper()->is_starred();
117 AddItemWithStringId(IDC_BOOKMARK_PAGE,
118 starred ? IDS_TOOLTIP_STARRED : IDS_TOOLTIP_STAR);
119 SetIcon(GetIndexOfCommandId(IDC_BOOKMARK_PAGE),
120 rb.GetNativeImageNamed(starred ? IDR_STAR_LIT : IDR_STAR));
121
122 // Adds extensions to the model.
123 int command_id = kFirstExtensionCommandId;
124 const extensions::ExtensionList& action_box_items = action_box_menu_items();
125 if (!action_box_items.empty()) {
126 AddSeparator(ui::NORMAL_SEPARATOR);
127 for (size_t i = 0; i < action_box_items.size(); ++i) {
128 const extensions::Extension* extension = action_box_items[i];
129 AddItem(command_id, UTF8ToUTF16(extension->name()));
130 id_to_extension_id_map_[command_id++] = extension->id();
131 }
132 }
133 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698