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

Side by Side Diff: chrome/browser/ui/webui/sync_setup_handler2.cc

Issue 9104028: Unfork various Sync settings UI classes (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Another cros compilation fix. Created 8 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
« no previous file with comments | « chrome/browser/ui/webui/sync_setup_handler2.h ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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/ui/webui/sync_setup_handler2.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.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/google/google_util.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/profiles/profile_info_cache.h"
16 #include "chrome/browser/profiles/profile_manager.h"
17 #include "chrome/browser/profiles/profile_metrics.h"
18 #include "chrome/browser/signin/signin_manager.h"
19 #include "chrome/browser/sync/profile_sync_service.h"
20 #include "chrome/browser/sync/protocol/service_constants.h"
21 #include "chrome/browser/sync/sync_setup_flow.h"
22 #include "chrome/browser/sync/syncable/model_type.h"
23 #include "chrome/browser/sync/util/oauth.h"
24 #include "chrome/browser/ui/browser_list.h"
25 #include "chrome/browser/ui/webui/sync_promo/sync_promo_trial.h"
26 #include "chrome/browser/ui/webui/sync_promo/sync_promo_ui.h"
27 #include "chrome/common/net/gaia/gaia_constants.h"
28 #include "chrome/common/url_constants.h"
29 #include "content/browser/renderer_host/render_view_host.h"
30 #include "content/public/browser/render_view_host_delegate.h"
31 #include "content/public/browser/web_contents.h"
32 #include "grit/chromium_strings.h"
33 #include "grit/generated_resources.h"
34 #include "grit/locale_settings.h"
35 #include "ui/base/l10n/l10n_util.h"
36
37 using l10n_util::GetStringFUTF16;
38 using l10n_util::GetStringUTF16;
39
40 namespace {
41
42 bool GetAuthData(const std::string& json,
43 std::string* username,
44 std::string* password,
45 std::string* captcha,
46 std::string* access_code) {
47 scoped_ptr<Value> parsed_value(base::JSONReader::Read(json, false));
48 if (!parsed_value.get() || !parsed_value->IsType(Value::TYPE_DICTIONARY))
49 return false;
50
51 DictionaryValue* result = static_cast<DictionaryValue*>(parsed_value.get());
52 if (!result->GetString("user", username) ||
53 !result->GetString("pass", password) ||
54 !result->GetString("captcha", captcha) ||
55 !result->GetString("access_code", access_code)) {
56 return false;
57 }
58 return true;
59 }
60
61 bool GetConfiguration(const std::string& json, SyncConfiguration* config) {
62 scoped_ptr<Value> parsed_value(base::JSONReader::Read(json, false));
63 if (!parsed_value.get() || !parsed_value->IsType(Value::TYPE_DICTIONARY))
64 return false;
65
66 DictionaryValue* result = static_cast<DictionaryValue*>(parsed_value.get());
67 if (!result->GetBoolean("syncAllDataTypes", &config->sync_everything))
68 return false;
69
70 // These values need to be kept in sync with where they are written in
71 // choose_datatypes.html.
72 bool sync_bookmarks;
73 if (!result->GetBoolean("syncBookmarks", &sync_bookmarks))
74 return false;
75 if (sync_bookmarks)
76 config->data_types.Put(syncable::BOOKMARKS);
77
78 bool sync_preferences;
79 if (!result->GetBoolean("syncPreferences", &sync_preferences))
80 return false;
81 if (sync_preferences)
82 config->data_types.Put(syncable::PREFERENCES);
83
84 bool sync_themes;
85 if (!result->GetBoolean("syncThemes", &sync_themes))
86 return false;
87 if (sync_themes)
88 config->data_types.Put(syncable::THEMES);
89
90 bool sync_passwords;
91 if (!result->GetBoolean("syncPasswords", &sync_passwords))
92 return false;
93 if (sync_passwords)
94 config->data_types.Put(syncable::PASSWORDS);
95
96 bool sync_autofill;
97 if (!result->GetBoolean("syncAutofill", &sync_autofill))
98 return false;
99 if (sync_autofill)
100 config->data_types.Put(syncable::AUTOFILL);
101
102 bool sync_extensions;
103 if (!result->GetBoolean("syncExtensions", &sync_extensions))
104 return false;
105 if (sync_extensions) {
106 config->data_types.Put(syncable::EXTENSIONS);
107 config->data_types.Put(syncable::EXTENSION_SETTINGS);
108 }
109
110 bool sync_typed_urls;
111 if (!result->GetBoolean("syncTypedUrls", &sync_typed_urls))
112 return false;
113 if (sync_typed_urls)
114 config->data_types.Put(syncable::TYPED_URLS);
115
116 bool sync_sessions;
117 if (!result->GetBoolean("syncSessions", &sync_sessions))
118 return false;
119 if (sync_sessions)
120 config->data_types.Put(syncable::SESSIONS);
121
122 bool sync_apps;
123 if (!result->GetBoolean("syncApps", &sync_apps))
124 return false;
125 if (sync_apps) {
126 config->data_types.Put(syncable::APPS);
127 config->data_types.Put(syncable::APP_SETTINGS);
128 }
129
130 // Encryption settings.
131 if (!result->GetBoolean("encryptAllData", &config->encrypt_all))
132 return false;
133
134 // Passphrase settings.
135 bool have_passphrase;
136 if (!result->GetBoolean("usePassphrase", &have_passphrase))
137 return false;
138
139 if (have_passphrase) {
140 bool is_gaia;
141 if (!result->GetBoolean("isGooglePassphrase", &is_gaia))
142 return false;
143 std::string passphrase;
144 if (!result->GetString("passphrase", &passphrase))
145 return false;
146 // The user provided a passphrase - pass it off to SyncSetupFlow as either
147 // the secondary or GAIA passphrase as appropriate.
148 if (is_gaia) {
149 config->set_gaia_passphrase = true;
150 config->gaia_passphrase = passphrase;
151 } else {
152 config->set_secondary_passphrase = true;
153 config->secondary_passphrase = passphrase;
154 }
155 }
156 return true;
157 }
158
159 bool GetPassphrase(const std::string& json, std::string* passphrase) {
160 scoped_ptr<Value> parsed_value(base::JSONReader::Read(json, false));
161 if (!parsed_value.get() || !parsed_value->IsType(Value::TYPE_DICTIONARY))
162 return false;
163
164 DictionaryValue* result = static_cast<DictionaryValue*>(parsed_value.get());
165 return result->GetString("passphrase", passphrase);
166 }
167
168 string16 NormalizeUserName(const string16& user) {
169 if (user.find_first_of(ASCIIToUTF16("@")) != string16::npos)
170 return user;
171 return user + ASCIIToUTF16("@") + ASCIIToUTF16(DEFAULT_SIGNIN_DOMAIN);
172 }
173
174 bool AreUserNamesEqual(const string16& user1, const string16& user2) {
175 return NormalizeUserName(user1) == NormalizeUserName(user2);
176 }
177
178 } // namespace
179
180 namespace options2 {
181
182 SyncSetupHandler2::SyncSetupHandler2(ProfileManager* profile_manager)
183 : flow_(NULL),
184 profile_manager_(profile_manager) {
185 }
186
187 SyncSetupHandler2::~SyncSetupHandler2() {
188 // This case is hit when the user performs a back navigation.
189 if (flow_)
190 flow_->OnDialogClosed("");
191 }
192
193 void SyncSetupHandler2::GetLocalizedValues(DictionaryValue* localized_strings) {
194 GetStaticLocalizedValues(localized_strings, web_ui());
195 }
196
197 void SyncSetupHandler2::GetStaticLocalizedValues(
198 DictionaryValue* localized_strings,
199 content::WebUI* web_ui) {
200 DCHECK(localized_strings);
201
202 localized_strings->SetString(
203 "invalidPasswordHelpURL", chrome::kInvalidPasswordHelpURL);
204 localized_strings->SetString(
205 "cannotAccessAccountURL", chrome::kCanNotAccessAccountURL);
206 string16 product_name(GetStringUTF16(IDS_PRODUCT_NAME));
207 localized_strings->SetString(
208 "introduction",
209 GetStringFUTF16(IDS_SYNC_LOGIN_INTRODUCTION, product_name));
210 localized_strings->SetString(
211 "chooseDataTypesInstructions",
212 GetStringFUTF16(IDS_SYNC_CHOOSE_DATATYPES_INSTRUCTIONS, product_name));
213 localized_strings->SetString(
214 "encryptionInstructions",
215 GetStringFUTF16(IDS_SYNC_ENCRYPTION_INSTRUCTIONS, product_name));
216 localized_strings->SetString(
217 "encryptionHelpURL", chrome::kSyncEncryptionHelpURL);
218 localized_strings->SetString(
219 "passphraseEncryptionMessage",
220 GetStringFUTF16(IDS_SYNC_PASSPHRASE_ENCRYPTION_MESSAGE, product_name));
221 localized_strings->SetString(
222 "passphraseRecover",
223 GetStringFUTF16(IDS_SYNC_PASSPHRASE_RECOVER,
224 ASCIIToUTF16(google_util::StringAppendGoogleLocaleParam(
225 chrome::kSyncGoogleDashboardURL))));
226
227 bool is_launch_page = web_ui && SyncPromoUI::GetIsLaunchPageForSyncPromoURL(
228 web_ui->GetWebContents()->GetURL());
229 int title_id = is_launch_page ? IDS_SYNC_PROMO_TITLE_SHORT :
230 IDS_SYNC_PROMO_TITLE_EXISTING_USER;
231 string16 short_product_name(GetStringUTF16(IDS_SHORT_PRODUCT_NAME));
232 localized_strings->SetString(
233 "promoTitle", GetStringFUTF16(title_id, short_product_name));
234
235 localized_strings->SetString(
236 "promoMessageTitle",
237 GetStringFUTF16(IDS_SYNC_PROMO_MESSAGE_TITLE, short_product_name));
238 localized_strings->SetString(
239 "syncEverythingHelpURL", chrome::kSyncEverythingLearnMoreURL);
240 localized_strings->SetString(
241 "syncErrorHelpURL", chrome::kSyncErrorsHelpURL);
242
243 std::string create_account_url = google_util::StringAppendGoogleLocaleParam(
244 chrome::kSyncCreateNewAccountURL);
245 string16 create_account = GetStringUTF16(IDS_SYNC_CREATE_ACCOUNT);
246 create_account= UTF8ToUTF16("<a id='create-account-link' target='_blank' "
247 "class='account-link' href='" + create_account_url + "'>") +
248 create_account + UTF8ToUTF16("</a>");
249 localized_strings->SetString("createAccountLinkHTML",
250 GetStringFUTF16(IDS_SYNC_CREATE_ACCOUNT_PREFIX, create_account));
251
252 localized_strings->SetString("promoVerboseTitle", short_product_name);
253 localized_strings->SetString("promoVerboseMessageBody",
254 GetStringFUTF16(IDS_SYNC_PROMO_V_MESSAGE_BODY, short_product_name));
255
256 string16 sync_benefits_url(
257 UTF8ToUTF16(google_util::StringAppendGoogleLocaleParam(
258 chrome::kSyncLearnMoreURL)));
259 localized_strings->SetString("promoVerboseLearnMore",
260 GetStringFUTF16(IDS_SYNC_PROMO_V_LEARN_MORE, sync_benefits_url));
261 localized_strings->SetString("promoVerboseBackupBody",
262 GetStringFUTF16(IDS_SYNC_PROMO_V_BACKUP_BODY, short_product_name));
263 localized_strings->SetString("signUpURL", create_account_url);
264
265 static OptionsStringResource resources[] = {
266 { "syncSetupOverlayTitle", IDS_SYNC_SETUP_TITLE },
267 { "syncSetupConfigureTitle", IDS_SYNC_SETUP_CONFIGURE_TITLE },
268 { "cannotBeBlank", IDS_SYNC_CANNOT_BE_BLANK },
269 { "emailLabel", IDS_SYNC_LOGIN_EMAIL_NEW_LINE },
270 { "passwordLabel", IDS_SYNC_LOGIN_PASSWORD_NEW_LINE },
271 { "invalidCredentials", IDS_SYNC_INVALID_USER_CREDENTIALS },
272 { "signin", IDS_SYNC_SIGNIN },
273 { "couldNotConnect", IDS_SYNC_LOGIN_COULD_NOT_CONNECT },
274 { "unrecoverableError", IDS_SYNC_UNRECOVERABLE_ERROR },
275 { "errorLearnMore", IDS_LEARN_MORE },
276 { "unrecoverableErrorHelpURL", IDS_SYNC_UNRECOVERABLE_ERROR_HELP_URL },
277 { "cannotAccessAccount", IDS_SYNC_CANNOT_ACCESS_ACCOUNT },
278 { "cancel", IDS_CANCEL },
279 { "settingUp", IDS_SYNC_LOGIN_SETTING_UP },
280 { "errorSigningIn", IDS_SYNC_ERROR_SIGNING_IN },
281 { "signinHeader", IDS_SYNC_PROMO_SIGNIN_HEADER},
282 { "captchaInstructions", IDS_SYNC_GAIA_CAPTCHA_INSTRUCTIONS },
283 { "invalidAccessCode", IDS_SYNC_INVALID_ACCESS_CODE_LABEL },
284 { "enterAccessCode", IDS_SYNC_ENTER_ACCESS_CODE_LABEL },
285 { "getAccessCodeHelp", IDS_SYNC_ACCESS_CODE_HELP_LABEL },
286 { "getAccessCodeURL", IDS_SYNC_GET_ACCESS_CODE_URL },
287 { "syncAllDataTypes", IDS_SYNC_EVERYTHING },
288 { "chooseDataTypes", IDS_SYNC_CHOOSE_DATATYPES },
289 { "bookmarks", IDS_SYNC_DATATYPE_BOOKMARKS },
290 { "preferences", IDS_SYNC_DATATYPE_PREFERENCES },
291 { "autofill", IDS_SYNC_DATATYPE_AUTOFILL },
292 { "themes", IDS_SYNC_DATATYPE_THEMES },
293 { "passwords", IDS_SYNC_DATATYPE_PASSWORDS },
294 { "extensions", IDS_SYNC_DATATYPE_EXTENSIONS },
295 { "typedURLs", IDS_SYNC_DATATYPE_TYPED_URLS },
296 { "apps", IDS_SYNC_DATATYPE_APPS },
297 { "openTabs", IDS_SYNC_DATATYPE_TABS },
298 { "syncZeroDataTypesError", IDS_SYNC_ZERO_DATA_TYPES_ERROR },
299 { "serviceUnavailableError", IDS_SYNC_SETUP_ABORTED_BY_PENDING_CLEAR },
300 { "encryptAllLabel", IDS_SYNC_ENCRYPT_ALL_LABEL },
301 { "googleOption", IDS_SYNC_PASSPHRASE_OPT_GOOGLE },
302 { "explicitOption", IDS_SYNC_PASSPHRASE_OPT_EXPLICIT },
303 { "sectionGoogleMessage", IDS_SYNC_PASSPHRASE_MSG_GOOGLE },
304 { "sectionExplicitMessage", IDS_SYNC_PASSPHRASE_MSG_EXPLICIT },
305 { "passphraseLabel", IDS_SYNC_PASSPHRASE_LABEL },
306 { "confirmLabel", IDS_SYNC_CONFIRM_PASSPHRASE_LABEL },
307 { "emptyErrorMessage", IDS_SYNC_EMPTY_PASSPHRASE_ERROR },
308 { "mismatchErrorMessage", IDS_SYNC_PASSPHRASE_MISMATCH_ERROR },
309 { "passphraseWarning", IDS_SYNC_PASSPHRASE_WARNING },
310 { "customizeLinkLabel", IDS_SYNC_CUSTOMIZE_LINK_LABEL },
311 { "confirmSyncPreferences", IDS_SYNC_CONFIRM_SYNC_PREFERENCES },
312 { "syncEverything", IDS_SYNC_SYNC_EVERYTHING },
313 { "useDefaultSettings", IDS_SYNC_USE_DEFAULT_SETTINGS },
314 { "passphraseSectionTitle", IDS_SYNC_PASSPHRASE_SECTION_TITLE },
315 { "privacyDashboardLink", IDS_SYNC_PRIVACY_DASHBOARD_LINK_LABEL },
316 { "enterPassphraseTitle", IDS_SYNC_ENTER_PASSPHRASE_TITLE },
317 { "enterPassphraseBody", IDS_SYNC_ENTER_PASSPHRASE_BODY },
318 { "enterOtherPassphraseBody", IDS_SYNC_ENTER_OTHER_PASSPHRASE_BODY },
319 { "enterGooglePassphraseBody", IDS_SYNC_ENTER_GOOGLE_PASSPHRASE_BODY },
320 { "passphraseLabel", IDS_SYNC_PASSPHRASE_LABEL },
321 { "incorrectPassphrase", IDS_SYNC_INCORRECT_PASSPHRASE },
322 { "passphraseWarning", IDS_SYNC_PASSPHRASE_WARNING },
323 { "cancelWarningHeader", IDS_SYNC_PASSPHRASE_CANCEL_WARNING_HEADER },
324 { "cancelWarning", IDS_SYNC_PASSPHRASE_CANCEL_WARNING },
325 { "yes", IDS_SYNC_PASSPHRASE_CANCEL_YES },
326 { "no", IDS_SYNC_PASSPHRASE_CANCEL_NO },
327 { "sectionExplicitMessagePrefix", IDS_SYNC_PASSPHRASE_MSG_EXPLICIT_PREFIX },
328 { "sectionExplicitMessagePostfix",
329 IDS_SYNC_PASSPHRASE_MSG_EXPLICIT_POSTFIX },
330 { "encryptedDataTypesTitle", IDS_SYNC_ENCRYPTION_DATA_TYPES_TITLE },
331 { "encryptSensitiveOption", IDS_SYNC_ENCRYPT_SENSITIVE_DATA },
332 { "encryptAllOption", IDS_SYNC_ENCRYPT_ALL_DATA },
333 { "encryptAllOption", IDS_SYNC_ENCRYPT_ALL_DATA },
334 { "aspWarningText", IDS_SYNC_ASP_PASSWORD_WARNING_TEXT },
335 { "promoPageTitle", IDS_SYNC_PROMO_TAB_TITLE },
336 { "promoSkipButton", IDS_SYNC_PROMO_SKIP_BUTTON },
337 { "promoAdvanced", IDS_SYNC_PROMO_ADVANCED },
338 { "promoLearnMoreShow", IDS_SYNC_PROMO_LEARN_MORE_SHOW },
339 { "promoLearnMoreHide", IDS_SYNC_PROMO_LEARN_MORE_HIDE },
340 { "promoInformation", IDS_SYNC_PROMO_INFORMATION },
341 { "promoVerboseSyncTitle", IDS_SYNC_PROMO_V_SYNC_TITLE },
342 { "promoVerboseSyncBody", IDS_SYNC_PROMO_V_SYNC_BODY },
343 { "promoVerboseBackupTitle", IDS_SYNC_PROMO_V_BACKUP_TITLE },
344 { "promoVerboseServicesTitle", IDS_SYNC_PROMO_V_SERVICES_TITLE },
345 { "promoVerboseServicesBody", IDS_SYNC_PROMO_V_SERVICES_BODY },
346 { "promoVerboseSignUp", IDS_SYNC_PROMO_V_SIGN_UP },
347 { "promoTitleShort", IDS_SYNC_PROMO_MESSAGE_TITLE_SHORT },
348 { "promoMessageBody", IDS_SYNC_PROMO_MESSAGE_BODY },
349 };
350
351 RegisterStrings(localized_strings, resources, arraysize(resources));
352 }
353
354 void SyncSetupHandler2::Initialize() {
355 }
356
357 void SyncSetupHandler2::OnGetOAuthTokenSuccess(const std::string& oauth_token) {
358 flow_->OnUserSubmittedOAuth(oauth_token);
359 }
360
361 void SyncSetupHandler2::OnGetOAuthTokenFailure(
362 const GoogleServiceAuthError& error) {
363 CloseSyncSetup();
364 }
365
366 void SyncSetupHandler2::RegisterMessages() {
367 web_ui()->RegisterMessageCallback("SyncSetupDidClosePage",
368 base::Bind(&SyncSetupHandler2::OnDidClosePage,
369 base::Unretained(this)));
370 web_ui()->RegisterMessageCallback("SyncSetupSubmitAuth",
371 base::Bind(&SyncSetupHandler2::HandleSubmitAuth,
372 base::Unretained(this)));
373 web_ui()->RegisterMessageCallback("SyncSetupConfigure",
374 base::Bind(&SyncSetupHandler2::HandleConfigure,
375 base::Unretained(this)));
376 web_ui()->RegisterMessageCallback("SyncSetupPassphrase",
377 base::Bind(&SyncSetupHandler2::HandlePassphraseEntry,
378 base::Unretained(this)));
379 web_ui()->RegisterMessageCallback("SyncSetupPassphraseCancel",
380 base::Bind(&SyncSetupHandler2::HandlePassphraseCancel,
381 base::Unretained(this)));
382 web_ui()->RegisterMessageCallback("SyncSetupAttachHandler",
383 base::Bind(&SyncSetupHandler2::HandleAttachHandler,
384 base::Unretained(this)));
385 web_ui()->RegisterMessageCallback("SyncSetupShowErrorUI",
386 base::Bind(&SyncSetupHandler2::HandleShowErrorUI,
387 base::Unretained(this)));
388 web_ui()->RegisterMessageCallback("SyncSetupShowSetupUI",
389 base::Bind(&SyncSetupHandler2::HandleShowSetupUI,
390 base::Unretained(this)));
391 }
392
393 // Ideal(?) solution here would be to mimic the ClientLogin overlay. Since
394 // this UI must render an external URL, that overlay cannot be used directly.
395 // The current implementation is functional, but fails asthetically.
396 // TODO(rickcam): Bug 90711: Update UI for OAuth sign-in flow
397 void SyncSetupHandler2::ShowOAuthLogin() {
398 DCHECK(browser_sync::IsUsingOAuth());
399
400 Profile* profile = Profile::FromWebUI(web_ui());
401 oauth_login_.reset(new GaiaOAuthFetcher(this,
402 profile->GetRequestContext(),
403 profile,
404 GaiaConstants::kSyncServiceOAuth));
405 oauth_login_->SetAutoFetchLimit(GaiaOAuthFetcher::OAUTH1_REQUEST_TOKEN);
406 oauth_login_->StartGetOAuthToken();
407 }
408
409 void SyncSetupHandler2::ShowGaiaLogin(const DictionaryValue& args) {
410 DCHECK(!browser_sync::IsUsingOAuth());
411 StringValue page("login");
412 web_ui()->CallJavascriptFunction(
413 "SyncSetupOverlay.showSyncSetupPage", page, args);
414 }
415
416 void SyncSetupHandler2::ShowGaiaSuccessAndClose() {
417 web_ui()->CallJavascriptFunction("SyncSetupOverlay.showSuccessAndClose");
418 }
419
420 void SyncSetupHandler2::ShowGaiaSuccessAndSettingUp() {
421 web_ui()->CallJavascriptFunction("SyncSetupOverlay.showSuccessAndSettingUp");
422 }
423
424 void SyncSetupHandler2::ShowConfigure(const DictionaryValue& args) {
425 StringValue page("configure");
426 web_ui()->CallJavascriptFunction(
427 "SyncSetupOverlay.showSyncSetupPage", page, args);
428 }
429
430 void SyncSetupHandler2::ShowPassphraseEntry(const DictionaryValue& args) {
431 StringValue page("passphrase");
432 web_ui()->CallJavascriptFunction(
433 "SyncSetupOverlay.showSyncSetupPage", page, args);
434 }
435
436 void SyncSetupHandler2::ShowSettingUp() {
437 StringValue page("settingUp");
438 web_ui()->CallJavascriptFunction(
439 "SyncSetupOverlay.showSyncSetupPage", page);
440 }
441
442 void SyncSetupHandler2::ShowSetupDone(const string16& user) {
443 StringValue page("done");
444 web_ui()->CallJavascriptFunction(
445 "SyncSetupOverlay.showSyncSetupPage", page);
446
447 // Suppress the sync promo once the user signs into sync. This way the user
448 // doesn't see the sync promo even if they sign out of sync later on.
449 SyncPromoUI::SetUserSkippedSyncPromo(Profile::FromWebUI(web_ui()));
450
451 Profile* profile = Profile::FromWebUI(web_ui());
452 ProfileSyncService* service = profile->GetProfileSyncService();
453 if (!service->HasSyncSetupCompleted()) {
454 FilePath profile_file_path = profile->GetPath();
455 ProfileMetrics::LogProfileSyncSignIn(profile_file_path);
456 }
457 }
458
459 void SyncSetupHandler2::SetFlow(SyncSetupFlow* flow) {
460 flow_ = flow;
461 }
462
463 void SyncSetupHandler2::Focus() {
464 web_ui()->GetWebContents()->GetRenderViewHost()->delegate()->Activate();
465 }
466
467 void SyncSetupHandler2::OnDidClosePage(const ListValue* args) {
468 CloseSyncSetup();
469 }
470
471 void SyncSetupHandler2::HandleSubmitAuth(const ListValue* args) {
472 std::string json;
473 if (!args->GetString(0, &json)) {
474 NOTREACHED() << "Could not read JSON argument";
475 return;
476 }
477
478 if (json.empty())
479 return;
480
481 std::string username, password, captcha, access_code;
482 if (!GetAuthData(json, &username, &password, &captcha, &access_code)) {
483 // The page sent us something that we didn't understand.
484 // This probably indicates a programming error.
485 NOTREACHED();
486 return;
487 }
488
489 string16 error_message;
490 if (!IsLoginAuthDataValid(username, &error_message)) {
491 ShowLoginErrorMessage(error_message);
492 return;
493 }
494
495 if (flow_)
496 flow_->OnUserSubmittedAuth(username, password, captcha, access_code);
497 }
498
499 void SyncSetupHandler2::HandleConfigure(const ListValue* args) {
500 std::string json;
501 if (!args->GetString(0, &json)) {
502 NOTREACHED() << "Could not read JSON argument";
503 return;
504 }
505 if (json.empty()) {
506 NOTREACHED();
507 return;
508 }
509
510 SyncConfiguration configuration;
511 if (!GetConfiguration(json, &configuration)) {
512 // The page sent us something that we didn't understand.
513 // This probably indicates a programming error.
514 NOTREACHED();
515 return;
516 }
517
518 DCHECK(flow_);
519 flow_->OnUserConfigured(configuration);
520
521 ProfileMetrics::LogProfileSyncInfo(ProfileMetrics::SYNC_CUSTOMIZE);
522 if (configuration.encrypt_all) {
523 ProfileMetrics::LogProfileSyncInfo(ProfileMetrics::SYNC_ENCRYPT);
524 }
525 if (configuration.set_secondary_passphrase) {
526 ProfileMetrics::LogProfileSyncInfo(ProfileMetrics::SYNC_PASSPHRASE);
527 }
528 if (!configuration.sync_everything) {
529 ProfileMetrics::LogProfileSyncInfo(ProfileMetrics::SYNC_CHOOSE);
530 }
531 }
532
533 void SyncSetupHandler2::HandlePassphraseEntry(const ListValue* args) {
534 std::string json;
535 if (!args->GetString(0, &json)) {
536 NOTREACHED() << "Could not read JSON argument";
537 return;
538 }
539
540 if (json.empty())
541 return;
542
543 std::string passphrase;
544 if (!GetPassphrase(json, &passphrase)) {
545 // Couldn't understand what the page sent. Indicates a programming error.
546 NOTREACHED();
547 return;
548 }
549
550 DCHECK(flow_);
551 flow_->OnPassphraseEntry(passphrase);
552 }
553
554 void SyncSetupHandler2::HandlePassphraseCancel(const ListValue* args) {
555 DCHECK(flow_);
556 flow_->OnPassphraseCancel();
557 }
558
559 void SyncSetupHandler2::HandleAttachHandler(const ListValue* args) {
560 OpenSyncSetup();
561 }
562
563 void SyncSetupHandler2::HandleShowErrorUI(const ListValue* args) {
564 DCHECK(!flow_);
565
566 Profile* profile = Profile::FromWebUI(web_ui());
567 ProfileSyncService* service = profile->GetProfileSyncService();
568 DCHECK(service);
569
570 service->ShowErrorUI();
571 }
572
573 void SyncSetupHandler2::HandleShowSetupUI(const ListValue* args) {
574 DCHECK(!flow_);
575 if (FocusExistingWizard()) {
576 CloseOverlay();
577 return;
578 }
579 ShowSetupUI();
580 }
581
582 void SyncSetupHandler2::CloseSyncSetup() {
583 if (flow_) {
584 flow_->OnDialogClosed(std::string());
585 flow_ = NULL;
586 }
587 }
588
589 void SyncSetupHandler2::OpenSyncSetup() {
590 DCHECK(!flow_);
591
592 Profile* profile = Profile::FromWebUI(web_ui());
593 ProfileSyncService* service = profile->GetProfileSyncService();
594 if (!service) {
595 // If there's no sync service, the user tried to manually invoke a syncSetup
596 // URL, but sync features are disabled. We need to close the overlay for
597 // this (rare) case.
598 CloseOverlay();
599 return;
600 }
601
602 // If the wizard is already visible, it must be attached to another flow
603 // handler.
604 if (FocusExistingWizard()) {
605 CloseOverlay();
606 return;
607 }
608
609 // Attach this as the sync setup handler, before calling ShowSetupUI().
610 if (!service->get_wizard().AttachSyncSetupHandler(this)) {
611 LOG(ERROR) << "SyncSetupHandler attach failed!";
612 CloseOverlay();
613 return;
614 }
615
616 ShowSetupUI();
617 }
618
619 // Private member functions.
620
621 bool SyncSetupHandler2::FocusExistingWizard() {
622 Profile* profile = Profile::FromWebUI(web_ui());
623 ProfileSyncService* service = profile->GetProfileSyncService();
624 if (!service)
625 return false;
626
627 // If the wizard is already visible, focus it.
628 if (service->get_wizard().IsVisible()) {
629 service->get_wizard().Focus();
630 return true;
631 }
632 return false;
633 }
634
635 void SyncSetupHandler2::CloseOverlay() {
636 web_ui()->CallJavascriptFunction("OptionsPage.closeOverlay");
637 }
638
639 bool SyncSetupHandler2::IsLoginAuthDataValid(const std::string& username,
640 string16* error_message) {
641 // Happens during unit tests.
642 if (!web_ui() || !profile_manager_)
643 return true;
644
645 if (username.empty())
646 return true;
647
648 // Check if the username is already in use by another profile.
649 const ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache();
650 size_t current_profile_index = cache.GetIndexOfProfileWithPath(
651 Profile::FromWebUI(web_ui())->GetPath());
652 string16 username_utf16 = UTF8ToUTF16(username);
653
654 for (size_t i = 0; i < cache.GetNumberOfProfiles(); ++i) {
655 if (i != current_profile_index && AreUserNamesEqual(
656 cache.GetUserNameOfProfileAtIndex(i), username_utf16)) {
657 *error_message = l10n_util::GetStringUTF16(
658 IDS_SYNC_USER_NAME_IN_USE_ERROR);
659 return false;
660 }
661 }
662
663 return true;
664 }
665
666 void SyncSetupHandler2::ShowLoginErrorMessage(const string16& error_message) {
667 DCHECK(flow_);
668 DictionaryValue args;
669 flow_->GetArgsForGaiaLogin(&args);
670 args.SetString("error_message", error_message);
671 ShowGaiaLogin(args);
672 }
673
674 } // namespace options2
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/sync_setup_handler2.h ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698