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

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: Fixed menu item 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
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"
25 #include "chrome/browser/ui/toolbar/encoding_menu_controller.h" 27 #include "chrome/browser/ui/toolbar/encoding_menu_controller.h"
26 #include "chrome/browser/upgrade_detector.h" 28 #include "chrome/browser/upgrade_detector.h"
29 #include "chrome/common/chrome_paths.h"
27 #include "chrome/common/chrome_switches.h" 30 #include "chrome/common/chrome_switches.h"
28 #include "chrome/common/pref_names.h" 31 #include "chrome/common/pref_names.h"
29 #include "chrome/common/profiling.h" 32 #include "chrome/common/profiling.h"
30 #include "content/browser/tab_contents/tab_contents.h" 33 #include "content/browser/tab_contents/tab_contents.h"
31 #include "content/common/notification_service.h" 34 #include "content/common/notification_service.h"
32 #include "content/common/notification_source.h" 35 #include "content/common/notification_source.h"
33 #include "content/common/notification_type.h" 36 #include "content/common/notification_type.h"
34 #include "grit/chromium_strings.h" 37 #include "grit/chromium_strings.h"
35 #include "grit/generated_resources.h" 38 #include "grit/generated_resources.h"
36 #include "grit/theme_resources.h" 39 #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); 206 AddItemWithStringId(IDC_IMPORT_SETTINGS, IDS_IMPORT_SETTINGS_TITLE);
204 #if defined(OS_MACOSX) || defined(TOOLKIT_VIEWS) 207 #if defined(OS_MACOSX) || defined(TOOLKIT_VIEWS)
205 AddSeparator(); 208 AddSeparator();
206 #else 209 #else
207 // TODO: add submenu for bookmarks themselves, restore separator. 210 // TODO: add submenu for bookmarks themselves, restore separator.
208 #endif 211 #endif
209 } 212 }
210 213
211 214
212 //////////////////////////////////////////////////////////////////////////////// 215 ////////////////////////////////////////////////////////////////////////////////
216 // ProfilesSubMenuModel
217
218 ProfilesSubMenuModel::ProfilesSubMenuModel(
219 ui::SimpleMenuModel::Delegate* delegate, Browser* browser)
220 : SimpleMenuModel(this),
221 browser_(browser),
222 delegate_(delegate) {
223 Build();
224 }
225
226 void ProfilesSubMenuModel::Build() {
227 ProfileManager* profile_manager = g_browser_process->profile_manager();
228 size_t count = profile_manager->GetNumberOfProfiles();
229 for (size_t i = 0; i < count; ++i) {
230 AddItem(COMMAND_SWITCH_TO_PROFILE + i,
231 profile_manager->GetNameOfProfileAtIndex(i));
232 }
233
234 AddSeparator();
235
236 const string16 short_product_name =
237 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME);
238 AddItem(IDC_CREATE_NEW_PROFILE, l10n_util::GetStringFUTF16(
239 IDS_PROFILES_CREATE_NEW_PROFILE_OPTION, short_product_name));
240 }
241
242 static void OpenWindowWithProfile(const FilePath& profile_path) {
243 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
244 ProfileManager* profile_manager = g_browser_process->profile_manager();
245 Profile* profile = profile_manager->GetProfileByPath(profile_path);
246 if (!profile)
247 return;
248
249 Browser* browser = BrowserList::FindTabbedBrowser(profile, false);
250 if (browser) {
251 browser->window()->Activate();
252 } else {
253 Browser::NewWindowWithProfile(profile);
254 }
255 }
256
257 static void GetProfileOnIOThread(const FilePath& profile_path) {
258 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
259 ProfileManager* profile_manager = g_browser_process->profile_manager();
260 Profile* profile = profile_manager->GetProfile(profile_path);
261 if (profile) {
262 BrowserThread::PostTask(
263 BrowserThread::UI, FROM_HERE,
264 NewRunnableFunction(&OpenWindowWithProfile, profile_path));
265 }
266 }
267
268 void ProfilesSubMenuModel::ExecuteCommand(int command_id) {
269 ProfileManager* profile_manager = g_browser_process->profile_manager();
270 size_t index = command_id;
271 if (index < profile_manager->GetNumberOfProfiles()) {
272 FilePath userDataFolder;
273 PathService::Get(chrome::DIR_USER_DATA, &userDataFolder);
274 FilePath profile_path =
275 profile_manager->GetFilePathOfProfileAtIndex(index, userDataFolder);
276
277 if (profile_manager->GetProfileByPath(profile_path)) {
278 OpenWindowWithProfile(profile_path);
279 } else {
280 BrowserThread::PostTask(
281 BrowserThread::IO, FROM_HERE,
282 NewRunnableFunction(GetProfileOnIOThread, profile_path));
283 }
284 } else {
285 delegate_->ExecuteCommand(command_id);
286 }
287 }
288
289 bool ProfilesSubMenuModel::IsCommandIdChecked(int command_id) const {
290 ProfileManager* profile_manager = g_browser_process->profile_manager();
291 size_t index = command_id;
292 if (index < profile_manager->GetNumberOfProfiles()) {
293 FilePath userDataFolder;
294 PathService::Get(chrome::DIR_USER_DATA, &userDataFolder);
295 FilePath profile_path =
296 profile_manager->GetFilePathOfProfileAtIndex(index, userDataFolder);
297
298 return browser_->GetProfile()->GetPath() == profile_path;
299 }
300 return delegate_->IsCommandIdChecked(command_id);
301 }
302
303 bool ProfilesSubMenuModel::IsCommandIdEnabled(int command_id) const {
304 ProfileManager* profile_manager = g_browser_process->profile_manager();
305 size_t index = command_id;
306 if (index < profile_manager->GetNumberOfProfiles())
307 return true;
308 return delegate_->IsCommandIdEnabled(command_id);
309 }
310
311 bool ProfilesSubMenuModel::GetAcceleratorForCommandId(
312 int command_id,
313 ui::Accelerator* accelerator) {
314 ProfileManager* profile_manager = g_browser_process->profile_manager();
315 size_t index = command_id;
316 if (index < profile_manager->GetNumberOfProfiles())
317 return false;
318 return delegate_->GetAcceleratorForCommandId(command_id, accelerator);
319 }
320
321 ////////////////////////////////////////////////////////////////////////////////
213 // WrenchMenuModel 322 // WrenchMenuModel
214 323
215 WrenchMenuModel::WrenchMenuModel(ui::AcceleratorProvider* provider, 324 WrenchMenuModel::WrenchMenuModel(ui::AcceleratorProvider* provider,
216 Browser* browser) 325 Browser* browser)
217 : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)), 326 : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)),
218 provider_(provider), 327 provider_(provider),
219 browser_(browser), 328 browser_(browser),
220 tabstrip_model_(browser_->tabstrip_model()) { 329 tabstrip_model_(browser_->tabstrip_model()) {
221 Build(); 330 Build();
222 UpdateZoomControls(); 331 UpdateZoomControls();
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 return true; 406 return true;
298 } 407 }
299 } 408 }
300 default: 409 default:
301 break; 410 break;
302 } 411 }
303 return false; 412 return false;
304 } 413 }
305 414
306 void WrenchMenuModel::ExecuteCommand(int command_id) { 415 void WrenchMenuModel::ExecuteCommand(int command_id) {
307 browser_->ExecuteCommand(command_id); 416 switch (command_id) {
417 case IDC_CREATE_NEW_PROFILE:
418 ProfileManager::CreateMultiProfileAsync();
419 break;
420 default:
421 browser_->ExecuteCommand(command_id);
422 break;
423 }
308 } 424 }
309 425
310 bool WrenchMenuModel::IsCommandIdChecked(int command_id) const { 426 bool WrenchMenuModel::IsCommandIdChecked(int command_id) const {
311 if (command_id == IDC_SHOW_BOOKMARK_BAR) { 427 if (command_id == IDC_SHOW_BOOKMARK_BAR) {
312 return browser_->profile()->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar); 428 return browser_->profile()->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar);
313 } else if (command_id == IDC_PROFILING_ENABLED) { 429 } else if (command_id == IDC_PROFILING_ENABLED) {
314 return Profiling::BeingProfiled(); 430 return Profiling::BeingProfiled();
315 } 431 }
316 432
317 return false; 433 return false;
318 } 434 }
319 435
320 bool WrenchMenuModel::IsCommandIdEnabled(int command_id) const { 436 bool WrenchMenuModel::IsCommandIdEnabled(int command_id) const {
321 if (command_id == IDC_SHOW_BOOKMARK_BAR) { 437 switch (command_id) {
322 return !browser_->profile()->GetPrefs()->IsManagedPreference( 438 case IDC_SHOW_BOOKMARK_BAR:
323 prefs::kEnableBookmarkBar); 439 return !browser_->profile()->GetPrefs()->IsManagedPreference(
440 prefs::kEnableBookmarkBar);
441 case IDC_CREATE_NEW_PROFILE:
442 return true;
443 default:
444 return browser_->command_updater()->IsCommandEnabled(command_id);
324 } 445 }
325 return browser_->command_updater()->IsCommandEnabled(command_id);
326 } 446 }
327 447
328 bool WrenchMenuModel::IsCommandIdVisible(int command_id) const { 448 bool WrenchMenuModel::IsCommandIdVisible(int command_id) const {
329 if (command_id == IDC_UPGRADE_DIALOG) { 449 if (command_id == IDC_UPGRADE_DIALOG) {
330 return UpgradeDetector::GetInstance()->notify_upgrade(); 450 return UpgradeDetector::GetInstance()->notify_upgrade();
331 } else if (command_id == IDC_VIEW_INCOMPATIBILITIES) { 451 } else if (command_id == IDC_VIEW_INCOMPATIBILITIES) {
332 #if defined(OS_WIN) 452 #if defined(OS_WIN)
333 EnumerateModulesModel* loaded_modules = 453 EnumerateModulesModel* loaded_modules =
334 EnumerateModulesModel::GetInstance(); 454 EnumerateModulesModel::GetInstance();
335 if (loaded_modules->confirmed_bad_modules_detected() <= 0) 455 if (loaded_modules->confirmed_bad_modules_detected() <= 0)
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
451 571
452 AddSeparator(); 572 AddSeparator();
453 573
454 bookmark_sub_menu_model_.reset(new BookmarkSubMenuModel(this, browser_)); 574 bookmark_sub_menu_model_.reset(new BookmarkSubMenuModel(this, browser_));
455 AddSubMenuWithStringId(IDC_BOOKMARKS_MENU, IDS_BOOKMARKS_MENU, 575 AddSubMenuWithStringId(IDC_BOOKMARKS_MENU, IDS_BOOKMARKS_MENU,
456 bookmark_sub_menu_model_.get()); 576 bookmark_sub_menu_model_.get());
457 AddItemWithStringId(IDC_SHOW_HISTORY, IDS_SHOW_HISTORY); 577 AddItemWithStringId(IDC_SHOW_HISTORY, IDS_SHOW_HISTORY);
458 AddItemWithStringId(IDC_SHOW_DOWNLOADS, IDS_SHOW_DOWNLOADS); 578 AddItemWithStringId(IDC_SHOW_DOWNLOADS, IDS_SHOW_DOWNLOADS);
459 AddSeparator(); 579 AddSeparator();
460 580
581 #if !defined(OS_CHROMEOS)
582 const string16 short_product_name =
583 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME);
584 const CommandLine& browser_command_line = *CommandLine::ForCurrentProcess();
585 if (browser_command_line.HasSwitch(switches::kMultiProfiles)) {
586 if (g_browser_process->profile_manager()->GetNumberOfProfiles() > 1) {
587 profiles_sub_menu_model_.reset(new ProfilesSubMenuModel(this, browser_));
588 AddSubMenu(IDC_PROFILE_MENU, l10n_util::GetStringFUTF16(
589 IDS_PROFILES_MENU, short_product_name),
590 profiles_sub_menu_model_.get());
591 } else {
592 profiles_sub_menu_model_.reset();
593 AddItem(IDC_CREATE_NEW_PROFILE, l10n_util::GetStringFUTF16(
594 IDS_PROFILES_CREATE_NEW_PROFILE_OPTION, short_product_name));
595 }
596 }
597 AddSeparator();
598 #endif
599
461 #if defined(OS_CHROMEOS) 600 #if defined(OS_CHROMEOS)
462 AddItemWithStringId(IDC_OPTIONS, IDS_SETTINGS); 601 AddItemWithStringId(IDC_OPTIONS, IDS_SETTINGS);
463 #elif defined(OS_MACOSX) 602 #elif defined(OS_MACOSX)
464 AddItemWithStringId(IDC_OPTIONS, IDS_PREFERENCES); 603 AddItemWithStringId(IDC_OPTIONS, IDS_PREFERENCES);
465 #elif defined(TOOLKIT_USES_GTK) 604 #elif defined(TOOLKIT_USES_GTK)
466 string16 preferences = gtk_util::GetStockPreferencesMenuLabel(); 605 string16 preferences = gtk_util::GetStockPreferencesMenuLabel();
467 if (!preferences.empty()) 606 if (!preferences.empty())
468 AddItem(IDC_OPTIONS, preferences); 607 AddItem(IDC_OPTIONS, preferences);
469 else 608 else
470 AddItemWithStringId(IDC_OPTIONS, IDS_PREFERENCES); 609 AddItemWithStringId(IDC_OPTIONS, IDS_PREFERENCES);
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
543 &enable_increment, &enable_decrement); 682 &enable_increment, &enable_decrement);
544 } 683 }
545 zoom_label_ = l10n_util::GetStringFUTF16( 684 zoom_label_ = l10n_util::GetStringFUTF16(
546 IDS_ZOOM_PERCENT, base::IntToString16(zoom_percent)); 685 IDS_ZOOM_PERCENT, base::IntToString16(zoom_percent));
547 } 686 }
548 687
549 string16 WrenchMenuModel::GetSyncMenuLabel() const { 688 string16 WrenchMenuModel::GetSyncMenuLabel() const {
550 return sync_ui_util::GetSyncMenuLabel( 689 return sync_ui_util::GetSyncMenuLabel(
551 browser_->profile()->GetOriginalProfile()->GetProfileSyncService()); 690 browser_->profile()->GetOriginalProfile()->GetProfileSyncService());
552 } 691 }
OLDNEW
« chrome/browser/profiles/profile_manager.cc ('K') | « 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