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

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

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

Powered by Google App Engine
This is Rietveld 408576698