OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/string_number_conversions.h" | 12 #include "base/string_number_conversions.h" |
13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
14 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
15 #include "chrome/app/chrome_command_ids.h" | 15 #include "chrome/app/chrome_command_ids.h" |
16 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
17 #include "chrome/browser/defaults.h" | 17 #include "chrome/browser/defaults.h" |
18 #include "chrome/browser/prefs/pref_service.h" | 18 #include "chrome/browser/prefs/pref_service.h" |
19 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
20 #include "chrome/browser/profiles/profile_manager.h" | 20 #include "chrome/browser/profiles/profile_manager.h" |
21 #include "chrome/browser/signin/signin_global_error.h" | 21 #include "chrome/browser/signin/signin_ui_util.h" |
22 #include "chrome/browser/signin/signin_manager.h" | |
23 #include "chrome/browser/signin/signin_manager_factory.h" | |
24 #include "chrome/browser/sync/profile_sync_service.h" | |
25 #include "chrome/browser/sync/profile_sync_service_factory.h" | |
26 #include "chrome/browser/sync/sync_global_error.h" | |
27 #include "chrome/browser/task_manager/task_manager.h" | 22 #include "chrome/browser/task_manager/task_manager.h" |
28 #include "chrome/browser/ui/browser.h" | 23 #include "chrome/browser/ui/browser.h" |
29 #include "chrome/browser/ui/browser_commands.h" | 24 #include "chrome/browser/ui/browser_commands.h" |
30 #include "chrome/browser/ui/browser_finder.h" | 25 #include "chrome/browser/ui/browser_finder.h" |
31 #include "chrome/browser/ui/browser_window.h" | 26 #include "chrome/browser/ui/browser_window.h" |
32 #include "chrome/browser/ui/global_error/global_error.h" | 27 #include "chrome/browser/ui/global_error/global_error.h" |
33 #include "chrome/browser/ui/global_error/global_error_service.h" | 28 #include "chrome/browser/ui/global_error/global_error_service.h" |
34 #include "chrome/browser/ui/global_error/global_error_service_factory.h" | 29 #include "chrome/browser/ui/global_error/global_error_service_factory.h" |
35 #include "chrome/browser/ui/search/search.h" | 30 #include "chrome/browser/ui/search/search.h" |
36 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 31 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
(...skipping 12 matching lines...) Expand all Loading... |
49 #include "content/public/browser/notification_types.h" | 44 #include "content/public/browser/notification_types.h" |
50 #include "content/public/browser/user_metrics.h" | 45 #include "content/public/browser/user_metrics.h" |
51 #include "content/public/browser/web_contents.h" | 46 #include "content/public/browser/web_contents.h" |
52 #include "grit/chromium_strings.h" | 47 #include "grit/chromium_strings.h" |
53 #include "grit/generated_resources.h" | 48 #include "grit/generated_resources.h" |
54 #include "grit/theme_resources.h" | 49 #include "grit/theme_resources.h" |
55 #include "ui/base/l10n/l10n_util.h" | 50 #include "ui/base/l10n/l10n_util.h" |
56 #include "ui/base/layout.h" | 51 #include "ui/base/layout.h" |
57 #include "ui/base/models/button_menu_item_model.h" | 52 #include "ui/base/models/button_menu_item_model.h" |
58 #include "ui/base/resource/resource_bundle.h" | 53 #include "ui/base/resource/resource_bundle.h" |
59 #include "ui/base/text/text_elider.h" | |
60 #include "ui/gfx/font.h" | |
61 #include "ui/gfx/image/image.h" | 54 #include "ui/gfx/image/image.h" |
62 #include "ui/gfx/image/image_skia.h" | 55 #include "ui/gfx/image/image_skia.h" |
63 | 56 |
64 #if defined(OS_WIN) | 57 #if defined(OS_WIN) |
65 #include "base/win/metro.h" | 58 #include "base/win/metro.h" |
66 #include "base/win/windows_version.h" | 59 #include "base/win/windows_version.h" |
67 #include "chrome/browser/enumerate_modules_model_win.h" | 60 #include "chrome/browser/enumerate_modules_model_win.h" |
68 #include "chrome/browser/ui/metro_pin_tab_helper_win.h" | 61 #include "chrome/browser/ui/metro_pin_tab_helper_win.h" |
69 #include "win8/util/win8_util.h" | 62 #include "win8/util/win8_util.h" |
70 #endif | 63 #endif |
71 | 64 |
72 #if defined(USE_ASH) | 65 #if defined(USE_ASH) |
73 #include "ash/shell.h" | 66 #include "ash/shell.h" |
74 #endif | 67 #endif |
75 | 68 |
76 using content::HostZoomMap; | 69 using content::HostZoomMap; |
77 using content::UserMetricsAction; | 70 using content::UserMetricsAction; |
78 using content::WebContents; | 71 using content::WebContents; |
79 | 72 |
80 namespace { | |
81 // Maximum width of a username - we trim emails that are wider than this so | |
82 // the wrench menu doesn't get ridiculously wide. | |
83 const int kUsernameMaxWidth = 200; | |
84 } // namespace | |
85 | |
86 //////////////////////////////////////////////////////////////////////////////// | 73 //////////////////////////////////////////////////////////////////////////////// |
87 // EncodingMenuModel | 74 // EncodingMenuModel |
88 | 75 |
89 EncodingMenuModel::EncodingMenuModel(Browser* browser) | 76 EncodingMenuModel::EncodingMenuModel(Browser* browser) |
90 : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)), | 77 : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)), |
91 browser_(browser) { | 78 browser_(browser) { |
92 Build(); | 79 Build(); |
93 } | 80 } |
94 | 81 |
95 EncodingMenuModel::~EncodingMenuModel() { | 82 EncodingMenuModel::~EncodingMenuModel() { |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 } | 273 } |
287 #endif | 274 #endif |
288 case IDC_VIEW_BACKGROUND_PAGES: { | 275 case IDC_VIEW_BACKGROUND_PAGES: { |
289 string16 num_background_pages = base::FormatNumber( | 276 string16 num_background_pages = base::FormatNumber( |
290 TaskManager::GetBackgroundPageCount()); | 277 TaskManager::GetBackgroundPageCount()); |
291 return l10n_util::GetStringFUTF16(IDS_VIEW_BACKGROUND_PAGES, | 278 return l10n_util::GetStringFUTF16(IDS_VIEW_BACKGROUND_PAGES, |
292 num_background_pages); | 279 num_background_pages); |
293 } | 280 } |
294 case IDC_UPGRADE_DIALOG: | 281 case IDC_UPGRADE_DIALOG: |
295 return l10n_util::GetStringUTF16(IDS_UPDATE_NOW); | 282 return l10n_util::GetStringUTF16(IDS_UPDATE_NOW); |
296 case IDC_SHOW_SIGNIN: { | 283 case IDC_SHOW_SIGNIN: |
297 GlobalError* error = GetActiveSignedInServiceError(); | 284 return signin_ui_util::GetSigninMenuLabel( |
298 if (error) | 285 browser_->profile()->GetOriginalProfile()); |
299 return error->MenuItemLabel(); | |
300 | |
301 // No errors, so just display the signed in user, if any. | |
302 Profile* profile = browser_->profile()->GetOriginalProfile(); | |
303 ProfileSyncService* service = profile->IsSyncAccessible() ? | |
304 ProfileSyncServiceFactory::GetForProfile(profile) : NULL; | |
305 | |
306 // Even if the user is signed in, don't display the "signed in as..." | |
307 // label if we're still setting up sync. | |
308 if (!service || service->HasSyncSetupCompleted()) { | |
309 SigninManager* signin_manager = | |
310 SigninManagerFactory::GetForProfile(profile); | |
311 std::string username = signin_manager->GetAuthenticatedUsername(); | |
312 if (!username.empty() && !signin_manager->AuthInProgress()) { | |
313 string16 elided_username = ui::ElideEmail(UTF8ToUTF16(username), | |
314 gfx::Font(), | |
315 kUsernameMaxWidth); | |
316 return l10n_util::GetStringFUTF16(IDS_SYNC_MENU_SYNCED_LABEL, | |
317 elided_username); | |
318 } | |
319 } | |
320 return l10n_util::GetStringFUTF16(IDS_SYNC_MENU_PRE_SYNCED_LABEL, | |
321 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME)); | |
322 } | |
323 default: | 286 default: |
324 NOTREACHED(); | 287 NOTREACHED(); |
325 return string16(); | 288 return string16(); |
326 } | 289 } |
327 } | 290 } |
328 | 291 |
329 bool WrenchMenuModel::GetIconForCommandId(int command_id, | 292 bool WrenchMenuModel::GetIconForCommandId(int command_id, |
330 gfx::Image* icon) const { | 293 gfx::Image* icon) const { |
331 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 294 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
332 switch (command_id) { | 295 switch (command_id) { |
333 case IDC_UPGRADE_DIALOG: { | 296 case IDC_UPGRADE_DIALOG: { |
334 if (UpgradeDetector::GetInstance()->notify_upgrade()) { | 297 if (UpgradeDetector::GetInstance()->notify_upgrade()) { |
335 *icon = rb.GetNativeImageNamed( | 298 *icon = rb.GetNativeImageNamed( |
336 UpgradeDetector::GetInstance()->GetIconResourceID( | 299 UpgradeDetector::GetInstance()->GetIconResourceID( |
337 UpgradeDetector::UPGRADE_ICON_TYPE_MENU_ICON)); | 300 UpgradeDetector::UPGRADE_ICON_TYPE_MENU_ICON)); |
338 return true; | 301 return true; |
339 } | 302 } |
340 return false; | 303 return false; |
341 } | 304 } |
342 case IDC_SHOW_SIGNIN: { | 305 case IDC_SHOW_SIGNIN: { |
343 GlobalError* error = GetActiveSignedInServiceError(); | 306 GlobalError* error = signin_ui_util::GetSignedInServiceError( |
| 307 browser_->profile()->GetOriginalProfile()); |
344 if (error) { | 308 if (error) { |
345 int icon_id = error->MenuItemIconResourceID(); | 309 int icon_id = error->MenuItemIconResourceID(); |
346 if (icon_id) { | 310 if (icon_id) { |
347 *icon = rb.GetNativeImageNamed(icon_id); | 311 *icon = rb.GetNativeImageNamed(icon_id); |
348 return true; | 312 return true; |
349 } | 313 } |
350 } | 314 } |
351 return false; | 315 return false; |
352 } | 316 } |
353 default: | 317 default: |
354 break; | 318 break; |
355 } | 319 } |
356 return false; | 320 return false; |
357 } | 321 } |
358 | 322 |
359 void WrenchMenuModel::ExecuteCommand(int command_id) { | 323 void WrenchMenuModel::ExecuteCommand(int command_id) { |
360 GlobalError* error = GlobalErrorServiceFactory::GetForProfile( | 324 GlobalError* error = GlobalErrorServiceFactory::GetForProfile( |
361 browser_->profile())->GetGlobalErrorByMenuItemCommandID(command_id); | 325 browser_->profile())->GetGlobalErrorByMenuItemCommandID(command_id); |
362 if (error) { | 326 if (error) { |
363 error->ExecuteMenuItem(browser_); | 327 error->ExecuteMenuItem(browser_); |
364 return; | 328 return; |
365 } | 329 } |
366 | 330 |
367 if (command_id == IDC_SHOW_SIGNIN) { | 331 if (command_id == IDC_SHOW_SIGNIN) { |
368 // If a custom error message is being shown, display it. | 332 // If a custom error message is being shown, handle it. |
369 GlobalError* error = GetActiveSignedInServiceError(); | 333 GlobalError* error = signin_ui_util::GetSignedInServiceError( |
| 334 browser_->profile()->GetOriginalProfile()); |
370 if (error) { | 335 if (error) { |
371 error->ExecuteMenuItem(browser_); | 336 error->ExecuteMenuItem(browser_); |
372 return; | 337 return; |
373 } | 338 } |
374 } | 339 } |
375 | 340 |
376 if (command_id == IDC_HELP_PAGE_VIA_MENU) | 341 if (command_id == IDC_HELP_PAGE_VIA_MENU) |
377 content::RecordAction(UserMetricsAction("ShowHelpTabViaWrenchMenu")); | 342 content::RecordAction(UserMetricsAction("ShowHelpTabViaWrenchMenu")); |
378 | 343 |
379 chrome::ExecuteCommand(browser_, command_id); | 344 chrome::ExecuteCommand(browser_, command_id); |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
562 | 527 |
563 if (is_new_menu) | 528 if (is_new_menu) |
564 CreateZoomMenu(is_new_menu); | 529 CreateZoomMenu(is_new_menu); |
565 else | 530 else |
566 AddSeparator(ui::NORMAL_SEPARATOR); | 531 AddSeparator(ui::NORMAL_SEPARATOR); |
567 | 532 |
568 AddItemWithStringId(IDC_SHOW_HISTORY, IDS_SHOW_HISTORY); | 533 AddItemWithStringId(IDC_SHOW_HISTORY, IDS_SHOW_HISTORY); |
569 AddItemWithStringId(IDC_SHOW_DOWNLOADS, IDS_SHOW_DOWNLOADS); | 534 AddItemWithStringId(IDC_SHOW_DOWNLOADS, IDS_SHOW_DOWNLOADS); |
570 AddSeparator(ui::NORMAL_SEPARATOR); | 535 AddSeparator(ui::NORMAL_SEPARATOR); |
571 | 536 |
572 // TODO(atwilson): Remove call to IsSyncAccessible() once we fully support | 537 #if !defined(OS_CHROMEOS) |
573 // signin while sync is disabled. | 538 // No "Sign in to Chromium..." menu item on ChromeOS. |
574 if (browser_defaults::kShowSigninMenuItem && | 539 const string16 short_product_name = |
575 browser_->profile()->GetOriginalProfile()->IsSyncAccessible()) { | 540 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME); |
576 const string16 short_product_name = | 541 AddItem(IDC_SHOW_SYNC_SETUP, l10n_util::GetStringFUTF16( |
577 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME); | 542 IDS_SYNC_MENU_PRE_SYNCED_LABEL, short_product_name)); |
578 AddItem(IDC_SHOW_SYNC_SETUP, l10n_util::GetStringFUTF16( | 543 AddSeparator(ui::NORMAL_SEPARATOR); |
579 IDS_SYNC_MENU_PRE_SYNCED_LABEL, short_product_name)); | 544 #endif |
580 AddSeparator(ui::NORMAL_SEPARATOR); | |
581 } | |
582 | 545 |
583 AddItemWithStringId(IDC_OPTIONS, IDS_SETTINGS); | 546 AddItemWithStringId(IDC_OPTIONS, IDS_SETTINGS); |
584 | 547 |
585 #if defined(OS_CHROMEOS) | 548 #if defined(OS_CHROMEOS) |
586 if (CommandLine::ForCurrentProcess()->HasSwitch( | 549 if (CommandLine::ForCurrentProcess()->HasSwitch( |
587 switches::kEnableRequestTabletSite)) | 550 switches::kEnableRequestTabletSite)) |
588 AddCheckItemWithStringId(IDC_TOGGLE_REQUEST_TABLET_SITE, | 551 AddCheckItemWithStringId(IDC_TOGGLE_REQUEST_TABLET_SITE, |
589 IDS_TOGGLE_REQUEST_TABLET_SITE); | 552 IDS_TOGGLE_REQUEST_TABLET_SITE); |
590 #endif | 553 #endif |
591 | 554 |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
727 bool enable_decrement = false; | 690 bool enable_decrement = false; |
728 int zoom_percent = 100; | 691 int zoom_percent = 100; |
729 if (browser_->tab_strip_model()->GetActiveWebContents()) { | 692 if (browser_->tab_strip_model()->GetActiveWebContents()) { |
730 zoom_percent = | 693 zoom_percent = |
731 browser_->tab_strip_model()->GetActiveWebContents()->GetZoomPercent( | 694 browser_->tab_strip_model()->GetActiveWebContents()->GetZoomPercent( |
732 &enable_increment, &enable_decrement); | 695 &enable_increment, &enable_decrement); |
733 } | 696 } |
734 zoom_label_ = l10n_util::GetStringFUTF16( | 697 zoom_label_ = l10n_util::GetStringFUTF16( |
735 IDS_ZOOM_PERCENT, base::IntToString16(zoom_percent)); | 698 IDS_ZOOM_PERCENT, base::IntToString16(zoom_percent)); |
736 } | 699 } |
737 | |
738 GlobalError* WrenchMenuModel::GetActiveSignedInServiceError() const { | |
739 Profile* profile = browser_->profile()->GetOriginalProfile(); | |
740 // Auth errors have the highest priority - after that, individual service | |
741 // errors. | |
742 SigninManager* signin_manager = SigninManagerFactory::GetForProfile(profile); | |
743 SigninGlobalError* signin_error = signin_manager->signin_global_error(); | |
744 if (signin_error && signin_error->HasBadge()) | |
745 return signin_error; | |
746 | |
747 // No auth error - now try other services. Currently the list is just hard- | |
748 // coded but in the future if we add more we can create some kind of | |
749 // registration framework. | |
750 if (profile->IsSyncAccessible()) { | |
751 ProfileSyncService* service = | |
752 ProfileSyncServiceFactory::GetForProfile(profile); | |
753 SyncGlobalError* error = service->sync_global_error(); | |
754 if (error && error->HasBadge()) | |
755 return error; | |
756 } | |
757 return NULL; | |
758 } | |
OLD | NEW |