1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_SYNC_GLUE_SYNC_BACKEND_HOST_H_
6 #define CHROME_BROWSER_SYNC_GLUE_SYNC_BACKEND_HOST_H_
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/threading/thread.h"
15 #include "components/sync_driver/backend_data_type_configurer.h"
16 #include "sync/internal_api/public/base/model_type.h"
17 #include "sync/internal_api/public/configure_reason.h"
18 #include "sync/internal_api/public/sessions/sync_session_snapshot.h"
19 #include "sync/internal_api/public/shutdown_reason.h"
20 #include "sync/internal_api/public/sync_context_proxy.h"
21 #include "sync/internal_api/public/sync_manager.h"
22 #include "sync/internal_api/public/sync_manager_factory.h"
23 #include "sync/internal_api/public/util/weak_handle.h"
32 class NetworkResources
;
33 class SyncManagerFactory
;
34 class UnrecoverableErrorHandler
;
37 namespace sync_driver
{
38 class ChangeProcessor
;
42 namespace browser_sync
{
44 // An API to "host" the top level SyncAPI element.
46 // This class handles dispatch of potentially blocking calls to appropriate
47 // threads and ensures that the SyncFrontend is only accessed on the UI loop.
48 class SyncBackendHost
: public sync_driver::BackendDataTypeConfigurer
{
50 typedef syncer::SyncStatus Status
;
52 // Stubs used by implementing classes.
54 ~SyncBackendHost() override
;
56 // Called on the frontend's thread to kick off asynchronous initialization.
57 // Optionally deletes the "Sync Data" folder during init in order to make
58 // sure we're starting fresh.
60 // |saved_nigori_state| is optional nigori state to restore from a previous
61 // backend instance. May be null.
62 virtual void Initialize(
63 sync_driver::SyncFrontend
* frontend
,
64 scoped_ptr
<base::Thread
> sync_thread
,
65 const syncer::WeakHandle
<syncer::JsEventHandler
>& event_handler
,
66 const GURL
& service_url
,
67 const std::string
& sync_user_agent
,
68 const syncer::SyncCredentials
& credentials
,
69 bool delete_sync_data_folder
,
70 scoped_ptr
<syncer::SyncManagerFactory
> sync_manager_factory
,
71 const syncer::WeakHandle
<syncer::UnrecoverableErrorHandler
>&
72 unrecoverable_error_handler
,
73 const base::Closure
& report_unrecoverable_error_function
,
74 syncer::NetworkResources
* network_resources
,
75 scoped_ptr
<syncer::SyncEncryptionHandler::NigoriState
>
76 saved_nigori_state
) = 0;
78 // Called on the frontend's thread to update SyncCredentials.
79 virtual void UpdateCredentials(
80 const syncer::SyncCredentials
& credentials
) = 0;
82 // This starts the SyncerThread running a Syncer object to communicate with
83 // sync servers. Until this is called, no changes will leave or enter this
84 // browser from the cloud / sync servers.
85 // Called on |frontend_loop_|.
86 virtual void StartSyncingWithServer() = 0;
88 // Called on |frontend_loop_| to asynchronously set a new passphrase for
89 // encryption. Note that it is an error to call SetEncryptionPassphrase under
90 // the following circumstances:
91 // - An explicit passphrase has already been set
92 // - |is_explicit| is true and we have pending keys.
93 // When |is_explicit| is false, a couple of things could happen:
94 // - If there are pending keys, we try to decrypt them. If decryption works,
95 // this acts like a call to SetDecryptionPassphrase. If not, the GAIA
96 // passphrase passed in is cached so we can re-encrypt with it in future.
97 // - If there are no pending keys, data is encrypted with |passphrase| (this
98 // is a no-op if data was already encrypted with |passphrase|.)
99 virtual void SetEncryptionPassphrase(
100 const std::string
& passphrase
,
101 bool is_explicit
) = 0;
103 // Called on |frontend_loop_| to use the provided passphrase to asynchronously
104 // attempt decryption. Returns false immediately if the passphrase could not
105 // be used to decrypt a locally cached copy of encrypted keys; returns true
106 // otherwise. If new encrypted keys arrive during the asynchronous call,
107 // OnPassphraseRequired may be triggered at a later time. It is an error to
108 // call this when there are no pending keys.
109 virtual bool SetDecryptionPassphrase(const std::string
& passphrase
)
110 WARN_UNUSED_RESULT
= 0;
112 // Called on |frontend_loop_| to kick off shutdown procedure. Attempts to cut
113 // short any long-lived or blocking sync thread tasks so that the shutdown on
114 // sync thread task that we're about to post won't have to wait very long.
115 virtual void StopSyncingForShutdown() = 0;
117 // Called on |frontend_loop_| to kick off shutdown.
118 // See the implementation and Core::DoShutdown for details.
119 // Must be called *after* StopSyncingForShutdown.
120 // For any reason other than BROWSER_SHUTDOWN, caller should claim sync
122 // * during browser shutdown sync thread is not claimed to avoid blocking
123 // browser shutdown on sync shutdown.
124 // * otherwise sync thread is claimed so that if sync backend is recreated
125 // later, initialization of new backend is serialized on previous sync
126 // thread after cleanup of previous backend to avoid old/new backends
127 // interfere with each other.
128 virtual scoped_ptr
<base::Thread
> Shutdown(syncer::ShutdownReason reason
) = 0;
130 // Removes all current registrations from the backend on the
131 // InvalidationService.
132 virtual void UnregisterInvalidationIds() = 0;
134 // Changes the set of data types that are currently being synced.
135 // The ready_task will be run when configuration is done with the
136 // set of all types that failed configuration (i.e., if its argument
137 // is non-empty, then an error was encountered).
138 // Returns the set of types that are ready to start without needing any
139 // further sync activity.
140 // BackendDataTypeConfigurer implementation.
141 syncer::ModelTypeSet
ConfigureDataTypes(
142 syncer::ConfigureReason reason
,
143 const DataTypeConfigStateMap
& config_state_map
,
144 const base::Callback
<void(syncer::ModelTypeSet
, syncer::ModelTypeSet
)>&
146 const base::Callback
<void()>& retry_callback
) override
= 0;
148 // Turns on encryption of all present and future sync data.
149 virtual void EnableEncryptEverything() = 0;
151 // Called on |frontend_loop_| to obtain a handle to the UserShare needed for
152 // creating transactions. Should not be called before we signal
153 // initialization is complete with OnBackendInitialized().
154 virtual syncer::UserShare
* GetUserShare() const = 0;
156 // Called on |frontend_loop_| to obtain a handle to the SyncContext needed by
157 // the non-blocking sync types to communicate with the server.
159 // Should be called only when the backend is initialized.
160 virtual scoped_ptr
<syncer_v2::SyncContextProxy
> GetSyncContextProxy() = 0;
162 // Called from any thread to obtain current status information in detailed or
164 virtual Status
GetDetailedStatus() = 0;
165 virtual syncer::sessions::SyncSessionSnapshot
166 GetLastSessionSnapshot() const = 0;
168 // Determines if the underlying sync engine has made any local changes to
169 // items that have not yet been synced with the server.
170 // ONLY CALL THIS IF OnInitializationComplete was called!
171 virtual bool HasUnsyncedItems() const = 0;
173 // Whether or not we are syncing encryption keys.
174 virtual bool IsNigoriEnabled() const = 0;
176 // Returns the type of passphrase being used to encrypt data. See
177 // sync_encryption_handler.h.
178 virtual syncer::PassphraseType
GetPassphraseType() const = 0;
180 // If an explicit passphrase is in use, returns the time at which that
181 // passphrase was set (if available).
182 virtual base::Time
GetExplicitPassphraseTime() const = 0;
184 // True if the cryptographer has any keys available to attempt decryption.
185 // Could mean we've downloaded and loaded Nigori objects, or we bootstrapped
186 // using a token previously received.
187 virtual bool IsCryptographerReady(
188 const syncer::BaseTransaction
* trans
) const = 0;
190 virtual void GetModelSafeRoutingInfo(
191 syncer::ModelSafeRoutingInfo
* out
) const = 0;
193 // Send a message to the sync thread to persist the Directory to disk.
194 virtual void FlushDirectory() const = 0;
196 // Requests that the backend forward to the fronent any protocol events in
197 // its buffer and begin forwarding automatically from now on. Repeated calls
198 // to this function may result in the same events being emitted several
200 virtual void RequestBufferedProtocolEventsAndEnableForwarding() = 0;
202 // Disables protocol event forwarding.
203 virtual void DisableProtocolEventForwarding() = 0;
205 // Returns a ListValue representing all nodes for the specified types through
206 // |callback| on this thread.
207 virtual void GetAllNodesForTypes(
208 syncer::ModelTypeSet types
,
209 base::Callback
<void(const std::vector
<syncer::ModelType
>&,
210 ScopedVector
<base::ListValue
>)> type
) = 0;
212 // Enables the sending of directory type debug counters. Also, for every
213 // time it is called, it makes an explicit request that updates to an update
214 // for all counters be emitted.
215 virtual void EnableDirectoryTypeDebugInfoForwarding() = 0;
217 // Disables the sending of directory type debug counters.
218 virtual void DisableDirectoryTypeDebugInfoForwarding() = 0;
220 virtual base::MessageLoop
* GetSyncLoopForTesting() = 0;
222 // Triggers sync cycle to update |types|.
223 virtual void RefreshTypesForTest(syncer::ModelTypeSet types
) = 0;
225 // See SyncManager::ClearServerData.
226 virtual void ClearServerData(
227 const syncer::SyncManager::ClearServerDataCallback
& callback
) = 0;
229 DISALLOW_COPY_AND_ASSIGN(SyncBackendHost
);
232 } // namespace browser_sync
234 #endif // CHROME_BROWSER_SYNC_GLUE_SYNC_BACKEND_HOST_H_