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.
5 #ifndef CHROME_BROWSER_SYNC_GLUE_SHARED_CHANGE_PROCESSOR_H_
6 #define CHROME_BROWSER_SYNC_GLUE_SHARED_CHANGE_PROCESSOR_H_
8 #include "base/location.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/weak_ptr.h"
11 #include "base/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"
20 class ProfileSyncComponentsFactory
;
21 class ProfileSyncService
;
25 class SyncableService
;
27 typedef std::vector
<syncer::SyncData
> SyncDataList
;
30 namespace browser_sync
{
32 class GenericChangeProcessor
;
33 class DataTypeErrorHandler
;
35 // A ref-counted wrapper around a GenericChangeProcessor for use with datatypes
36 // that don't live on the UI thread.
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.
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.
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).
51 // We use virtual methods so that we can use mock's in testing.
52 class SharedChangeProcessor
53 : public base::RefCountedThreadSafe
<SharedChangeProcessor
> {
55 // Create an uninitialized SharedChangeProcessor (to be later connected).
56 SharedChangeProcessor();
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
);
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();
80 // GenericChangeProcessor stubs (with disconnect support).
81 // Should only be called on the same thread the datatype resides.
82 virtual syncer::SyncError
GetSyncData(
83 syncer::SyncDataList
* current_sync_data
);
84 virtual int GetSyncCount();
85 virtual syncer::SyncError
ProcessSyncChanges(
86 const tracked_objects::Location
& from_here
,
87 const syncer::SyncChangeList
& change_list
);
88 virtual bool SyncModelHasUserCreatedNodes(bool* has_nodes
);
89 virtual bool CryptoReadyIfNecessary();
91 // Register |generic_change_processor_| as the change processor for the
92 // current type on |model_safe_group|.
93 // Does nothing if |disconnected_| is true.
94 virtual void ActivateDataType(syncer::ModelSafeGroup model_safe_group
);
96 virtual syncer::SyncError
CreateAndUploadError(
97 const tracked_objects::Location
& location
,
98 const std::string
& message
);
101 friend class base::RefCountedThreadSafe
<SharedChangeProcessor
>;
102 virtual ~SharedChangeProcessor();
105 // Monitor lock for this object. All methods that interact with the change
106 // processor must aquire this lock and check whether we're disconnected or
107 // not. Once disconnected, all attempted changes to or loads from the change
108 // processor return errors. This enables us to shut down the syncer without
109 // having to wait for possibly non-UI thread datatypes to complete work.
110 mutable base::Lock monitor_lock_
;
113 // The sync datatype we were last connected to.
114 syncer::ModelType type_
;
116 // The ProfileSyncService we're currently connected to.
117 ProfileSyncService
* sync_service_
;
119 // The loop that all methods except the constructor, destructor, and
120 // Disconnect() should be called on. Set in Connect().
121 scoped_refptr
<base::MessageLoopProxy
> backend_loop_
;
123 // Used only on |backend_loop_|.
124 GenericChangeProcessor
* generic_change_processor_
;
126 DataTypeErrorHandler
* error_handler_
;
128 DISALLOW_COPY_AND_ASSIGN(SharedChangeProcessor
);
131 } // namespace browser_sync
133 #endif // CHROME_BROWSER_SYNC_GLUE_SHARED_CHANGE_PROCESSOR_H_