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

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: Opening windows 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() {
sky 2011/06/10 16:24:14 Rename this Init and have the caller invoke it (ht
sail 2011/06/11 01:27:37 I agree but all the other classes in this file do
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 AddItemWithStringId(IDC_CREATE_NEW_PROFILE,
sky 2011/06/10 16:24:14 Are you sure you don't want this first? Having the
sail 2011/06/11 01:27:37 This menu will mainly be used for switching betwee
236 IDS_PROFILES_CREATE_NEW_PROFILE_OPTION);
237 }
238
239 void ProfilesSubMenuModel::ExecuteCommand(int command_id) {
240 ProfileManager* profile_manager = g_browser_process->profile_manager();
241 size_t index = command_id;
242 if (index < profile_manager->GetNumberOfProfiles()) {
243 FilePath userDataFolder;
244 PathService::Get(chrome::DIR_USER_DATA, &userDataFolder);
245 FilePath profile_path =
246 profile_manager->GetFilePathOfProfileAtIndex(index, userDataFolder);
247
248 if (profile_manager->GetProfileByPath(profile_path)) {
249 OpenWindowWithProfile(profile_path);
250 } else {
251 BrowserThread::PostTask(
252 BrowserThread::IO, FROM_HERE,
253 NewRunnableMethod(this, &ProfilesSubMenuModel::GetProfileOnIOThread,
254 profile_path));
Miranda Callahan 2011/06/11 15:09:08 You don't want to do this on the IO Thread -- see
sail 2011/06/13 19:00:01 Done.
255 }
256 } else {
257 delegate_->ExecuteCommand(command_id);
258 }
259 }
260
261 void ProfilesSubMenuModel::GetProfileOnIOThread(FilePath profile_path) {
262 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
263 ProfileManager* profile_manager = g_browser_process->profile_manager();
sky 2011/06/10 16:24:14 Is ProfileManager, Profile and all the classes tha
sail 2011/06/11 01:27:37 The GetProfile() function is meant to be called on
Miranda Callahan 2011/06/11 15:09:08 I am trying to remember a discussion about this, b
sail 2011/06/13 19:00:01 Hey Miranda, we didn't discuss this. I just confus
264 Profile* profile = profile_manager->GetProfile(profile_path);
265 if (profile) {
266 BrowserThread::PostTask(
267 BrowserThread::UI, FROM_HERE,
268 NewRunnableMethod(this, &ProfilesSubMenuModel::OpenWindowWithProfile,
269 profile_path));
270 }
271 }
272
273 void ProfilesSubMenuModel::OpenWindowWithProfile(FilePath profile_path) {
274 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
275 ProfileManager* profile_manager = g_browser_process->profile_manager();
276 Profile* profile = profile_manager->GetProfileByPath(profile_path);
277 if (!profile)
278 return;
279
280 Browser* browser = BrowserList::FindAnyBrowser(profile, false);
sky 2011/06/10 16:24:14 Are you sure you don't want FindTabbedBrowser(prof
sail 2011/06/11 01:27:37 Ahh, right. Switched to FindTabbedBrowser(). I don
Miranda Callahan 2011/06/11 15:09:08 Could you rename this FindAnyBrowserWithProfile, t
Miranda Callahan 2011/06/11 15:13:36 nm -- I see that these are existing methods in Bro
281 if (browser) {
282 browser->window()->Activate();
283 } else {
284 Browser::NewWindowWithProfile(profile);
285 }
286 }
287
288 bool ProfilesSubMenuModel::IsCommandIdChecked(int command_id) const {
289 ProfileManager* profile_manager = g_browser_process->profile_manager();
290 size_t index = command_id;
291 if (index < profile_manager->GetNumberOfProfiles()) {
292 FilePath userDataFolder;
293 PathService::Get(chrome::DIR_USER_DATA, &userDataFolder);
294 FilePath profile_path =
295 profile_manager->GetFilePathOfProfileAtIndex(index, userDataFolder);
296
297 return browser_->GetProfile()->GetPath() == profile_path;
298 }
299 return delegate_->IsCommandIdChecked(command_id);
300 }
301
302 bool ProfilesSubMenuModel::IsCommandIdEnabled(int command_id) const {
303 ProfileManager* profile_manager = g_browser_process->profile_manager();
304 size_t index = command_id;
305 if (index < profile_manager->GetNumberOfProfiles())
306 return true;
307 return delegate_->IsCommandIdEnabled(command_id);
308 }
309
310 bool ProfilesSubMenuModel::GetAcceleratorForCommandId(
311 int command_id,
312 ui::Accelerator* accelerator) {
313 ProfileManager* profile_manager = g_browser_process->profile_manager();
314 size_t index = command_id;
315 if (index < profile_manager->GetNumberOfProfiles())
316 return false;
317 return delegate_->GetAcceleratorForCommandId(command_id, accelerator);
318 }
319
320 ////////////////////////////////////////////////////////////////////////////////
213 // WrenchMenuModel 321 // WrenchMenuModel
214 322
215 WrenchMenuModel::WrenchMenuModel(ui::AcceleratorProvider* provider, 323 WrenchMenuModel::WrenchMenuModel(ui::AcceleratorProvider* provider,
216 Browser* browser) 324 Browser* browser)
217 : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)), 325 : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)),
218 provider_(provider), 326 provider_(provider),
219 browser_(browser), 327 browser_(browser),
220 tabstrip_model_(browser_->tabstrip_model()) { 328 tabstrip_model_(browser_->tabstrip_model()) {
221 Build(); 329 Build();
222 UpdateZoomControls(); 330 UpdateZoomControls();
223 331
224 tabstrip_model_->AddObserver(this); 332 tabstrip_model_->AddObserver(this);
225 333
226 registrar_.Add(this, NotificationType::ZOOM_LEVEL_CHANGED, 334 registrar_.Add(this, NotificationType::ZOOM_LEVEL_CHANGED,
227 Source<HostZoomMap>(browser_->profile()->GetHostZoomMap())); 335 Source<HostZoomMap>(browser_->profile()->GetHostZoomMap()));
228 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED, 336 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
229 NotificationService::AllSources()); 337 NotificationService::AllSources());
338 registrar_.Add(this, NotificationType::PROFILE_ADDED,
339 NotificationService::AllSources());
230 } 340 }
231 341
232 WrenchMenuModel::~WrenchMenuModel() { 342 WrenchMenuModel::~WrenchMenuModel() {
233 if (tabstrip_model_) 343 if (tabstrip_model_)
234 tabstrip_model_->RemoveObserver(this); 344 tabstrip_model_->RemoveObserver(this);
235 } 345 }
236 346
237 bool WrenchMenuModel::DoesCommandIdDismissMenu(int command_id) const { 347 bool WrenchMenuModel::DoesCommandIdDismissMenu(int command_id) const {
238 return command_id != IDC_ZOOM_MINUS && command_id != IDC_ZOOM_PLUS; 348 return command_id != IDC_ZOOM_MINUS && command_id != IDC_ZOOM_PLUS;
239 } 349 }
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 return true; 407 return true;
298 } 408 }
299 } 409 }
300 default: 410 default:
301 break; 411 break;
302 } 412 }
303 return false; 413 return false;
304 } 414 }
305 415
306 void WrenchMenuModel::ExecuteCommand(int command_id) { 416 void WrenchMenuModel::ExecuteCommand(int command_id) {
307 browser_->ExecuteCommand(command_id); 417 switch (command_id) {
418 case IDC_CREATE_NEW_PROFILE:
419 ProfileManager::CreateMultiProfileAsync();
420 break;
421 default:
422 browser_->ExecuteCommand(command_id);
423 break;
424 }
308 } 425 }
309 426
310 bool WrenchMenuModel::IsCommandIdChecked(int command_id) const { 427 bool WrenchMenuModel::IsCommandIdChecked(int command_id) const {
311 if (command_id == IDC_SHOW_BOOKMARK_BAR) { 428 if (command_id == IDC_SHOW_BOOKMARK_BAR) {
312 return browser_->profile()->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar); 429 return browser_->profile()->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar);
313 } else if (command_id == IDC_PROFILING_ENABLED) { 430 } else if (command_id == IDC_PROFILING_ENABLED) {
314 return Profiling::BeingProfiled(); 431 return Profiling::BeingProfiled();
315 } 432 }
316 433
317 return false; 434 return false;
318 } 435 }
319 436
320 bool WrenchMenuModel::IsCommandIdEnabled(int command_id) const { 437 bool WrenchMenuModel::IsCommandIdEnabled(int command_id) const {
321 if (command_id == IDC_SHOW_BOOKMARK_BAR) { 438 switch (command_id) {
322 return !browser_->profile()->GetPrefs()->IsManagedPreference( 439 case IDC_SHOW_BOOKMARK_BAR:
323 prefs::kEnableBookmarkBar); 440 return !browser_->profile()->GetPrefs()->IsManagedPreference(
441 prefs::kEnableBookmarkBar);
442 case IDC_CREATE_NEW_PROFILE:
443 return true;
444 default:
445 return browser_->command_updater()->IsCommandEnabled(command_id);
324 } 446 }
325 return browser_->command_updater()->IsCommandEnabled(command_id);
326 } 447 }
327 448
328 bool WrenchMenuModel::IsCommandIdVisible(int command_id) const { 449 bool WrenchMenuModel::IsCommandIdVisible(int command_id) const {
329 if (command_id == IDC_UPGRADE_DIALOG) { 450 if (command_id == IDC_UPGRADE_DIALOG) {
330 return UpgradeDetector::GetInstance()->notify_upgrade(); 451 return UpgradeDetector::GetInstance()->notify_upgrade();
331 } else if (command_id == IDC_VIEW_INCOMPATIBILITIES) { 452 } else if (command_id == IDC_VIEW_INCOMPATIBILITIES) {
332 #if defined(OS_WIN) 453 #if defined(OS_WIN)
333 EnumerateModulesModel* loaded_modules = 454 EnumerateModulesModel* loaded_modules =
334 EnumerateModulesModel::GetInstance(); 455 EnumerateModulesModel::GetInstance();
335 if (loaded_modules->confirmed_bad_modules_detected() <= 0) 456 if (loaded_modules->confirmed_bad_modules_detected() <= 0)
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
375 } 496 }
376 497
377 void WrenchMenuModel::Observe(NotificationType type, 498 void WrenchMenuModel::Observe(NotificationType type,
378 const NotificationSource& source, 499 const NotificationSource& source,
379 const NotificationDetails& details) { 500 const NotificationDetails& details) {
380 switch (type.value) { 501 switch (type.value) {
381 case NotificationType::ZOOM_LEVEL_CHANGED: 502 case NotificationType::ZOOM_LEVEL_CHANGED:
382 case NotificationType::NAV_ENTRY_COMMITTED: 503 case NotificationType::NAV_ENTRY_COMMITTED:
383 UpdateZoomControls(); 504 UpdateZoomControls();
384 break; 505 break;
506 case NotificationType::PROFILE_ADDED: {
sky 2011/06/10 16:24:14 This will cause problems if the menu is showing. S
sail 2011/06/11 01:27:37 Agreed, removed.
507 int index = GetIndexOfCommandId(IDC_CREATE_NEW_PROFILE);
508 if (index == -1)
509 index = GetIndexOfCommandId(IDC_PROFILE_MENU);
510 if (index != -1) {
511 RemoveItemAt(index);
512 AddProfilesMenuItemAt(index);
513 }
514 break;
515 }
385 default: 516 default:
386 NOTREACHED(); 517 NOTREACHED();
387 } 518 }
388 } 519 }
389 520
390 // For testing. 521 // For testing.
391 WrenchMenuModel::WrenchMenuModel() 522 WrenchMenuModel::WrenchMenuModel()
392 : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)), 523 : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)),
393 provider_(NULL), 524 provider_(NULL),
394 browser_(NULL), 525 browser_(NULL),
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
451 582
452 AddSeparator(); 583 AddSeparator();
453 584
454 bookmark_sub_menu_model_.reset(new BookmarkSubMenuModel(this, browser_)); 585 bookmark_sub_menu_model_.reset(new BookmarkSubMenuModel(this, browser_));
455 AddSubMenuWithStringId(IDC_BOOKMARKS_MENU, IDS_BOOKMARKS_MENU, 586 AddSubMenuWithStringId(IDC_BOOKMARKS_MENU, IDS_BOOKMARKS_MENU,
456 bookmark_sub_menu_model_.get()); 587 bookmark_sub_menu_model_.get());
457 AddItemWithStringId(IDC_SHOW_HISTORY, IDS_SHOW_HISTORY); 588 AddItemWithStringId(IDC_SHOW_HISTORY, IDS_SHOW_HISTORY);
458 AddItemWithStringId(IDC_SHOW_DOWNLOADS, IDS_SHOW_DOWNLOADS); 589 AddItemWithStringId(IDC_SHOW_DOWNLOADS, IDS_SHOW_DOWNLOADS);
459 AddSeparator(); 590 AddSeparator();
460 591
592 #if !defined(OS_CHROMEOS)
593 AddProfilesMenuItemAt(GetItemCount());
594 AddSeparator();
595 #endif
596
461 #if defined(OS_CHROMEOS) 597 #if defined(OS_CHROMEOS)
462 AddItemWithStringId(IDC_OPTIONS, IDS_SETTINGS); 598 AddItemWithStringId(IDC_OPTIONS, IDS_SETTINGS);
463 #elif defined(OS_MACOSX) 599 #elif defined(OS_MACOSX)
464 AddItemWithStringId(IDC_OPTIONS, IDS_PREFERENCES); 600 AddItemWithStringId(IDC_OPTIONS, IDS_PREFERENCES);
465 #elif defined(TOOLKIT_USES_GTK) 601 #elif defined(TOOLKIT_USES_GTK)
466 string16 preferences = gtk_util::GetStockPreferencesMenuLabel(); 602 string16 preferences = gtk_util::GetStockPreferencesMenuLabel();
467 if (!preferences.empty()) 603 if (!preferences.empty())
468 AddItem(IDC_OPTIONS, preferences); 604 AddItem(IDC_OPTIONS, preferences);
469 else 605 else
470 AddItemWithStringId(IDC_OPTIONS, IDS_PREFERENCES); 606 AddItemWithStringId(IDC_OPTIONS, IDS_PREFERENCES);
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
543 &enable_increment, &enable_decrement); 679 &enable_increment, &enable_decrement);
544 } 680 }
545 zoom_label_ = l10n_util::GetStringFUTF16( 681 zoom_label_ = l10n_util::GetStringFUTF16(
546 IDS_ZOOM_PERCENT, base::IntToString16(zoom_percent)); 682 IDS_ZOOM_PERCENT, base::IntToString16(zoom_percent));
547 } 683 }
548 684
549 string16 WrenchMenuModel::GetSyncMenuLabel() const { 685 string16 WrenchMenuModel::GetSyncMenuLabel() const {
550 return sync_ui_util::GetSyncMenuLabel( 686 return sync_ui_util::GetSyncMenuLabel(
551 browser_->profile()->GetOriginalProfile()->GetProfileSyncService()); 687 browser_->profile()->GetOriginalProfile()->GetProfileSyncService());
552 } 688 }
689
690 void WrenchMenuModel::AddProfilesMenuItemAt(int index) {
691 if (g_browser_process->profile_manager()->GetNumberOfProfiles() > 1) {
sky 2011/06/10 16:24:14 Seems weird to me to change the type based on cont
sail 2011/06/11 01:27:37 Yea, we went through this UI in the profile meetin
692 profiles_sub_menu_model_.reset(new ProfilesSubMenuModel(this, browser_));
693 InsertSubMenuWithStringIdAt(index, IDC_PROFILE_MENU, IDS_PROFILES_MENU,
694 profiles_sub_menu_model_.get());
695 } else {
696 profiles_sub_menu_model_.reset();
697 InsertItemWithStringIdAt(index, IDC_CREATE_NEW_PROFILE,
698 IDS_PROFILES_CREATE_NEW_PROFILE_OPTION);
699 }
700 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698