OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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/ui/webui/chromeos/login/enterprise_oauth_enrollment_scr een_handler.h" | |
6 | |
7 #include "base/callback.h" | |
8 #include "base/command_line.h" | |
9 #include "base/json/json_reader.h" | |
10 #include "base/json/json_writer.h" | |
11 #include "base/utf_string_conversions.h" | |
12 #include "base/values.h" | |
13 #include "chrome/browser/browsing_data_remover.h" | |
14 #include "chrome/browser/net/gaia/gaia_oauth_fetcher.h" | |
15 #include "chrome/browser/profiles/profile.h" | |
16 #include "chrome/common/chrome_switches.h" | |
17 #include "chrome/common/net/gaia/google_service_auth_error.h" | |
18 #include "chrome/common/url_constants.h" | |
19 #include "content/browser/renderer_host/render_view_host.h" | |
20 #include "content/browser/tab_contents/tab_contents.h" | |
21 #include "grit/chromium_strings.h" | |
22 #include "grit/generated_resources.h" | |
23 #include "ui/base/l10n/l10n_util.h" | |
24 | |
25 namespace { | |
26 | |
27 // Start page of GAIA authentication extension. | |
28 const char kGaiaExtStartPage[] = | |
29 "chrome-extension://mfffpogegjflfpflabcdkioaeobkgjik/main.html"; | |
30 | |
31 // OAuth V2 service scope for device management. | |
32 const char kServiceScopeChromeOSDeviceManagement[] = | |
33 "https://www.googleapis.com/auth/chromeosdevicemanagement"; | |
34 | |
35 // Enrollment step names. | |
36 const char kEnrollmentStepSignin[] = "signin"; | |
37 const char kEnrollmentStepWorking[] = "working"; | |
38 const char kEnrollmentStepError[] = "error"; | |
39 const char kEnrollmentStepSuccess[] = "success"; | |
40 | |
41 } // namespace | |
42 | |
43 namespace chromeos { | |
44 | |
45 // EnterpriseOAuthEnrollmentScreenHandler, public ------------------------------ | |
46 | |
47 EnterpriseOAuthEnrollmentScreenHandler::EnterpriseOAuthEnrollmentScreenHandler() | |
48 : controller_(NULL), editable_user_(true), show_on_init_(false) { | |
49 } | |
50 | |
51 EnterpriseOAuthEnrollmentScreenHandler:: | |
52 ~EnterpriseOAuthEnrollmentScreenHandler() {} | |
53 | |
54 // EnterpriseOAuthEnrollmentScreenHandler, WebUIMessageHandler implementation -- | |
55 | |
56 void EnterpriseOAuthEnrollmentScreenHandler::RegisterMessages() { | |
57 web_ui_->RegisterMessageCallback( | |
58 "oauthEnrollClose", | |
59 NewCallback( | |
60 this, | |
61 &EnterpriseOAuthEnrollmentScreenHandler::HandleClose)); | |
62 web_ui_->RegisterMessageCallback( | |
63 "oauthEnrollCompleteLogin", | |
64 NewCallback( | |
65 this, | |
66 &EnterpriseOAuthEnrollmentScreenHandler::HandleCompleteLogin)); | |
67 web_ui_->RegisterMessageCallback( | |
68 "oauthEnrollRetry", | |
69 NewCallback( | |
70 this, | |
71 &EnterpriseOAuthEnrollmentScreenHandler::HandleRetry)); | |
72 } | |
73 | |
74 // EnterpriseOAuthEnrollmentScreenHandler | |
75 // EnterpriseEnrollmentScreenActor implementation ------------------------- | |
76 | |
77 void EnterpriseOAuthEnrollmentScreenHandler::SetController( | |
78 Controller* controller) { | |
79 controller_ = controller; | |
80 } | |
81 | |
82 void EnterpriseOAuthEnrollmentScreenHandler::PrepareToShow() { | |
83 } | |
84 | |
85 void EnterpriseOAuthEnrollmentScreenHandler::Show() { | |
86 if (!page_is_ready()) { | |
87 show_on_init_ = true; | |
88 return; | |
89 } | |
90 | |
91 DictionaryValue screen_data; | |
92 screen_data.SetString("signin_url", kGaiaExtStartPage); | |
93 ShowScreen("oauth-enrollment", &screen_data); | |
94 } | |
95 | |
96 void EnterpriseOAuthEnrollmentScreenHandler::Hide() { | |
97 } | |
98 | |
99 void EnterpriseOAuthEnrollmentScreenHandler::SetEditableUser(bool editable) { | |
100 editable_user_ = editable; | |
101 } | |
102 | |
103 void EnterpriseOAuthEnrollmentScreenHandler::ShowConfirmationScreen() { | |
104 ShowStep(kEnrollmentStepSuccess); | |
105 NotifyObservers(true); | |
106 } | |
107 | |
108 void EnterpriseOAuthEnrollmentScreenHandler::ShowAuthError( | |
109 const GoogleServiceAuthError& error) { | |
110 switch (error.state()) { | |
111 case GoogleServiceAuthError::NONE: | |
112 case GoogleServiceAuthError::CAPTCHA_REQUIRED: | |
113 case GoogleServiceAuthError::TWO_FACTOR: | |
114 case GoogleServiceAuthError::HOSTED_NOT_ALLOWED: | |
115 case GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS: | |
116 case GoogleServiceAuthError::REQUEST_CANCELED: | |
117 LOG(ERROR) << "Auth error " << error.state(); | |
118 ShowFatalAuthError(); | |
119 break; | |
120 case GoogleServiceAuthError::USER_NOT_SIGNED_UP: | |
121 case GoogleServiceAuthError::ACCOUNT_DELETED: | |
122 case GoogleServiceAuthError::ACCOUNT_DISABLED: | |
123 LOG(ERROR) << "Account error " << error.state(); | |
124 ShowAccountError(); | |
125 break; | |
126 case GoogleServiceAuthError::CONNECTION_FAILED: | |
127 case GoogleServiceAuthError::SERVICE_UNAVAILABLE: | |
128 LOG(WARNING) << "Network error " << error.state(); | |
129 ShowNetworkEnrollmentError(); | |
130 break; | |
131 } | |
132 NotifyObservers(false); | |
133 } | |
134 | |
135 void EnterpriseOAuthEnrollmentScreenHandler::ShowAccountError() { | |
136 ShowError(IDS_ENTERPRISE_ENROLLMENT_ACCOUNT_ERROR, true); | |
137 NotifyObservers(false); | |
138 } | |
139 | |
140 void EnterpriseOAuthEnrollmentScreenHandler::ShowFatalAuthError() { | |
141 ShowError(IDS_ENTERPRISE_ENROLLMENT_FATAL_AUTH_ERROR, false); | |
142 NotifyObservers(false); | |
143 } | |
144 | |
145 void EnterpriseOAuthEnrollmentScreenHandler::ShowFatalEnrollmentError() { | |
146 ShowError(IDS_ENTERPRISE_ENROLLMENT_FATAL_ENROLLMENT_ERROR, false); | |
147 NotifyObservers(false); | |
148 } | |
149 | |
150 void EnterpriseOAuthEnrollmentScreenHandler::ShowNetworkEnrollmentError() { | |
151 ShowError(IDS_ENTERPRISE_ENROLLMENT_NETWORK_ENROLLMENT_ERROR, true); | |
152 NotifyObservers(false); | |
153 } | |
154 | |
155 // EnterpriseOAuthEnrollmentScreenHandler BaseScreenHandler implementation ----- | |
156 | |
157 void EnterpriseOAuthEnrollmentScreenHandler::GetLocalizedStrings( | |
158 base::DictionaryValue *localized_strings) { | |
159 localized_strings->SetString( | |
160 "oauthEnrollScreenTitle", | |
161 l10n_util::GetStringUTF16(IDS_ENTERPRISE_ENROLLMENT_SCREEN_TITLE)); | |
162 localized_strings->SetString( | |
163 "oauthEnrollRetry", | |
164 l10n_util::GetStringUTF16(IDS_ENTERPRISE_ENROLLMENT_RETRY)); | |
165 localized_strings->SetString( | |
166 "oauthEnrollCancel", | |
167 l10n_util::GetStringUTF16(IDS_ENTERPRISE_ENROLLMENT_CANCEL)); | |
168 localized_strings->SetString( | |
169 "oauthEnrollDone", | |
170 l10n_util::GetStringUTF16(IDS_ENTERPRISE_ENROLLMENT_DONE)); | |
171 localized_strings->SetString( | |
172 "oauthEnrollSuccess", | |
173 l10n_util::GetStringUTF16(IDS_ENTERPRISE_ENROLLMENT_SUCCESS)); | |
174 localized_strings->SetString( | |
175 "oauthEnrollWorking", | |
176 l10n_util::GetStringUTF16(IDS_ENTERPRISE_ENROLLMENT_WORKING)); | |
177 } | |
178 | |
179 void EnterpriseOAuthEnrollmentScreenHandler::OnGetOAuthTokenSuccess( | |
180 const std::string& oauth_token) { | |
181 } | |
182 | |
183 void EnterpriseOAuthEnrollmentScreenHandler::OnGetOAuthTokenFailure() { | |
184 ResetAuth(); | |
185 ShowFatalAuthError(); | |
186 } | |
187 | |
188 void EnterpriseOAuthEnrollmentScreenHandler::OnOAuthGetAccessTokenSuccess( | |
189 const std::string& token, | |
190 const std::string& secret) { | |
191 } | |
192 | |
193 void EnterpriseOAuthEnrollmentScreenHandler::OnOAuthGetAccessTokenFailure( | |
194 const GoogleServiceAuthError& error) { | |
195 ResetAuth(); | |
196 ShowAuthError(error); | |
197 } | |
198 | |
199 void EnterpriseOAuthEnrollmentScreenHandler::OnOAuthWrapBridgeSuccess( | |
200 const std::string& token, | |
201 const std::string& expires_in) { | |
202 ResetAuth(); | |
203 | |
204 if (!controller_ || user_.empty()) { | |
205 NOTREACHED(); | |
206 return; | |
207 } | |
208 | |
209 controller_->OnOAuthTokenAvailable(user_, token); | |
210 } | |
211 | |
212 void EnterpriseOAuthEnrollmentScreenHandler::OnOAuthWrapBridgeFailure( | |
213 const GoogleServiceAuthError& error) { | |
214 ResetAuth(); | |
215 ShowAuthError(error); | |
216 } | |
217 | |
218 void EnterpriseOAuthEnrollmentScreenHandler::OnUserInfoSuccess( | |
219 const std::string& email) { | |
220 ResetAuth(); | |
221 NOTREACHED(); | |
222 } | |
223 | |
224 void EnterpriseOAuthEnrollmentScreenHandler::OnUserInfoFailure( | |
225 const GoogleServiceAuthError& error) { | |
226 ResetAuth(); | |
227 NOTREACHED(); | |
228 } | |
229 | |
230 void EnterpriseOAuthEnrollmentScreenHandler::Initialize() { | |
231 if (show_on_init_) { | |
232 Show(); | |
233 show_on_init_ = false; | |
234 } | |
235 } | |
236 | |
237 // EnterpriseOAuthEnrollmentScreenHandler, private ----------------------------- | |
238 | |
239 void EnterpriseOAuthEnrollmentScreenHandler::HandleClose( | |
240 const base::ListValue* value) { | |
241 ResetAuth(); | |
242 | |
243 if (!controller_) { | |
244 NOTREACHED(); | |
245 return; | |
246 } | |
247 | |
248 controller_->OnConfirmationClosed(); | |
249 } | |
250 | |
251 void EnterpriseOAuthEnrollmentScreenHandler::HandleCompleteLogin( | |
252 const base::ListValue* value) { | |
253 if (!controller_) { | |
254 NOTREACHED(); | |
255 return; | |
256 } | |
257 | |
258 if (!value->GetString(0, &user_)) { | |
259 NOTREACHED() << "Invalid user parameter from UI."; | |
260 return; | |
261 } | |
262 | |
263 Profile* profile = | |
264 Profile::FromBrowserContext(web_ui_->tab_contents()->browser_context()); | |
265 oauth_fetcher_.reset( | |
266 new GaiaOAuthFetcher(this, | |
whywhat
2011/08/04 10:47:52
This ctor also has one more parameter now.
Mattias Nissler (ping if slow)
2011/08/04 14:38:44
Done.
| |
267 profile->GetRequestContext(), | |
268 profile, | |
269 kServiceScopeChromeOSDeviceManagement)); | |
270 oauth_fetcher_->SetAutoFetchLimit( | |
271 GaiaOAuthFetcher::OAUTH2_SERVICE_ACCESS_TOKEN); | |
272 oauth_fetcher_->StartGetOAuthTokenRequest(); | |
273 | |
274 ShowStep(kEnrollmentStepWorking); | |
275 } | |
276 | |
277 void EnterpriseOAuthEnrollmentScreenHandler::HandleRetry( | |
278 const base::ListValue* value) { | |
279 Show(); | |
280 } | |
281 | |
282 void EnterpriseOAuthEnrollmentScreenHandler::ShowStep(const char* step) { | |
283 base::StringValue step_value(step); | |
284 web_ui_->CallJavascriptFunction("oobe.OAuthEnrollmentScreen.showStep", | |
285 step_value); | |
286 } | |
287 | |
288 void EnterpriseOAuthEnrollmentScreenHandler::ShowError(int message_id, | |
289 bool retry) { | |
290 const std::string message(l10n_util::GetStringUTF8(message_id)); | |
291 base::StringValue message_value(message); | |
292 base::FundamentalValue retry_value(retry); | |
293 web_ui_->CallJavascriptFunction("oobe.OAuthEnrollmentScreen.showError", | |
294 message_value, | |
295 retry_value); | |
296 } | |
297 | |
298 void EnterpriseOAuthEnrollmentScreenHandler::ResetAuth() { | |
299 oauth_fetcher_.reset(); | |
300 | |
301 // Clear page state. | |
302 int remove_mask = | |
303 BrowsingDataRemover::REMOVE_COOKIES | | |
304 BrowsingDataRemover::REMOVE_LSO_DATA; | |
305 (new BrowsingDataRemover( | |
306 Profile::FromBrowserContext(web_ui_->tab_contents()->browser_context()), | |
307 BrowsingDataRemover::EVERYTHING, | |
308 base::Time()))->Remove(remove_mask); | |
309 } | |
310 | |
311 | |
312 } // namespace chromeos | |
OLD | NEW |