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