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 scoped_refptr
<base::SingleThreadTaskRunner
>& db_thread
,
66 const scoped_refptr
<base::SingleThreadTaskRunner
>& file_thread
,
67 const syncer::WeakHandle
<syncer::JsEventHandler
>& event_handler
,
68 const GURL
& service_url
,
69 const std::string
& sync_user_agent
,
70 const syncer::SyncCredentials
& credentials
,
71 bool delete_sync_data_folder
,
72 scoped_ptr
<syncer::SyncManagerFactory
> sync_manager_factory
,
73 const syncer::WeakHandle
<syncer::UnrecoverableErrorHandler
>&
74 unrecoverable_error_handler
,
75 const base::Closure
& report_unrecoverable_error_function
,
76 syncer::NetworkResources
* network_resources
,
77 scoped_ptr
<syncer::SyncEncryptionHandler::NigoriState
>
78 saved_nigori_state
) = 0;
80 // Called on the frontend's thread to update SyncCredentials.
81 virtual void UpdateCredentials(
82 const syncer::SyncCredentials
& credentials
) = 0;
84 // This starts the SyncerThread running a Syncer object to communicate with
85 // sync servers. Until this is called, no changes will leave or enter this
86 // browser from the cloud / sync servers.
87 // Called on |frontend_loop_|.
88 virtual void StartSyncingWithServer() = 0;
90 // Called on |frontend_loop_| to asynchronously set a new passphrase for
91 // encryption. Note that it is an error to call SetEncryptionPassphrase under
92 // the following circumstances:
93 // - An explicit passphrase has already been set
94 // - |is_explicit| is true and we have pending keys.
95 // When |is_explicit| is false, a couple of things could happen:
96 // - If there are pending keys, we try to decrypt them. If decryption works,
97 // this acts like a call to SetDecryptionPassphrase. If not, the GAIA
98 // passphrase passed in is cached so we can re-encrypt with it in future.
99 // - If there are no pending keys, data is encrypted with |passphrase| (this
100 // is a no-op if data was already encrypted with |passphrase|.)
101 virtual void SetEncryptionPassphrase(
102 const std::string
& passphrase
,
103 bool is_explicit
) = 0;
105 // Called on |frontend_loop_| to use the provided passphrase to asynchronously
106 // attempt decryption. Returns false immediately if the passphrase could not
107 // be used to decrypt a locally cached copy of encrypted keys; returns true
108 // otherwise. If new encrypted keys arrive during the asynchronous call,
109 // OnPassphraseRequired may be triggered at a later time. It is an error to
110 // call this when there are no pending keys.
111 virtual bool SetDecryptionPassphrase(const std::string
& passphrase
)
112 WARN_UNUSED_RESULT
= 0;
114 // Called on |frontend_loop_| to kick off shutdown procedure. Attempts to cut
115 // short any long-lived or blocking sync thread tasks so that the shutdown on
116 // sync thread task that we're about to post won't have to wait very long.
117 virtual void StopSyncingForShutdown() = 0;
119 // Called on |frontend_loop_| to kick off shutdown.
120 // See the implementation and Core::DoShutdown for details.
121 // Must be called *after* StopSyncingForShutdown.
122 // For any reason other than BROWSER_SHUTDOWN, caller should claim sync
124 // * during browser shutdown sync thread is not claimed to avoid blocking
125 // browser shutdown on sync shutdown.
126 // * otherwise sync thread is claimed so that if sync backend is recreated
127 // later, initialization of new backend is serialized on previous sync
128 // thread after cleanup of previous backend to avoid old/new backends
129 // interfere with each other.
130 virtual scoped_ptr
<base::Thread
> Shutdown(syncer::ShutdownReason reason
) = 0;
132 // Removes all current registrations from the backend on the
133 // InvalidationService.
134 virtual void UnregisterInvalidationIds() = 0;
136 // Changes the set of data types that are currently being synced.
137 // The ready_task will be run when configuration is done with the
138 // set of all types that failed configuration (i.e., if its argument
139 // is non-empty, then an error was encountered).
140 // Returns the set of types that are ready to start without needing any
141 // further sync activity.
142 // BackendDataTypeConfigurer implementation.
143 syncer::ModelTypeSet
ConfigureDataTypes(
144 syncer::ConfigureReason reason
,
145 const DataTypeConfigStateMap
& config_state_map
,
146 const base::Callback
<void(syncer::ModelTypeSet
, syncer::ModelTypeSet
)>&
148 const base::Callback
<void()>& retry_callback
) override
= 0;
150 // Turns on encryption of all present and future sync data.
151 virtual void EnableEncryptEverything() = 0;
153 // Called on |frontend_loop_| to obtain a handle to the UserShare needed for
154 // creating transactions. Should not be called before we signal
155 // initialization is complete with OnBackendInitialized().
156 virtual syncer::UserShare
* GetUserShare() const = 0;
158 // Called on |frontend_loop_| to obtain a handle to the SyncContext needed by
159 // the non-blocking sync types to communicate with the server.
161 // Should be called only when the backend is initialized.
162 virtual scoped_ptr
<syncer_v2::SyncContextProxy
> GetSyncContextProxy() = 0;
164 // Called from any thread to obtain current status information in detailed or
166 virtual Status
GetDetailedStatus() = 0;
167 virtual syncer::sessions::SyncSessionSnapshot
168 GetLastSessionSnapshot() const = 0;
170 // Determines if the underlying sync engine has made any local changes to
171 // items that have not yet been synced with the server.
172 // ONLY CALL THIS IF OnInitializationComplete was called!
173 virtual bool HasUnsyncedItems() const = 0;
175 // Whether or not we are syncing encryption keys.
176 virtual bool IsNigoriEnabled() const = 0;
178 // Returns the type of passphrase being used to encrypt data. See
179 // sync_encryption_handler.h.
180 virtual syncer::PassphraseType
GetPassphraseType() const = 0;
182 // If an explicit passphrase is in use, returns the time at which that
183 // passphrase was set (if available).
184 virtual base::Time
GetExplicitPassphraseTime() const = 0;
186 // True if the cryptographer has any keys available to attempt decryption.
187 // Could mean we've downloaded and loaded Nigori objects, or we bootstrapped
188 // using a token previously received.
189 virtual bool IsCryptographerReady(
190 const syncer::BaseTransaction
* trans
) const = 0;
192 virtual void GetModelSafeRoutingInfo(
193 syncer::ModelSafeRoutingInfo
* out
) const = 0;
195 // Send a message to the sync thread to persist the Directory to disk.
196 virtual void FlushDirectory() const = 0;
198 // Requests that the backend forward to the fronent any protocol events in
199 // its buffer and begin forwarding automatically from now on. Repeated calls
200 // to this function may result in the same events being emitted several
202 virtual void RequestBufferedProtocolEventsAndEnableForwarding() = 0;
204 // Disables protocol event forwarding.
205 virtual void DisableProtocolEventForwarding() = 0;
207 // Returns a ListValue representing all nodes for the specified types through
208 // |callback| on this thread.
209 virtual void GetAllNodesForTypes(
210 syncer::ModelTypeSet types
,
211 base::Callback
<void(const std::vector
<syncer::ModelType
>&,
212 ScopedVector
<base::ListValue
>)> type
) = 0;
214 // Enables the sending of directory type debug counters. Also, for every
215 // time it is called, it makes an explicit request that updates to an update
216 // for all counters be emitted.
217 virtual void EnableDirectoryTypeDebugInfoForwarding() = 0;
219 // Disables the sending of directory type debug counters.
220 virtual void DisableDirectoryTypeDebugInfoForwarding() = 0;
222 virtual base::MessageLoop
* GetSyncLoopForTesting() = 0;
224 // Triggers sync cycle to update |types|.
225 virtual void RefreshTypesForTest(syncer::ModelTypeSet types
) = 0;
227 // See SyncManager::ClearServerData.
228 virtual void ClearServerData(
229 const syncer::SyncManager::ClearServerDataCallback
& callback
) = 0;
231 DISALLOW_COPY_AND_ASSIGN(SyncBackendHost
);
234 } // namespace browser_sync
236 #endif // CHROME_BROWSER_SYNC_GLUE_SYNC_BACKEND_HOST_H_