1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_SYNC_DRIVER_SYNC_SERVICE_H_
#define COMPONENTS_SYNC_DRIVER_SYNC_SERVICE_H_
#include <memory>
#include <string>
#include "base/callback.h"
#include "base/location.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "components/sync/base/model_type.h"
#include "components/sync/driver/data_type_encryption_handler.h"
#include "components/sync/driver/sync_service_observer.h"
#include "components/sync/engine/connection_status.h"
#include "google_apis/gaia/google_service_auth_error.h"
class GoogleServiceAuthError;
namespace sync_sessions {
class OpenTabsUIDelegate;
} // namespace sync_sessions
namespace syncer {
class BaseTransaction;
class DataTypeController;
class JsController;
class LocalDeviceInfoProvider;
class ProtocolEventObserver;
class SyncClient;
class SyncCycleSnapshot;
class TypeDebugInfoObserver;
struct SyncStatus;
struct UserShare;
// UIs that need to prevent Sync startup should hold an instance of this class
// until the user has finished modifying sync settings. This is not an inner
// class of SyncService to enable forward declarations.
class SyncSetupInProgressHandle {
public:
// UIs should not construct this directly, but instead call
// SyncService::GetSetupInProgress().
explicit SyncSetupInProgressHandle(base::Closure on_destroy);
~SyncSetupInProgressHandle();
private:
base::Closure on_destroy_;
};
class SyncService : public DataTypeEncryptionHandler {
public:
// Used to specify the kind of passphrase with which sync data is encrypted.
enum PassphraseType {
IMPLICIT, // The user did not provide a custom passphrase for encryption.
// We implicitly use the GAIA password in such cases.
EXPLICIT, // The user selected the "use custom passphrase" radio button
// during sync setup and provided a passphrase.
};
// Passed as an argument to RequestStop to control whether or not the sync
// engine should clear its data directory when it shuts down. See
// RequestStop for more information.
enum SyncStopDataFate {
KEEP_DATA,
CLEAR_DATA,
};
// Status of sync server connection, sync token and token request.
struct SyncTokenStatus {
SyncTokenStatus();
// Sync server connection status reported by sync engine.
base::Time connection_status_update_time;
ConnectionStatus connection_status;
// Times when OAuth2 access token is requested and received.
base::Time token_request_time;
base::Time token_receive_time;
// Error returned by OAuth2TokenService for token request and time when
// next request is scheduled.
GoogleServiceAuthError last_get_token_error;
base::Time next_token_request_time;
};
~SyncService() override {}
// Whether sync is enabled by user or not. This does not necessarily mean
// that sync is currently running (due to delayed startup, unrecoverable
// errors, or shutdown). See IsSyncActive below for checking whether sync
// is actually running.
virtual bool IsFirstSetupComplete() const = 0;
// Whether sync is allowed to start. Command line flags, platform-level
// overrides, and account-level overrides are examples of reasons this
// might be false.
virtual bool IsSyncAllowed() const = 0;
// Returns true if sync is fully initialized and active. This implies that
// an initial configuration has successfully completed, although there may
// be datatype specific, auth, or other transient errors. To see which
// datetypes are actually syncing, see GetActiveTypes() below.
virtual bool IsSyncActive() const = 0;
// Returns true if the local sync backend server has been enabled through a
// command line flag or policy. In this case sync is considered active but any
// implied consent for further related services e.g. Suggestions, Web History
// etc. is considered not granted.
virtual bool IsLocalSyncEnabled() const = 0;
// Triggers a GetUpdates call for the specified |types|, pulling any new data
// from the sync server.
virtual void TriggerRefresh(const ModelTypeSet& types) = 0;
// Get the set of current active data types (those chosen or configured by
// the user which have not also encountered a runtime error).
// Note that if the Sync engine is in the middle of a configuration, this
// will the the empty set. Once the configuration completes the set will
// be updated.
virtual ModelTypeSet GetActiveDataTypes() const = 0;
// Returns the SyncClient instance associated with this service.
virtual SyncClient* GetSyncClient() const = 0;
// Adds/removes an observer. SyncService does not take ownership of the
// observer.
virtual void AddObserver(SyncServiceObserver* observer) = 0;
virtual void RemoveObserver(SyncServiceObserver* observer) = 0;
// Returns true if |observer| has already been added as an observer.
virtual bool HasObserver(const SyncServiceObserver* observer) const = 0;
// ---------------------------------------------------------------------------
// TODO(sync): The methods below were pulled from ProfileSyncService, and
// should be evaluated to see if they should stay.
// Called when a datatype (SyncableService) has a need for sync to start
// ASAP, presumably because a local change event has occurred but we're
// still in deferred start mode, meaning the SyncableService hasn't been
// told to MergeDataAndStartSyncing yet.
virtual void OnDataTypeRequestsSyncStartup(ModelType type) = 0;
// Returns true if sync is allowed, requested, and the user is logged in.
// (being logged in does not mean that tokens are available - tokens may
// be missing because they have not loaded yet, or because they were deleted
// due to http://crbug.com/121755).
virtual bool CanSyncStart() const = 0;
// Stops sync at the user's request. |data_fate| controls whether the sync
// engine should clear its data directory when it shuts down. Generally
// KEEP_DATA is used when the user just stops sync, and CLEAR_DATA is used
// when they sign out of the profile entirely.
virtual void RequestStop(SyncStopDataFate data_fate) = 0;
// The user requests that sync start. This only actually starts sync if
// IsSyncAllowed is true and the user is signed in. Once sync starts,
// other things such as IsFirstSetupComplete being false can still prevent
// it from moving into the "active" state.
virtual void RequestStart() = 0;
// Returns the set of types which are preferred for enabling. This is a
// superset of the active types (see GetActiveDataTypes()).
virtual ModelTypeSet GetPreferredDataTypes() const = 0;
// Called when a user chooses which data types to sync. |sync_everything|
// represents whether they chose the "keep everything synced" option; if
// true, |chosen_types| will be ignored and all data types will be synced.
// |sync_everything| means "sync all current and future data types."
// |chosen_types| must be a subset of UserSelectableTypes().
virtual void OnUserChoseDatatypes(bool sync_everything,
ModelTypeSet chosen_types) = 0;
// Called whe Sync has been setup by the user and can be started.
virtual void SetFirstSetupComplete() = 0;
// Returns true if initial sync setup is in progress (does not return true
// if the user is customizing sync after already completing setup once).
// SyncService uses this to determine if it's OK to start syncing, or if the
// user is still setting up the initial sync configuration.
virtual bool IsFirstSetupInProgress() const = 0;
// Called by the UI to notify the SyncService that UI is visible so it will
// not start syncing. This tells sync whether it's safe to start downloading
// data types yet (we don't start syncing until after sync setup is complete).
// The UI calls this and holds onto the instance for as long as any part of
// the signin wizard is displayed (even just the login UI).
// When the last outstanding handle is deleted, this kicks off the sync engine
// to ensure that data download starts. In this case,
// |ReconfigureDatatypeManager| will get triggered.
virtual std::unique_ptr<SyncSetupInProgressHandle>
GetSetupInProgressHandle() = 0;
// Used by tests.
virtual bool IsSetupInProgress() const = 0;
// Whether the data types active for the current mode have finished
// configuration.
virtual bool ConfigurationDone() const = 0;
virtual const GoogleServiceAuthError& GetAuthError() const = 0;
virtual bool HasUnrecoverableError() const = 0;
// Returns true if the SyncEngine has told us it's ready to accept changes.
virtual bool IsEngineInitialized() const = 0;
// Return the active OpenTabsUIDelegate. If open/proxy tabs is not enabled or
// not currently syncing, returns nullptr.
virtual sync_sessions::OpenTabsUIDelegate* GetOpenTabsUIDelegate() = 0;
// Returns true if OnPassphraseRequired has been called for decryption and
// we have an encrypted data type enabled.
virtual bool IsPassphraseRequiredForDecryption() const = 0;
// Returns the time the current explicit passphrase (if any), was set.
// If no secondary passphrase is in use, or no time is available, returns an
// unset base::Time.
virtual base::Time GetExplicitPassphraseTime() const = 0;
// Returns true if a secondary (explicit) passphrase is being used. It is not
// legal to call this method before the engine is initialized.
virtual bool IsUsingSecondaryPassphrase() const = 0;
// Turns on encryption for all data. Callers must call OnUserChoseDatatypes()
// after calling this to force the encryption to occur.
virtual void EnableEncryptEverything() = 0;
// Returns true if we are currently set to encrypt all the sync data.
virtual bool IsEncryptEverythingEnabled() const = 0;
// Asynchronously sets the passphrase to |passphrase| for encryption. |type|
// specifies whether the passphrase is a custom passphrase or the GAIA
// password being reused as a passphrase.
// TODO(atwilson): Change this so external callers can only set an EXPLICIT
// passphrase with this API.
virtual void SetEncryptionPassphrase(const std::string& passphrase,
PassphraseType type) = 0;
// Asynchronously decrypts pending keys using |passphrase|. Returns false
// immediately if the passphrase could not be used to decrypt a locally cached
// copy of encrypted keys; returns true otherwise.
virtual bool SetDecryptionPassphrase(const std::string& passphrase)
WARN_UNUSED_RESULT = 0;
// Checks whether the Cryptographer is ready to encrypt and decrypt updates
// for sensitive data types. Caller must be holding a
// syncapi::BaseTransaction to ensure thread safety.
virtual bool IsCryptographerReady(const BaseTransaction* trans) const = 0;
// TODO(akalin): This is called mostly by ModelAssociators and
// tests. Figure out how to pass the handle to the ModelAssociators
// directly, figure out how to expose this to tests, and remove this
// function.
virtual UserShare* GetUserShare() const = 0;
// Returns DeviceInfo provider for the local device.
virtual LocalDeviceInfoProvider* GetLocalDeviceInfoProvider() const = 0;
// Registers a data type controller with the sync service. This
// makes the data type controller available for use, it does not
// enable or activate the synchronization of the data type (see
// ActivateDataType). Takes ownership of the pointer.
virtual void RegisterDataTypeController(
std::unique_ptr<DataTypeController> data_type_controller) = 0;
// Called to re-enable a type disabled by DisableDatatype(..). Note, this does
// not change the preferred state of a datatype, and is not persisted across
// restarts.
virtual void ReenableDatatype(ModelType type) = 0;
// Return sync token status.
virtual SyncTokenStatus GetSyncTokenStatus() const = 0;
// Get a description of the sync status for displaying in the user interface.
virtual std::string QuerySyncStatusSummaryString() = 0;
// Initializes a struct of status indicators with data from the engine.
// Returns false if the engine was not available for querying; in that case
// the struct will be filled with default data.
virtual bool QueryDetailedSyncStatus(SyncStatus* result) = 0;
// Returns a user-friendly string form of last synced time (in minutes).
virtual base::string16 GetLastSyncedTimeString() const = 0;
// Returns a human readable string describing engine initialization state.
virtual std::string GetEngineInitializationStateString() const = 0;
virtual SyncCycleSnapshot GetLastCycleSnapshot() const = 0;
// Returns a ListValue indicating the status of all registered types.
//
// The format is:
// [ {"name": <name>, "value": <value>, "status": <status> }, ... ]
// where <name> is a type's name, <value> is a string providing details for
// the type's status, and <status> is one of "error", "warning" or "ok"
// depending on the type's current status.
//
// This function is used by about_sync_util.cc to help populate the about:sync
// page. It returns a ListValue rather than a DictionaryValue in part to make
// it easier to iterate over its elements when constructing that page.
virtual std::unique_ptr<base::Value> GetTypeStatusMap() = 0;
virtual const GURL& sync_service_url() const = 0;
virtual std::string unrecoverable_error_message() const = 0;
virtual tracked_objects::Location unrecoverable_error_location() const = 0;
virtual void AddProtocolEventObserver(ProtocolEventObserver* observer) = 0;
virtual void RemoveProtocolEventObserver(ProtocolEventObserver* observer) = 0;
virtual void AddTypeDebugInfoObserver(TypeDebugInfoObserver* observer) = 0;
virtual void RemoveTypeDebugInfoObserver(TypeDebugInfoObserver* observer) = 0;
// Returns a weak pointer to the service's JsController.
virtual base::WeakPtr<JsController> GetJsController() = 0;
// Asynchronously fetches base::Value representations of all sync nodes and
// returns them to the specified callback on this thread.
//
// These requests can live a long time and return when you least expect it.
// For safety, the callback should be bound to some sort of WeakPtr<> or
// scoped_refptr<>.
virtual void GetAllNodes(
const base::Callback<void(std::unique_ptr<base::ListValue>)>&
callback) = 0;
protected:
SyncService() {}
private:
DISALLOW_COPY_AND_ASSIGN(SyncService);
};
} // namespace syncer
#endif // COMPONENTS_SYNC_DRIVER_SYNC_SERVICE_H_
|