OLD | NEW |
---|---|
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 Loading... | |
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 AddCheckItem(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 class ProfileSwitchObserver : public ProfileManagerObserver { | |
243 virtual void OnProfileCreated(Profile* profile) OVERRIDE { | |
244 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
245 | |
246 Browser* browser = BrowserList::FindTabbedBrowser(profile, false); | |
247 if (browser) | |
248 browser->window()->Activate(); | |
249 else | |
250 Browser::NewWindowWithProfile(profile); | |
251 } | |
252 | |
253 virtual bool DeleteAfterCreation() OVERRIDE { return true; } | |
254 }; | |
255 | |
256 void ProfilesSubMenuModel::ExecuteCommand(int command_id) { | |
257 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
258 size_t index = command_id; | |
259 if (index < profile_manager->GetNumberOfProfiles()) { | |
260 FilePath userDataFolder; | |
261 PathService::Get(chrome::DIR_USER_DATA, &userDataFolder); | |
262 FilePath profile_path = | |
263 profile_manager->GetFilePathOfProfileAtIndex(index, userDataFolder); | |
264 | |
265 ProfileSwitchObserver* observer = new ProfileSwitchObserver; | |
266 // The observer is deleted by the manager when profile creation is finished. | |
267 profile_manager->CreateProfileAsync(profile_path, observer); | |
268 } else { | |
269 delegate_->ExecuteCommand(command_id); | |
270 } | |
271 } | |
272 | |
273 bool ProfilesSubMenuModel::IsCommandIdChecked(int command_id) const { | |
Miranda Callahan
2011/06/13 22:08:11
Are these really going to be checkboxed menu items
sail
2011/06/13 23:08:46
I found that the checkmark helped when switching b
| |
274 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
275 size_t index = command_id; | |
276 if (index < profile_manager->GetNumberOfProfiles()) { | |
277 FilePath userDataFolder; | |
278 PathService::Get(chrome::DIR_USER_DATA, &userDataFolder); | |
279 FilePath profile_path = | |
280 profile_manager->GetFilePathOfProfileAtIndex(index, userDataFolder); | |
281 return browser_->GetProfile()->GetPath() == profile_path; | |
282 } | |
283 return delegate_->IsCommandIdChecked(command_id); | |
284 } | |
285 | |
286 bool ProfilesSubMenuModel::IsCommandIdEnabled(int command_id) const { | |
287 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
288 size_t index = command_id; | |
289 if (index < profile_manager->GetNumberOfProfiles()) | |
290 return true; | |
291 return delegate_->IsCommandIdEnabled(command_id); | |
292 } | |
293 | |
294 bool ProfilesSubMenuModel::GetAcceleratorForCommandId( | |
295 int command_id, | |
296 ui::Accelerator* accelerator) { | |
297 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
298 size_t index = command_id; | |
299 if (index < profile_manager->GetNumberOfProfiles()) | |
300 return false; | |
301 return delegate_->GetAcceleratorForCommandId(command_id, accelerator); | |
302 } | |
303 | |
304 //////////////////////////////////////////////////////////////////////////////// | |
213 // WrenchMenuModel | 305 // WrenchMenuModel |
214 | 306 |
215 WrenchMenuModel::WrenchMenuModel(ui::AcceleratorProvider* provider, | 307 WrenchMenuModel::WrenchMenuModel(ui::AcceleratorProvider* provider, |
216 Browser* browser) | 308 Browser* browser) |
217 : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)), | 309 : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)), |
218 provider_(provider), | 310 provider_(provider), |
219 browser_(browser), | 311 browser_(browser), |
220 tabstrip_model_(browser_->tabstrip_model()) { | 312 tabstrip_model_(browser_->tabstrip_model()) { |
221 Build(); | 313 Build(); |
222 UpdateZoomControls(); | 314 UpdateZoomControls(); |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
297 return true; | 389 return true; |
298 } | 390 } |
299 } | 391 } |
300 default: | 392 default: |
301 break; | 393 break; |
302 } | 394 } |
303 return false; | 395 return false; |
304 } | 396 } |
305 | 397 |
306 void WrenchMenuModel::ExecuteCommand(int command_id) { | 398 void WrenchMenuModel::ExecuteCommand(int command_id) { |
307 browser_->ExecuteCommand(command_id); | 399 switch (command_id) { |
400 case IDC_CREATE_NEW_PROFILE: | |
401 ProfileManager::CreateMultiProfileAsync(); | |
402 break; | |
403 default: | |
404 browser_->ExecuteCommand(command_id); | |
405 break; | |
406 } | |
308 } | 407 } |
309 | 408 |
310 bool WrenchMenuModel::IsCommandIdChecked(int command_id) const { | 409 bool WrenchMenuModel::IsCommandIdChecked(int command_id) const { |
311 if (command_id == IDC_SHOW_BOOKMARK_BAR) { | 410 if (command_id == IDC_SHOW_BOOKMARK_BAR) { |
312 return browser_->profile()->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar); | 411 return browser_->profile()->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar); |
313 } else if (command_id == IDC_PROFILING_ENABLED) { | 412 } else if (command_id == IDC_PROFILING_ENABLED) { |
314 return Profiling::BeingProfiled(); | 413 return Profiling::BeingProfiled(); |
315 } | 414 } |
316 | 415 |
317 return false; | 416 return false; |
318 } | 417 } |
319 | 418 |
320 bool WrenchMenuModel::IsCommandIdEnabled(int command_id) const { | 419 bool WrenchMenuModel::IsCommandIdEnabled(int command_id) const { |
321 if (command_id == IDC_SHOW_BOOKMARK_BAR) { | 420 switch (command_id) { |
322 return !browser_->profile()->GetPrefs()->IsManagedPreference( | 421 case IDC_SHOW_BOOKMARK_BAR: |
323 prefs::kEnableBookmarkBar); | 422 return !browser_->profile()->GetPrefs()->IsManagedPreference( |
423 prefs::kEnableBookmarkBar); | |
424 case IDC_CREATE_NEW_PROFILE: | |
425 return true; | |
426 default: | |
427 return browser_->command_updater()->IsCommandEnabled(command_id); | |
324 } | 428 } |
325 return browser_->command_updater()->IsCommandEnabled(command_id); | |
326 } | 429 } |
327 | 430 |
328 bool WrenchMenuModel::IsCommandIdVisible(int command_id) const { | 431 bool WrenchMenuModel::IsCommandIdVisible(int command_id) const { |
329 if (command_id == IDC_UPGRADE_DIALOG) { | 432 if (command_id == IDC_UPGRADE_DIALOG) { |
330 return UpgradeDetector::GetInstance()->notify_upgrade(); | 433 return UpgradeDetector::GetInstance()->notify_upgrade(); |
331 } else if (command_id == IDC_VIEW_INCOMPATIBILITIES) { | 434 } else if (command_id == IDC_VIEW_INCOMPATIBILITIES) { |
332 #if defined(OS_WIN) | 435 #if defined(OS_WIN) |
333 EnumerateModulesModel* loaded_modules = | 436 EnumerateModulesModel* loaded_modules = |
334 EnumerateModulesModel::GetInstance(); | 437 EnumerateModulesModel::GetInstance(); |
335 if (loaded_modules->confirmed_bad_modules_detected() <= 0) | 438 if (loaded_modules->confirmed_bad_modules_detected() <= 0) |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
451 | 554 |
452 AddSeparator(); | 555 AddSeparator(); |
453 | 556 |
454 bookmark_sub_menu_model_.reset(new BookmarkSubMenuModel(this, browser_)); | 557 bookmark_sub_menu_model_.reset(new BookmarkSubMenuModel(this, browser_)); |
455 AddSubMenuWithStringId(IDC_BOOKMARKS_MENU, IDS_BOOKMARKS_MENU, | 558 AddSubMenuWithStringId(IDC_BOOKMARKS_MENU, IDS_BOOKMARKS_MENU, |
456 bookmark_sub_menu_model_.get()); | 559 bookmark_sub_menu_model_.get()); |
457 AddItemWithStringId(IDC_SHOW_HISTORY, IDS_SHOW_HISTORY); | 560 AddItemWithStringId(IDC_SHOW_HISTORY, IDS_SHOW_HISTORY); |
458 AddItemWithStringId(IDC_SHOW_DOWNLOADS, IDS_SHOW_DOWNLOADS); | 561 AddItemWithStringId(IDC_SHOW_DOWNLOADS, IDS_SHOW_DOWNLOADS); |
459 AddSeparator(); | 562 AddSeparator(); |
460 | 563 |
564 #if !defined(OS_CHROMEOS) | |
565 const string16 short_product_name = | |
566 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME); | |
567 const CommandLine& browser_command_line = *CommandLine::ForCurrentProcess(); | |
568 if (browser_command_line.HasSwitch(switches::kMultiProfiles)) { | |
569 if (g_browser_process->profile_manager()->GetNumberOfProfiles() > 1) { | |
570 profiles_sub_menu_model_.reset(new ProfilesSubMenuModel(this, browser_)); | |
571 AddSubMenu(IDC_PROFILE_MENU, l10n_util::GetStringFUTF16( | |
572 IDS_PROFILES_MENU, short_product_name), | |
573 profiles_sub_menu_model_.get()); | |
574 } else { | |
575 profiles_sub_menu_model_.reset(); | |
576 AddItem(IDC_CREATE_NEW_PROFILE, l10n_util::GetStringFUTF16( | |
577 IDS_PROFILES_CREATE_NEW_PROFILE_OPTION, short_product_name)); | |
578 } | |
579 | |
580 AddSeparator(); | |
581 } | |
582 #endif | |
583 | |
461 #if defined(OS_CHROMEOS) | 584 #if defined(OS_CHROMEOS) |
462 AddItemWithStringId(IDC_OPTIONS, IDS_SETTINGS); | 585 AddItemWithStringId(IDC_OPTIONS, IDS_SETTINGS); |
463 #elif defined(OS_MACOSX) | 586 #elif defined(OS_MACOSX) |
464 AddItemWithStringId(IDC_OPTIONS, IDS_PREFERENCES); | 587 AddItemWithStringId(IDC_OPTIONS, IDS_PREFERENCES); |
465 #elif defined(TOOLKIT_USES_GTK) | 588 #elif defined(TOOLKIT_USES_GTK) |
466 string16 preferences = gtk_util::GetStockPreferencesMenuLabel(); | 589 string16 preferences = gtk_util::GetStockPreferencesMenuLabel(); |
467 if (!preferences.empty()) | 590 if (!preferences.empty()) |
468 AddItem(IDC_OPTIONS, preferences); | 591 AddItem(IDC_OPTIONS, preferences); |
469 else | 592 else |
470 AddItemWithStringId(IDC_OPTIONS, IDS_PREFERENCES); | 593 AddItemWithStringId(IDC_OPTIONS, IDS_PREFERENCES); |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
543 &enable_increment, &enable_decrement); | 666 &enable_increment, &enable_decrement); |
544 } | 667 } |
545 zoom_label_ = l10n_util::GetStringFUTF16( | 668 zoom_label_ = l10n_util::GetStringFUTF16( |
546 IDS_ZOOM_PERCENT, base::IntToString16(zoom_percent)); | 669 IDS_ZOOM_PERCENT, base::IntToString16(zoom_percent)); |
547 } | 670 } |
548 | 671 |
549 string16 WrenchMenuModel::GetSyncMenuLabel() const { | 672 string16 WrenchMenuModel::GetSyncMenuLabel() const { |
550 return sync_ui_util::GetSyncMenuLabel( | 673 return sync_ui_util::GetSyncMenuLabel( |
551 browser_->profile()->GetOriginalProfile()->GetProfileSyncService()); | 674 browser_->profile()->GetOriginalProfile()->GetProfileSyncService()); |
552 } | 675 } |
OLD | NEW |