- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / sync / sync_global_error.cc
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/sync/sync_global_error.h"
6
7 #include "chrome/app/chrome_command_ids.h"
8 #include "chrome/browser/sync/profile_sync_service.h"
9 #include "chrome/browser/sync/profile_sync_service_observer.h"
10 #include "chrome/browser/sync/sync_ui_util.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/browser_commands.h"
13 #include "chrome/browser/ui/chrome_pages.h"
14 #include "chrome/browser/ui/global_error/global_error_service.h"
15 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
16 #include "chrome/browser/ui/webui/signin/login_ui_service.h"
17 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
18 #include "chrome/common/url_constants.h"
19 #include "google_apis/gaia/google_service_auth_error.h"
20 #include "grit/chromium_strings.h"
21 #include "grit/generated_resources.h"
22 #include "ui/base/l10n/l10n_util.h"
23
24 SyncGlobalError::SyncGlobalError(ProfileSyncService* service,
25                                  SigninManagerBase* signin)
26     : service_(service),
27       signin_(signin) {
28   DCHECK(service_);
29   DCHECK(signin_);
30   OnStateChanged();
31 }
32
33 SyncGlobalError::~SyncGlobalError() {
34 }
35
36 bool SyncGlobalError::HasMenuItem() {
37   return !menu_label_.empty();
38 }
39
40 int SyncGlobalError::MenuItemCommandID() {
41   return IDC_SHOW_SIGNIN_ERROR;
42 }
43
44 string16 SyncGlobalError::MenuItemLabel() {
45   return menu_label_;
46 }
47
48 void SyncGlobalError::ExecuteMenuItem(Browser* browser) {
49   LoginUIService* login_ui = LoginUIServiceFactory::GetForProfile(
50       service_->profile());
51   if (login_ui->current_login_ui()) {
52     login_ui->current_login_ui()->FocusUI();
53     return;
54   }
55   // Need to navigate to the settings page and display the UI.
56   chrome::ShowSettingsSubPage(browser, chrome::kSyncSetupSubPage);
57 }
58
59 bool SyncGlobalError::HasBubbleView() {
60   return !bubble_message_.empty() && !bubble_accept_label_.empty();
61 }
62
63 string16 SyncGlobalError::GetBubbleViewTitle() {
64   return l10n_util::GetStringUTF16(IDS_SYNC_ERROR_BUBBLE_VIEW_TITLE);
65 }
66
67 std::vector<string16> SyncGlobalError::GetBubbleViewMessages() {
68   return std::vector<string16>(1, bubble_message_);
69 }
70
71 string16 SyncGlobalError::GetBubbleViewAcceptButtonLabel() {
72   return bubble_accept_label_;
73 }
74
75 string16 SyncGlobalError::GetBubbleViewCancelButtonLabel() {
76   return string16();
77 }
78
79 void SyncGlobalError::OnBubbleViewDidClose(Browser* browser) {
80 }
81
82 void SyncGlobalError::BubbleViewAcceptButtonPressed(Browser* browser) {
83   ExecuteMenuItem(browser);
84 }
85
86 void SyncGlobalError::BubbleViewCancelButtonPressed(Browser* browser) {
87   NOTREACHED();
88 }
89
90 void SyncGlobalError::OnStateChanged() {
91   string16 menu_label;
92   string16 bubble_message;
93   string16 bubble_accept_label;
94   sync_ui_util::GetStatusLabelsForSyncGlobalError(
95       service_, *signin_, &menu_label, &bubble_message, &bubble_accept_label);
96
97   // All the labels should be empty or all of them non-empty.
98   DCHECK((menu_label.empty() && bubble_message.empty() &&
99           bubble_accept_label.empty()) ||
100          (!menu_label.empty() && !bubble_message.empty() &&
101           !bubble_accept_label.empty()));
102
103   if (menu_label != menu_label_ || bubble_message != bubble_message_ ||
104       bubble_accept_label != bubble_accept_label_) {
105     menu_label_ = menu_label;
106     bubble_message_ = bubble_message;
107     bubble_accept_label_ = bubble_accept_label;
108
109     // Profile can be NULL during tests.
110     Profile* profile = service_->profile();
111     if (profile) {
112       GlobalErrorServiceFactory::GetForProfile(
113           profile)->NotifyErrorsChanged(this);
114     }
115   }
116 }