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

Side by Side Diff: chrome/browser/ui/toolbar/wrench_menu_model.cc

Issue 12077030: Allow signin to continue even if sync is disabled by policy. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed mac tests. Created 7 years, 10 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) 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_tabstrip.h" 26 #include "chrome/browser/ui/browser_tabstrip.h"
32 #include "chrome/browser/ui/browser_window.h" 27 #include "chrome/browser/ui/browser_window.h"
33 #include "chrome/browser/ui/global_error/global_error.h" 28 #include "chrome/browser/ui/global_error/global_error.h"
34 #include "chrome/browser/ui/global_error/global_error_service.h" 29 #include "chrome/browser/ui/global_error/global_error_service.h"
35 #include "chrome/browser/ui/global_error/global_error_service_factory.h" 30 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
36 #include "chrome/browser/ui/search/search.h" 31 #include "chrome/browser/ui/search/search.h"
(...skipping 13 matching lines...) Expand all
50 #include "content/public/browser/notification_types.h" 45 #include "content/public/browser/notification_types.h"
51 #include "content/public/browser/user_metrics.h" 46 #include "content/public/browser/user_metrics.h"
52 #include "content/public/browser/web_contents.h" 47 #include "content/public/browser/web_contents.h"
53 #include "grit/chromium_strings.h" 48 #include "grit/chromium_strings.h"
54 #include "grit/generated_resources.h" 49 #include "grit/generated_resources.h"
55 #include "grit/theme_resources.h" 50 #include "grit/theme_resources.h"
56 #include "ui/base/l10n/l10n_util.h" 51 #include "ui/base/l10n/l10n_util.h"
57 #include "ui/base/layout.h" 52 #include "ui/base/layout.h"
58 #include "ui/base/models/button_menu_item_model.h" 53 #include "ui/base/models/button_menu_item_model.h"
59 #include "ui/base/resource/resource_bundle.h" 54 #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" 55 #include "ui/gfx/image/image.h"
63 #include "ui/gfx/image/image_skia.h" 56 #include "ui/gfx/image/image_skia.h"
64 57
65 #if defined(OS_WIN) 58 #if defined(OS_WIN)
66 #include "base/win/metro.h" 59 #include "base/win/metro.h"
67 #include "base/win/windows_version.h" 60 #include "base/win/windows_version.h"
68 #include "chrome/browser/enumerate_modules_model_win.h" 61 #include "chrome/browser/enumerate_modules_model_win.h"
69 #include "chrome/browser/ui/metro_pin_tab_helper_win.h" 62 #include "chrome/browser/ui/metro_pin_tab_helper_win.h"
70 #include "win8/util/win8_util.h" 63 #include "win8/util/win8_util.h"
71 #endif 64 #endif
72 65
73 #if defined(USE_ASH) 66 #if defined(USE_ASH)
74 #include "ash/shell.h" 67 #include "ash/shell.h"
75 #endif 68 #endif
76 69
77 using content::HostZoomMap; 70 using content::HostZoomMap;
78 using content::UserMetricsAction; 71 using content::UserMetricsAction;
79 using content::WebContents; 72 using content::WebContents;
80 73
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 //////////////////////////////////////////////////////////////////////////////// 74 ////////////////////////////////////////////////////////////////////////////////
88 // EncodingMenuModel 75 // EncodingMenuModel
89 76
90 EncodingMenuModel::EncodingMenuModel(Browser* browser) 77 EncodingMenuModel::EncodingMenuModel(Browser* browser)
91 : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)), 78 : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)),
92 browser_(browser) { 79 browser_(browser) {
93 Build(); 80 Build();
94 } 81 }
95 82
96 EncodingMenuModel::~EncodingMenuModel() { 83 EncodingMenuModel::~EncodingMenuModel() {
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
285 } 272 }
286 #endif 273 #endif
287 case IDC_VIEW_BACKGROUND_PAGES: { 274 case IDC_VIEW_BACKGROUND_PAGES: {
288 string16 num_background_pages = base::FormatNumber( 275 string16 num_background_pages = base::FormatNumber(
289 TaskManager::GetBackgroundPageCount()); 276 TaskManager::GetBackgroundPageCount());
290 return l10n_util::GetStringFUTF16(IDS_VIEW_BACKGROUND_PAGES, 277 return l10n_util::GetStringFUTF16(IDS_VIEW_BACKGROUND_PAGES,
291 num_background_pages); 278 num_background_pages);
292 } 279 }
293 case IDC_UPGRADE_DIALOG: 280 case IDC_UPGRADE_DIALOG:
294 return l10n_util::GetStringUTF16(IDS_UPDATE_NOW); 281 return l10n_util::GetStringUTF16(IDS_UPDATE_NOW);
295 case IDC_SHOW_SIGNIN: { 282 case IDC_SHOW_SIGNIN:
296 GlobalError* error = GetActiveSignedInServiceError(); 283 return signin_ui_util::GetSigninMenuLabel(
297 if (error) 284 browser_->profile()->GetOriginalProfile());
298 return error->MenuItemLabel();
299
300 // No errors, so just display the signed in user, if any.
301 Profile* profile = browser_->profile()->GetOriginalProfile();
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);
315 return l10n_util::GetStringFUTF16(IDS_SYNC_MENU_SYNCED_LABEL,
316 elided_username);
317 }
318 }
319 return l10n_util::GetStringFUTF16(IDS_SYNC_MENU_PRE_SYNCED_LABEL,
320 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME));
321 }
322 default: 285 default:
323 NOTREACHED(); 286 NOTREACHED();
324 return string16(); 287 return string16();
325 } 288 }
326 } 289 }
327 290
328 bool WrenchMenuModel::GetIconForCommandId(int command_id, 291 bool WrenchMenuModel::GetIconForCommandId(int command_id,
329 gfx::Image* icon) const { 292 gfx::Image* icon) const {
330 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 293 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
331 switch (command_id) { 294 switch (command_id) {
332 case IDC_UPGRADE_DIALOG: { 295 case IDC_UPGRADE_DIALOG: {
333 if (UpgradeDetector::GetInstance()->notify_upgrade()) { 296 if (UpgradeDetector::GetInstance()->notify_upgrade()) {
334 *icon = rb.GetNativeImageNamed( 297 *icon = rb.GetNativeImageNamed(
335 UpgradeDetector::GetInstance()->GetIconResourceID( 298 UpgradeDetector::GetInstance()->GetIconResourceID(
336 UpgradeDetector::UPGRADE_ICON_TYPE_MENU_ICON)); 299 UpgradeDetector::UPGRADE_ICON_TYPE_MENU_ICON));
337 return true; 300 return true;
338 } 301 }
339 return false; 302 return false;
340 } 303 }
341 case IDC_SHOW_SIGNIN: { 304 case IDC_SHOW_SIGNIN: {
342 GlobalError* error = GetActiveSignedInServiceError(); 305 GlobalError* error = signin_ui_util::GetSignedInServiceError(
306 browser_->profile()->GetOriginalProfile());
343 if (error) { 307 if (error) {
344 int icon_id = error->MenuItemIconResourceID(); 308 int icon_id = error->MenuItemIconResourceID();
345 if (icon_id) { 309 if (icon_id) {
346 *icon = rb.GetNativeImageNamed(icon_id); 310 *icon = rb.GetNativeImageNamed(icon_id);
347 return true; 311 return true;
348 } 312 }
349 } 313 }
350 return false; 314 return false;
351 } 315 }
352 default: 316 default:
353 break; 317 break;
354 } 318 }
355 return false; 319 return false;
356 } 320 }
357 321
358 void WrenchMenuModel::ExecuteCommand(int command_id) { 322 void WrenchMenuModel::ExecuteCommand(int command_id) {
359 GlobalError* error = GlobalErrorServiceFactory::GetForProfile( 323 GlobalError* error = GlobalErrorServiceFactory::GetForProfile(
360 browser_->profile())->GetGlobalErrorByMenuItemCommandID(command_id); 324 browser_->profile())->GetGlobalErrorByMenuItemCommandID(command_id);
361 if (error) { 325 if (error) {
362 error->ExecuteMenuItem(browser_); 326 error->ExecuteMenuItem(browser_);
363 return; 327 return;
364 } 328 }
365 329
366 if (command_id == IDC_SHOW_SIGNIN) { 330 if (command_id == IDC_SHOW_SIGNIN) {
367 // If a custom error message is being shown, display it. 331 // If a custom error message is being shown, handle it.
368 GlobalError* error = GetActiveSignedInServiceError(); 332 GlobalError* error = signin_ui_util::GetSignedInServiceError(
333 browser_->profile()->GetOriginalProfile());
369 if (error) { 334 if (error) {
370 error->ExecuteMenuItem(browser_); 335 error->ExecuteMenuItem(browser_);
371 return; 336 return;
372 } 337 }
373 } 338 }
374 339
375 if (command_id == IDC_HELP_PAGE_VIA_MENU) 340 if (command_id == IDC_HELP_PAGE_VIA_MENU)
376 content::RecordAction(UserMetricsAction("ShowHelpTabViaWrenchMenu")); 341 content::RecordAction(UserMetricsAction("ShowHelpTabViaWrenchMenu"));
377 342
378 chrome::ExecuteCommand(browser_, command_id); 343 chrome::ExecuteCommand(browser_, command_id);
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
561 526
562 if (is_new_menu) 527 if (is_new_menu)
563 CreateZoomMenu(is_new_menu); 528 CreateZoomMenu(is_new_menu);
564 else 529 else
565 AddSeparator(ui::NORMAL_SEPARATOR); 530 AddSeparator(ui::NORMAL_SEPARATOR);
566 531
567 AddItemWithStringId(IDC_SHOW_HISTORY, IDS_SHOW_HISTORY); 532 AddItemWithStringId(IDC_SHOW_HISTORY, IDS_SHOW_HISTORY);
568 AddItemWithStringId(IDC_SHOW_DOWNLOADS, IDS_SHOW_DOWNLOADS); 533 AddItemWithStringId(IDC_SHOW_DOWNLOADS, IDS_SHOW_DOWNLOADS);
569 AddSeparator(ui::NORMAL_SEPARATOR); 534 AddSeparator(ui::NORMAL_SEPARATOR);
570 535
571 // TODO(atwilson): Remove call to IsSyncAccessible() once we fully support 536 if (browser_defaults::kShowSigninMenuItem) {
572 // signin while sync is disabled.
573 if (browser_defaults::kShowSigninMenuItem &&
574 browser_->profile()->GetOriginalProfile()->IsSyncAccessible()) {
575 const string16 short_product_name = 537 const string16 short_product_name =
576 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME); 538 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME);
577 AddItem(IDC_SHOW_SYNC_SETUP, l10n_util::GetStringFUTF16( 539 AddItem(IDC_SHOW_SYNC_SETUP, l10n_util::GetStringFUTF16(
578 IDS_SYNC_MENU_PRE_SYNCED_LABEL, short_product_name)); 540 IDS_SYNC_MENU_PRE_SYNCED_LABEL, short_product_name));
579 AddSeparator(ui::NORMAL_SEPARATOR); 541 AddSeparator(ui::NORMAL_SEPARATOR);
580 } 542 }
581 543
582 AddItemWithStringId(IDC_OPTIONS, IDS_SETTINGS); 544 AddItemWithStringId(IDC_OPTIONS, IDS_SETTINGS);
583 545
584 #if defined(OS_CHROMEOS) 546 #if defined(OS_CHROMEOS)
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
725 bool enable_increment = false; 687 bool enable_increment = false;
726 bool enable_decrement = false; 688 bool enable_decrement = false;
727 int zoom_percent = 100; 689 int zoom_percent = 100;
728 if (chrome::GetActiveWebContents(browser_)) { 690 if (chrome::GetActiveWebContents(browser_)) {
729 zoom_percent = chrome::GetActiveWebContents(browser_)->GetZoomPercent( 691 zoom_percent = chrome::GetActiveWebContents(browser_)->GetZoomPercent(
730 &enable_increment, &enable_decrement); 692 &enable_increment, &enable_decrement);
731 } 693 }
732 zoom_label_ = l10n_util::GetStringFUTF16( 694 zoom_label_ = l10n_util::GetStringFUTF16(
733 IDS_ZOOM_PERCENT, base::IntToString16(zoom_percent)); 695 IDS_ZOOM_PERCENT, base::IntToString16(zoom_percent));
734 } 696 }
735
736 GlobalError* WrenchMenuModel::GetActiveSignedInServiceError() const {
737 Profile* profile = browser_->profile()->GetOriginalProfile();
738 // Auth errors have the highest priority - after that, individual service
739 // errors.
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;
756 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698