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_manager.h" | 22 #include "chrome/browser/signin/signin_manager.h" |
22 #include "chrome/browser/signin/signin_manager_factory.h" | 23 #include "chrome/browser/signin/signin_manager_factory.h" |
23 #include "chrome/browser/sync/profile_sync_service.h" | 24 #include "chrome/browser/sync/profile_sync_service.h" |
24 #include "chrome/browser/sync/profile_sync_service_factory.h" | 25 #include "chrome/browser/sync/profile_sync_service_factory.h" |
25 #include "chrome/browser/sync/sync_global_error.h" | 26 #include "chrome/browser/sync/sync_global_error.h" |
26 #include "chrome/browser/sync/sync_ui_util.h" | |
27 #include "chrome/browser/task_manager/task_manager.h" | 27 #include "chrome/browser/task_manager/task_manager.h" |
28 #include "chrome/browser/ui/browser.h" | 28 #include "chrome/browser/ui/browser.h" |
29 #include "chrome/browser/ui/browser_commands.h" | 29 #include "chrome/browser/ui/browser_commands.h" |
30 #include "chrome/browser/ui/browser_finder.h" | 30 #include "chrome/browser/ui/browser_finder.h" |
31 #include "chrome/browser/ui/browser_tabstrip.h" | 31 #include "chrome/browser/ui/browser_tabstrip.h" |
32 #include "chrome/browser/ui/browser_window.h" | 32 #include "chrome/browser/ui/browser_window.h" |
33 #include "chrome/browser/ui/global_error/global_error.h" | 33 #include "chrome/browser/ui/global_error/global_error.h" |
34 #include "chrome/browser/ui/global_error/global_error_service.h" | 34 #include "chrome/browser/ui/global_error/global_error_service.h" |
35 #include "chrome/browser/ui/global_error/global_error_service_factory.h" | 35 #include "chrome/browser/ui/global_error/global_error_service_factory.h" |
36 #include "chrome/browser/ui/search/search.h" | 36 #include "chrome/browser/ui/search/search.h" |
(...skipping 13 matching lines...) Expand all Loading... |
50 #include "content/public/browser/notification_types.h" | 50 #include "content/public/browser/notification_types.h" |
51 #include "content/public/browser/user_metrics.h" | 51 #include "content/public/browser/user_metrics.h" |
52 #include "content/public/browser/web_contents.h" | 52 #include "content/public/browser/web_contents.h" |
53 #include "grit/chromium_strings.h" | 53 #include "grit/chromium_strings.h" |
54 #include "grit/generated_resources.h" | 54 #include "grit/generated_resources.h" |
55 #include "grit/theme_resources.h" | 55 #include "grit/theme_resources.h" |
56 #include "ui/base/l10n/l10n_util.h" | 56 #include "ui/base/l10n/l10n_util.h" |
57 #include "ui/base/layout.h" | 57 #include "ui/base/layout.h" |
58 #include "ui/base/models/button_menu_item_model.h" | 58 #include "ui/base/models/button_menu_item_model.h" |
59 #include "ui/base/resource/resource_bundle.h" | 59 #include "ui/base/resource/resource_bundle.h" |
| 60 #include "ui/base/text/text_elider.h" |
| 61 #include "ui/gfx/font.h" |
60 #include "ui/gfx/image/image.h" | 62 #include "ui/gfx/image/image.h" |
61 #include "ui/gfx/image/image_skia.h" | 63 #include "ui/gfx/image/image_skia.h" |
62 | 64 |
63 #if defined(OS_WIN) | 65 #if defined(OS_WIN) |
64 #include "base/win/metro.h" | 66 #include "base/win/metro.h" |
65 #include "base/win/windows_version.h" | 67 #include "base/win/windows_version.h" |
66 #include "chrome/browser/enumerate_modules_model_win.h" | 68 #include "chrome/browser/enumerate_modules_model_win.h" |
67 #include "chrome/browser/ui/metro_pin_tab_helper_win.h" | 69 #include "chrome/browser/ui/metro_pin_tab_helper_win.h" |
68 #include "win8/util/win8_util.h" | 70 #include "win8/util/win8_util.h" |
69 #endif | 71 #endif |
70 | 72 |
71 #if defined(USE_ASH) | 73 #if defined(USE_ASH) |
72 #include "ash/shell.h" | 74 #include "ash/shell.h" |
73 #endif | 75 #endif |
74 | 76 |
75 using content::HostZoomMap; | 77 using content::HostZoomMap; |
76 using content::UserMetricsAction; | 78 using content::UserMetricsAction; |
77 using content::WebContents; | 79 using content::WebContents; |
78 | 80 |
| 81 namespace { |
| 82 // Maximum width of a username - we trim emails that are wider than this so |
| 83 // the wrench menu doesn't get ridiculously wide. |
| 84 const int kUsernameMaxWidth = 200; |
| 85 } // namespace |
| 86 |
79 //////////////////////////////////////////////////////////////////////////////// | 87 //////////////////////////////////////////////////////////////////////////////// |
80 // EncodingMenuModel | 88 // EncodingMenuModel |
81 | 89 |
82 EncodingMenuModel::EncodingMenuModel(Browser* browser) | 90 EncodingMenuModel::EncodingMenuModel(Browser* browser) |
83 : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)), | 91 : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)), |
84 browser_(browser) { | 92 browser_(browser) { |
85 Build(); | 93 Build(); |
86 } | 94 } |
87 | 95 |
88 EncodingMenuModel::~EncodingMenuModel() { | 96 EncodingMenuModel::~EncodingMenuModel() { |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 | 250 |
243 bool WrenchMenuModel::IsItemForCommandIdDynamic(int command_id) const { | 251 bool WrenchMenuModel::IsItemForCommandIdDynamic(int command_id) const { |
244 return command_id == IDC_ZOOM_PERCENT_DISPLAY || | 252 return command_id == IDC_ZOOM_PERCENT_DISPLAY || |
245 #if defined(OS_MACOSX) | 253 #if defined(OS_MACOSX) |
246 command_id == IDC_FULLSCREEN || | 254 command_id == IDC_FULLSCREEN || |
247 #elif defined(OS_WIN) | 255 #elif defined(OS_WIN) |
248 command_id == IDC_PIN_TO_START_SCREEN || | 256 command_id == IDC_PIN_TO_START_SCREEN || |
249 #endif | 257 #endif |
250 command_id == IDC_VIEW_BACKGROUND_PAGES || | 258 command_id == IDC_VIEW_BACKGROUND_PAGES || |
251 command_id == IDC_UPGRADE_DIALOG || | 259 command_id == IDC_UPGRADE_DIALOG || |
252 command_id == IDC_SHOW_SYNC_SETUP; | 260 command_id == IDC_SHOW_SIGNIN; |
253 } | 261 } |
254 | 262 |
255 string16 WrenchMenuModel::GetLabelForCommandId(int command_id) const { | 263 string16 WrenchMenuModel::GetLabelForCommandId(int command_id) const { |
256 switch (command_id) { | 264 switch (command_id) { |
257 case IDC_ZOOM_PERCENT_DISPLAY: | 265 case IDC_ZOOM_PERCENT_DISPLAY: |
258 return zoom_label_; | 266 return zoom_label_; |
259 #if defined(OS_MACOSX) | 267 #if defined(OS_MACOSX) |
260 case IDC_FULLSCREEN: { | 268 case IDC_FULLSCREEN: { |
261 int string_id = IDS_ENTER_FULLSCREEN_MAC; // Default to Enter. | 269 int string_id = IDS_ENTER_FULLSCREEN_MAC; // Default to Enter. |
262 // Note: On startup, |window()| may be NULL. | 270 // Note: On startup, |window()| may be NULL. |
(...skipping 14 matching lines...) Expand all Loading... |
277 } | 285 } |
278 #endif | 286 #endif |
279 case IDC_VIEW_BACKGROUND_PAGES: { | 287 case IDC_VIEW_BACKGROUND_PAGES: { |
280 string16 num_background_pages = base::FormatNumber( | 288 string16 num_background_pages = base::FormatNumber( |
281 TaskManager::GetBackgroundPageCount()); | 289 TaskManager::GetBackgroundPageCount()); |
282 return l10n_util::GetStringFUTF16(IDS_VIEW_BACKGROUND_PAGES, | 290 return l10n_util::GetStringFUTF16(IDS_VIEW_BACKGROUND_PAGES, |
283 num_background_pages); | 291 num_background_pages); |
284 } | 292 } |
285 case IDC_UPGRADE_DIALOG: | 293 case IDC_UPGRADE_DIALOG: |
286 return l10n_util::GetStringUTF16(IDS_UPDATE_NOW); | 294 return l10n_util::GetStringUTF16(IDS_UPDATE_NOW); |
287 case IDC_SHOW_SYNC_SETUP: { | 295 case IDC_SHOW_SIGNIN: { |
288 ProfileSyncService* service = | 296 GlobalError* error = GetActiveSignedInServiceError(); |
289 ProfileSyncServiceFactory::GetInstance()->GetForProfile( | 297 if (error) |
290 browser_->profile()->GetOriginalProfile()); | |
291 SyncGlobalError* error = service->sync_global_error(); | |
292 if (error && error->HasCustomizedSyncMenuItem()) | |
293 return error->MenuItemLabel(); | 298 return error->MenuItemLabel(); |
294 if (service->HasSyncSetupCompleted()) { | 299 |
295 std::string username = browser_->profile()->GetPrefs()->GetString( | 300 // No errors, so just display the signed in user, if any. |
296 prefs::kGoogleServicesUsername); | 301 Profile* profile = browser_->profile()->GetOriginalProfile(); |
297 if (!username.empty()) { | 302 ProfileSyncService* service = profile->IsSyncAccessible() ? |
| 303 ProfileSyncServiceFactory::GetForProfile(profile) : NULL; |
| 304 |
| 305 // Even if the user is signed in, don't display the "signed in as..." |
| 306 // label if we're still setting up sync. |
| 307 if (!service || service->HasSyncSetupCompleted()) { |
| 308 SigninManager* signin_manager = |
| 309 SigninManagerFactory::GetForProfile(profile); |
| 310 std::string username = signin_manager->GetAuthenticatedUsername(); |
| 311 if (!username.empty() && !signin_manager->AuthInProgress()) { |
| 312 string16 elided_username = ui::ElideEmail(UTF8ToUTF16(username), |
| 313 gfx::Font(), |
| 314 kUsernameMaxWidth); |
298 return l10n_util::GetStringFUTF16(IDS_SYNC_MENU_SYNCED_LABEL, | 315 return l10n_util::GetStringFUTF16(IDS_SYNC_MENU_SYNCED_LABEL, |
299 UTF8ToUTF16(username)); | 316 elided_username); |
300 } | 317 } |
301 } | 318 } |
302 return l10n_util::GetStringFUTF16(IDS_SYNC_MENU_PRE_SYNCED_LABEL, | 319 return l10n_util::GetStringFUTF16(IDS_SYNC_MENU_PRE_SYNCED_LABEL, |
303 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME)); | 320 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME)); |
304 } | 321 } |
305 default: | 322 default: |
306 NOTREACHED(); | 323 NOTREACHED(); |
307 return string16(); | 324 return string16(); |
308 } | 325 } |
309 } | 326 } |
310 | 327 |
311 bool WrenchMenuModel::GetIconForCommandId(int command_id, | 328 bool WrenchMenuModel::GetIconForCommandId(int command_id, |
312 gfx::Image* icon) const { | 329 gfx::Image* icon) const { |
313 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 330 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
314 switch (command_id) { | 331 switch (command_id) { |
315 case IDC_UPGRADE_DIALOG: { | 332 case IDC_UPGRADE_DIALOG: { |
316 if (UpgradeDetector::GetInstance()->notify_upgrade()) { | 333 if (UpgradeDetector::GetInstance()->notify_upgrade()) { |
317 *icon = rb.GetNativeImageNamed( | 334 *icon = rb.GetNativeImageNamed( |
318 UpgradeDetector::GetInstance()->GetIconResourceID( | 335 UpgradeDetector::GetInstance()->GetIconResourceID( |
319 UpgradeDetector::UPGRADE_ICON_TYPE_MENU_ICON)); | 336 UpgradeDetector::UPGRADE_ICON_TYPE_MENU_ICON)); |
320 return true; | 337 return true; |
321 } | 338 } |
322 return false; | 339 return false; |
323 } | 340 } |
324 case IDC_SHOW_SYNC_SETUP: { | 341 case IDC_SHOW_SIGNIN: { |
325 ProfileSyncService* service = | 342 GlobalError* error = GetActiveSignedInServiceError(); |
326 ProfileSyncServiceFactory::GetInstance()->GetForProfile( | 343 if (error) { |
327 browser_->profile()->GetOriginalProfile()); | |
328 SyncGlobalError* error = service->sync_global_error(); | |
329 if (error && error->HasCustomizedSyncMenuItem()) { | |
330 int icon_id = error->MenuItemIconResourceID(); | 344 int icon_id = error->MenuItemIconResourceID(); |
331 if (icon_id) { | 345 if (icon_id) { |
332 *icon = rb.GetNativeImageNamed(icon_id); | 346 *icon = rb.GetNativeImageNamed(icon_id); |
333 return true; | 347 return true; |
334 } | 348 } |
335 } | 349 } |
336 return false; | 350 return false; |
337 } | 351 } |
338 default: | 352 default: |
339 break; | 353 break; |
340 } | 354 } |
341 return false; | 355 return false; |
342 } | 356 } |
343 | 357 |
344 void WrenchMenuModel::ExecuteCommand(int command_id) { | 358 void WrenchMenuModel::ExecuteCommand(int command_id) { |
345 GlobalError* error = GlobalErrorServiceFactory::GetForProfile( | 359 GlobalError* error = GlobalErrorServiceFactory::GetForProfile( |
346 browser_->profile())->GetGlobalErrorByMenuItemCommandID(command_id); | 360 browser_->profile())->GetGlobalErrorByMenuItemCommandID(command_id); |
347 if (error) { | 361 if (error) { |
348 error->ExecuteMenuItem(browser_); | 362 error->ExecuteMenuItem(browser_); |
349 return; | 363 return; |
350 } | 364 } |
351 | 365 |
352 if (command_id == IDC_SHOW_SYNC_SETUP) { | 366 if (command_id == IDC_SHOW_SIGNIN) { |
353 ProfileSyncService* service = | 367 // If a custom error message is being shown, display it. |
354 ProfileSyncServiceFactory::GetInstance()->GetForProfile( | 368 GlobalError* error = GetActiveSignedInServiceError(); |
355 browser_->profile()->GetOriginalProfile()); | 369 if (error) { |
356 SyncGlobalError* error = service->sync_global_error(); | |
357 if (error && error->HasCustomizedSyncMenuItem()) { | |
358 error->ExecuteMenuItem(browser_); | 370 error->ExecuteMenuItem(browser_); |
359 return; | 371 return; |
360 } | 372 } |
361 } | 373 } |
362 | 374 |
363 if (command_id == IDC_HELP_PAGE_VIA_MENU) | 375 if (command_id == IDC_HELP_PAGE_VIA_MENU) |
364 content::RecordAction(UserMetricsAction("ShowHelpTabViaWrenchMenu")); | 376 content::RecordAction(UserMetricsAction("ShowHelpTabViaWrenchMenu")); |
365 | 377 |
366 chrome::ExecuteCommand(browser_, command_id); | 378 chrome::ExecuteCommand(browser_, command_id); |
367 } | 379 } |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
549 | 561 |
550 if (is_new_menu) | 562 if (is_new_menu) |
551 CreateZoomMenu(is_new_menu); | 563 CreateZoomMenu(is_new_menu); |
552 else | 564 else |
553 AddSeparator(ui::NORMAL_SEPARATOR); | 565 AddSeparator(ui::NORMAL_SEPARATOR); |
554 | 566 |
555 AddItemWithStringId(IDC_SHOW_HISTORY, IDS_SHOW_HISTORY); | 567 AddItemWithStringId(IDC_SHOW_HISTORY, IDS_SHOW_HISTORY); |
556 AddItemWithStringId(IDC_SHOW_DOWNLOADS, IDS_SHOW_DOWNLOADS); | 568 AddItemWithStringId(IDC_SHOW_DOWNLOADS, IDS_SHOW_DOWNLOADS); |
557 AddSeparator(ui::NORMAL_SEPARATOR); | 569 AddSeparator(ui::NORMAL_SEPARATOR); |
558 | 570 |
559 if (browser_defaults::kShowSyncSetupMenuItem && | 571 // TODO(atwilson): Remove call to IsSyncAccessible() once we fully support |
| 572 // signin while sync is disabled. |
| 573 if (browser_defaults::kShowSigninMenuItem && |
560 browser_->profile()->GetOriginalProfile()->IsSyncAccessible()) { | 574 browser_->profile()->GetOriginalProfile()->IsSyncAccessible()) { |
561 const string16 short_product_name = | 575 const string16 short_product_name = |
562 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME); | 576 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME); |
563 AddItem(IDC_SHOW_SYNC_SETUP, l10n_util::GetStringFUTF16( | 577 AddItem(IDC_SHOW_SYNC_SETUP, l10n_util::GetStringFUTF16( |
564 IDS_SYNC_MENU_PRE_SYNCED_LABEL, short_product_name)); | 578 IDS_SYNC_MENU_PRE_SYNCED_LABEL, short_product_name)); |
565 AddSeparator(ui::NORMAL_SEPARATOR); | 579 AddSeparator(ui::NORMAL_SEPARATOR); |
566 } | 580 } |
567 | 581 |
568 AddItemWithStringId(IDC_OPTIONS, IDS_SETTINGS); | 582 AddItemWithStringId(IDC_OPTIONS, IDS_SETTINGS); |
569 | 583 |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
712 bool enable_decrement = false; | 726 bool enable_decrement = false; |
713 int zoom_percent = 100; | 727 int zoom_percent = 100; |
714 if (chrome::GetActiveWebContents(browser_)) { | 728 if (chrome::GetActiveWebContents(browser_)) { |
715 zoom_percent = chrome::GetActiveWebContents(browser_)->GetZoomPercent( | 729 zoom_percent = chrome::GetActiveWebContents(browser_)->GetZoomPercent( |
716 &enable_increment, &enable_decrement); | 730 &enable_increment, &enable_decrement); |
717 } | 731 } |
718 zoom_label_ = l10n_util::GetStringFUTF16( | 732 zoom_label_ = l10n_util::GetStringFUTF16( |
719 IDS_ZOOM_PERCENT, base::IntToString16(zoom_percent)); | 733 IDS_ZOOM_PERCENT, base::IntToString16(zoom_percent)); |
720 } | 734 } |
721 | 735 |
722 string16 WrenchMenuModel::GetSyncMenuLabel() const { | 736 GlobalError* WrenchMenuModel::GetActiveSignedInServiceError() const { |
723 Profile* profile = browser_->profile()->GetOriginalProfile(); | 737 Profile* profile = browser_->profile()->GetOriginalProfile(); |
724 return sync_ui_util::GetSyncMenuLabel( | 738 // Auth errors have the highest priority - after that, individual service |
725 ProfileSyncServiceFactory::GetForProfile(profile), | 739 // errors. |
726 *SigninManagerFactory::GetForProfile(profile)); | 740 SigninManager* signin_manager = SigninManagerFactory::GetForProfile(profile); |
| 741 SigninGlobalError* signin_error = signin_manager->signin_global_error(); |
| 742 if (signin_error && signin_error->HasBadge()) |
| 743 return signin_error; |
| 744 |
| 745 // No auth error - now try other services. Currently the list is just hard- |
| 746 // coded but in the future if we add more we can create some kind of |
| 747 // registration framework. |
| 748 if (profile->IsSyncAccessible()) { |
| 749 ProfileSyncService* service = |
| 750 ProfileSyncServiceFactory::GetForProfile(profile); |
| 751 SyncGlobalError* error = service->sync_global_error(); |
| 752 if (error && error->HasBadge()) |
| 753 return error; |
| 754 } |
| 755 return NULL; |
727 } | 756 } |
OLD | NEW |