This source file includes following definitions.
- ShouldShowActionOnUI
- startup_controller_
- IsSyncEnabledAndLoggedIn
- IsOAuthRefreshTokenAvailable
- Initialize
- TrySyncDatatypePrefRecovery
- StartSyncingWithServer
- RegisterAuthNotifications
- UnregisterAuthNotifications
- RegisterDataTypeController
- RegisterNonBlockingType
- GetSessionModelAssociatorDeprecated
- IsSessionsDataTypeControllerRunning
- GetOpenTabsUIDelegate
- GetFaviconCache
- GetLocalDeviceInfo
- GetDeviceInfo
- GetAllSignedInDevices
- GetLocalSyncCacheGUID
- AddObserverForDeviceInfoChange
- RemoveObserverForDeviceInfoChange
- GetDataTypeControllerStates
- InitSettings
- GetCredentials
- InitializeBackend
- IsEncryptedDatatypeEnabled
- OnSyncConfigureRetry
- OnProtocolEvent
- OnDataTypeRequestsSyncStartup
- StartUpSlowBackendComponents
- OnGetTokenSuccess
- OnGetTokenFailure
- OnRefreshTokenAvailable
- OnRefreshTokenRevoked
- OnRefreshTokensLoaded
- Shutdown
- ShutdownImpl
- DisableForUser
- HasSyncSetupCompleted
- SetSyncSetupCompleted
- UpdateLastSyncedTime
- NotifyObservers
- NotifySyncCycleCompleted
- ClearStaleErrors
- ClearUnrecoverableError
- RegisterNewDataType
- OnUnrecoverableError
- OnUnrecoverableErrorImpl
- DisableBrokenDatatype
- OnBackendInitialized
- OnSyncCycleCompleted
- OnExperimentsChanged
- UpdateAuthErrorState
- ConnectionStatusToAuthError
- OnConnectionStatusChange
- StopSyncingPermanently
- OnPassphraseRequired
- OnPassphraseAccepted
- OnEncryptedTypesChanged
- OnEncryptionComplete
- OnMigrationNeededForTypes
- OnActionableError
- OnConfigureDone
- OnConfigureRetry
- OnConfigureStart
- QuerySyncStatusSummary
- QuerySyncStatusSummaryString
- GetBackendInitializationStateString
- auto_start_enabled
- setup_in_progress
- QueryDetailedSyncStatus
- GetAuthError
- FirstSetupInProgress
- SetSetupInProgress
- sync_initialized
- waiting_for_auth
- current_experiments
- HasUnrecoverableError
- IsPassphraseRequired
- IsPassphraseRequiredForDecryption
- GetLastSyncedTimeString
- UpdateSelectedTypesHistogram
- RefreshSpareBootstrapToken
- OnUserChoseDatatypes
- ChangePreferredDataTypes
- GetActiveDataTypes
- GetPreferredDataTypes
- GetPreferredDirectoryDataTypes
- GetPreferredNonBlockingDataTypes
- GetRegisteredDataTypes
- GetRegisteredDirectoryDataTypes
- GetRegisteredNonBlockingDataTypes
- IsUsingSecondaryPassphrase
- GetPassphraseType
- GetExplicitPassphraseTime
- IsCryptographerReady
- ConfigurePriorityDataTypes
- ConfigureDataTypeManager
- GetUserShare
- GetLastSessionSnapshot
- HasUnsyncedItems
- GetBackendMigratorForTest
- GetModelSafeRoutingInfo
- GetTypeStatusMap
- ActivateDataType
- DeactivateDataType
- ConsumeCachedPassphraseIfPossible
- RequestAccessToken
- SetEncryptionPassphrase
- SetDecryptionPassphrase
- EnableEncryptEverything
- encryption_pending
- EncryptEverythingEnabled
- GetEncryptedDataTypes
- OnSyncManagedPrefChange
- GoogleSigninSucceeded
- GoogleSignedOut
- AddObserver
- RemoveObserver
- AddProtocolEventObserver
- RemoveProtocolEventObserver
- HasObserver
- GetJsController
- SyncEvent
- IsSyncEnabled
- IsManaged
- ShouldPushChanges
- StopAndSuppress
- IsStartSuppressed
- signin
- UnsuppressAndStart
- AcknowledgeSyncedTypes
- ReconfigureDatatypeManager
- failed_data_types_handler
- OnInternalUnrecoverableError
- IsRetryingAccessTokenFetchForTest
- GetAccessTokenForTest
- GetJsEventHandler
- GetSessionsSyncableService
- last_get_token_error
- GetSyncTokenStatus
- OverrideNetworkResourcesForTest
#include "chrome/browser/sync/profile_sync_service.h"
#include <cstddef>
#include <map>
#include <set>
#include <utility>
#include "base/basictypes.h"
#include "base/bind.h"
#include "base/callback.h"
#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/message_loop/message_loop.h"
#include "base/metrics/histogram.h"
#include "base/strings/string16.h"
#include "base/strings/stringprintf.h"
#include "base/threading/thread_restrictions.h"
#include "build/build_config.h"
#include "chrome/browser/bookmarks/enhanced_bookmarks_features.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/defaults.h"
#include "chrome/browser/net/chrome_cookie_notification_details.h"
#include "chrome/browser/prefs/pref_service_syncable.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/services/gcm/gcm_profile_service.h"
#include "chrome/browser/services/gcm/gcm_profile_service_factory.h"
#include "chrome/browser/signin/about_signin_internals_factory.h"
#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
#include "chrome/browser/signin/signin_manager_factory.h"
#include "chrome/browser/sync/backend_migrator.h"
#include "chrome/browser/sync/glue/change_processor.h"
#include "chrome/browser/sync/glue/chrome_report_unrecoverable_error.h"
#include "chrome/browser/sync/glue/device_info.h"
#include "chrome/browser/sync/glue/favicon_cache.h"
#include "chrome/browser/sync/glue/session_data_type_controller.h"
#include "chrome/browser/sync/glue/session_model_associator.h"
#include "chrome/browser/sync/glue/sync_backend_host.h"
#include "chrome/browser/sync/glue/sync_backend_host_impl.h"
#include "chrome/browser/sync/glue/sync_start_util.h"
#include "chrome/browser/sync/glue/synced_device_tracker.h"
#include "chrome/browser/sync/glue/typed_url_data_type_controller.h"
#include "chrome/browser/sync/managed_user_signin_manager_wrapper.h"
#include "chrome/browser/sync/profile_sync_components_factory_impl.h"
#include "chrome/browser/sync/sessions2/notification_service_sessions_router.h"
#include "chrome/browser/sync/sessions2/sessions_sync_manager.h"
#include "chrome/browser/sync/sync_error_controller.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/global_error/global_error_service.h"
#include "chrome/browser/ui/global_error/global_error_service_factory.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/chrome_version_info.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "components/signin/core/browser/about_signin_internals.h"
#include "components/signin/core/browser/profile_oauth2_token_service.h"
#include "components/signin/core/browser/signin_manager.h"
#include "components/sync_driver/data_type_controller.h"
#include "components/sync_driver/pref_names.h"
#include "components/sync_driver/system_encryptor.h"
#include "components/sync_driver/user_selectable_sync_type.h"
#include "components/user_prefs/pref_registry_syncable.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"
#include "google_apis/gaia/gaia_constants.h"
#include "grit/generated_resources.h"
#include "net/cookies/cookie_monster.h"
#include "net/url_request/url_request_context_getter.h"
#include "sync/api/sync_error.h"
#include "sync/internal_api/public/configure_reason.h"
#include "sync/internal_api/public/http_bridge_network_resources.h"
#include "sync/internal_api/public/network_resources.h"
#include "sync/internal_api/public/sync_core_proxy.h"
#include "sync/internal_api/public/sync_encryption_handler.h"
#include "sync/internal_api/public/util/experiments.h"
#include "sync/internal_api/public/util/sync_string_conversions.h"
#include "sync/js/js_arg_list.h"
#include "sync/js/js_event_details.h"
#include "sync/util/cryptographer.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/l10n/time_format.h"
#if defined(ENABLE_MANAGED_USERS)
#include "chrome/browser/managed_mode/managed_user_constants.h"
#endif
#if defined(OS_ANDROID)
#include "sync/internal_api/public/read_transaction.h"
#endif
using browser_sync::ChangeProcessor;
using browser_sync::DataTypeController;
using browser_sync::DataTypeManager;
using browser_sync::FailedDataTypesHandler;
using browser_sync::NotificationServiceSessionsRouter;
using browser_sync::ProfileSyncServiceStartBehavior;
using browser_sync::SyncBackendHost;
using syncer::ModelType;
using syncer::ModelTypeSet;
using syncer::JsBackend;
using syncer::JsController;
using syncer::JsEventDetails;
using syncer::JsEventHandler;
using syncer::ModelSafeRoutingInfo;
using syncer::SyncCredentials;
using syncer::SyncProtocolError;
using syncer::WeakHandle;
typedef GoogleServiceAuthError AuthError;
const char* ProfileSyncService::kSyncServerUrl =
"https://clients4.google.com/chrome-sync";
const char* ProfileSyncService::kDevServerUrl =
"https://clients4.google.com/chrome-sync/dev";
const char kSyncUnrecoverableErrorHistogram[] =
"Sync.UnrecoverableErrors";
const net::BackoffEntry::Policy kRequestAccessTokenBackoffPolicy = {
0,
2000,
2,
0.2,
1000 * 3600 * 4,
-1,
false,
};
bool ShouldShowActionOnUI(
const syncer::SyncProtocolError& error) {
return (error.action != syncer::UNKNOWN_ACTION &&
error.action != syncer::DISABLE_SYNC_ON_CLIENT &&
error.action != syncer::STOP_SYNC_FOR_DISABLED_ACCOUNT);
}
ProfileSyncService::ProfileSyncService(
ProfileSyncComponentsFactory* factory,
Profile* profile,
ManagedUserSigninManagerWrapper* signin_wrapper,
ProfileOAuth2TokenService* oauth2_token_service,
ProfileSyncServiceStartBehavior start_behavior)
: OAuth2TokenService::Consumer("sync"),
last_auth_error_(AuthError::AuthErrorNone()),
passphrase_required_reason_(syncer::REASON_PASSPHRASE_NOT_REQUIRED),
factory_(factory),
profile_(profile),
sync_prefs_(profile_->GetPrefs()),
sync_service_url_(kDevServerUrl),
is_first_time_sync_configure_(false),
backend_initialized_(false),
sync_disabled_by_admin_(false),
is_auth_in_progress_(false),
signin_(signin_wrapper),
unrecoverable_error_reason_(ERROR_REASON_UNSET),
expect_sync_configuration_aborted_(false),
encrypted_types_(syncer::SyncEncryptionHandler::SensitiveTypes()),
encrypt_everything_(false),
encryption_pending_(false),
configure_status_(DataTypeManager::UNKNOWN),
oauth2_token_service_(oauth2_token_service),
request_access_token_backoff_(&kRequestAccessTokenBackoffPolicy),
weak_factory_(this),
startup_controller_weak_factory_(this),
connection_status_(syncer::CONNECTION_NOT_ATTEMPTED),
last_get_token_error_(GoogleServiceAuthError::AuthErrorNone()),
network_resources_(new syncer::HttpBridgeNetworkResources),
startup_controller_(
start_behavior,
oauth2_token_service,
&sync_prefs_,
signin_wrapper,
base::Bind(&ProfileSyncService::StartUpSlowBackendComponents,
startup_controller_weak_factory_.GetWeakPtr())) {
DCHECK(profile);
base::ThreadRestrictions::ScopedAllowIO allow_io;
chrome::VersionInfo::Channel channel = chrome::VersionInfo::GetChannel();
if (channel == chrome::VersionInfo::CHANNEL_STABLE ||
channel == chrome::VersionInfo::CHANNEL_BETA) {
sync_service_url_ = GURL(kSyncServerUrl);
}
if (!CommandLine::ForCurrentProcess()->HasSwitch(
switches::kDisableSyncSessionsV2)) {
syncer::SyncableService::StartSyncFlare flare(
sync_start_util::GetFlareForSyncableService(profile->GetPath()));
scoped_ptr<browser_sync::LocalSessionEventRouter> router(
new NotificationServiceSessionsRouter(profile, flare));
sessions_sync_manager_.reset(
new SessionsSyncManager(profile, this, router.Pass()));
}
}
ProfileSyncService::~ProfileSyncService() {
sync_prefs_.RemoveSyncPrefObserver(this);
CHECK(!backend_initialized_);
}
bool ProfileSyncService::IsSyncEnabledAndLoggedIn() {
if (IsManaged() || sync_prefs_.IsStartSuppressed())
return false;
return !signin_->GetEffectiveUsername().empty();
}
bool ProfileSyncService::IsOAuthRefreshTokenAvailable() {
if (!oauth2_token_service_)
return false;
return oauth2_token_service_->RefreshTokenIsAvailable(
signin_->GetAccountIdToUse());
}
void ProfileSyncService::Initialize() {
InitSettings();
ClearStaleErrors();
sync_prefs_.AddSyncPrefObserver(this);
if (IsManaged()) {
DisableForUser();
return;
}
RegisterAuthNotifications();
if (!HasSyncSetupCompleted() || signin_->GetEffectiveUsername().empty()) {
DisableForUser();
}
TrySyncDatatypePrefRecovery();
last_synced_time_ = sync_prefs_.GetLastSyncedTime();
#if defined(OS_CHROMEOS)
std::string bootstrap_token = sync_prefs_.GetEncryptionBootstrapToken();
if (bootstrap_token.empty()) {
sync_prefs_.SetEncryptionBootstrapToken(
sync_prefs_.GetSpareBootstrapToken());
}
#endif
#if !defined(OS_ANDROID)
DCHECK(sync_error_controller_ == NULL)
<< "Initialize() called more than once.";
sync_error_controller_.reset(new SyncErrorController(this));
AddObserver(sync_error_controller_.get());
#endif
startup_controller_.Reset(GetRegisteredDataTypes());
startup_controller_.TryStart();
}
void ProfileSyncService::TrySyncDatatypePrefRecovery() {
DCHECK(!sync_initialized());
if (!HasSyncSetupCompleted())
return;
PrefService* const pref_service = profile_->GetPrefs();
if (!pref_service)
return;
if (GetPreferredDataTypes().Size() > 1)
return;
const PrefService::Preference* keep_everything_synced =
pref_service->FindPreference(
sync_driver::prefs::kSyncKeepEverythingSynced);
if (!keep_everything_synced->IsDefaultValue())
return;
UMA_HISTOGRAM_COUNTS("Sync.DatatypePrefRecovery", 1);
sync_prefs_.SetKeepEverythingSynced(true);
syncer::ModelTypeSet registered_types = GetRegisteredDataTypes();
sync_prefs_.SetPreferredDataTypes(registered_types,
registered_types);
}
void ProfileSyncService::StartSyncingWithServer() {
if (backend_)
backend_->StartSyncingWithServer();
}
void ProfileSyncService::RegisterAuthNotifications() {
oauth2_token_service_->AddObserver(this);
if (signin())
signin()->AddObserver(this);
}
void ProfileSyncService::UnregisterAuthNotifications() {
if (signin())
signin()->RemoveObserver(this);
oauth2_token_service_->RemoveObserver(this);
}
void ProfileSyncService::RegisterDataTypeController(
DataTypeController* data_type_controller) {
DCHECK_EQ(data_type_controllers_.count(data_type_controller->type()), 0U);
DCHECK(!GetRegisteredNonBlockingDataTypes().Has(
data_type_controller->type()));
data_type_controllers_[data_type_controller->type()] =
data_type_controller;
}
void ProfileSyncService::RegisterNonBlockingType(syncer::ModelType type) {
DCHECK_EQ(data_type_controllers_.count(type), 0U);
DCHECK(!GetRegisteredNonBlockingDataTypes().Has(type));
non_blocking_types_.Put(type);
}
browser_sync::SessionModelAssociator*
ProfileSyncService::GetSessionModelAssociatorDeprecated() {
if (!IsSessionsDataTypeControllerRunning())
return NULL;
if (sessions_sync_manager_.get())
return NULL;
return static_cast<browser_sync::SessionDataTypeController*>(
data_type_controllers_.find(
syncer::SESSIONS)->second.get())->GetModelAssociator();
}
bool ProfileSyncService::IsSessionsDataTypeControllerRunning() const {
return data_type_controllers_.find(syncer::SESSIONS) !=
data_type_controllers_.end() &&
data_type_controllers_.find(syncer::SESSIONS)->second->state() ==
DataTypeController::RUNNING;
}
browser_sync::OpenTabsUIDelegate* ProfileSyncService::GetOpenTabsUIDelegate() {
if (!IsSessionsDataTypeControllerRunning())
return NULL;
if (!CommandLine::ForCurrentProcess()->HasSwitch(
switches::kDisableSyncSessionsV2)) {
return sessions_sync_manager_.get();
} else {
return GetSessionModelAssociatorDeprecated();
}
}
browser_sync::FaviconCache* ProfileSyncService::GetFaviconCache() {
if (!CommandLine::ForCurrentProcess()->HasSwitch(
switches::kDisableSyncSessionsV2)) {
return sessions_sync_manager_->GetFaviconCache();
} else if (GetSessionModelAssociatorDeprecated()) {
return GetSessionModelAssociatorDeprecated()->GetFaviconCache();
} else {
return NULL;
}
}
scoped_ptr<browser_sync::DeviceInfo>
ProfileSyncService::GetLocalDeviceInfo() const {
if (backend_) {
browser_sync::SyncedDeviceTracker* device_tracker =
backend_->GetSyncedDeviceTracker();
if (device_tracker)
return device_tracker->ReadLocalDeviceInfo();
}
return scoped_ptr<browser_sync::DeviceInfo>();
}
scoped_ptr<browser_sync::DeviceInfo>
ProfileSyncService::GetDeviceInfo(const std::string& client_id) const {
if (backend_) {
browser_sync::SyncedDeviceTracker* device_tracker =
backend_->GetSyncedDeviceTracker();
if (device_tracker)
return device_tracker->ReadDeviceInfo(client_id);
}
return scoped_ptr<browser_sync::DeviceInfo>();
}
ScopedVector<browser_sync::DeviceInfo>
ProfileSyncService::GetAllSignedInDevices() const {
ScopedVector<browser_sync::DeviceInfo> devices;
if (backend_) {
browser_sync::SyncedDeviceTracker* device_tracker =
backend_->GetSyncedDeviceTracker();
if (device_tracker) {
device_tracker->GetAllSyncedDeviceInfo(&devices);
}
}
return devices.Pass();
}
std::string ProfileSyncService::GetLocalSyncCacheGUID() const {
if (backend_) {
browser_sync::SyncedDeviceTracker* device_tracker =
backend_->GetSyncedDeviceTracker();
if (device_tracker) {
return device_tracker->cache_guid();
}
}
return std::string();
}
void ProfileSyncService::AddObserverForDeviceInfoChange(
browser_sync::SyncedDeviceTracker::Observer* observer) {
if (backend_) {
browser_sync::SyncedDeviceTracker* device_tracker =
backend_->GetSyncedDeviceTracker();
if (device_tracker) {
device_tracker->AddObserver(observer);
}
}
}
void ProfileSyncService::RemoveObserverForDeviceInfoChange(
browser_sync::SyncedDeviceTracker::Observer* observer) {
if (backend_) {
browser_sync::SyncedDeviceTracker* device_tracker =
backend_->GetSyncedDeviceTracker();
if (device_tracker) {
device_tracker->RemoveObserver(observer);
}
}
}
void ProfileSyncService::GetDataTypeControllerStates(
browser_sync::DataTypeController::StateMap* state_map) const {
for (browser_sync::DataTypeController::TypeMap::const_iterator iter =
data_type_controllers_.begin(); iter != data_type_controllers_.end();
++iter)
(*state_map)[iter->first] = iter->second.get()->state();
}
void ProfileSyncService::InitSettings() {
const CommandLine& command_line = *CommandLine::ForCurrentProcess();
if (command_line.HasSwitch(switches::kSyncServiceURL)) {
std::string value(command_line.GetSwitchValueASCII(
switches::kSyncServiceURL));
if (!value.empty()) {
GURL custom_sync_url(value);
if (custom_sync_url.is_valid()) {
sync_service_url_ = custom_sync_url;
} else {
LOG(WARNING) << "The following sync URL specified at the command-line "
<< "is invalid: " << value;
}
}
}
}
SyncCredentials ProfileSyncService::GetCredentials() {
SyncCredentials credentials;
credentials.email = signin_->GetEffectiveUsername();
DCHECK(!credentials.email.empty());
credentials.sync_token = access_token_;
if (credentials.sync_token.empty())
credentials.sync_token = "credentials_lost";
return credentials;
}
void ProfileSyncService::InitializeBackend(bool delete_stale_data) {
if (!backend_) {
NOTREACHED();
return;
}
SyncCredentials credentials = GetCredentials();
scoped_refptr<net::URLRequestContextGetter> request_context_getter(
profile_->GetRequestContext());
if (delete_stale_data)
ClearStaleErrors();
scoped_ptr<syncer::UnrecoverableErrorHandler>
backend_unrecoverable_error_handler(
new browser_sync::BackendUnrecoverableErrorHandler(
MakeWeakHandle(weak_factory_.GetWeakPtr())));
backend_->Initialize(
this,
sync_thread_.Pass(),
GetJsEventHandler(),
sync_service_url_,
credentials,
delete_stale_data,
scoped_ptr<syncer::SyncManagerFactory>(
new syncer::SyncManagerFactory).Pass(),
backend_unrecoverable_error_handler.Pass(),
&browser_sync::ChromeReportUnrecoverableError,
network_resources_.get());
}
bool ProfileSyncService::IsEncryptedDatatypeEnabled() const {
if (encryption_pending())
return true;
const syncer::ModelTypeSet preferred_types = GetPreferredDataTypes();
const syncer::ModelTypeSet encrypted_types = GetEncryptedDataTypes();
DCHECK(encrypted_types.Has(syncer::PASSWORDS));
return !Intersection(preferred_types, encrypted_types).Empty();
}
void ProfileSyncService::OnSyncConfigureRetry() {
NotifyObservers();
}
void ProfileSyncService::OnProtocolEvent(
const syncer::ProtocolEvent& event) {
FOR_EACH_OBSERVER(browser_sync::ProtocolEventObserver,
protocol_event_observers_,
OnProtocolEvent(event));
}
void ProfileSyncService::OnDataTypeRequestsSyncStartup(
syncer::ModelType type) {
DCHECK(syncer::UserTypes().Has(type));
if (backend_.get()) {
DVLOG(1) << "A data type requested sync startup, but it looks like "
"something else beat it to the punch.";
return;
}
if (!GetPreferredDataTypes().Has(type)) {
DVLOG(1) << "Dropping sync startup request because type "
<< syncer::ModelTypeToString(type) << "not enabled.";
return;
}
startup_controller_.OnDataTypeRequestsSyncStartup(type);
}
void ProfileSyncService::StartUpSlowBackendComponents() {
DCHECK(!backend_);
DCHECK(IsSyncEnabledAndLoggedIn());
DCHECK(!sync_disabled_by_admin_);
backend_.reset(
factory_->CreateSyncBackendHost(
profile_->GetDebugName(),
profile_,
sync_prefs_.AsWeakPtr()));
InitializeBackend(!HasSyncSetupCompleted());
}
void ProfileSyncService::OnGetTokenSuccess(
const OAuth2TokenService::Request* request,
const std::string& access_token,
const base::Time& expiration_time) {
DCHECK_EQ(access_token_request_, request);
access_token_request_.reset();
access_token_ = access_token;
token_receive_time_ = base::Time::Now();
last_get_token_error_ = GoogleServiceAuthError::AuthErrorNone();
if (sync_prefs_.SyncHasAuthError()) {
sync_prefs_.SetSyncAuthError(false);
UMA_HISTOGRAM_ENUMERATION("Sync.SyncAuthError",
AUTH_ERROR_FIXED,
AUTH_ERROR_LIMIT);
}
if (backend_)
backend_->UpdateCredentials(GetCredentials());
else
startup_controller_.TryStart();
}
void ProfileSyncService::OnGetTokenFailure(
const OAuth2TokenService::Request* request,
const GoogleServiceAuthError& error) {
DCHECK_EQ(access_token_request_, request);
DCHECK_NE(error.state(), GoogleServiceAuthError::NONE);
access_token_request_.reset();
last_get_token_error_ = error;
switch (error.state()) {
case GoogleServiceAuthError::CONNECTION_FAILED:
case GoogleServiceAuthError::SERVICE_UNAVAILABLE: {
request_access_token_backoff_.InformOfRequest(false);
next_token_request_time_ = base::Time::Now() +
request_access_token_backoff_.GetTimeUntilRelease();
request_access_token_retry_timer_.Start(
FROM_HERE,
request_access_token_backoff_.GetTimeUntilRelease(),
base::Bind(&ProfileSyncService::RequestAccessToken,
weak_factory_.GetWeakPtr()));
NotifyObservers();
break;
}
case GoogleServiceAuthError::SERVICE_ERROR:
case GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS: {
if (!sync_prefs_.SyncHasAuthError()) {
sync_prefs_.SetSyncAuthError(true);
UMA_HISTOGRAM_ENUMERATION("Sync.SyncAuthError",
AUTH_ERROR_ENCOUNTERED,
AUTH_ERROR_LIMIT);
}
}
default: {
UpdateAuthErrorState(error);
}
}
}
void ProfileSyncService::OnRefreshTokenAvailable(
const std::string& account_id) {
if (account_id == signin_->GetAccountIdToUse())
OnRefreshTokensLoaded();
}
void ProfileSyncService::OnRefreshTokenRevoked(
const std::string& account_id) {
if (!IsOAuthRefreshTokenAvailable()) {
access_token_.clear();
UpdateAuthErrorState(
GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED));
}
}
void ProfileSyncService::OnRefreshTokensLoaded() {
if (backend_) {
RequestAccessToken();
} else {
startup_controller_.TryStart();
}
}
void ProfileSyncService::Shutdown() {
UnregisterAuthNotifications();
ShutdownImpl(browser_sync::SyncBackendHost::STOP);
if (sync_error_controller_) {
RemoveObserver(sync_error_controller_.get());
sync_error_controller_.reset();
}
if (sync_thread_)
sync_thread_->Stop();
}
void ProfileSyncService::ShutdownImpl(
browser_sync::SyncBackendHost::ShutdownOption option) {
if (!backend_)
return;
base::Time shutdown_start_time = base::Time::Now();
backend_->StopSyncingForShutdown();
if (data_type_manager_) {
if (data_type_manager_->state() != DataTypeManager::STOPPED) {
expect_sync_configuration_aborted_ = true;
data_type_manager_->Stop();
}
data_type_manager_.reset();
}
migrator_.reset();
sync_js_controller_.AttachJsBackend(WeakHandle<syncer::JsBackend>());
scoped_ptr<SyncBackendHost> doomed_backend(backend_.release());
if (doomed_backend) {
sync_thread_ = doomed_backend->Shutdown(option);
doomed_backend.reset();
}
base::TimeDelta shutdown_time = base::Time::Now() - shutdown_start_time;
UMA_HISTOGRAM_TIMES("Sync.Shutdown.BackendDestroyedTime", shutdown_time);
weak_factory_.InvalidateWeakPtrs();
startup_controller_.Reset(GetRegisteredDataTypes());
expect_sync_configuration_aborted_ = false;
is_auth_in_progress_ = false;
backend_initialized_ = false;
cached_passphrase_.clear();
access_token_.clear();
encryption_pending_ = false;
encrypt_everything_ = false;
encrypted_types_ = syncer::SyncEncryptionHandler::SensitiveTypes();
passphrase_required_reason_ = syncer::REASON_PASSPHRASE_NOT_REQUIRED;
request_access_token_retry_timer_.Stop();
if (last_auth_error_.state() != GoogleServiceAuthError::NONE)
UpdateAuthErrorState(GoogleServiceAuthError::AuthErrorNone());
NotifyObservers();
}
void ProfileSyncService::DisableForUser() {
sync_prefs_.ClearPreferences();
ClearUnrecoverableError();
ShutdownImpl(browser_sync::SyncBackendHost::DISABLE_AND_CLAIM_THREAD);
}
bool ProfileSyncService::HasSyncSetupCompleted() const {
return sync_prefs_.HasSyncSetupCompleted();
}
void ProfileSyncService::SetSyncSetupCompleted() {
sync_prefs_.SetSyncSetupCompleted();
}
void ProfileSyncService::UpdateLastSyncedTime() {
last_synced_time_ = base::Time::Now();
sync_prefs_.SetLastSyncedTime(last_synced_time_);
}
void ProfileSyncService::NotifyObservers() {
FOR_EACH_OBSERVER(ProfileSyncServiceBase::Observer, observers_,
OnStateChanged());
}
void ProfileSyncService::NotifySyncCycleCompleted() {
FOR_EACH_OBSERVER(ProfileSyncServiceBase::Observer, observers_,
OnSyncCycleCompleted());
}
void ProfileSyncService::ClearStaleErrors() {
ClearUnrecoverableError();
last_actionable_error_ = SyncProtocolError();
failed_data_types_handler_.Reset();
}
void ProfileSyncService::ClearUnrecoverableError() {
unrecoverable_error_reason_ = ERROR_REASON_UNSET;
unrecoverable_error_message_.clear();
unrecoverable_error_location_ = tracked_objects::Location();
}
void ProfileSyncService::RegisterNewDataType(syncer::ModelType data_type) {
if (data_type_controllers_.count(data_type) > 0)
return;
NOTREACHED();
}
void ProfileSyncService::OnUnrecoverableError(
const tracked_objects::Location& from_here,
const std::string& message) {
unrecoverable_error_reason_ = ERROR_REASON_SYNCER;
OnUnrecoverableErrorImpl(from_here, message, true);
}
void ProfileSyncService::OnUnrecoverableErrorImpl(
const tracked_objects::Location& from_here,
const std::string& message,
bool delete_sync_database) {
DCHECK(HasUnrecoverableError());
unrecoverable_error_message_ = message;
unrecoverable_error_location_ = from_here;
UMA_HISTOGRAM_ENUMERATION(kSyncUnrecoverableErrorHistogram,
unrecoverable_error_reason_,
ERROR_REASON_LIMIT);
NotifyObservers();
std::string location;
from_here.Write(true, true, &location);
LOG(ERROR)
<< "Unrecoverable error detected at " << location
<< " -- ProfileSyncService unusable: " << message;
base::MessageLoop::current()->PostTask(FROM_HERE,
base::Bind(&ProfileSyncService::ShutdownImpl,
weak_factory_.GetWeakPtr(),
delete_sync_database ?
browser_sync::SyncBackendHost::DISABLE_AND_CLAIM_THREAD :
browser_sync::SyncBackendHost::STOP_AND_CLAIM_THREAD));
}
void ProfileSyncService::DisableBrokenDatatype(
syncer::ModelType type,
const tracked_objects::Location& from_here,
std::string message) {
DeactivateDataType(type);
syncer::SyncError error(from_here,
syncer::SyncError::DATATYPE_ERROR,
message,
type);
std::map<syncer::ModelType, syncer::SyncError> errors;
errors[type] = error;
failed_data_types_handler_.UpdateFailedDataTypes(errors);
base::MessageLoop::current()->PostTask(FROM_HERE,
base::Bind(&ProfileSyncService::ReconfigureDatatypeManager,
weak_factory_.GetWeakPtr()));
}
void ProfileSyncService::OnBackendInitialized(
const syncer::WeakHandle<syncer::JsBackend>& js_backend,
const syncer::WeakHandle<syncer::DataTypeDebugInfoListener>&
debug_info_listener,
bool success) {
is_first_time_sync_configure_ = !HasSyncSetupCompleted();
if (is_first_time_sync_configure_) {
UMA_HISTOGRAM_BOOLEAN("Sync.BackendInitializeFirstTimeSuccess", success);
} else {
UMA_HISTOGRAM_BOOLEAN("Sync.BackendInitializeRestoreSuccess", success);
}
base::Time on_backend_initialized_time = base::Time::Now();
base::TimeDelta delta = on_backend_initialized_time -
startup_controller_.start_backend_time();
if (is_first_time_sync_configure_) {
UMA_HISTOGRAM_LONG_TIMES("Sync.BackendInitializeFirstTime", delta);
} else {
UMA_HISTOGRAM_LONG_TIMES("Sync.BackendInitializeRestoreTime", delta);
}
if (!success) {
OnInternalUnrecoverableError(FROM_HERE,
"BackendInitialize failure",
false,
ERROR_REASON_BACKEND_INIT_FAILURE);
return;
}
backend_initialized_ = true;
sync_js_controller_.AttachJsBackend(js_backend);
debug_info_listener_ = debug_info_listener;
if (protocol_event_observers_.might_have_observers()) {
backend_->RequestBufferedProtocolEventsAndEnableForwarding();
}
syncer::SyncCoreProxy sync_core_proxy_ = backend_->GetSyncCoreProxy();
ConsumeCachedPassphraseIfPossible();
if (last_synced_time_.is_null()) {
UpdateLastSyncedTime();
}
if (startup_controller_.auto_start_enabled() && !FirstSetupInProgress()) {
SetSyncSetupCompleted();
}
if (HasSyncSetupCompleted()) {
ConfigureDataTypeManager();
} else {
DCHECK(FirstSetupInProgress());
}
NotifyObservers();
}
void ProfileSyncService::OnSyncCycleCompleted() {
UpdateLastSyncedTime();
if (IsSessionsDataTypeControllerRunning()) {
if (sessions_sync_manager_) {
base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
&browser_sync::SessionsSyncManager::DoGarbageCollection,
base::AsWeakPtr(sessions_sync_manager_.get())));
} else {
base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
&browser_sync::SessionModelAssociator::DeleteStaleSessions,
GetSessionModelAssociatorDeprecated()->AsWeakPtr()));
}
}
DVLOG(2) << "Notifying observers sync cycle completed";
NotifySyncCycleCompleted();
}
void ProfileSyncService::OnExperimentsChanged(
const syncer::Experiments& experiments) {
if (current_experiments_.Matches(experiments))
return;
current_experiments_ = experiments;
if (experiments.gcm_channel_state != syncer::Experiments::UNSET) {
profile()->GetPrefs()->SetBoolean(prefs::kGCMChannelEnabled,
experiments.gcm_channel_state ==
syncer::Experiments::ENABLED);
gcm::GCMProfileService* gcm_profile_service =
gcm::GCMProfileServiceFactory::GetForProfile(profile());
if (gcm_profile_service) {
if (experiments.gcm_channel_state == syncer::Experiments::SUPPRESSED)
gcm_profile_service->Stop();
else
gcm_profile_service->Start();
}
} else {
profile()->GetPrefs()->ClearPref(prefs::kGCMChannelEnabled);
}
profile()->GetPrefs()->SetBoolean(prefs::kInvalidationServiceUseGCMChannel,
experiments.gcm_invalidations_enabled);
int bookmarks_experiment_state_before = profile_->GetPrefs()->GetInteger(
sync_driver::prefs::kEnhancedBookmarksExperimentEnabled);
if ((CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
switches::kEnhancedBookmarksExperiment) != "0")) {
profile_->GetPrefs()->SetInteger(
sync_driver::prefs::kEnhancedBookmarksExperimentEnabled,
experiments.enhanced_bookmarks_enabled ? kBookmarksExperimentEnabled
: kNoBookmarksExperiment);
profile_->GetPrefs()->SetString(
sync_driver::prefs::kEnhancedBookmarksExtensionId,
experiments.enhanced_bookmarks_ext_id);
} else {
if (experiments.enhanced_bookmarks_enabled) {
profile_->GetPrefs()->SetInteger(
sync_driver::prefs::kEnhancedBookmarksExperimentEnabled,
kBookmarksExperimentEnabledUserOptOut);
profile_->GetPrefs()->SetString(
sync_driver::prefs::kEnhancedBookmarksExtensionId,
experiments.enhanced_bookmarks_ext_id);
} else {
profile_->GetPrefs()->ClearPref(
sync_driver::prefs::kEnhancedBookmarksExperimentEnabled);
profile_->GetPrefs()->ClearPref(
sync_driver::prefs::kEnhancedBookmarksExtensionId);
}
}
BookmarksExperimentState bookmarks_experiment_state =
static_cast<BookmarksExperimentState>(profile_->GetPrefs()->GetInteger(
sync_driver::prefs::kEnhancedBookmarksExperimentEnabled));
if (bookmarks_experiment_state_before != bookmarks_experiment_state) {
UpdateBookmarksExperiment(g_browser_process->local_state(),
bookmarks_experiment_state);
}
if (migrator_.get() &&
migrator_->state() != browser_sync::BackendMigrator::IDLE) {
DVLOG(1) << "Dropping OnExperimentsChanged due to migrator busy.";
return;
}
const syncer::ModelTypeSet registered_types = GetRegisteredDataTypes();
syncer::ModelTypeSet to_add;
const syncer::ModelTypeSet to_register =
Difference(to_add, registered_types);
DVLOG(2) << "OnExperimentsChanged called with types: "
<< syncer::ModelTypeSetToString(to_add);
DVLOG(2) << "Enabling types: " << syncer::ModelTypeSetToString(to_register);
for (syncer::ModelTypeSet::Iterator it = to_register.First();
it.Good(); it.Inc()) {
RegisterNewDataType(it.Get());
}
if (sync_prefs_.HasKeepEverythingSynced()) {
sync_prefs_.SetPreferredDataTypes(registered_types, registered_types);
if (!to_register.Empty() && HasSyncSetupCompleted() && migrator_) {
DVLOG(1) << "Dynamically enabling new datatypes: "
<< syncer::ModelTypeSetToString(to_register);
OnMigrationNeededForTypes(to_register);
}
}
}
void ProfileSyncService::UpdateAuthErrorState(const AuthError& error) {
is_auth_in_progress_ = false;
last_auth_error_ = error;
NotifyObservers();
}
namespace {
AuthError ConnectionStatusToAuthError(
syncer::ConnectionStatus status) {
switch (status) {
case syncer::CONNECTION_OK:
return AuthError::AuthErrorNone();
break;
case syncer::CONNECTION_AUTH_ERROR:
return AuthError(AuthError::INVALID_GAIA_CREDENTIALS);
break;
case syncer::CONNECTION_SERVER_ERROR:
return AuthError(AuthError::CONNECTION_FAILED);
break;
default:
NOTREACHED();
return AuthError(AuthError::CONNECTION_FAILED);
}
}
}
void ProfileSyncService::OnConnectionStatusChange(
syncer::ConnectionStatus status) {
connection_status_update_time_ = base::Time::Now();
connection_status_ = status;
if (status == syncer::CONNECTION_AUTH_ERROR) {
if (request_access_token_retry_timer_.IsRunning()) {
NOTREACHED();
} else if (request_access_token_backoff_.failure_count() == 0) {
request_access_token_backoff_.InformOfRequest(false);
RequestAccessToken();
} else {
request_access_token_backoff_.InformOfRequest(false);
request_access_token_retry_timer_.Start(
FROM_HERE,
request_access_token_backoff_.GetTimeUntilRelease(),
base::Bind(&ProfileSyncService::RequestAccessToken,
weak_factory_.GetWeakPtr()));
}
} else {
if (status == syncer::CONNECTION_OK) {
if (request_access_token_retry_timer_.IsRunning()) {
NOTREACHED();
} else {
request_access_token_backoff_.Reset();
}
}
const GoogleServiceAuthError auth_error =
ConnectionStatusToAuthError(status);
DVLOG(1) << "Connection status change: " << auth_error.ToString();
UpdateAuthErrorState(auth_error);
}
}
void ProfileSyncService::StopSyncingPermanently() {
sync_prefs_.SetStartSuppressed(true);
DisableForUser();
}
void ProfileSyncService::OnPassphraseRequired(
syncer::PassphraseRequiredReason reason,
const sync_pb::EncryptedData& pending_keys) {
DCHECK(backend_.get());
DCHECK(backend_->IsNigoriEnabled());
if (HasUnrecoverableError()) {
return;
}
DVLOG(1) << "Passphrase required with reason: "
<< syncer::PassphraseRequiredReasonToString(reason);
passphrase_required_reason_ = reason;
const syncer::ModelTypeSet types = GetPreferredDirectoryDataTypes();
if (data_type_manager_) {
data_type_manager_->Configure(types,
syncer::CONFIGURE_REASON_CRYPTO);
}
NotifyObservers();
}
void ProfileSyncService::OnPassphraseAccepted() {
DVLOG(1) << "Received OnPassphraseAccepted.";
if (!cached_passphrase_.empty())
cached_passphrase_.clear();
passphrase_required_reason_ = syncer::REASON_PASSPHRASE_NOT_REQUIRED;
const syncer::ModelTypeSet types = GetPreferredDirectoryDataTypes();
if (data_type_manager_) {
data_type_manager_->Configure(types,
syncer::CONFIGURE_REASON_CRYPTO);
}
NotifyObservers();
}
void ProfileSyncService::OnEncryptedTypesChanged(
syncer::ModelTypeSet encrypted_types,
bool encrypt_everything) {
encrypted_types_ = encrypted_types;
encrypt_everything_ = encrypt_everything;
DVLOG(1) << "Encrypted types changed to "
<< syncer::ModelTypeSetToString(encrypted_types_)
<< " (encrypt everything is set to "
<< (encrypt_everything_ ? "true" : "false") << ")";
DCHECK(encrypted_types_.Has(syncer::PASSWORDS));
if (GetActiveDataTypes().Has(syncer::HISTORY_DELETE_DIRECTIVES) &&
encrypted_types_.Has(syncer::SESSIONS)) {
DisableBrokenDatatype(syncer::HISTORY_DELETE_DIRECTIVES,
FROM_HERE,
"Delete directives not supported with encryption.");
}
}
void ProfileSyncService::OnEncryptionComplete() {
DVLOG(1) << "Encryption complete";
if (encryption_pending_ && encrypt_everything_) {
encryption_pending_ = false;
NotifyObservers();
}
}
void ProfileSyncService::OnMigrationNeededForTypes(
syncer::ModelTypeSet types) {
DCHECK(backend_initialized_);
DCHECK(data_type_manager_.get());
migrator_->MigrateTypes(types);
}
void ProfileSyncService::OnActionableError(const SyncProtocolError& error) {
last_actionable_error_ = error;
DCHECK_NE(last_actionable_error_.action,
syncer::UNKNOWN_ACTION);
switch (error.action) {
case syncer::UPGRADE_CLIENT:
case syncer::CLEAR_USER_DATA_AND_RESYNC:
case syncer::ENABLE_SYNC_ON_ACCOUNT:
case syncer::STOP_AND_RESTART_SYNC:
if (startup_controller_.setup_in_progress()) {
StopSyncingPermanently();
expect_sync_configuration_aborted_ = true;
}
OnInternalUnrecoverableError(FROM_HERE,
last_actionable_error_.error_description,
true,
ERROR_REASON_ACTIONABLE_ERROR);
break;
case syncer::DISABLE_SYNC_ON_CLIENT:
StopSyncingPermanently();
#if !defined(OS_CHROMEOS)
if (!startup_controller_.auto_start_enabled())
SigninManagerFactory::GetForProfile(profile_)->SignOut();
#endif
break;
case syncer::STOP_SYNC_FOR_DISABLED_ACCOUNT:
sync_disabled_by_admin_ = true;
ShutdownImpl(browser_sync::SyncBackendHost::DISABLE_AND_CLAIM_THREAD);
break;
default:
NOTREACHED();
}
NotifyObservers();
}
void ProfileSyncService::OnConfigureDone(
const browser_sync::DataTypeManager::ConfigureResult& result) {
DCHECK(cached_passphrase_.empty());
if (!sync_configure_start_time_.is_null()) {
if (result.status == DataTypeManager::OK ||
result.status == DataTypeManager::PARTIAL_SUCCESS) {
base::Time sync_configure_stop_time = base::Time::Now();
base::TimeDelta delta = sync_configure_stop_time -
sync_configure_start_time_;
if (is_first_time_sync_configure_) {
UMA_HISTOGRAM_LONG_TIMES("Sync.ServiceInitialConfigureTime", delta);
} else {
UMA_HISTOGRAM_LONG_TIMES("Sync.ServiceSubsequentConfigureTime",
delta);
}
}
sync_configure_start_time_ = base::Time();
}
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_SYNC_CONFIGURE_DONE,
content::Source<ProfileSyncService>(this),
content::NotificationService::NoDetails());
configure_status_ = result.status;
DVLOG(1) << "PSS OnConfigureDone called with status: " << configure_status_;
if (configure_status_ == DataTypeManager::ABORTED &&
expect_sync_configuration_aborted_) {
DVLOG(0) << "ProfileSyncService::Observe Sync Configure aborted";
expect_sync_configuration_aborted_ = false;
return;
}
if (configure_status_ != DataTypeManager::OK &&
configure_status_ != DataTypeManager::PARTIAL_SUCCESS) {
DCHECK_EQ(result.failed_data_types.size(),
static_cast<unsigned int>(1));
syncer::SyncError error = result.failed_data_types.begin()->second;
DCHECK(error.IsSet());
std::string message =
"Sync configuration failed with status " +
DataTypeManager::ConfigureStatusToString(configure_status_) +
" during " + syncer::ModelTypeToString(error.model_type()) +
": " + error.message();
LOG(ERROR) << "ProfileSyncService error: " << message;
OnInternalUnrecoverableError(error.location(),
message,
true,
ERROR_REASON_CONFIGURATION_FAILURE);
return;
}
DCHECK(!(IsPassphraseRequiredForDecryption() &&
!IsEncryptedDatatypeEnabled()));
if (encryption_pending_)
backend_->EnableEncryptEverything();
NotifyObservers();
if (migrator_.get() &&
migrator_->state() != browser_sync::BackendMigrator::IDLE) {
migrator_->OnConfigureDone(result);
} else {
StartSyncingWithServer();
}
}
void ProfileSyncService::OnConfigureRetry() {
DCHECK(cached_passphrase_.empty());
OnSyncConfigureRetry();
}
void ProfileSyncService::OnConfigureStart() {
sync_configure_start_time_ = base::Time::Now();
NotifyObservers();
}
ProfileSyncService::SyncStatusSummary
ProfileSyncService::QuerySyncStatusSummary() {
if (HasUnrecoverableError()) {
return UNRECOVERABLE_ERROR;
} else if (!backend_) {
return NOT_ENABLED;
} else if (backend_.get() && !HasSyncSetupCompleted()) {
return SETUP_INCOMPLETE;
} else if (backend_.get() && HasSyncSetupCompleted() &&
data_type_manager_.get() &&
data_type_manager_->state() != DataTypeManager::CONFIGURED) {
return DATATYPES_NOT_INITIALIZED;
} else if (ShouldPushChanges()) {
return INITIALIZED;
}
return UNKNOWN_ERROR;
}
std::string ProfileSyncService::QuerySyncStatusSummaryString() {
SyncStatusSummary status = QuerySyncStatusSummary();
switch (status) {
case UNRECOVERABLE_ERROR:
return "Unrecoverable error detected";
case NOT_ENABLED:
return "Syncing not enabled";
case SETUP_INCOMPLETE:
return "First time sync setup incomplete";
case DATATYPES_NOT_INITIALIZED:
return "Datatypes not fully initialized";
case INITIALIZED:
return "Sync service initialized";
default:
return "Status unknown: Internal error?";
}
}
std::string ProfileSyncService::GetBackendInitializationStateString() const {
return startup_controller_.GetBackendInitializationStateString();
}
bool ProfileSyncService::auto_start_enabled() const {
return startup_controller_.auto_start_enabled();
}
bool ProfileSyncService::setup_in_progress() const {
return startup_controller_.setup_in_progress();
}
bool ProfileSyncService::QueryDetailedSyncStatus(
SyncBackendHost::Status* result) {
if (backend_.get() && backend_initialized_) {
*result = backend_->GetDetailedStatus();
return true;
} else {
SyncBackendHost::Status status;
status.sync_protocol_error = last_actionable_error_;
*result = status;
return false;
}
}
const AuthError& ProfileSyncService::GetAuthError() const {
return last_auth_error_;
}
bool ProfileSyncService::FirstSetupInProgress() const {
return !HasSyncSetupCompleted() && startup_controller_.setup_in_progress();
}
void ProfileSyncService::SetSetupInProgress(bool setup_in_progress) {
if (startup_controller_.setup_in_progress() == setup_in_progress)
return;
startup_controller_.set_setup_in_progress(setup_in_progress);
if (!setup_in_progress && sync_initialized())
ReconfigureDatatypeManager();
NotifyObservers();
}
bool ProfileSyncService::sync_initialized() const {
return backend_initialized_;
}
bool ProfileSyncService::waiting_for_auth() const {
return is_auth_in_progress_;
}
const syncer::Experiments& ProfileSyncService::current_experiments() const {
return current_experiments_;
}
bool ProfileSyncService::HasUnrecoverableError() const {
return unrecoverable_error_reason_ != ERROR_REASON_UNSET;
}
bool ProfileSyncService::IsPassphraseRequired() const {
return passphrase_required_reason_ !=
syncer::REASON_PASSPHRASE_NOT_REQUIRED;
}
bool ProfileSyncService::IsPassphraseRequiredForDecryption() const {
return IsEncryptedDatatypeEnabled() && IsPassphraseRequired();
}
base::string16 ProfileSyncService::GetLastSyncedTimeString() const {
if (last_synced_time_.is_null())
return l10n_util::GetStringUTF16(IDS_SYNC_TIME_NEVER);
base::TimeDelta last_synced = base::Time::Now() - last_synced_time_;
if (last_synced < base::TimeDelta::FromMinutes(1))
return l10n_util::GetStringUTF16(IDS_SYNC_TIME_JUST_NOW);
return ui::TimeFormat::Simple(ui::TimeFormat::FORMAT_ELAPSED,
ui::TimeFormat::LENGTH_SHORT, last_synced);
}
void ProfileSyncService::UpdateSelectedTypesHistogram(
bool sync_everything, const syncer::ModelTypeSet chosen_types) const {
if (!HasSyncSetupCompleted() ||
sync_everything != sync_prefs_.HasKeepEverythingSynced()) {
UMA_HISTOGRAM_BOOLEAN("Sync.SyncEverything", sync_everything);
}
const browser_sync::user_selectable_type::UserSelectableSyncType
user_selectable_types[] = {
browser_sync::user_selectable_type::BOOKMARKS,
browser_sync::user_selectable_type::PREFERENCES,
browser_sync::user_selectable_type::PASSWORDS,
browser_sync::user_selectable_type::AUTOFILL,
browser_sync::user_selectable_type::THEMES,
browser_sync::user_selectable_type::TYPED_URLS,
browser_sync::user_selectable_type::EXTENSIONS,
browser_sync::user_selectable_type::APPS,
browser_sync::user_selectable_type::PROXY_TABS
};
COMPILE_ASSERT(32 == syncer::MODEL_TYPE_COUNT, UpdateCustomConfigHistogram);
if (!sync_everything) {
const syncer::ModelTypeSet current_types = GetPreferredDataTypes();
syncer::ModelTypeSet type_set = syncer::UserSelectableTypes();
syncer::ModelTypeSet::Iterator it = type_set.First();
DCHECK_EQ(arraysize(user_selectable_types), type_set.Size());
for (size_t i = 0; i < arraysize(user_selectable_types) && it.Good();
++i, it.Inc()) {
const syncer::ModelType type = it.Get();
if (chosen_types.Has(type) &&
(!HasSyncSetupCompleted() || !current_types.Has(type))) {
UMA_HISTOGRAM_ENUMERATION(
"Sync.CustomSync",
user_selectable_types[i],
browser_sync::user_selectable_type::SELECTABLE_DATATYPE_COUNT + 1);
}
}
}
}
#if defined(OS_CHROMEOS)
void ProfileSyncService::RefreshSpareBootstrapToken(
const std::string& passphrase) {
browser_sync::SystemEncryptor encryptor;
syncer::Cryptographer temp_cryptographer(&encryptor);
syncer::KeyParams key_params = {"localhost", "dummy", passphrase};
std::string bootstrap_token;
if (!temp_cryptographer.AddKey(key_params)) {
NOTREACHED() << "Failed to add key to cryptographer.";
}
temp_cryptographer.GetBootstrapToken(&bootstrap_token);
sync_prefs_.SetSpareBootstrapToken(bootstrap_token);
}
#endif
void ProfileSyncService::OnUserChoseDatatypes(
bool sync_everything,
syncer::ModelTypeSet chosen_types) {
if (!backend_.get() && !HasUnrecoverableError()) {
NOTREACHED();
return;
}
UpdateSelectedTypesHistogram(sync_everything, chosen_types);
sync_prefs_.SetKeepEverythingSynced(sync_everything);
failed_data_types_handler_.Reset();
if (GetActiveDataTypes().Has(syncer::HISTORY_DELETE_DIRECTIVES) &&
encrypted_types_.Has(syncer::SESSIONS)) {
DisableBrokenDatatype(syncer::HISTORY_DELETE_DIRECTIVES,
FROM_HERE,
"Delete directives not supported with encryption.");
}
ChangePreferredDataTypes(chosen_types);
AcknowledgeSyncedTypes();
NotifyObservers();
}
void ProfileSyncService::ChangePreferredDataTypes(
syncer::ModelTypeSet preferred_types) {
DVLOG(1) << "ChangePreferredDataTypes invoked";
const syncer::ModelTypeSet registered_types = GetRegisteredDataTypes();
const syncer::ModelTypeSet registered_preferred_types =
Intersection(registered_types, preferred_types);
sync_prefs_.SetPreferredDataTypes(registered_types,
registered_preferred_types);
ReconfigureDatatypeManager();
}
syncer::ModelTypeSet ProfileSyncService::GetActiveDataTypes() const {
const syncer::ModelTypeSet preferred_types = GetPreferredDataTypes();
const syncer::ModelTypeSet failed_types =
failed_data_types_handler_.GetFailedTypes();
return Difference(preferred_types, failed_types);
}
syncer::ModelTypeSet ProfileSyncService::GetPreferredDataTypes() const {
const syncer::ModelTypeSet registered_types = GetRegisteredDataTypes();
const syncer::ModelTypeSet preferred_types =
sync_prefs_.GetPreferredDataTypes(registered_types);
return preferred_types;
}
syncer::ModelTypeSet
ProfileSyncService::GetPreferredDirectoryDataTypes() const {
const syncer::ModelTypeSet registered_directory_types =
GetRegisteredDirectoryDataTypes();
const syncer::ModelTypeSet preferred_types =
sync_prefs_.GetPreferredDataTypes(registered_directory_types);
return preferred_types;
}
syncer::ModelTypeSet
ProfileSyncService::GetPreferredNonBlockingDataTypes() const {
return sync_prefs_.GetPreferredDataTypes(GetRegisteredNonBlockingDataTypes());
}
syncer::ModelTypeSet ProfileSyncService::GetRegisteredDataTypes() const {
return Union(GetRegisteredDirectoryDataTypes(),
GetRegisteredNonBlockingDataTypes());
}
syncer::ModelTypeSet
ProfileSyncService::GetRegisteredDirectoryDataTypes() const {
syncer::ModelTypeSet registered_types;
for (DataTypeController::TypeMap::const_iterator it =
data_type_controllers_.begin();
it != data_type_controllers_.end(); ++it) {
registered_types.Put(it->first);
}
return registered_types;
}
syncer::ModelTypeSet
ProfileSyncService::GetRegisteredNonBlockingDataTypes() const {
return non_blocking_types_;
}
bool ProfileSyncService::IsUsingSecondaryPassphrase() const {
syncer::PassphraseType passphrase_type = GetPassphraseType();
return passphrase_type == syncer::FROZEN_IMPLICIT_PASSPHRASE ||
passphrase_type == syncer::CUSTOM_PASSPHRASE;
}
syncer::PassphraseType ProfileSyncService::GetPassphraseType() const {
return backend_->GetPassphraseType();
}
base::Time ProfileSyncService::GetExplicitPassphraseTime() const {
return backend_->GetExplicitPassphraseTime();
}
bool ProfileSyncService::IsCryptographerReady(
const syncer::BaseTransaction* trans) const {
return backend_.get() && backend_->IsCryptographerReady(trans);
}
void ProfileSyncService::ConfigurePriorityDataTypes() {
const syncer::ModelTypeSet priority_types =
Intersection(GetPreferredDirectoryDataTypes(),
syncer::PriorityUserTypes());
if (!priority_types.Empty()) {
const syncer::ConfigureReason reason = HasSyncSetupCompleted() ?
syncer::CONFIGURE_REASON_RECONFIGURATION :
syncer::CONFIGURE_REASON_NEW_CLIENT;
data_type_manager_->Configure(priority_types, reason);
}
}
void ProfileSyncService::ConfigureDataTypeManager() {
if (startup_controller_.setup_in_progress())
return;
bool restart = false;
if (!data_type_manager_) {
restart = true;
data_type_manager_.reset(
factory_->CreateDataTypeManager(debug_info_listener_,
&data_type_controllers_,
this,
backend_.get(),
this,
&failed_data_types_handler_));
migrator_.reset(
new browser_sync::BackendMigrator(
profile_->GetDebugName(), GetUserShare(),
this, data_type_manager_.get(),
base::Bind(&ProfileSyncService::StartSyncingWithServer,
base::Unretained(this))));
}
const syncer::ModelTypeSet types = GetPreferredDirectoryDataTypes();
syncer::ConfigureReason reason = syncer::CONFIGURE_REASON_UNKNOWN;
if (!HasSyncSetupCompleted()) {
reason = syncer::CONFIGURE_REASON_NEW_CLIENT;
} else if (restart) {
reason = syncer::CONFIGURE_REASON_NEWLY_ENABLED_DATA_TYPE;
} else {
reason = syncer::CONFIGURE_REASON_RECONFIGURATION;
}
data_type_manager_->Configure(types, reason);
}
syncer::UserShare* ProfileSyncService::GetUserShare() const {
if (backend_.get() && backend_initialized_) {
return backend_->GetUserShare();
}
NOTREACHED();
return NULL;
}
syncer::sessions::SyncSessionSnapshot
ProfileSyncService::GetLastSessionSnapshot() const {
if (backend_.get() && backend_initialized_) {
return backend_->GetLastSessionSnapshot();
}
NOTREACHED();
return syncer::sessions::SyncSessionSnapshot();
}
bool ProfileSyncService::HasUnsyncedItems() const {
if (backend_.get() && backend_initialized_) {
return backend_->HasUnsyncedItems();
}
NOTREACHED();
return false;
}
browser_sync::BackendMigrator*
ProfileSyncService::GetBackendMigratorForTest() {
return migrator_.get();
}
void ProfileSyncService::GetModelSafeRoutingInfo(
syncer::ModelSafeRoutingInfo* out) const {
if (backend_.get() && backend_initialized_) {
backend_->GetModelSafeRoutingInfo(out);
} else {
NOTREACHED();
}
}
base::Value* ProfileSyncService::GetTypeStatusMap() const {
scoped_ptr<base::ListValue> result(new base::ListValue());
if (!backend_.get() || !backend_initialized_) {
return result.release();
}
FailedDataTypesHandler::TypeErrorMap error_map =
failed_data_types_handler_.GetAllErrors();
ModelTypeSet active_types;
ModelTypeSet passive_types;
ModelSafeRoutingInfo routing_info;
backend_->GetModelSafeRoutingInfo(&routing_info);
for (ModelSafeRoutingInfo::const_iterator it = routing_info.begin();
it != routing_info.end(); ++it) {
if (it->second == syncer::GROUP_PASSIVE) {
passive_types.Put(it->first);
} else {
active_types.Put(it->first);
}
}
SyncBackendHost::Status detailed_status = backend_->GetDetailedStatus();
ModelTypeSet &throttled_types(detailed_status.throttled_types);
ModelTypeSet registered = GetRegisteredDataTypes();
scoped_ptr<base::DictionaryValue> type_status_header(
new base::DictionaryValue());
type_status_header->SetString("name", "Model Type");
type_status_header->SetString("status", "header");
type_status_header->SetString("value", "Group Type");
type_status_header->SetString("num_entries", "Total Entries");
type_status_header->SetString("num_live", "Live Entries");
result->Append(type_status_header.release());
scoped_ptr<base::DictionaryValue> type_status;
for (ModelTypeSet::Iterator it = registered.First(); it.Good(); it.Inc()) {
ModelType type = it.Get();
type_status.reset(new base::DictionaryValue());
type_status->SetString("name", ModelTypeToString(type));
if (error_map.find(type) != error_map.end()) {
const syncer::SyncError &error = error_map.find(type)->second;
DCHECK(error.IsSet());
std::string error_text = "Error: " + error.location().ToString() +
", " + error.message();
type_status->SetString("status", "error");
type_status->SetString("value", error_text);
} else if (throttled_types.Has(type) && passive_types.Has(type)) {
type_status->SetString("status", "warning");
type_status->SetString("value", "Passive, Throttled");
} else if (passive_types.Has(type)) {
type_status->SetString("status", "warning");
type_status->SetString("value", "Passive");
} else if (throttled_types.Has(type)) {
type_status->SetString("status", "warning");
type_status->SetString("value", "Throttled");
} else if (GetRegisteredNonBlockingDataTypes().Has(type)) {
type_status->SetString("status", "ok");
type_status->SetString("value", "Non-Blocking");
} else if (active_types.Has(type)) {
type_status->SetString("status", "ok");
type_status->SetString("value", "Active: " +
ModelSafeGroupToString(routing_info[type]));
} else {
type_status->SetString("status", "warning");
type_status->SetString("value", "Disabled by User");
}
int live_count = detailed_status.num_entries_by_type[type] -
detailed_status.num_to_delete_entries_by_type[type];
type_status->SetInteger("num_entries",
detailed_status.num_entries_by_type[type]);
type_status->SetInteger("num_live", live_count);
result->Append(type_status.release());
}
return result.release();
}
void ProfileSyncService::ActivateDataType(
syncer::ModelType type, syncer::ModelSafeGroup group,
ChangeProcessor* change_processor) {
if (!backend_) {
NOTREACHED();
return;
}
DCHECK(backend_initialized_);
backend_->ActivateDataType(type, group, change_processor);
}
void ProfileSyncService::DeactivateDataType(syncer::ModelType type) {
if (!backend_)
return;
backend_->DeactivateDataType(type);
}
void ProfileSyncService::ConsumeCachedPassphraseIfPossible() {
if (cached_passphrase_.empty() || !sync_initialized())
return;
std::string passphrase = cached_passphrase_;
cached_passphrase_.clear();
if (passphrase_required_reason() == syncer::REASON_DECRYPTION) {
if (SetDecryptionPassphrase(passphrase)) {
DVLOG(1) << "Cached passphrase successfully decrypted pending keys";
return;
}
}
if (!IsUsingSecondaryPassphrase())
SetEncryptionPassphrase(passphrase, IMPLICIT);
}
void ProfileSyncService::RequestAccessToken() {
if (access_token_request_ != NULL)
return;
request_access_token_retry_timer_.Stop();
OAuth2TokenService::ScopeSet oauth2_scopes;
if (profile_->IsManaged()) {
oauth2_scopes.insert(GaiaConstants::kChromeSyncManagedOAuth2Scope);
} else {
oauth2_scopes.insert(GaiaConstants::kChromeSyncOAuth2Scope);
}
const std::string& account_id = signin_->GetAccountIdToUse();
if (!access_token_.empty()) {
oauth2_token_service_->InvalidateToken(
account_id, oauth2_scopes, access_token_);
}
access_token_.clear();
token_request_time_ = base::Time::Now();
token_receive_time_ = base::Time();
next_token_request_time_ = base::Time();
access_token_request_ =
oauth2_token_service_->StartRequest(account_id, oauth2_scopes, this);
}
void ProfileSyncService::SetEncryptionPassphrase(const std::string& passphrase,
PassphraseType type) {
DCHECK(sync_initialized());
DCHECK(!(type == IMPLICIT && IsUsingSecondaryPassphrase())) <<
"Data is already encrypted using an explicit passphrase";
DCHECK(!(type == EXPLICIT &&
passphrase_required_reason_ == syncer::REASON_DECRYPTION)) <<
"Can not set explicit passphrase when decryption is needed.";
DVLOG(1) << "Setting " << (type == EXPLICIT ? "explicit" : "implicit")
<< " passphrase for encryption.";
if (passphrase_required_reason_ == syncer::REASON_ENCRYPTION) {
passphrase_required_reason_ = syncer::REASON_PASSPHRASE_NOT_REQUIRED;
NotifyObservers();
}
backend_->SetEncryptionPassphrase(passphrase, type == EXPLICIT);
}
bool ProfileSyncService::SetDecryptionPassphrase(
const std::string& passphrase) {
if (IsPassphraseRequired()) {
DVLOG(1) << "Setting passphrase for decryption.";
return backend_->SetDecryptionPassphrase(passphrase);
} else {
NOTREACHED() << "SetDecryptionPassphrase must not be called when "
"IsPassphraseRequired() is false.";
return false;
}
}
void ProfileSyncService::EnableEncryptEverything() {
DCHECK(sync_initialized());
if (!encrypt_everything_)
encryption_pending_ = true;
}
bool ProfileSyncService::encryption_pending() const {
return encryption_pending_;
}
bool ProfileSyncService::EncryptEverythingEnabled() const {
DCHECK(backend_initialized_);
return encrypt_everything_ || encryption_pending_;
}
syncer::ModelTypeSet ProfileSyncService::GetEncryptedDataTypes() const {
DCHECK(encrypted_types_.Has(syncer::PASSWORDS));
return encrypted_types_;
}
void ProfileSyncService::OnSyncManagedPrefChange(bool is_sync_managed) {
NotifyObservers();
if (is_sync_managed) {
DisableForUser();
} else {
startup_controller_.TryStart();
}
}
void ProfileSyncService::GoogleSigninSucceeded(const std::string& username,
const std::string& password) {
if (!sync_prefs_.IsStartSuppressed() && !password.empty()) {
cached_passphrase_ = password;
ConsumeCachedPassphraseIfPossible();
}
#if defined(OS_CHROMEOS)
RefreshSpareBootstrapToken(password);
#endif
if (!sync_initialized() || GetAuthError().state() != AuthError::NONE) {
is_auth_in_progress_ = true;
}
}
void ProfileSyncService::GoogleSignedOut(const std::string& username) {
sync_disabled_by_admin_ = false;
DisableForUser();
}
void ProfileSyncService::AddObserver(
ProfileSyncServiceBase::Observer* observer) {
observers_.AddObserver(observer);
}
void ProfileSyncService::RemoveObserver(
ProfileSyncServiceBase::Observer* observer) {
observers_.RemoveObserver(observer);
}
void ProfileSyncService::AddProtocolEventObserver(
browser_sync::ProtocolEventObserver* observer) {
protocol_event_observers_.AddObserver(observer);
if (backend_) {
backend_->RequestBufferedProtocolEventsAndEnableForwarding();
}
}
void ProfileSyncService::RemoveProtocolEventObserver(
browser_sync::ProtocolEventObserver* observer) {
protocol_event_observers_.RemoveObserver(observer);
if (backend_ && !protocol_event_observers_.might_have_observers()) {
backend_->DisableProtocolEventForwarding();
}
}
bool ProfileSyncService::HasObserver(
ProfileSyncServiceBase::Observer* observer) const {
return observers_.HasObserver(observer);
}
base::WeakPtr<syncer::JsController> ProfileSyncService::GetJsController() {
return sync_js_controller_.AsWeakPtr();
}
void ProfileSyncService::SyncEvent(SyncEventCodes code) {
UMA_HISTOGRAM_ENUMERATION("Sync.EventCodes", code, MAX_SYNC_EVENT_CODE);
}
bool ProfileSyncService::IsSyncEnabled() {
return !CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableSync);
}
bool ProfileSyncService::IsManaged() const {
return sync_prefs_.IsManaged() || sync_disabled_by_admin_;
}
bool ProfileSyncService::ShouldPushChanges() {
if (HasUnrecoverableError())
return false;
if (!data_type_manager_)
return false;
return data_type_manager_->state() == DataTypeManager::CONFIGURED;
}
void ProfileSyncService::StopAndSuppress() {
sync_prefs_.SetStartSuppressed(true);
if (backend_) {
backend_->UnregisterInvalidationIds();
}
ShutdownImpl(browser_sync::SyncBackendHost::STOP_AND_CLAIM_THREAD);
}
bool ProfileSyncService::IsStartSuppressed() const {
return sync_prefs_.IsStartSuppressed();
}
SigninManagerBase* ProfileSyncService::signin() const {
if (!signin_)
return NULL;
return signin_->GetOriginal();
}
void ProfileSyncService::UnsuppressAndStart() {
DCHECK(profile_);
sync_prefs_.SetStartSuppressed(false);
if (signin_.get() &&
signin_->GetOriginal()->GetAuthenticatedUsername().empty()) {
signin_->GetOriginal()->SetAuthenticatedUsername(
profile_->GetPrefs()->GetString(prefs::kGoogleServicesUsername));
}
startup_controller_.TryStart();
}
void ProfileSyncService::AcknowledgeSyncedTypes() {
sync_prefs_.AcknowledgeSyncedTypes(GetRegisteredDataTypes());
}
void ProfileSyncService::ReconfigureDatatypeManager() {
if (backend_initialized_) {
DCHECK(backend_.get());
ConfigureDataTypeManager();
} else if (HasUnrecoverableError()) {
NotifyObservers();
DVLOG(1) << "ConfigureDataTypeManager not invoked because of an "
<< "Unrecoverable error.";
} else {
DVLOG(0) << "ConfigureDataTypeManager not invoked because backend is not "
<< "initialized";
}
}
const FailedDataTypesHandler& ProfileSyncService::failed_data_types_handler()
const {
return failed_data_types_handler_;
}
void ProfileSyncService::OnInternalUnrecoverableError(
const tracked_objects::Location& from_here,
const std::string& message,
bool delete_sync_database,
UnrecoverableErrorReason reason) {
DCHECK(!HasUnrecoverableError());
unrecoverable_error_reason_ = reason;
OnUnrecoverableErrorImpl(from_here, message, delete_sync_database);
}
bool ProfileSyncService::IsRetryingAccessTokenFetchForTest() const {
return request_access_token_retry_timer_.IsRunning();
}
std::string ProfileSyncService::GetAccessTokenForTest() const {
return access_token_;
}
WeakHandle<syncer::JsEventHandler> ProfileSyncService::GetJsEventHandler() {
return MakeWeakHandle(sync_js_controller_.AsWeakPtr());
}
syncer::SyncableService* ProfileSyncService::GetSessionsSyncableService() {
return sessions_sync_manager_.get();
}
ProfileSyncService::SyncTokenStatus::SyncTokenStatus()
: connection_status(syncer::CONNECTION_NOT_ATTEMPTED),
last_get_token_error(GoogleServiceAuthError::AuthErrorNone()) {}
ProfileSyncService::SyncTokenStatus::~SyncTokenStatus() {}
ProfileSyncService::SyncTokenStatus
ProfileSyncService::GetSyncTokenStatus() const {
SyncTokenStatus status;
status.connection_status_update_time = connection_status_update_time_;
status.connection_status = connection_status_;
status.token_request_time = token_request_time_;
status.token_receive_time = token_receive_time_;
status.last_get_token_error = last_get_token_error_;
if (request_access_token_retry_timer_.IsRunning())
status.next_token_request_time = next_token_request_time_;
return status;
}
void ProfileSyncService::OverrideNetworkResourcesForTest(
scoped_ptr<syncer::NetworkResources> network_resources) {
network_resources_ = network_resources.Pass();
}