This source file includes following definitions.
- passphrase_is_gaia
- GetSelectableTypeNameMap
- GetConfiguration
- profile_manager_
- GetLocalizedValues
- GetStaticLocalizedValues
- DisplayConfigureSync
- ConfigureSyncDone
- IsActiveLogin
- RegisterMessages
- DisplayGaiaLogin
- DisplayGaiaLoginInNewTabOrWindow
- PrepareSyncSetup
- DisplaySpinner
- DisplayTimeout
- OnDidClosePage
- SyncStartupFailed
- SyncStartupCompleted
- GetProfile
- GetSyncService
- HandleConfigure
- HandleShowSetupUI
- HandleDoSignOutOnAuthError
- HandleStartSignin
- HandleStopSyncing
- HandleCloseTimeout
- CloseSyncSetup
- OpenSyncSetup
- OpenConfigureSync
- FocusUI
- CloseUI
- IsExistingWizardPresent
- FocusExistingWizardIfPresent
- GetLoginUIService
#include "chrome/browser/ui/webui/sync_setup_handler.h"
#include "base/basictypes.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/i18n/time_formatting.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/metrics/histogram.h"
#include "base/prefs/pref_service.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/google/google_util.h"
#include "chrome/browser/lifetime/application_lifetime.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_info_cache.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profile_metrics.h"
#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
#include "chrome/browser/signin/signin_manager_factory.h"
#include "chrome/browser/signin/signin_promo.h"
#include "chrome/browser/sync/profile_sync_service.h"
#include "chrome/browser/sync/profile_sync_service_factory.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_navigator.h"
#include "chrome/browser/ui/singleton_tabs.h"
#include "chrome/browser/ui/sync/signin_histogram.h"
#include "chrome/browser/ui/webui/signin/login_ui_service.h"
#include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "components/signin/core/browser/profile_oauth2_token_service.h"
#include "components/signin/core/browser/signin_error_controller.h"
#include "components/sync_driver/sync_prefs.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_delegate.h"
#include "google_apis/gaia/gaia_auth_util.h"
#include "google_apis/gaia/gaia_constants.h"
#include "grit/chromium_strings.h"
#include "grit/generated_resources.h"
#include "grit/locale_settings.h"
#include "net/base/url_util.h"
#include "ui/base/l10n/l10n_util.h"
#if defined(OS_CHROMEOS)
#include "components/signin/core/browser/signin_manager_base.h"
#else
#include "components/signin/core/browser/signin_manager.h"
#endif
using content::WebContents;
using l10n_util::GetStringFUTF16;
using l10n_util::GetStringUTF16;
namespace {
struct SyncConfigInfo {
SyncConfigInfo();
~SyncConfigInfo();
bool encrypt_all;
bool sync_everything;
bool sync_nothing;
syncer::ModelTypeSet data_types;
std::string passphrase;
bool passphrase_is_gaia;
};
SyncConfigInfo::SyncConfigInfo()
: encrypt_all(false),
sync_everything(false),
sync_nothing(false),
passphrase_is_gaia(false) {
}
SyncConfigInfo::~SyncConfigInfo() {}
const char* kDataTypeNames[] = {
"bookmarks",
"preferences",
"passwords",
"autofill",
"themes",
"typedUrls",
"extensions",
"apps",
"tabs"
};
COMPILE_ASSERT(32 == syncer::MODEL_TYPE_COUNT,
update_kDataTypeNames_to_match_UserSelectableTypes);
typedef std::map<syncer::ModelType, const char*> ModelTypeNameMap;
ModelTypeNameMap GetSelectableTypeNameMap() {
ModelTypeNameMap type_names;
syncer::ModelTypeSet type_set = syncer::UserSelectableTypes();
syncer::ModelTypeSet::Iterator it = type_set.First();
DCHECK_EQ(arraysize(kDataTypeNames), type_set.Size());
for (size_t i = 0; i < arraysize(kDataTypeNames) && it.Good();
++i, it.Inc()) {
type_names[it.Get()] = kDataTypeNames[i];
}
return type_names;
}
bool GetConfiguration(const std::string& json, SyncConfigInfo* config) {
scoped_ptr<base::Value> parsed_value(base::JSONReader::Read(json));
base::DictionaryValue* result;
if (!parsed_value || !parsed_value->GetAsDictionary(&result)) {
DLOG(ERROR) << "GetConfiguration() not passed a Dictionary";
return false;
}
if (!result->GetBoolean("syncAllDataTypes", &config->sync_everything)) {
DLOG(ERROR) << "GetConfiguration() not passed a syncAllDataTypes value";
return false;
}
if (!result->GetBoolean("syncNothing", &config->sync_nothing)) {
DLOG(ERROR) << "GetConfiguration() not passed a syncNothing value";
return false;
}
DCHECK(!(config->sync_everything && config->sync_nothing))
<< "syncAllDataTypes and syncNothing cannot both be true";
ModelTypeNameMap type_names = GetSelectableTypeNameMap();
for (ModelTypeNameMap::const_iterator it = type_names.begin();
it != type_names.end(); ++it) {
std::string key_name = it->second + std::string("Synced");
bool sync_value;
if (!result->GetBoolean(key_name, &sync_value)) {
DLOG(ERROR) << "GetConfiguration() not passed a value for " << key_name;
return false;
}
if (sync_value)
config->data_types.Put(it->first);
}
if (!result->GetBoolean("encryptAllData", &config->encrypt_all)) {
DLOG(ERROR) << "GetConfiguration() not passed a value for encryptAllData";
return false;
}
bool have_passphrase;
if (!result->GetBoolean("usePassphrase", &have_passphrase)) {
DLOG(ERROR) << "GetConfiguration() not passed a usePassphrase value";
return false;
}
if (have_passphrase) {
if (!result->GetBoolean("isGooglePassphrase",
&config->passphrase_is_gaia)) {
DLOG(ERROR) << "GetConfiguration() not passed isGooglePassphrase value";
return false;
}
if (!result->GetString("passphrase", &config->passphrase)) {
DLOG(ERROR) << "GetConfiguration() not passed a passphrase value";
return false;
}
}
return true;
}
}
SyncSetupHandler::SyncSetupHandler(ProfileManager* profile_manager)
: configuring_sync_(false),
profile_manager_(profile_manager) {
}
SyncSetupHandler::~SyncSetupHandler() {
if (!web_ui())
return;
CloseSyncSetup();
}
void SyncSetupHandler::GetLocalizedValues(
base::DictionaryValue* localized_strings) {
GetStaticLocalizedValues(localized_strings, web_ui());
}
void SyncSetupHandler::GetStaticLocalizedValues(
base::DictionaryValue* localized_strings,
content::WebUI* web_ui) {
DCHECK(localized_strings);
base::string16 product_name(GetStringUTF16(IDS_PRODUCT_NAME));
localized_strings->SetString(
"chooseDataTypesInstructions",
GetStringFUTF16(IDS_SYNC_CHOOSE_DATATYPES_INSTRUCTIONS, product_name));
localized_strings->SetString(
"encryptionInstructions",
GetStringFUTF16(IDS_SYNC_ENCRYPTION_INSTRUCTIONS, product_name));
localized_strings->SetString(
"encryptionHelpURL", chrome::kSyncEncryptionHelpURL);
localized_strings->SetString(
"encryptionSectionMessage",
GetStringFUTF16(IDS_SYNC_ENCRYPTION_SECTION_MESSAGE, product_name));
localized_strings->SetString(
"passphraseRecover",
GetStringFUTF16(IDS_SYNC_PASSPHRASE_RECOVER,
base::ASCIIToUTF16(
google_util::StringAppendGoogleLocaleParam(
chrome::kSyncGoogleDashboardURL))));
localized_strings->SetString("stopSyncingExplanation",
l10n_util::GetStringFUTF16(
IDS_SYNC_STOP_SYNCING_EXPLANATION_LABEL,
l10n_util::GetStringUTF16(IDS_PRODUCT_NAME),
base::ASCIIToUTF16(google_util::StringAppendGoogleLocaleParam(
chrome::kSyncGoogleDashboardURL))));
localized_strings->SetString("stopSyncingTitle",
l10n_util::GetStringUTF16(IDS_SYNC_STOP_SYNCING_DIALOG_TITLE));
localized_strings->SetString("stopSyncingConfirm",
l10n_util::GetStringUTF16(IDS_SYNC_STOP_SYNCING_CONFIRM_BUTTON_LABEL));
localized_strings->SetString(
"syncEverythingHelpURL", chrome::kSyncEverythingLearnMoreURL);
localized_strings->SetString(
"syncErrorHelpURL", chrome::kSyncErrorsHelpURL);
static OptionsStringResource resources[] = {
{ "syncSetupConfigureTitle", IDS_SYNC_SETUP_CONFIGURE_TITLE },
{ "syncSetupSpinnerTitle", IDS_SYNC_SETUP_SPINNER_TITLE },
{ "syncSetupTimeoutTitle", IDS_SYNC_SETUP_TIME_OUT_TITLE },
{ "syncSetupTimeoutContent", IDS_SYNC_SETUP_TIME_OUT_CONTENT },
{ "errorLearnMore", IDS_LEARN_MORE },
{ "cancel", IDS_CANCEL },
{ "loginSuccess", IDS_SYNC_SUCCESS },
{ "settingUp", IDS_SYNC_LOGIN_SETTING_UP },
{ "syncAllDataTypes", IDS_SYNC_EVERYTHING },
{ "chooseDataTypes", IDS_SYNC_CHOOSE_DATATYPES },
{ "syncNothing", IDS_SYNC_NOTHING },
{ "bookmarks", IDS_SYNC_DATATYPE_BOOKMARKS },
{ "preferences", IDS_SYNC_DATATYPE_PREFERENCES },
{ "autofill", IDS_SYNC_DATATYPE_AUTOFILL },
{ "themes", IDS_SYNC_DATATYPE_THEMES },
{ "passwords", IDS_SYNC_DATATYPE_PASSWORDS },
{ "extensions", IDS_SYNC_DATATYPE_EXTENSIONS },
{ "typedURLs", IDS_SYNC_DATATYPE_TYPED_URLS },
{ "apps", IDS_SYNC_DATATYPE_APPS },
{ "openTabs", IDS_SYNC_DATATYPE_TABS },
{ "serviceUnavailableError", IDS_SYNC_SETUP_ABORTED_BY_PENDING_CLEAR },
{ "confirmLabel", IDS_SYNC_CONFIRM_PASSPHRASE_LABEL },
{ "emptyErrorMessage", IDS_SYNC_EMPTY_PASSPHRASE_ERROR },
{ "mismatchErrorMessage", IDS_SYNC_PASSPHRASE_MISMATCH_ERROR },
{ "customizeLinkLabel", IDS_SYNC_CUSTOMIZE_LINK_LABEL },
{ "confirmSyncPreferences", IDS_SYNC_CONFIRM_SYNC_PREFERENCES },
{ "syncEverything", IDS_SYNC_SYNC_EVERYTHING },
{ "useDefaultSettings", IDS_SYNC_USE_DEFAULT_SETTINGS },
{ "enterPassphraseBody", IDS_SYNC_ENTER_PASSPHRASE_BODY },
{ "enterGooglePassphraseBody", IDS_SYNC_ENTER_GOOGLE_PASSPHRASE_BODY },
{ "passphraseLabel", IDS_SYNC_PASSPHRASE_LABEL },
{ "incorrectPassphrase", IDS_SYNC_INCORRECT_PASSPHRASE },
{ "passphraseWarning", IDS_SYNC_PASSPHRASE_WARNING },
{ "yes", IDS_SYNC_PASSPHRASE_CANCEL_YES },
{ "no", IDS_SYNC_PASSPHRASE_CANCEL_NO },
{ "sectionExplicitMessagePrefix", IDS_SYNC_PASSPHRASE_MSG_EXPLICIT_PREFIX },
{ "sectionExplicitMessagePostfix",
IDS_SYNC_PASSPHRASE_MSG_EXPLICIT_POSTFIX },
{ "promoPageTitle", IDS_SYNC_PROMO_TAB_TITLE },
{ "promoSkipButton", IDS_SYNC_PROMO_SKIP_BUTTON },
{ "promoAdvanced", IDS_SYNC_PROMO_ADVANCED },
{ "promoLearnMore", IDS_LEARN_MORE },
{ "promoTitleShort", IDS_SYNC_PROMO_MESSAGE_TITLE_SHORT },
{ "encryptionSectionTitle", IDS_SYNC_ENCRYPTION_SECTION_TITLE },
{ "basicEncryptionOption", IDS_SYNC_BASIC_ENCRYPTION_DATA },
{ "fullEncryptionOption", IDS_SYNC_FULL_ENCRYPTION_DATA },
};
RegisterStrings(localized_strings, resources, arraysize(resources));
RegisterTitle(localized_strings, "syncSetupOverlay", IDS_SYNC_SETUP_TITLE);
}
void SyncSetupHandler::DisplayConfigureSync(bool show_advanced,
bool passphrase_failed) {
DCHECK(!SigninManagerFactory::GetForProfile(
GetProfile())->GetAuthenticatedUsername().empty());
ProfileSyncService* service = GetSyncService();
DCHECK(service);
if (!service->sync_initialized()) {
service->UnsuppressAndStart();
if (SyncStartupTracker::GetSyncServiceState(GetProfile()) !=
SyncStartupTracker::SYNC_STARTUP_ERROR) {
DisplaySpinner();
}
sync_startup_tracker_.reset(
new SyncStartupTracker(GetProfile(), this));
return;
}
sync_startup_tracker_.reset();
configuring_sync_ = true;
DCHECK(service->sync_initialized()) <<
"Cannot configure sync until the sync backend is initialized";
base::DictionaryValue args;
const syncer::ModelTypeSet registered_types =
service->GetRegisteredDataTypes();
const syncer::ModelTypeSet preferred_types =
service->GetPreferredDataTypes();
ModelTypeNameMap type_names = GetSelectableTypeNameMap();
for (ModelTypeNameMap::const_iterator it = type_names.begin();
it != type_names.end(); ++it) {
syncer::ModelType sync_type = it->first;
const std::string key_name = it->second;
args.SetBoolean(key_name + "Registered",
registered_types.Has(sync_type));
args.SetBoolean(key_name + "Synced", preferred_types.Has(sync_type));
}
sync_driver::SyncPrefs sync_prefs(GetProfile()->GetPrefs());
args.SetBoolean("passphraseFailed", passphrase_failed);
args.SetBoolean("showSyncEverythingPage", !show_advanced);
args.SetBoolean("syncAllDataTypes", sync_prefs.HasKeepEverythingSynced());
args.SetBoolean("syncNothing", false);
args.SetBoolean("encryptAllData", service->EncryptEverythingEnabled());
args.SetBoolean("showPassphrase", service->IsPassphraseRequired());
args.SetBoolean("usePassphrase",
service->GetPassphraseType() == syncer::CUSTOM_PASSPHRASE);
base::Time passphrase_time = service->GetExplicitPassphraseTime();
syncer::PassphraseType passphrase_type = service->GetPassphraseType();
if (!passphrase_time.is_null()) {
base::string16 passphrase_time_str =
base::TimeFormatShortDate(passphrase_time);
args.SetString(
"enterPassphraseBody",
GetStringFUTF16(IDS_SYNC_ENTER_PASSPHRASE_BODY_WITH_DATE,
passphrase_time_str));
args.SetString(
"enterGooglePassphraseBody",
GetStringFUTF16(IDS_SYNC_ENTER_GOOGLE_PASSPHRASE_BODY_WITH_DATE,
passphrase_time_str));
switch (passphrase_type) {
case syncer::FROZEN_IMPLICIT_PASSPHRASE:
args.SetString(
"fullEncryptionBody",
GetStringFUTF16(IDS_SYNC_FULL_ENCRYPTION_BODY_GOOGLE_WITH_DATE,
passphrase_time_str));
break;
case syncer::CUSTOM_PASSPHRASE:
args.SetString(
"fullEncryptionBody",
GetStringFUTF16(IDS_SYNC_FULL_ENCRYPTION_BODY_CUSTOM_WITH_DATE,
passphrase_time_str));
break;
default:
args.SetString(
"fullEncryptionBody",
GetStringUTF16(IDS_SYNC_FULL_ENCRYPTION_BODY_CUSTOM));
break;
}
} else if (passphrase_type == syncer::CUSTOM_PASSPHRASE) {
args.SetString(
"fullEncryptionBody",
GetStringUTF16(IDS_SYNC_FULL_ENCRYPTION_BODY_CUSTOM));
} else {
args.SetString(
"fullEncryptionBody",
GetStringUTF16(IDS_SYNC_FULL_ENCRYPTION_DATA));
}
base::StringValue page("configure");
web_ui()->CallJavascriptFunction(
"SyncSetupOverlay.showSyncSetupPage", page, args);
FocusUI();
}
void SyncSetupHandler::ConfigureSyncDone() {
base::StringValue page("done");
web_ui()->CallJavascriptFunction(
"SyncSetupOverlay.showSyncSetupPage", page);
signin::SetUserSkippedPromo(GetProfile());
ProfileSyncService* service = GetSyncService();
DCHECK(service);
if (!service->HasSyncSetupCompleted()) {
base::FilePath profile_file_path = GetProfile()->GetPath();
ProfileMetrics::LogProfileSyncSignIn(profile_file_path);
service->SetSetupInProgress(false);
service->SetSyncSetupCompleted();
}
}
bool SyncSetupHandler::IsActiveLogin() const {
LoginUIService* service = GetLoginUIService();
return service && (service->current_login_ui() == this);
}
void SyncSetupHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback(
"SyncSetupDidClosePage",
base::Bind(&SyncSetupHandler::OnDidClosePage,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"SyncSetupConfigure",
base::Bind(&SyncSetupHandler::HandleConfigure,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"SyncSetupShowSetupUI",
base::Bind(&SyncSetupHandler::HandleShowSetupUI,
base::Unretained(this)));
web_ui()->RegisterMessageCallback("CloseTimeout",
base::Bind(&SyncSetupHandler::HandleCloseTimeout,
base::Unretained(this)));
#if defined(OS_CHROMEOS)
web_ui()->RegisterMessageCallback(
"SyncSetupDoSignOutOnAuthError",
base::Bind(&SyncSetupHandler::HandleDoSignOutOnAuthError,
base::Unretained(this)));
#else
web_ui()->RegisterMessageCallback("SyncSetupStopSyncing",
base::Bind(&SyncSetupHandler::HandleStopSyncing,
base::Unretained(this)));
web_ui()->RegisterMessageCallback("SyncSetupStartSignIn",
base::Bind(&SyncSetupHandler::HandleStartSignin,
base::Unretained(this)));
#endif
}
#if !defined(OS_CHROMEOS)
void SyncSetupHandler::DisplayGaiaLogin() {
DCHECK(!sync_startup_tracker_);
configuring_sync_ = false;
DisplayGaiaLoginInNewTabOrWindow();
}
void SyncSetupHandler::DisplayGaiaLoginInNewTabOrWindow() {
Browser* browser = chrome::FindBrowserWithWebContents(
web_ui()->GetWebContents());
if (!browser) {
browser = new Browser(Browser::CreateParams(
Browser::TYPE_TABBED, GetProfile(), chrome::GetActiveDesktop()));
}
GURL url;
std::string email = SigninManagerFactory::GetForProfile(
browser->profile())->GetAuthenticatedUsername();
if (!email.empty()) {
UMA_HISTOGRAM_ENUMERATION("Signin.Reauth",
signin::HISTOGRAM_SHOWN,
signin::HISTOGRAM_MAX);
SigninErrorController* error_controller =
ProfileOAuth2TokenServiceFactory::GetForProfile(browser->profile())->
signin_error_controller();
DCHECK(error_controller->HasError());
url = signin::GetReauthURL(browser->profile(),
error_controller->error_account_id());
} else {
url = signin::GetPromoURL(signin::SOURCE_SETTINGS, true);
}
chrome::ShowSingletonTab(browser, url);
}
#endif
bool SyncSetupHandler::PrepareSyncSetup() {
if (FocusExistingWizardIfPresent()) {
if (!IsActiveLogin())
CloseSyncSetup();
return false;
}
GetLoginUIService()->SetLoginUI(this);
ProfileSyncService* service = GetSyncService();
if (service)
service->SetSetupInProgress(true);
return true;
}
void SyncSetupHandler::DisplaySpinner() {
configuring_sync_ = true;
base::StringValue page("spinner");
base::DictionaryValue args;
const int kTimeoutSec = 30;
DCHECK(!backend_start_timer_);
backend_start_timer_.reset(new base::OneShotTimer<SyncSetupHandler>());
backend_start_timer_->Start(FROM_HERE,
base::TimeDelta::FromSeconds(kTimeoutSec),
this, &SyncSetupHandler::DisplayTimeout);
web_ui()->CallJavascriptFunction(
"SyncSetupOverlay.showSyncSetupPage", page, args);
}
void SyncSetupHandler::DisplayTimeout() {
backend_start_timer_.reset();
sync_startup_tracker_.reset();
base::StringValue page("timeout");
base::DictionaryValue args;
web_ui()->CallJavascriptFunction(
"SyncSetupOverlay.showSyncSetupPage", page, args);
}
void SyncSetupHandler::OnDidClosePage(const base::ListValue* args) {
CloseSyncSetup();
}
void SyncSetupHandler::SyncStartupFailed() {
backend_start_timer_.reset();
CloseUI();
}
void SyncSetupHandler::SyncStartupCompleted() {
ProfileSyncService* service = GetSyncService();
DCHECK(service->sync_initialized());
backend_start_timer_.reset();
DisplayConfigureSync(true, false);
}
Profile* SyncSetupHandler::GetProfile() const {
return Profile::FromWebUI(web_ui());
}
ProfileSyncService* SyncSetupHandler::GetSyncService() const {
Profile* profile = GetProfile();
return profile->IsSyncAccessible() ?
ProfileSyncServiceFactory::GetForProfile(GetProfile()) : NULL;
}
void SyncSetupHandler::HandleConfigure(const base::ListValue* args) {
DCHECK(!sync_startup_tracker_);
std::string json;
if (!args->GetString(0, &json)) {
NOTREACHED() << "Could not read JSON argument";
return;
}
if (json.empty()) {
NOTREACHED();
return;
}
SyncConfigInfo configuration;
if (!GetConfiguration(json, &configuration)) {
NOTREACHED();
return;
}
ProfileSyncService* service = GetSyncService();
if (!service || !service->sync_initialized()) {
CloseUI();
return;
}
if (configuration.sync_nothing) {
ProfileSyncService::SyncEvent(
ProfileSyncService::STOP_FROM_ADVANCED_DIALOG);
CloseUI();
service->StopSyncingPermanently();
service->SetSetupInProgress(false);
return;
}
if (configuration.encrypt_all)
service->EnableEncryptEverything();
bool passphrase_failed = false;
if (!configuration.passphrase.empty()) {
if (service->IsPassphraseRequired()) {
passphrase_failed =
!service->SetDecryptionPassphrase(configuration.passphrase);
} else {
if (!configuration.passphrase_is_gaia &&
!service->IsUsingSecondaryPassphrase()) {
service->SetEncryptionPassphrase(configuration.passphrase,
ProfileSyncService::EXPLICIT);
}
}
}
bool user_was_prompted_for_passphrase =
service->IsPassphraseRequiredForDecryption();
service->OnUserChoseDatatypes(configuration.sync_everything,
configuration.data_types);
if (passphrase_failed || service->IsPassphraseRequiredForDecryption()) {
DisplayConfigureSync(
true, passphrase_failed || user_was_prompted_for_passphrase);
} else {
ConfigureSyncDone();
}
ProfileMetrics::LogProfileSyncInfo(ProfileMetrics::SYNC_CUSTOMIZE);
if (configuration.encrypt_all)
ProfileMetrics::LogProfileSyncInfo(ProfileMetrics::SYNC_ENCRYPT);
if (configuration.passphrase_is_gaia && !configuration.passphrase.empty())
ProfileMetrics::LogProfileSyncInfo(ProfileMetrics::SYNC_PASSPHRASE);
if (!configuration.sync_everything)
ProfileMetrics::LogProfileSyncInfo(ProfileMetrics::SYNC_CHOOSE);
}
void SyncSetupHandler::HandleShowSetupUI(const base::ListValue* args) {
ProfileSyncService* service = GetSyncService();
DCHECK(service);
SigninManagerBase* signin =
SigninManagerFactory::GetForProfile(GetProfile());
if (signin->GetAuthenticatedUsername().empty()) {
DLOG(WARNING) << "Cannot display sync setup UI when not signed in";
CloseUI();
return;
}
if (IsExistingWizardPresent() && !IsActiveLogin()) {
CloseUI();
}
if (!FocusExistingWizardIfPresent())
OpenSyncSetup();
}
#if defined(OS_CHROMEOS)
void SyncSetupHandler::HandleDoSignOutOnAuthError(const base::ListValue* args) {
DVLOG(1) << "Signing out the user to fix a sync error.";
chrome::AttemptUserExit();
}
#endif
#if !defined(OS_CHROMEOS)
void SyncSetupHandler::HandleStartSignin(const base::ListValue* args) {
DCHECK(SigninManagerFactory::GetForProfile(GetProfile())->
GetAuthenticatedUsername().empty());
OpenSyncSetup();
}
void SyncSetupHandler::HandleStopSyncing(const base::ListValue* args) {
if (GetSyncService())
ProfileSyncService::SyncEvent(ProfileSyncService::STOP_FROM_OPTIONS);
SigninManagerFactory::GetForProfile(GetProfile())->SignOut();
}
#endif
void SyncSetupHandler::HandleCloseTimeout(const base::ListValue* args) {
CloseSyncSetup();
}
void SyncSetupHandler::CloseSyncSetup() {
backend_start_timer_.reset();
sync_startup_tracker_.reset();
ProfileSyncService* sync_service = GetSyncService();
if (IsActiveLogin()) {
if (!sync_service || (!sync_service->HasSyncSetupCompleted() &&
sync_service->GetAuthError().state() == GoogleServiceAuthError::NONE)) {
if (configuring_sync_) {
ProfileSyncService::SyncEvent(
ProfileSyncService::CANCEL_DURING_CONFIGURE);
}
if (sync_service) {
DVLOG(1) << "Sync setup aborted by user action";
sync_service->StopSyncingPermanently();
#if !defined(OS_CHROMEOS)
if (sync_service->FirstSetupInProgress())
SigninManagerFactory::GetForProfile(GetProfile())->SignOut();
#endif
}
}
GetLoginUIService()->LoginUIClosed(this);
}
if (sync_service)
sync_service->SetSetupInProgress(false);
configuring_sync_ = false;
}
void SyncSetupHandler::OpenSyncSetup() {
if (!PrepareSyncSetup())
return;
#if !defined(OS_CHROMEOS)
SigninManagerBase* signin =
SigninManagerFactory::GetForProfile(GetProfile());
if (signin->GetAuthenticatedUsername().empty() ||
ProfileOAuth2TokenServiceFactory::GetForProfile(GetProfile())->
signin_error_controller()->HasError()) {
CloseUI();
DisplayGaiaLogin();
return;
}
#endif
if (!GetSyncService()) {
DLOG(WARNING) << "Cannot display sync UI when sync is disabled";
CloseUI();
return;
}
DisplayConfigureSync(true, false);
}
void SyncSetupHandler::OpenConfigureSync() {
if (!PrepareSyncSetup())
return;
DisplayConfigureSync(true, false);
}
void SyncSetupHandler::FocusUI() {
DCHECK(IsActiveLogin());
WebContents* web_contents = web_ui()->GetWebContents();
web_contents->GetDelegate()->ActivateContents(web_contents);
}
void SyncSetupHandler::CloseUI() {
CloseSyncSetup();
base::StringValue page("done");
web_ui()->CallJavascriptFunction(
"SyncSetupOverlay.showSyncSetupPage", page);
}
bool SyncSetupHandler::IsExistingWizardPresent() {
LoginUIService* service = GetLoginUIService();
DCHECK(service);
return service->current_login_ui() != NULL;
}
bool SyncSetupHandler::FocusExistingWizardIfPresent() {
if (!IsExistingWizardPresent())
return false;
LoginUIService* service = GetLoginUIService();
DCHECK(service);
service->current_login_ui()->FocusUI();
return true;
}
LoginUIService* SyncSetupHandler::GetLoginUIService() const {
return LoginUIServiceFactory::GetForProfile(GetProfile());
}