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