- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / sync / glue / shared_change_processor.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_SHARED_CHANGE_PROCESSOR_H_
6 #define CHROME_BROWSER_SYNC_GLUE_SHARED_CHANGE_PROCESSOR_H_
7
8 #include "base/location.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/weak_ptr.h"
11 #include "base/message_loop/message_loop_proxy.h"
12 #include "base/synchronization/lock.h"
13 #include "chrome/browser/sync/glue/data_type_error_handler.h"
14 #include "sync/api/sync_change_processor.h"
15 #include "sync/api/sync_error.h"
16 #include "sync/api/sync_error_factory.h"
17 #include "sync/api/sync_merge_result.h"
18 #include "sync/internal_api/public/engine/model_safe_worker.h"
19
20 class ProfileSyncComponentsFactory;
21 class ProfileSyncService;
22
23 namespace syncer {
24 class SyncData;
25 class SyncableService;
26
27 typedef std::vector<syncer::SyncData> SyncDataList;
28 }  // namespace syncer
29
30 namespace browser_sync {
31
32 class GenericChangeProcessor;
33 class DataTypeErrorHandler;
34
35 // A ref-counted wrapper around a GenericChangeProcessor for use with datatypes
36 // that don't live on the UI thread.
37 //
38 // We need to make it refcounted as the ownership transfer from the
39 // DataTypeController is dependent on threading, and hence racy. The
40 // SharedChangeProcessor should be created on the UI thread, but should only be
41 // connected and used on the same thread as the datatype it interacts with.
42 //
43 // The only thread-safe method is Disconnect, which will disconnect from the
44 // generic change processor, letting us shut down the syncer/datatype without
45 // waiting for non-UI threads.
46 //
47 // Note: since we control the work being done while holding the lock, we ensure
48 // no I/O or other intensive work is done while blocking the UI thread (all
49 // the work is in-memory sync interactions).
50 //
51 // We use virtual methods so that we can use mock's in testing.
52 class SharedChangeProcessor
53     : public base::RefCountedThreadSafe<SharedChangeProcessor> {
54  public:
55   // Create an uninitialized SharedChangeProcessor (to be later connected).
56   SharedChangeProcessor();
57
58   // Connect to the Syncer and prepare to handle changes for |type|. Will
59   // create and store a new GenericChangeProcessor and return a weak pointer to
60   // the syncer::SyncableService associated with |type|.
61   // Note: If this SharedChangeProcessor has been disconnected, or the
62   // syncer::SyncableService was not alive, will return a null weak pointer.
63   virtual base::WeakPtr<syncer::SyncableService> Connect(
64     ProfileSyncComponentsFactory* sync_factory,
65     ProfileSyncService* sync_service,
66     DataTypeErrorHandler* error_handler,
67     syncer::ModelType type,
68     const base::WeakPtr<syncer::SyncMergeResult>& merge_result);
69
70   // Disconnects from the generic change processor. May be called from any
71   // thread. After this, all attempts to interact with the change processor by
72   // |local_service_| are dropped and return errors. The syncer will be safe to
73   // shut down from the point of view of this datatype.
74   // Note: Once disconnected, you cannot reconnect without creating a new
75   // SharedChangeProcessor.
76   // Returns: true if we were previously succesfully connected, false if we were
77   // already disconnected.
78   virtual bool Disconnect();
79
80   // GenericChangeProcessor stubs (with disconnect support).
81   // Should only be called on the same thread the datatype resides.
82   virtual int GetSyncCount();
83   virtual syncer::SyncError ProcessSyncChanges(
84       const tracked_objects::Location& from_here,
85       const syncer::SyncChangeList& change_list);
86   virtual syncer::SyncDataList GetAllSyncData(syncer::ModelType type) const;
87   virtual syncer::SyncError GetAllSyncDataReturnError(
88       syncer::ModelType type,
89       syncer::SyncDataList* data) const;
90   virtual bool SyncModelHasUserCreatedNodes(bool* has_nodes);
91   virtual bool CryptoReadyIfNecessary();
92
93   // Register |generic_change_processor_| as the change processor for the
94   // current type on |model_safe_group|.
95   // Does nothing if |disconnected_| is true.
96   virtual void ActivateDataType(syncer::ModelSafeGroup model_safe_group);
97
98   virtual syncer::SyncError CreateAndUploadError(
99       const tracked_objects::Location& location,
100       const std::string& message);
101
102  protected:
103   friend class base::RefCountedThreadSafe<SharedChangeProcessor>;
104   virtual ~SharedChangeProcessor();
105
106  private:
107   // Monitor lock for this object. All methods that interact with the change
108   // processor must aquire this lock and check whether we're disconnected or
109   // not. Once disconnected, all attempted changes to or loads from the change
110   // processor return errors. This enables us to shut down the syncer without
111   // having to wait for possibly non-UI thread datatypes to complete work.
112   mutable base::Lock monitor_lock_;
113   bool disconnected_;
114
115   // The sync datatype we were last connected to.
116   syncer::ModelType type_;
117
118   // The ProfileSyncService we're currently connected to.
119   ProfileSyncService* sync_service_;
120
121   // The loop that all methods except the constructor, destructor, and
122   // Disconnect() should be called on.  Set in Connect().
123   scoped_refptr<base::MessageLoopProxy> backend_loop_;
124
125   // Used only on |backend_loop_|.
126   GenericChangeProcessor* generic_change_processor_;
127
128   DataTypeErrorHandler* error_handler_;
129
130   DISALLOW_COPY_AND_ASSIGN(SharedChangeProcessor);
131 };
132
133 }  // namespace browser_sync
134
135 #endif  // CHROME_BROWSER_SYNC_GLUE_SHARED_CHANGE_PROCESSOR_H_