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

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

Issue 7328005: Multi-Profile: Move multi-profile commands from wrench menu to avatar menu (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: '' Created 9 years, 5 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/wrench_menu_model.h" 5 #include "chrome/browser/ui/toolbar/wrench_menu_model.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <cmath> 8 #include <cmath>
9 9
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/i18n/number_formatting.h" 11 #include "base/i18n/number_formatting.h"
12 #include "base/path_service.h"
13 #include "base/string_number_conversions.h" 12 #include "base/string_number_conversions.h"
14 #include "base/string_util.h" 13 #include "base/string_util.h"
15 #include "base/utf_string_conversions.h" 14 #include "base/utf_string_conversions.h"
16 #include "chrome/app/chrome_command_ids.h" 15 #include "chrome/app/chrome_command_ids.h"
17 #include "chrome/browser/browser_process.h" 16 #include "chrome/browser/browser_process.h"
18 #include "chrome/browser/defaults.h" 17 #include "chrome/browser/defaults.h"
19 #include "chrome/browser/prefs/pref_service.h" 18 #include "chrome/browser/prefs/pref_service.h"
20 #include "chrome/browser/profiles/profile.h" 19 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/browser/profiles/profile_info_cache.h"
22 #include "chrome/browser/profiles/profile_manager.h"
23 #include "chrome/browser/sync/profile_sync_service.h"
24 #include "chrome/browser/sync/sync_ui_util.h" 20 #include "chrome/browser/sync/sync_ui_util.h"
25 #include "chrome/browser/tabs/tab_strip_model.h" 21 #include "chrome/browser/tabs/tab_strip_model.h"
26 #include "chrome/browser/task_manager/task_manager.h" 22 #include "chrome/browser/task_manager/task_manager.h"
27 #include "chrome/browser/ui/browser.h" 23 #include "chrome/browser/ui/browser.h"
28 #include "chrome/browser/ui/browser_window.h" 24 #include "chrome/browser/ui/browser_window.h"
29 #include "chrome/browser/ui/toolbar/encoding_menu_controller.h" 25 #include "chrome/browser/ui/toolbar/encoding_menu_controller.h"
30 #include "chrome/browser/upgrade_detector.h" 26 #include "chrome/browser/upgrade_detector.h"
31 #include "chrome/common/chrome_paths.h" 27 #include "chrome/common/chrome_paths.h"
32 #include "chrome/common/chrome_switches.h" 28 #include "chrome/common/chrome_switches.h"
33 #include "chrome/common/pref_names.h" 29 #include "chrome/common/pref_names.h"
34 #include "chrome/common/profiling.h" 30 #include "chrome/common/profiling.h"
35 #include "content/browser/tab_contents/tab_contents.h" 31 #include "content/browser/tab_contents/tab_contents.h"
36 #include "content/common/notification_service.h" 32 #include "content/common/notification_service.h"
37 #include "content/common/notification_source.h" 33 #include "content/common/notification_source.h"
38 #include "content/common/notification_type.h" 34 #include "content/common/notification_type.h"
39 #include "grit/chromium_strings.h" 35 #include "grit/chromium_strings.h"
40 #include "grit/generated_resources.h" 36 #include "grit/generated_resources.h"
41 #include "grit/theme_resources.h" 37 #include "grit/theme_resources.h"
42 #include "ui/base/l10n/l10n_util.h" 38 #include "ui/base/l10n/l10n_util.h"
43 #include "ui/base/models/button_menu_item_model.h" 39 #include "ui/base/models/button_menu_item_model.h"
44 #include "ui/base/resource/resource_bundle.h" 40 #include "ui/base/resource/resource_bundle.h"
45 41
46 #if defined(TOOLKIT_USES_GTK) 42 #if defined(TOOLKIT_USES_GTK)
47 #include <gtk/gtk.h> 43 #include <gtk/gtk.h>
48 #include "chrome/browser/ui/gtk/gtk_util.h" 44 #include "chrome/browser/ui/gtk/gtk_util.h"
49 #endif 45 #endif
50 46
51 #if defined(OS_MACOSX)
52 #include "chrome/browser/ui/browser_window.h"
53 #endif
54
55 #if defined(OS_WIN) 47 #if defined(OS_WIN)
56 #include "chrome/browser/enumerate_modules_model_win.h" 48 #include "chrome/browser/enumerate_modules_model_win.h"
57 #endif 49 #endif
58 50
59 //////////////////////////////////////////////////////////////////////////////// 51 ////////////////////////////////////////////////////////////////////////////////
60 // EncodingMenuModel 52 // EncodingMenuModel
61 53
62 EncodingMenuModel::EncodingMenuModel(Browser* browser) 54 EncodingMenuModel::EncodingMenuModel(Browser* browser)
63 : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)), 55 : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)),
64 browser_(browser) { 56 browser_(browser) {
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 #endif 202 #endif
211 #if defined(OS_MACOSX) || defined(TOOLKIT_VIEWS) 203 #if defined(OS_MACOSX) || defined(TOOLKIT_VIEWS)
212 AddSeparator(); 204 AddSeparator();
213 #else 205 #else
214 // TODO: add submenu for bookmarks themselves, restore separator. 206 // TODO: add submenu for bookmarks themselves, restore separator.
215 #endif 207 #endif
216 } 208 }
217 209
218 210
219 //////////////////////////////////////////////////////////////////////////////// 211 ////////////////////////////////////////////////////////////////////////////////
220 // ProfilesSubMenuModel
221
222 ProfilesSubMenuModel::ProfilesSubMenuModel(
223 ui::SimpleMenuModel::Delegate* delegate, Browser* browser)
224 : SimpleMenuModel(this),
225 browser_(browser),
226 delegate_(delegate) {
227 Build();
228 }
229
230 void ProfilesSubMenuModel::Build() {
231 ProfileInfoCache& cache =
232 g_browser_process->profile_manager()->GetProfileInfoCache();
233 size_t count = cache.GetNumberOfProfiles();
234 for (size_t i = 0; i < count; ++i) {
235 AddCheckItem(COMMAND_SWITCH_TO_PROFILE + i,
236 cache.GetNameOfProfileAtIndex(i));
237 }
238
239 AddSeparator();
240
241 const string16 short_product_name =
242 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME);
243 AddItem(IDC_CREATE_NEW_PROFILE, l10n_util::GetStringFUTF16(
244 IDS_PROFILES_CREATE_NEW_PROFILE_OPTION, short_product_name));
245 }
246
247 class ProfileSwitchObserver : public ProfileManagerObserver {
248 virtual void OnProfileCreated(Profile* profile) OVERRIDE {
249 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
250
251 Browser* browser = BrowserList::FindTabbedBrowser(profile, false);
252 if (browser)
253 browser->window()->Activate();
254 else
255 Browser::NewWindowWithProfile(profile);
256 }
257
258 virtual bool DeleteAfterCreation() OVERRIDE { return true; }
259 };
260
261 void ProfilesSubMenuModel::ExecuteCommand(int command_id) {
262 ProfileInfoCache& cache =
263 g_browser_process->profile_manager()->GetProfileInfoCache();
264 size_t index = command_id;
265 if (index < cache.GetNumberOfProfiles()) {
266 FilePath profile_path = cache.GetPathOfProfileAtIndex(index);
267 ProfileSwitchObserver* observer = new ProfileSwitchObserver;
268 // The observer is deleted by the manager when profile creation is finished.
269 g_browser_process->profile_manager()->CreateProfileAsync(
270 profile_path, observer);
271 } else {
272 delegate_->ExecuteCommand(command_id);
273 }
274 }
275
276 bool ProfilesSubMenuModel::IsCommandIdChecked(int command_id) const {
277 ProfileInfoCache& cache =
278 g_browser_process->profile_manager()->GetProfileInfoCache();
279 size_t index = command_id;
280 if (index < cache.GetNumberOfProfiles()) {
281 FilePath userDataFolder;
282 PathService::Get(chrome::DIR_USER_DATA, &userDataFolder);
283 FilePath profile_path = cache.GetPathOfProfileAtIndex(index);
284 return browser_->GetProfile()->GetPath() == profile_path;
285 }
286 return delegate_->IsCommandIdChecked(command_id);
287 }
288
289 bool ProfilesSubMenuModel::IsCommandIdEnabled(int command_id) const {
290 ProfileInfoCache& cache =
291 g_browser_process->profile_manager()->GetProfileInfoCache();
292 size_t index = command_id;
293 if (index < cache.GetNumberOfProfiles())
294 return true;
295 return delegate_->IsCommandIdEnabled(command_id);
296 }
297
298 bool ProfilesSubMenuModel::GetAcceleratorForCommandId(
299 int command_id,
300 ui::Accelerator* accelerator) {
301 ProfileInfoCache& cache =
302 g_browser_process->profile_manager()->GetProfileInfoCache();
303 size_t index = command_id;
304 if (index < cache.GetNumberOfProfiles())
305 return false;
306 return delegate_->GetAcceleratorForCommandId(command_id, accelerator);
307 }
308
309 ////////////////////////////////////////////////////////////////////////////////
310 // WrenchMenuModel 212 // WrenchMenuModel
311 213
312 WrenchMenuModel::WrenchMenuModel(ui::AcceleratorProvider* provider, 214 WrenchMenuModel::WrenchMenuModel(ui::AcceleratorProvider* provider,
313 Browser* browser) 215 Browser* browser)
314 : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)), 216 : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)),
315 provider_(provider), 217 provider_(provider),
316 browser_(browser), 218 browser_(browser),
317 tabstrip_model_(browser_->tabstrip_model()) { 219 tabstrip_model_(browser_->tabstrip_model()) {
318 Build(); 220 Build();
319 UpdateZoomControls(); 221 UpdateZoomControls();
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
394 return true; 296 return true;
395 } 297 }
396 } 298 }
397 default: 299 default:
398 break; 300 break;
399 } 301 }
400 return false; 302 return false;
401 } 303 }
402 304
403 void WrenchMenuModel::ExecuteCommand(int command_id) { 305 void WrenchMenuModel::ExecuteCommand(int command_id) {
404 switch (command_id) { 306 browser_->ExecuteCommand(command_id);
405 case IDC_CREATE_NEW_PROFILE:
406 ProfileManager::CreateMultiProfileAsync();
407 break;
408 default:
409 browser_->ExecuteCommand(command_id);
410 break;
411 }
412 } 307 }
413 308
414 bool WrenchMenuModel::IsCommandIdChecked(int command_id) const { 309 bool WrenchMenuModel::IsCommandIdChecked(int command_id) const {
415 if (command_id == IDC_SHOW_BOOKMARK_BAR) { 310 if (command_id == IDC_SHOW_BOOKMARK_BAR) {
416 return browser_->profile()->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar); 311 return browser_->profile()->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar);
417 } else if (command_id == IDC_PROFILING_ENABLED) { 312 } else if (command_id == IDC_PROFILING_ENABLED) {
418 return Profiling::BeingProfiled(); 313 return Profiling::BeingProfiled();
419 } 314 }
420 315
421 return false; 316 return false;
422 } 317 }
423 318
424 bool WrenchMenuModel::IsCommandIdEnabled(int command_id) const { 319 bool WrenchMenuModel::IsCommandIdEnabled(int command_id) const {
425 switch (command_id) { 320 switch (command_id) {
426 case IDC_SHOW_BOOKMARK_BAR: 321 case IDC_SHOW_BOOKMARK_BAR:
427 return !browser_->profile()->GetPrefs()->IsManagedPreference( 322 return !browser_->profile()->GetPrefs()->IsManagedPreference(
428 prefs::kEnableBookmarkBar); 323 prefs::kEnableBookmarkBar);
429 case IDC_CREATE_NEW_PROFILE:
430 return true;
431 case IDC_PROFILE_MENU:
432 return true;
433 default: 324 default:
434 return browser_->command_updater()->IsCommandEnabled(command_id); 325 return browser_->command_updater()->IsCommandEnabled(command_id);
435 } 326 }
436 } 327 }
437 328
438 bool WrenchMenuModel::IsCommandIdVisible(int command_id) const { 329 bool WrenchMenuModel::IsCommandIdVisible(int command_id) const {
439 if (command_id == IDC_UPGRADE_DIALOG) { 330 if (command_id == IDC_UPGRADE_DIALOG) {
440 return UpgradeDetector::GetInstance()->notify_upgrade(); 331 return UpgradeDetector::GetInstance()->notify_upgrade();
441 } else if (command_id == IDC_VIEW_INCOMPATIBILITIES) { 332 } else if (command_id == IDC_VIEW_INCOMPATIBILITIES) {
442 #if defined(OS_WIN) 333 #if defined(OS_WIN)
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
561 452
562 AddSeparator(); 453 AddSeparator();
563 454
564 bookmark_sub_menu_model_.reset(new BookmarkSubMenuModel(this, browser_)); 455 bookmark_sub_menu_model_.reset(new BookmarkSubMenuModel(this, browser_));
565 AddSubMenuWithStringId(IDC_BOOKMARKS_MENU, IDS_BOOKMARKS_MENU, 456 AddSubMenuWithStringId(IDC_BOOKMARKS_MENU, IDS_BOOKMARKS_MENU,
566 bookmark_sub_menu_model_.get()); 457 bookmark_sub_menu_model_.get());
567 AddItemWithStringId(IDC_SHOW_HISTORY, IDS_SHOW_HISTORY); 458 AddItemWithStringId(IDC_SHOW_HISTORY, IDS_SHOW_HISTORY);
568 AddItemWithStringId(IDC_SHOW_DOWNLOADS, IDS_SHOW_DOWNLOADS); 459 AddItemWithStringId(IDC_SHOW_DOWNLOADS, IDS_SHOW_DOWNLOADS);
569 AddSeparator(); 460 AddSeparator();
570 461
571 #if !defined(OS_CHROMEOS)
572 const string16 short_product_name =
573 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME);
574 const CommandLine& browser_command_line = *CommandLine::ForCurrentProcess();
575 if (browser_command_line.HasSwitch(switches::kMultiProfiles)) {
576 ProfileInfoCache& cache =
577 g_browser_process->profile_manager()->GetProfileInfoCache();
578 if (cache.GetNumberOfProfiles() > 1) {
579 profiles_sub_menu_model_.reset(new ProfilesSubMenuModel(this, browser_));
580 AddSubMenu(IDC_PROFILE_MENU, l10n_util::GetStringFUTF16(
581 IDS_PROFILES_MENU, short_product_name),
582 profiles_sub_menu_model_.get());
583 } else {
584 profiles_sub_menu_model_.reset();
585 AddItem(IDC_CREATE_NEW_PROFILE, l10n_util::GetStringFUTF16(
586 IDS_PROFILES_CREATE_NEW_PROFILE_OPTION, short_product_name));
587 }
588
589 AddSeparator();
590 }
591 #endif
592
593 #if defined(OS_CHROMEOS) 462 #if defined(OS_CHROMEOS)
594 AddItemWithStringId(IDC_OPTIONS, IDS_SETTINGS); 463 AddItemWithStringId(IDC_OPTIONS, IDS_SETTINGS);
595 #elif defined(OS_MACOSX) 464 #elif defined(OS_MACOSX)
596 AddItemWithStringId(IDC_OPTIONS, IDS_PREFERENCES); 465 AddItemWithStringId(IDC_OPTIONS, IDS_PREFERENCES);
597 #elif defined(TOOLKIT_USES_GTK) 466 #elif defined(TOOLKIT_USES_GTK)
598 string16 preferences = gtk_util::GetStockPreferencesMenuLabel(); 467 string16 preferences = gtk_util::GetStockPreferencesMenuLabel();
599 if (!preferences.empty()) 468 if (!preferences.empty())
600 AddItem(IDC_OPTIONS, preferences); 469 AddItem(IDC_OPTIONS, preferences);
601 else 470 else
602 AddItemWithStringId(IDC_OPTIONS, IDS_PREFERENCES); 471 AddItemWithStringId(IDC_OPTIONS, IDS_PREFERENCES);
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
675 &enable_increment, &enable_decrement); 544 &enable_increment, &enable_decrement);
676 } 545 }
677 zoom_label_ = l10n_util::GetStringFUTF16( 546 zoom_label_ = l10n_util::GetStringFUTF16(
678 IDS_ZOOM_PERCENT, base::IntToString16(zoom_percent)); 547 IDS_ZOOM_PERCENT, base::IntToString16(zoom_percent));
679 } 548 }
680 549
681 string16 WrenchMenuModel::GetSyncMenuLabel() const { 550 string16 WrenchMenuModel::GetSyncMenuLabel() const {
682 return sync_ui_util::GetSyncMenuLabel( 551 return sync_ui_util::GetSyncMenuLabel(
683 browser_->profile()->GetOriginalProfile()->GetProfileSyncService()); 552 browser_->profile()->GetOriginalProfile()->GetProfileSyncService());
684 } 553 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/toolbar/wrench_menu_model.h ('k') | chrome/browser/ui/views/frame/browser_view.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698