32615d45f632b9a0c710c23e9a270254ea73d785
[platform/framework/web/crosswalk.git] / src / chrome / browser / sync / glue / sync_backend_host.h
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
27 class GURL;
28
29 namespace base {
30 class MessageLoop;
31 }
32
33 namespace syncer {
34 class NetworkResources;
35 class SyncManagerFactory;
36 }
37
38 namespace sync_driver {
39 class ChangeProcessor;
40 class SyncFrontend;
41 }
42
43 namespace browser_sync {
44
45 class SyncedDeviceTracker;
46
47 // An API to "host" the top level SyncAPI element.
48 //
49 // This class handles dispatch of potentially blocking calls to appropriate
50 // threads and ensures that the SyncFrontend is only accessed on the UI loop.
51 class SyncBackendHost : public sync_driver::BackendDataTypeConfigurer {
52  public:
53   typedef syncer::SyncStatus Status;
54
55   // Stubs used by implementing classes.
56   SyncBackendHost();
57   virtual ~SyncBackendHost();
58
59   // Called on the frontend's thread to kick off asynchronous initialization.
60   // Optionally deletes the "Sync Data" folder during init in order to make
61   // sure we're starting fresh.
62   //
63   // |report_unrecoverable_error_function| can be NULL.  Note:
64   // |unrecoverable_error_handler| may be invoked from any thread.
65   virtual void Initialize(
66       sync_driver::SyncFrontend* frontend,
67       scoped_ptr<base::Thread> sync_thread,
68       const syncer::WeakHandle<syncer::JsEventHandler>& event_handler,
69       const GURL& service_url,
70       const syncer::SyncCredentials& credentials,
71       bool delete_sync_data_folder,
72       scoped_ptr<syncer::SyncManagerFactory> sync_manager_factory,
73       scoped_ptr<syncer::UnrecoverableErrorHandler> unrecoverable_error_handler,
74       syncer::ReportUnrecoverableErrorFunction
75           report_unrecoverable_error_function,
76       syncer::NetworkResources* network_resources) = 0;
77
78   // Called on the frontend's thread to update SyncCredentials.
79   virtual void UpdateCredentials(
80       const syncer::SyncCredentials& credentials) = 0;
81
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;
87
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;
102
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;
111
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;
116
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
121   // thread because:
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;
129
130   // Removes all current registrations from the backend on the
131   // InvalidationService.
132   virtual void UnregisterInvalidationIds() = 0;
133
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   virtual void ConfigureDataTypes(
139       syncer::ConfigureReason reason,
140       const DataTypeConfigStateMap& config_state_map,
141       const base::Callback<void(syncer::ModelTypeSet,
142                                 syncer::ModelTypeSet)>& ready_task,
143       const base::Callback<void()>& retry_callback) OVERRIDE = 0;
144
145   // Turns on encryption of all present and future sync data.
146   virtual void EnableEncryptEverything() = 0;
147
148   // Called on |frontend_loop_| to obtain a handle to the UserShare needed for
149   // creating transactions.  Should not be called before we signal
150   // initialization is complete with OnBackendInitialized().
151   virtual syncer::UserShare* GetUserShare() const = 0;
152
153   // Called on |frontend_loop_| to obtain a handle to the SyncContext needed by
154   // the non-blocking sync types to communicate with the server.
155   //
156   // Should be called only when the backend is initialized.
157   virtual scoped_ptr<syncer::SyncContextProxy> GetSyncContextProxy() = 0;
158
159   // Called from any thread to obtain current status information in detailed or
160   // summarized form.
161   virtual Status GetDetailedStatus() = 0;
162   virtual syncer::sessions::SyncSessionSnapshot
163       GetLastSessionSnapshot() const = 0;
164
165   // Determines if the underlying sync engine has made any local changes to
166   // items that have not yet been synced with the server.
167   // ONLY CALL THIS IF OnInitializationComplete was called!
168   virtual bool HasUnsyncedItems() const = 0;
169
170   // Whether or not we are syncing encryption keys.
171   virtual bool IsNigoriEnabled() const = 0;
172
173   // Returns the type of passphrase being used to encrypt data. See
174   // sync_encryption_handler.h.
175   virtual syncer::PassphraseType GetPassphraseType() const = 0;
176
177   // If an explicit passphrase is in use, returns the time at which that
178   // passphrase was set (if available).
179   virtual base::Time GetExplicitPassphraseTime() const = 0;
180
181   // True if the cryptographer has any keys available to attempt decryption.
182   // Could mean we've downloaded and loaded Nigori objects, or we bootstrapped
183   // using a token previously received.
184   virtual bool IsCryptographerReady(
185       const syncer::BaseTransaction* trans) const = 0;
186
187   virtual void GetModelSafeRoutingInfo(
188       syncer::ModelSafeRoutingInfo* out) const = 0;
189
190   // Fetches the DeviceInfo tracker.
191   virtual SyncedDeviceTracker* GetSyncedDeviceTracker() const = 0;
192
193   // Requests that the backend forward to the fronent any protocol events in
194   // its buffer and begin forwarding automatically from now on.  Repeated calls
195   // to this function may result in the same events being emitted several
196   // times.
197   virtual void RequestBufferedProtocolEventsAndEnableForwarding() = 0;
198
199   // Disables protocol event forwarding.
200   virtual void DisableProtocolEventForwarding() = 0;
201
202   // Returns a ListValue representing all nodes for the specified types through
203   // |callback| on this thread.
204   virtual void GetAllNodesForTypes(
205       syncer::ModelTypeSet types,
206       base::Callback<void(const std::vector<syncer::ModelType>&,
207                           ScopedVector<base::ListValue>)> type) = 0;
208
209   // Enables the sending of directory type debug counters.  Also, for every
210   // time it is called, it makes an explicit request that updates to an update
211   // for all counters be emitted.
212   virtual void EnableDirectoryTypeDebugInfoForwarding() = 0;
213
214   // Disables the sending of directory type debug counters.
215   virtual void DisableDirectoryTypeDebugInfoForwarding() = 0;
216
217   virtual base::MessageLoop* GetSyncLoopForTesting() = 0;
218
219   DISALLOW_COPY_AND_ASSIGN(SyncBackendHost);
220 };
221
222 }  // namespace browser_sync
223
224 #endif  // CHROME_BROWSER_SYNC_GLUE_SYNC_BACKEND_HOST_H_