1f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// found in the LICENSE file.
4f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
5f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#ifndef CHROME_BROWSER_SYNC_GLUE_SYNC_BACKEND_HOST_IMPL_H_
6f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#define CHROME_BROWSER_SYNC_GLUE_SYNC_BACKEND_HOST_IMPL_H_
7f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
8f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include <string>
9f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
10f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "base/basictypes.h"
11f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "base/callback.h"
12f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "base/compiler_specific.h"
13f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "base/memory/scoped_ptr.h"
14f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "base/memory/weak_ptr.h"
15f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "base/threading/thread.h"
16f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "chrome/browser/sync/glue/extensions_activity_monitor.h"
17f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "chrome/browser/sync/glue/sync_backend_host.h"
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "components/sync_driver/backend_data_type_configurer.h"
19f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "content/public/browser/notification_observer.h"
20f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "content/public/browser/notification_registrar.h"
21f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "sync/internal_api/public/base/model_type.h"
22f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "sync/internal_api/public/configure_reason.h"
23f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "sync/internal_api/public/sessions/sync_session_snapshot.h"
24010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "sync/internal_api/public/sessions/type_debug_info_observer.h"
25f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "sync/internal_api/public/sync_manager.h"
26f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "sync/internal_api/public/util/report_unrecoverable_error_function.h"
27f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "sync/internal_api/public/util/unrecoverable_error_handler.h"
28f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "sync/internal_api/public/util/weak_handle.h"
29f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "sync/notifier/invalidation_handler.h"
30f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "sync/protocol/encryption.pb.h"
31f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "sync/protocol/sync_protocol_error.h"
32f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "sync/util/extensions_activity.h"
33f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
34f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)class GURL;
35f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)class Profile;
36f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
37f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)namespace base {
38f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)class MessageLoop;
39f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
40f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
41f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)namespace invalidation {
42f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)class InvalidationService;
43f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
44f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
45f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)namespace syncer {
46a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)class NetworkResources;
47f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)class SyncManagerFactory;
48f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
49f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
5023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)namespace sync_driver {
5123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)class SyncPrefs;
5223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)}
5323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
54f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)namespace browser_sync {
55f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
56f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)class ChangeProcessor;
57f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)class SyncBackendHostCore;
58f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)class SyncBackendRegistrar;
59f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)class SyncedDeviceTracker;
60f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)struct DoInitializeOptions;
61f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
62f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// The only real implementation of the SyncBackendHost.  See that interface's
63f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// definition for documentation of public methods.
64f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)class SyncBackendHostImpl
65f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    : public SyncBackendHost,
66f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      public content::NotificationObserver,
67f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      public syncer::InvalidationHandler {
68f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) public:
69f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  typedef syncer::SyncStatus Status;
70f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
71f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Create a SyncBackendHost with a reference to the |frontend| that
72f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // it serves and communicates to via the SyncFrontend interface (on
73f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // the same thread it used to call the constructor).  Must outlive
74f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // |sync_prefs|.
7523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  SyncBackendHostImpl(const std::string& name,
7623730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)                      Profile* profile,
77cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                      invalidation::InvalidationService* invalidator,
78cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                      const base::WeakPtr<sync_driver::SyncPrefs>& sync_prefs,
79cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                      const base::FilePath& sync_folder);
80f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual ~SyncBackendHostImpl();
81f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
82f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // SyncBackendHost implementation.
83f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual void Initialize(
84f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      SyncFrontend* frontend,
85f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      scoped_ptr<base::Thread> sync_thread,
86f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      const syncer::WeakHandle<syncer::JsEventHandler>& event_handler,
87f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      const GURL& service_url,
88f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      const syncer::SyncCredentials& credentials,
89f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      bool delete_sync_data_folder,
90f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      scoped_ptr<syncer::SyncManagerFactory> sync_manager_factory,
91f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      scoped_ptr<syncer::UnrecoverableErrorHandler> unrecoverable_error_handler,
92f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      syncer::ReportUnrecoverableErrorFunction
93a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)          report_unrecoverable_error_function,
94a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      syncer::NetworkResources* network_resources) OVERRIDE;
95f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual void UpdateCredentials(
96f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      const syncer::SyncCredentials& credentials) OVERRIDE;
97f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual void StartSyncingWithServer() OVERRIDE;
98f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual void SetEncryptionPassphrase(
99f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      const std::string& passphrase,
100f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      bool is_explicit) OVERRIDE;
101f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual bool SetDecryptionPassphrase(const std::string& passphrase)
1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      OVERRIDE WARN_UNUSED_RESULT;
103f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual void StopSyncingForShutdown() OVERRIDE;
104f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual scoped_ptr<base::Thread> Shutdown(ShutdownOption option) OVERRIDE;
105f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual void UnregisterInvalidationIds() OVERRIDE;
106f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual void ConfigureDataTypes(
107f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      syncer::ConfigureReason reason,
108f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      const DataTypeConfigStateMap& config_state_map,
109f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      const base::Callback<void(syncer::ModelTypeSet,
110f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                syncer::ModelTypeSet)>& ready_task,
111f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      const base::Callback<void()>& retry_callback) OVERRIDE;
112f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual void ActivateDataType(
113010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)     syncer::ModelType type, syncer::ModelSafeGroup group,
114010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)     ChangeProcessor* change_processor) OVERRIDE;
115f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual void DeactivateDataType(syncer::ModelType type) OVERRIDE;
116010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  virtual void EnableEncryptEverything() OVERRIDE;
117f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual syncer::UserShare* GetUserShare() const OVERRIDE;
118a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  virtual scoped_ptr<syncer::SyncCoreProxy> GetSyncCoreProxy() OVERRIDE;
119f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual Status GetDetailedStatus() OVERRIDE;
120f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual syncer::sessions::SyncSessionSnapshot
121f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      GetLastSessionSnapshot() const OVERRIDE;
122f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual bool HasUnsyncedItems() const OVERRIDE;
123f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual bool IsNigoriEnabled() const OVERRIDE;
124f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual syncer::PassphraseType GetPassphraseType() const OVERRIDE;
125f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual base::Time GetExplicitPassphraseTime() const OVERRIDE;
126f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual bool IsCryptographerReady(
127f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      const syncer::BaseTransaction* trans) const OVERRIDE;
128f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual void GetModelSafeRoutingInfo(
129f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      syncer::ModelSafeRoutingInfo* out) const OVERRIDE;
130f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual SyncedDeviceTracker* GetSyncedDeviceTracker() const OVERRIDE;
131e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  virtual void RequestBufferedProtocolEventsAndEnableForwarding() OVERRIDE;
132e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  virtual void DisableProtocolEventForwarding() OVERRIDE;
133010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  virtual void EnableDirectoryTypeDebugInfoForwarding() OVERRIDE;
134010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  virtual void DisableDirectoryTypeDebugInfoForwarding() OVERRIDE;
135c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  virtual void GetAllNodesForTypes(
136c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      syncer::ModelTypeSet types,
137c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      base::Callback<void(const std::vector<syncer::ModelType>&,
138c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                          ScopedVector<base::ListValue>)> type) OVERRIDE;
139f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual base::MessageLoop* GetSyncLoopForTesting() OVERRIDE;
140f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
141f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) protected:
142f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // The types and functions below are protected so that test
143f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // subclasses can use them.
144f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
145f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Allows tests to perform alternate core initialization work.
146f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual void InitCore(scoped_ptr<DoInitializeOptions> options);
147f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
148f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Request the syncer to reconfigure with the specfied params.
149f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Virtual for testing.
150f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual void RequestConfigureSyncer(
151f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      syncer::ConfigureReason reason,
152f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      syncer::ModelTypeSet to_download,
153f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      syncer::ModelTypeSet to_purge,
154f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      syncer::ModelTypeSet to_journal,
155f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      syncer::ModelTypeSet to_unapply,
156f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      syncer::ModelTypeSet to_ignore,
157f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      const syncer::ModelSafeRoutingInfo& routing_info,
158f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      const base::Callback<void(syncer::ModelTypeSet,
159f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                syncer::ModelTypeSet)>& ready_task,
160f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      const base::Closure& retry_callback);
161f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
162f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Called when the syncer has finished performing a configuration.
163f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  void FinishConfigureDataTypesOnFrontendLoop(
164f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      const syncer::ModelTypeSet enabled_types,
165f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      const syncer::ModelTypeSet succeeded_configuration_types,
166f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      const syncer::ModelTypeSet failed_configuration_types,
167f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      const base::Callback<void(syncer::ModelTypeSet,
168f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                syncer::ModelTypeSet)>& ready_task);
169f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
170f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Reports backend initialization success.  Includes some objects from sync
171f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // manager initialization to be passed back to the UI thread.
172a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  //
173a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  // |sync_core_proxy| points to an object owned by the SyncManager.  Ownership
174a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  // is not transferred, but we can obtain our own copy of the object using its
175a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  // Clone() method.
176f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual void HandleInitializationSuccessOnFrontendLoop(
177f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const syncer::WeakHandle<syncer::JsBackend> js_backend,
178f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const syncer::WeakHandle<syncer::DataTypeDebugInfoListener>
179c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch        debug_info_listener,
180a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    syncer::SyncCoreProxy* sync_core_proxy);
181f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
182f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Downloading of control types failed and will be retried. Invokes the
183f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // frontend's sync configure retry method.
184f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  void HandleControlTypesDownloadRetry();
185f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
186effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Forwards a ProtocolEvent to the frontend.  Will not be called unless a
187effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // call to SetForwardProtocolEvents() explicitly requested that we start
188effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // forwarding these events.
189effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  void HandleProtocolEventOnFrontendLoop(syncer::ProtocolEvent* event);
190effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
191010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // Forwards a directory commit counter update to the frontend loop.  Will not
192010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // be called unless a call to EnableDirectoryTypeDebugInfoForwarding()
193010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // explicitly requested that we start forwarding these events.
194010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  void HandleDirectoryCommitCountersUpdatedOnFrontendLoop(
195010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      syncer::ModelType type,
196010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      const syncer::CommitCounters& counters);
197010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
198010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // Forwards a directory update counter update to the frontend loop.  Will not
199010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // be called unless a call to EnableDirectoryTypeDebugInfoForwarding()
200010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // explicitly requested that we start forwarding these events.
201010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  void HandleDirectoryUpdateCountersUpdatedOnFrontendLoop(
202010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      syncer::ModelType type,
203010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      const syncer::UpdateCounters& counters);
204010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
205010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // Forwards a directory status counter update to the frontend loop.  Will not
206010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // be called unless a call to EnableDirectoryTypeDebugInfoForwarding()
207010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // explicitly requested that we start forwarding these events.
208010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  void HandleDirectoryStatusCountersUpdatedOnFrontendLoop(
209010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      syncer::ModelType type,
210010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      const syncer::StatusCounters& counters);
211010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
212f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  SyncFrontend* frontend() { return frontend_; }
213f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
214f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) private:
215f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  friend class SyncBackendHostCore;
216f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
217f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Checks if we have received a notice to turn on experimental datatypes
218f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // (via the nigori node) and informs the frontend if that is the case.
219f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Note: it is illegal to call this before the backend is initialized.
220f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  void AddExperimentalTypes();
221f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
222f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Handles backend initialization failure.
223f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  void HandleInitializationFailureOnFrontendLoop();
224f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
225f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Called from Core::OnSyncCycleCompleted to handle updating frontend
226f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // thread components.
227f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  void HandleSyncCycleCompletedOnFrontendLoop(
228f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      const syncer::sessions::SyncSessionSnapshot& snapshot);
229f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
230f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Called when the syncer failed to perform a configuration and will
231f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // eventually retry. FinishingConfigurationOnFrontendLoop(..) will be called
232f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // on successful completion.
233f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  void RetryConfigurationOnFrontendLoop(const base::Closure& retry_callback);
234f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
235f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Helpers to persist a token that can be used to bootstrap sync encryption
236f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // across browser restart to avoid requiring the user to re-enter their
237f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // passphrase.  |token| must be valid UTF-8 as we use the PrefService for
238f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // storage.
239f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  void PersistEncryptionBootstrapToken(
240f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      const std::string& token,
241f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      syncer::BootstrapTokenType token_type);
242f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
243f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // For convenience, checks if initialization state is INITIALIZED.
244f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  bool initialized() const { return initialized_; }
245f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
246f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Let the front end handle the actionable error event.
247f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  void HandleActionableErrorEventOnFrontendLoop(
248f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      const syncer::SyncProtocolError& sync_error);
249f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
2505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Handle a migration request.
2515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void HandleMigrationRequestedOnFrontendLoop(const syncer::ModelTypeSet types);
2525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
253f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Checks if |passphrase| can be used to decrypt the cryptographer's pending
254f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // keys that were cached during NotifyPassphraseRequired. Returns true if
255f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // decryption was successful. Returns false otherwise. Must be called with a
256f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // non-empty pending keys cache.
257f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  bool CheckPassphraseAgainstCachedPendingKeys(
258f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      const std::string& passphrase) const;
259f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
260f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Invoked when a passphrase is required to decrypt a set of Nigori keys,
261f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // or for encrypting. |reason| denotes why the passphrase was required.
262f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // |pending_keys| is a copy of the cryptographer's pending keys, that are
263f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // cached by the frontend. If there are no pending keys, or if the passphrase
264f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // required reason is REASON_ENCRYPTION, an empty EncryptedData object is
265f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // passed.
266f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  void NotifyPassphraseRequired(syncer::PassphraseRequiredReason reason,
267f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                sync_pb::EncryptedData pending_keys);
268f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
269f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Invoked when the passphrase provided by the user has been accepted.
270f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  void NotifyPassphraseAccepted();
271f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
272f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Invoked when the set of encrypted types or the encrypt
273f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // everything flag changes.
274f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  void NotifyEncryptedTypesChanged(
275f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      syncer::ModelTypeSet encrypted_types,
276f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      bool encrypt_everything);
277f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
278f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Invoked when sync finishes encrypting new datatypes.
279f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  void NotifyEncryptionComplete();
280f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
281f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Invoked when the passphrase state has changed. Caches the passphrase state
282f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // for later use on the UI thread.
283f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // If |type| is FROZEN_IMPLICIT_PASSPHRASE or CUSTOM_PASSPHRASE,
284f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // |explicit_passphrase_time| is the time at which that passphrase was set
285f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // (if available).
286f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  void HandlePassphraseTypeChangedOnFrontendLoop(
287f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      syncer::PassphraseType type,
288f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      base::Time explicit_passphrase_time);
289f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
290f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  void HandleStopSyncingPermanentlyOnFrontendLoop();
291f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
292f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Dispatched to from OnConnectionStatusChange to handle updating
293f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // frontend UI components.
294f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  void HandleConnectionStatusChangeOnFrontendLoop(
295f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      syncer::ConnectionStatus status);
296f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
297f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // NotificationObserver implementation.
298f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual void Observe(
299f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    int type,
300f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const content::NotificationSource& source,
301f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const content::NotificationDetails& details) OVERRIDE;
302f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
303f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // InvalidationHandler implementation.
304f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual void OnInvalidatorStateChange(
305f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      syncer::InvalidatorState state) OVERRIDE;
306f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual void OnIncomingInvalidation(
307f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      const syncer::ObjectIdInvalidationMap& invalidation_map) OVERRIDE;
308a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual std::string GetOwnerName() const OVERRIDE;
309f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
310f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  content::NotificationRegistrar notification_registrar_;
311f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
312f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // A reference to the MessageLoop used to construct |this|, so we know how
313f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // to safely talk back to the SyncFrontend.
314f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  base::MessageLoop* const frontend_loop_;
315f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
316f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  Profile* const profile_;
317f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
318f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Name used for debugging (set from profile_->GetDebugName()).
319f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  const std::string name_;
320f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
321f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Our core, which communicates directly to the syncapi. Use refptr instead
322f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // of WeakHandle because |core_| is created on UI loop but released on
323f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // sync loop.
324f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  scoped_refptr<SyncBackendHostCore> core_;
325f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
326c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // A handle referencing the main interface for non-blocking sync types.
327c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  scoped_ptr<syncer::SyncCoreProxy> sync_core_proxy_;
328c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
329f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  bool initialized_;
330f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
33123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  const base::WeakPtr<sync_driver::SyncPrefs> sync_prefs_;
332f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
333f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  ExtensionsActivityMonitor extensions_activity_monitor_;
334f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
335f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  scoped_ptr<SyncBackendRegistrar> registrar_;
336f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
337f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // The frontend which we serve (and are owned by).
338f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  SyncFrontend* frontend_;
339f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
340f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // We cache the cryptographer's pending keys whenever NotifyPassphraseRequired
341f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // is called. This way, before the UI calls SetDecryptionPassphrase on the
342f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // syncer, it can avoid the overhead of an asynchronous decryption call and
343f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // give the user immediate feedback about the passphrase entered by first
344f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // trying to decrypt the cached pending keys on the UI thread. Note that
345f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // SetDecryptionPassphrase can still fail after the cached pending keys are
346f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // successfully decrypted if the pending keys have changed since the time they
347f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // were cached.
348f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  sync_pb::EncryptedData cached_pending_keys_;
349f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
350f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // The state of the passphrase required to decrypt the bag of encryption keys
351f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // in the nigori node. Updated whenever a new nigori node arrives or the user
352f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // manually changes their passphrase state. Cached so we can synchronously
353f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // check it from the UI thread.
354f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  syncer::PassphraseType cached_passphrase_type_;
355f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
356f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // If an explicit passphrase is in use, the time at which the passphrase was
357f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // first set (if available).
358f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  base::Time cached_explicit_passphrase_time_;
359f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
360f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // UI-thread cache of the last SyncSessionSnapshot received from syncapi.
361f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  syncer::sessions::SyncSessionSnapshot last_snapshot_;
362f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
363f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  invalidation::InvalidationService* invalidator_;
364f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  bool invalidation_handler_registered_;
365f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
366f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  base::WeakPtrFactory<SyncBackendHostImpl> weak_ptr_factory_;
367f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
368f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(SyncBackendHostImpl);
369f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)};
370f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
371f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}  // namespace browser_sync
372f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
373f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#endif  // CHROME_BROWSER_SYNC_GLUE_SYNC_BACKEND_HOST_IMPL_H_
374f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
375