Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_SYNC_GLUE_GENERIC_CHANGE_PROCESSOR_H_ | 5 #ifndef CHROME_BROWSER_SYNC_GLUE_GENERIC_CHANGE_PROCESSOR_H_ |
| 6 #define CHROME_BROWSER_SYNC_GLUE_GENERIC_CHANGE_PROCESSOR_H_ | 6 #define CHROME_BROWSER_SYNC_GLUE_GENERIC_CHANGE_PROCESSOR_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/memory/ref_counted.h" | |
| 13 #include "base/synchronization/lock.h" | |
| 12 #include "chrome/browser/sync/api/sync_change_processor.h" | 14 #include "chrome/browser/sync/api/sync_change_processor.h" |
| 13 #include "chrome/browser/sync/glue/change_processor.h" | 15 #include "chrome/browser/sync/glue/change_processor.h" |
| 14 | 16 |
| 15 class SyncData; | 17 class SyncData; |
| 16 class SyncableService; | 18 class SyncableService; |
| 17 | 19 |
| 18 typedef std::vector<SyncData> SyncDataList; | 20 typedef std::vector<SyncData> SyncDataList; |
| 19 | 21 |
| 20 namespace browser_sync { | 22 namespace browser_sync { |
| 21 | 23 |
| 22 // TODO(sync): deprecate all change processors and have them replaced by | 24 // TODO(sync): deprecate all change processors and have them replaced by |
| 23 // instances of this. | 25 // instances of this. |
| 24 // Datatype agnostic change processor. One instance of GenericChangeProcessor | 26 // Datatype agnostic change processor. One instance of GenericChangeProcessor |
| 25 // is created for each datatype and lives on the datatype's thread. It then | 27 // is created for each datatype and lives on the datatype's thread. It then |
| 26 // handles all interaction with the sync api, both translating pushes from the | 28 // handles all interaction with the sync api, both translating pushes from the |
| 27 // local service into transactions and receiving changes from the sync model, | 29 // local service into transactions and receiving changes from the sync model, |
| 28 // which then get converted into SyncChange's and sent to the local service. | 30 // which then get converted into SyncChange's and sent to the local service. |
| 29 class GenericChangeProcessor : public ChangeProcessor, | 31 // |
| 30 public SyncChangeProcessor { | 32 // As a rule, the GenericChangeProcessor is not thread safe. It does support |
| 33 // being created on an arbitrary thread, as long as Connect(..) is called on | |
| 34 // the thread the datatype resides. In addition, Disconnect() may be called | |
| 35 // from any thread. | |
| 36 class GenericChangeProcessor | |
| 37 : public ChangeProcessor, | |
| 38 public SyncChangeProcessor, | |
| 39 public base::RefCountedThreadSafe<GenericChangeProcessor> { | |
|
akalin
2011/10/06 05:33:29
Okay, as you probably expected, I don't like this
Nicolas Zea
2011/10/06 22:10:54
Done.
| |
| 31 public: | 40 public: |
| 32 GenericChangeProcessor(SyncableService* local_service, | 41 // Create an uninitialized change processor (to be later connected). |
| 33 UnrecoverableErrorHandler* error_handler, | 42 explicit GenericChangeProcessor(UnrecoverableErrorHandler* error_handler); |
| 43 // Create a change processor and connect it to the syncer. | |
| 44 GenericChangeProcessor(UnrecoverableErrorHandler* error_handler, | |
| 45 SyncableService* local_service, | |
| 34 sync_api::UserShare* user_share); | 46 sync_api::UserShare* user_share); |
| 35 virtual ~GenericChangeProcessor(); | 47 |
| 48 // Connect to the syncer and the SyncableService specified. Should be called | |
| 49 // on the same thread the datatype resides. | |
| 50 virtual void Connect(SyncableService* local_service, | |
| 51 sync_api::UserShare* user_share); | |
| 52 | |
| 53 // Disconnects from the syncer. May be called from any thread. After this, all | |
| 54 // attempts to interact with the change processor by |local_service_| are | |
| 55 // dropped and return errors. The syncer will be safe to shut down from the | |
| 56 // point of view of this datatype. | |
| 57 // Note: Once disconnected, you cannot reconnect without creating a new | |
| 58 // change processor. | |
| 59 virtual void Disconnect(); | |
| 36 | 60 |
| 37 // ChangeProcessor interface. | 61 // ChangeProcessor interface. |
| 38 // Build and store a list of all changes into |syncer_changes_|. | 62 // Build and store a list of all changes into |syncer_changes_|. |
| 39 virtual void ApplyChangesFromSyncModel( | 63 virtual void ApplyChangesFromSyncModel( |
| 40 const sync_api::BaseTransaction* trans, | 64 const sync_api::BaseTransaction* trans, |
| 41 const sync_api::ImmutableChangeRecordList& changes) OVERRIDE; | 65 const sync_api::ImmutableChangeRecordList& changes) OVERRIDE; |
| 42 // Passes |syncer_changes_|, built in ApplyChangesFromSyncModel, onto | 66 // Passes |syncer_changes_|, built in ApplyChangesFromSyncModel, onto |
| 43 // |local_service_| by way of it's ProcessSyncChanges method. | 67 // |local_service_| by way of it's ProcessSyncChanges method. |
| 44 virtual void CommitChangesFromSyncModel() OVERRIDE; | 68 virtual void CommitChangesFromSyncModel() OVERRIDE; |
| 45 | 69 |
| 46 // SyncChangeProcessor implementation. | 70 // SyncChangeProcessor implementation. |
| 47 virtual SyncError ProcessSyncChanges( | 71 virtual SyncError ProcessSyncChanges( |
| 48 const tracked_objects::Location& from_here, | 72 const tracked_objects::Location& from_here, |
| 49 const SyncChangeList& change_list) OVERRIDE; | 73 const SyncChangeList& change_list) OVERRIDE; |
| 50 | 74 |
| 51 // Fills |current_sync_data| with all the syncer data for the specified type. | 75 // Fills |current_sync_data| with all the syncer data for the specified type. |
| 52 virtual SyncError GetSyncDataForType(syncable::ModelType type, | 76 virtual SyncError GetSyncDataForType(syncable::ModelType type, |
| 53 SyncDataList* current_sync_data); | 77 SyncDataList* current_sync_data); |
| 54 | 78 |
| 55 // Generic versions of AssociatorInterface methods. Called by | 79 // Generic versions of AssociatorInterface methods. Called by |
| 56 // SyncableServiceAdapter. | 80 // SyncableServiceAdapter or the DataTypeController. |
| 57 bool SyncModelHasUserCreatedNodes(syncable::ModelType type, | 81 virtual bool SyncModelHasUserCreatedNodes(syncable::ModelType type, |
| 58 bool* has_nodes); | 82 bool* has_nodes); |
| 59 bool CryptoReadyIfNecessary(syncable::ModelType type); | 83 virtual bool CryptoReadyIfNecessary(syncable::ModelType type); |
| 84 | |
| 60 protected: | 85 protected: |
| 86 friend class base::RefCountedThreadSafe<GenericChangeProcessor>; | |
| 87 | |
| 88 virtual ~GenericChangeProcessor() OVERRIDE; | |
| 89 | |
| 61 // ChangeProcessor interface. | 90 // ChangeProcessor interface. |
| 62 virtual void StartImpl(Profile* profile) OVERRIDE; // Not implemented. | 91 virtual void StartImpl(Profile* profile) OVERRIDE; // Not implemented. |
| 63 virtual void StopImpl() OVERRIDE; // Not implemented. | 92 virtual void StopImpl() OVERRIDE; // Not implemented. |
| 64 virtual sync_api::UserShare* share_handle() OVERRIDE; | 93 virtual sync_api::UserShare* share_handle() OVERRIDE; |
| 94 | |
| 65 private: | 95 private: |
| 66 // The SyncableService this change processor will forward changes on to. | 96 // The SyncableService this change processor will forward changes on to. |
| 67 SyncableService* local_service_; | 97 SyncableService* local_service_; |
| 68 | 98 |
| 69 // The current list of changes received from the syncer. We buffer because | 99 // The current list of changes received from the syncer. We buffer because |
| 70 // we must ensure no syncapi transaction is held when we pass it on to | 100 // we must ensure no syncapi transaction is held when we pass it on to |
| 71 // |local_service_|. | 101 // |local_service_|. |
| 72 // Set in ApplyChangesFromSyncModel, consumed in CommitChangesFromSyncModel. | 102 // Set in ApplyChangesFromSyncModel, consumed in CommitChangesFromSyncModel. |
| 73 SyncChangeList syncer_changes_; | 103 SyncChangeList syncer_changes_; |
| 74 | 104 |
| 75 // Our handle to the sync model. Unlike normal ChangeProcessors, we need to | 105 // Our handle to the sync model. Unlike normal ChangeProcessors, we need to |
| 76 // be able to access the sync model before the change processor begins | 106 // be able to access the sync model before the change processor begins |
| 77 // listening to changes (the local_service_ will be interacting with us | 107 // listening to changes (the local_service_ will be interacting with us |
| 78 // when it starts up). As such we can't wait until Start(_) has been called, | 108 // when it starts up). As such we can't wait until Start(_) has been called, |
| 79 // and have to keep a local pointer to the user_share. | 109 // and have to keep a local pointer to the user_share. |
| 80 sync_api::UserShare* user_share_; | 110 sync_api::UserShare* user_share_; |
| 111 | |
| 112 // Monitor lock for this object. All methods that interact with the Syncer | |
| 113 // must aquire this lock and check whether we're disconnected or not. Once | |
| 114 // disconnected, all attempted changes to or loads from the syncer return | |
| 115 // errors. This enables us to shut down the syncer without having to wait | |
| 116 // for possibly non-UI thread datatypes to complete work. | |
| 117 base::Lock monitor_lock_; | |
| 118 bool disconnected_; | |
| 119 | |
| 120 DISALLOW_COPY_AND_ASSIGN(GenericChangeProcessor); | |
| 81 }; | 121 }; |
| 82 | 122 |
| 83 } // namespace browser_sync | 123 } // namespace browser_sync |
| 84 | 124 |
| 85 #endif // CHROME_BROWSER_SYNC_GLUE_GENERIC_CHANGE_PROCESSOR_H_ | 125 #endif // CHROME_BROWSER_SYNC_GLUE_GENERIC_CHANGE_PROCESSOR_H_ |
| OLD | NEW |