OLD | NEW |
| (Empty) |
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/extensions/app_notify_channel_ui_impl.h" | |
6 | |
7 #include "base/utf_string_conversions.h" | |
8 #include "chrome/browser/api/infobars/confirm_infobar_delegate.h" | |
9 #include "chrome/browser/api/infobars/infobar_service.h" | |
10 #include "chrome/browser/profiles/profile.h" | |
11 #include "chrome/browser/signin/signin_manager.h" | |
12 #include "chrome/browser/signin/signin_manager_factory.h" | |
13 #include "chrome/browser/sync/profile_sync_service.h" | |
14 #include "chrome/browser/sync/profile_sync_service_factory.h" | |
15 #include "chrome/browser/ui/browser.h" | |
16 #include "chrome/browser/ui/browser_finder.h" | |
17 #include "chrome/browser/ui/chrome_pages.h" | |
18 #include "chrome/browser/ui/webui/signin/login_ui_service.h" | |
19 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h" | |
20 #include "chrome/common/url_constants.h" | |
21 #include "content/public/browser/notification_details.h" | |
22 #include "content/public/browser/notification_observer.h" | |
23 #include "content/public/browser/notification_registrar.h" | |
24 #include "content/public/browser/notification_service.h" | |
25 #include "content/public/browser/notification_source.h" | |
26 #include "content/public/browser/notification_types.h" | |
27 #include "grit/generated_resources.h" | |
28 #include "ui/base/l10n/l10n_util.h" | |
29 | |
30 namespace extensions { | |
31 | |
32 namespace { | |
33 | |
34 class AppNotifyChannelUIInfoBarDelegate : public ConfirmInfoBarDelegate { | |
35 public: | |
36 // Creates an app notify channel UI delegate and adds it to |infobar_service|. | |
37 static void Create(InfoBarService* infobar_service, | |
38 AppNotifyChannelUIImpl* creator, | |
39 const std::string& app_name); | |
40 | |
41 // ConfirmInfoBarDelegate. | |
42 virtual string16 GetMessageText() const OVERRIDE; | |
43 virtual string16 GetButtonLabel(InfoBarButton button) const OVERRIDE; | |
44 virtual bool Accept() OVERRIDE; | |
45 virtual bool Cancel() OVERRIDE; | |
46 virtual void InfoBarDismissed() OVERRIDE; | |
47 | |
48 private: | |
49 AppNotifyChannelUIInfoBarDelegate(AppNotifyChannelUIImpl* creator, | |
50 InfoBarService* infobar_service, | |
51 const std::string& app_name); | |
52 virtual ~AppNotifyChannelUIInfoBarDelegate(); | |
53 | |
54 AppNotifyChannelUIImpl* creator_; | |
55 std::string app_name_; | |
56 | |
57 DISALLOW_COPY_AND_ASSIGN(AppNotifyChannelUIInfoBarDelegate); | |
58 }; | |
59 | |
60 // static | |
61 void AppNotifyChannelUIInfoBarDelegate::Create(InfoBarService* infobar_service, | |
62 AppNotifyChannelUIImpl* creator, | |
63 const std::string& app_name) { | |
64 infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>( | |
65 new AppNotifyChannelUIInfoBarDelegate(creator, infobar_service, | |
66 app_name))); | |
67 } | |
68 | |
69 string16 AppNotifyChannelUIInfoBarDelegate::GetMessageText() const { | |
70 return l10n_util::GetStringFUTF16(IDS_APP_NOTIFICATION_NEED_SIGNIN, | |
71 UTF8ToUTF16(app_name_)); | |
72 } | |
73 | |
74 string16 AppNotifyChannelUIInfoBarDelegate::GetButtonLabel( | |
75 InfoBarButton button) const { | |
76 if (button == BUTTON_OK) { | |
77 return l10n_util::GetStringUTF16(IDS_APP_NOTIFICATION_NEED_SIGNIN_ACCEPT); | |
78 } else if (button == BUTTON_CANCEL) { | |
79 return l10n_util::GetStringUTF16(IDS_APP_NOTIFICATION_NEED_SIGNIN_CANCEL); | |
80 } else { | |
81 NOTREACHED(); | |
82 } | |
83 return string16(); | |
84 } | |
85 | |
86 bool AppNotifyChannelUIInfoBarDelegate::Accept() { | |
87 creator_->OnInfoBarResult(true); | |
88 return true; | |
89 } | |
90 | |
91 bool AppNotifyChannelUIInfoBarDelegate::Cancel() { | |
92 creator_->OnInfoBarResult(false); | |
93 return true; | |
94 } | |
95 | |
96 void AppNotifyChannelUIInfoBarDelegate::InfoBarDismissed() { | |
97 Cancel(); | |
98 } | |
99 | |
100 AppNotifyChannelUIInfoBarDelegate::AppNotifyChannelUIInfoBarDelegate( | |
101 AppNotifyChannelUIImpl* creator, | |
102 InfoBarService* infobar_service, | |
103 const std::string& app_name) | |
104 : ConfirmInfoBarDelegate(infobar_service), | |
105 creator_(creator), | |
106 app_name_(app_name) { | |
107 } | |
108 | |
109 AppNotifyChannelUIInfoBarDelegate::~AppNotifyChannelUIInfoBarDelegate() { | |
110 } | |
111 | |
112 } // namespace | |
113 | |
114 | |
115 AppNotifyChannelUIImpl::AppNotifyChannelUIImpl( | |
116 Profile* profile, | |
117 content::WebContents* web_contents, | |
118 const std::string& app_name, | |
119 AppNotifyChannelUI::UIType ui_type) | |
120 : profile_(profile->GetOriginalProfile()), | |
121 web_contents_(web_contents), | |
122 app_name_(app_name), | |
123 ui_type_(ui_type), | |
124 delegate_(NULL), | |
125 observing_sync_(false), | |
126 wizard_shown_to_user_(false) { | |
127 } | |
128 | |
129 AppNotifyChannelUIImpl::~AppNotifyChannelUIImpl() { | |
130 // We should have either not started observing sync, or already called | |
131 // StopObservingSync by this point. | |
132 CHECK(!observing_sync_); | |
133 } | |
134 | |
135 // static | |
136 AppNotifyChannelUI* AppNotifyChannelUI::Create(Profile* profile, | |
137 content::WebContents* web_contents, | |
138 const std::string& app_name, | |
139 AppNotifyChannelUI::UIType ui_type) { | |
140 return new AppNotifyChannelUIImpl(profile, web_contents, app_name, ui_type); | |
141 } | |
142 | |
143 void AppNotifyChannelUIImpl::PromptSyncSetup( | |
144 AppNotifyChannelUI::Delegate* delegate) { | |
145 CHECK(delegate_ == NULL); | |
146 delegate_ = delegate; | |
147 | |
148 if (!ProfileSyncServiceFactory::GetInstance()->HasProfileSyncService( | |
149 profile_)) { | |
150 delegate_->OnSyncSetupResult(false); | |
151 return; | |
152 } | |
153 | |
154 if (ui_type_ == NO_INFOBAR) { | |
155 OnInfoBarResult(true); | |
156 return; | |
157 } | |
158 | |
159 AppNotifyChannelUIInfoBarDelegate::Create( | |
160 InfoBarService::FromWebContents(web_contents_), this, app_name_); | |
161 } | |
162 | |
163 void AppNotifyChannelUIImpl::OnInfoBarResult(bool accepted) { | |
164 if (!accepted) { | |
165 delegate_->OnSyncSetupResult(false); | |
166 return; | |
167 } | |
168 | |
169 StartObservingSync(); | |
170 // Bring up the login page. | |
171 LoginUIService* login_ui_service = | |
172 LoginUIServiceFactory::GetForProfile(profile_); | |
173 LoginUIService::LoginUI* login_ui = login_ui_service->current_login_ui(); | |
174 if (login_ui) { | |
175 // Some sort of login UI is already visible. | |
176 SigninManager* signin = SigninManagerFactory::GetForProfile(profile_); | |
177 if (signin->GetAuthenticatedUsername().empty()) { | |
178 // User is not logged in yet, so just bring up the login UI (could be | |
179 // the promo UI). | |
180 login_ui->FocusUI(); | |
181 return; | |
182 } else { | |
183 // User is already logged in, so close whatever sync config UI the | |
184 // user is looking at and display new login UI. | |
185 login_ui->CloseUI(); | |
186 DCHECK(!login_ui_service->current_login_ui()); | |
187 } | |
188 } | |
189 // Any existing UI is now closed - display new login UI. | |
190 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_); | |
191 chrome::ShowSettingsSubPage(browser, chrome::kSyncSetupForceLoginSubPage); | |
192 } | |
193 | |
194 void AppNotifyChannelUIImpl::OnStateChanged() { | |
195 ProfileSyncService* sync_service = | |
196 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile_); | |
197 LoginUIService* login_service = | |
198 LoginUIServiceFactory::GetForProfile(profile_); | |
199 | |
200 bool wizard_visible = (login_service->current_login_ui() != NULL); | |
201 // ProfileSyncService raises OnStateChanged many times. Even multiple | |
202 // times before the wizard actually becomes visible for the first time. | |
203 // So we have to wait for the wizard to become visible once and then we | |
204 // wait for it to get dismissed. | |
205 bool finished = wizard_shown_to_user_ && !wizard_visible; | |
206 if (wizard_visible) | |
207 wizard_shown_to_user_ = true; | |
208 | |
209 if (finished) { | |
210 StopObservingSync(); | |
211 delegate_->OnSyncSetupResult(sync_service->HasSyncSetupCompleted()); | |
212 } | |
213 } | |
214 | |
215 void AppNotifyChannelUIImpl::StartObservingSync() { | |
216 CHECK(!observing_sync_); | |
217 observing_sync_ = true; | |
218 ProfileSyncServiceFactory::GetInstance()->GetForProfile( | |
219 profile_)->AddObserver(this); | |
220 } | |
221 | |
222 void AppNotifyChannelUIImpl::StopObservingSync() { | |
223 CHECK(observing_sync_); | |
224 observing_sync_ = false; | |
225 ProfileSyncServiceFactory::GetInstance()->GetForProfile( | |
226 profile_)->RemoveObserver(this); | |
227 } | |
228 | |
229 } // namespace extensions | |
OLD | NEW |