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.
4
5#ifndef CHROME_BROWSER_SYNC_GLUE_SYNC_BACKEND_HOST_H_
6#define CHROME_BROWSER_SYNC_GLUE_SYNC_BACKEND_HOST_H_
7
8#include <string>
9
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/report_unrecoverable_error_function.h"
24#include "sync/internal_api/public/util/unrecoverable_error_handler.h"
25#include "sync/internal_api/public/util/weak_handle.h"
26
27class GURL;
28
29namespace base {
30class MessageLoop;
31}
32
33namespace syncer {
34class NetworkResources;
35class SyncManagerFactory;
36}
37
38namespace sync_driver {
39class ChangeProcessor;
40class SyncFrontend;
41}
42
43namespace browser_sync {
44
45// An API to "host" the top level SyncAPI element.
46//
47// This class handles dispatch of potentially blocking calls to appropriate
48// threads and ensures that the SyncFrontend is only accessed on the UI loop.
49class SyncBackendHost : public sync_driver::BackendDataTypeConfigurer {
50 public:
51  typedef syncer::SyncStatus Status;
52
53  // Stubs used by implementing classes.
54  SyncBackendHost();
55  virtual ~SyncBackendHost();
56
57  // Called on the frontend's thread to kick off asynchronous initialization.
58  // Optionally deletes the "Sync Data" folder during init in order to make
59  // sure we're starting fresh.
60  //
61  // |report_unrecoverable_error_function| can be NULL.  Note:
62  // |unrecoverable_error_handler| may be invoked from any thread.
63  virtual void Initialize(
64      sync_driver::SyncFrontend* frontend,
65      scoped_ptr<base::Thread> sync_thread,
66      const syncer::WeakHandle<syncer::JsEventHandler>& event_handler,
67      const GURL& service_url,
68      const syncer::SyncCredentials& credentials,
69      bool delete_sync_data_folder,
70      scoped_ptr<syncer::SyncManagerFactory> sync_manager_factory,
71      scoped_ptr<syncer::UnrecoverableErrorHandler> unrecoverable_error_handler,
72      syncer::ReportUnrecoverableErrorFunction
73          report_unrecoverable_error_function,
74      syncer::NetworkResources* network_resources) = 0;
75
76  // Called on the frontend's thread to update SyncCredentials.
77  virtual void UpdateCredentials(
78      const syncer::SyncCredentials& credentials) = 0;
79
80  // This starts the SyncerThread running a Syncer object to communicate with
81  // sync servers.  Until this is called, no changes will leave or enter this
82  // browser from the cloud / sync servers.
83  // Called on |frontend_loop_|.
84  virtual void StartSyncingWithServer() = 0;
85
86  // Called on |frontend_loop_| to asynchronously set a new passphrase for
87  // encryption. Note that it is an error to call SetEncryptionPassphrase under
88  // the following circumstances:
89  // - An explicit passphrase has already been set
90  // - |is_explicit| is true and we have pending keys.
91  // When |is_explicit| is false, a couple of things could happen:
92  // - If there are pending keys, we try to decrypt them. If decryption works,
93  //   this acts like a call to SetDecryptionPassphrase. If not, the GAIA
94  //   passphrase passed in is cached so we can re-encrypt with it in future.
95  // - If there are no pending keys, data is encrypted with |passphrase| (this
96  //   is a no-op if data was already encrypted with |passphrase|.)
97  virtual void SetEncryptionPassphrase(
98      const std::string& passphrase,
99      bool is_explicit) = 0;
100
101  // Called on |frontend_loop_| to use the provided passphrase to asynchronously
102  // attempt decryption. Returns false immediately if the passphrase could not
103  // be used to decrypt a locally cached copy of encrypted keys; returns true
104  // otherwise. If new encrypted keys arrive during the asynchronous call,
105  // OnPassphraseRequired may be triggered at a later time. It is an error to
106  // call this when there are no pending keys.
107  virtual bool SetDecryptionPassphrase(const std::string& passphrase)
108      WARN_UNUSED_RESULT = 0;
109
110  // Called on |frontend_loop_| to kick off shutdown procedure.  Attempts to cut
111  // short any long-lived or blocking sync thread tasks so that the shutdown on
112  // sync thread task that we're about to post won't have to wait very long.
113  virtual void StopSyncingForShutdown() = 0;
114
115  // Called on |frontend_loop_| to kick off shutdown.
116  // See the implementation and Core::DoShutdown for details.
117  // Must be called *after* StopSyncingForShutdown.
118  // For any reason other than BROWSER_SHUTDOWN, caller should claim sync
119  // thread because:
120  // * during browser shutdown sync thread is not claimed to avoid blocking
121  //   browser shutdown on sync shutdown.
122  // * otherwise sync thread is claimed so that if sync backend is recreated
123  //   later, initialization of new backend is serialized on previous sync
124  //   thread after cleanup of previous backend to avoid old/new backends
125  //   interfere with each other.
126  virtual scoped_ptr<base::Thread> Shutdown(syncer::ShutdownReason reason) = 0;
127
128  // Removes all current registrations from the backend on the
129  // InvalidationService.
130  virtual void UnregisterInvalidationIds() = 0;
131
132  // Changes the set of data types that are currently being synced.
133  // The ready_task will be run when configuration is done with the
134  // set of all types that failed configuration (i.e., if its argument
135  // is non-empty, then an error was encountered).
136  virtual void ConfigureDataTypes(
137      syncer::ConfigureReason reason,
138      const DataTypeConfigStateMap& config_state_map,
139      const base::Callback<void(syncer::ModelTypeSet,
140                                syncer::ModelTypeSet)>& ready_task,
141      const base::Callback<void()>& retry_callback) OVERRIDE = 0;
142
143  // Turns on encryption of all present and future sync data.
144  virtual void EnableEncryptEverything() = 0;
145
146  // Called on |frontend_loop_| to obtain a handle to the UserShare needed for
147  // creating transactions.  Should not be called before we signal
148  // initialization is complete with OnBackendInitialized().
149  virtual syncer::UserShare* GetUserShare() const = 0;
150
151  // Called on |frontend_loop_| to obtain a handle to the SyncContext needed by
152  // the non-blocking sync types to communicate with the server.
153  //
154  // Should be called only when the backend is initialized.
155  virtual scoped_ptr<syncer::SyncContextProxy> GetSyncContextProxy() = 0;
156
157  // Called from any thread to obtain current status information in detailed or
158  // summarized form.
159  virtual Status GetDetailedStatus() = 0;
160  virtual syncer::sessions::SyncSessionSnapshot
161      GetLastSessionSnapshot() const = 0;
162
163  // Determines if the underlying sync engine has made any local changes to
164  // items that have not yet been synced with the server.
165  // ONLY CALL THIS IF OnInitializationComplete was called!
166  virtual bool HasUnsyncedItems() const = 0;
167
168  // Whether or not we are syncing encryption keys.
169  virtual bool IsNigoriEnabled() const = 0;
170
171  // Returns the type of passphrase being used to encrypt data. See
172  // sync_encryption_handler.h.
173  virtual syncer::PassphraseType GetPassphraseType() const = 0;
174
175  // If an explicit passphrase is in use, returns the time at which that
176  // passphrase was set (if available).
177  virtual base::Time GetExplicitPassphraseTime() const = 0;
178
179  // True if the cryptographer has any keys available to attempt decryption.
180  // Could mean we've downloaded and loaded Nigori objects, or we bootstrapped
181  // using a token previously received.
182  virtual bool IsCryptographerReady(
183      const syncer::BaseTransaction* trans) const = 0;
184
185  virtual void GetModelSafeRoutingInfo(
186      syncer::ModelSafeRoutingInfo* out) const = 0;
187
188  // Requests that the backend forward to the fronent any protocol events in
189  // its buffer and begin forwarding automatically from now on.  Repeated calls
190  // to this function may result in the same events being emitted several
191  // times.
192  virtual void RequestBufferedProtocolEventsAndEnableForwarding() = 0;
193
194  // Disables protocol event forwarding.
195  virtual void DisableProtocolEventForwarding() = 0;
196
197  // Returns a ListValue representing all nodes for the specified types through
198  // |callback| on this thread.
199  virtual void GetAllNodesForTypes(
200      syncer::ModelTypeSet types,
201      base::Callback<void(const std::vector<syncer::ModelType>&,
202                          ScopedVector<base::ListValue>)> type) = 0;
203
204  // Enables the sending of directory type debug counters.  Also, for every
205  // time it is called, it makes an explicit request that updates to an update
206  // for all counters be emitted.
207  virtual void EnableDirectoryTypeDebugInfoForwarding() = 0;
208
209  // Disables the sending of directory type debug counters.
210  virtual void DisableDirectoryTypeDebugInfoForwarding() = 0;
211
212  virtual base::MessageLoop* GetSyncLoopForTesting() = 0;
213
214  DISALLOW_COPY_AND_ASSIGN(SyncBackendHost);
215};
216
217}  // namespace browser_sync
218
219#endif  // CHROME_BROWSER_SYNC_GLUE_SYNC_BACKEND_HOST_H_
220