| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_SYNC_BACKEND_REGISTRAR_H_ | 5 #ifndef CHROME_BROWSER_SYNC_GLUE_SYNC_BACKEND_REGISTRAR_H_ |
| 6 #define CHROME_BROWSER_SYNC_GLUE_SYNC_BACKEND_REGISTRAR_H_ | 6 #define CHROME_BROWSER_SYNC_GLUE_SYNC_BACKEND_REGISTRAR_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 | 31 |
| 32 // A class that keep track of the workers, change processors, and | 32 // A class that keep track of the workers, change processors, and |
| 33 // routing info for the enabled sync types, and also routes change | 33 // routing info for the enabled sync types, and also routes change |
| 34 // events to the right processors. | 34 // events to the right processors. |
| 35 class SyncBackendRegistrar : public syncer::SyncManager::ChangeDelegate { | 35 class SyncBackendRegistrar : public syncer::SyncManager::ChangeDelegate { |
| 36 public: | 36 public: |
| 37 // |initial_types| contains the initial set of types to sync | 37 // |initial_types| contains the initial set of types to sync |
| 38 // (initially put in the passive group). |name| is used for | 38 // (initially put in the passive group). |name| is used for |
| 39 // debugging. Does not take ownership of |profile| or |sync_loop|. | 39 // debugging. Does not take ownership of |profile| or |sync_loop|. |
| 40 // Must be created on the UI thread. | 40 // Must be created on the UI thread. |
| 41 SyncBackendRegistrar(syncable::ModelTypeSet initial_types, | 41 SyncBackendRegistrar(syncer::ModelTypeSet initial_types, |
| 42 const std::string& name, | 42 const std::string& name, |
| 43 Profile* profile, | 43 Profile* profile, |
| 44 MessageLoop* sync_loop); | 44 MessageLoop* sync_loop); |
| 45 | 45 |
| 46 // SyncBackendRegistrar must be destroyed as follows: | 46 // SyncBackendRegistrar must be destroyed as follows: |
| 47 // | 47 // |
| 48 // 1) On the sync thread, call OnSyncerShutdownComplete() after | 48 // 1) On the sync thread, call OnSyncerShutdownComplete() after |
| 49 // the syncer is shutdown. | 49 // the syncer is shutdown. |
| 50 // 2) Meanwhile, on the UI thread, call StopOnUIThread(), which | 50 // 2) Meanwhile, on the UI thread, call StopOnUIThread(), which |
| 51 // blocks until OnSyncerShutdownComplete() is called. | 51 // blocks until OnSyncerShutdownComplete() is called. |
| 52 // 3) Destroy the SyncBackendRegistrar. | 52 // 3) Destroy the SyncBackendRegistrar. |
| 53 // | 53 // |
| 54 // This is to handle the complicated shutdown requirements of the | 54 // This is to handle the complicated shutdown requirements of the |
| 55 // UIModelWorker (since the UI thread is both the main thread and a | 55 // UIModelWorker (since the UI thread is both the main thread and a |
| 56 // thread which the syncer pushes changes to). | 56 // thread which the syncer pushes changes to). |
| 57 virtual ~SyncBackendRegistrar(); | 57 virtual ~SyncBackendRegistrar(); |
| 58 | 58 |
| 59 // Returns whether or not we are currently syncing encryption keys. | 59 // Returns whether or not we are currently syncing encryption keys. |
| 60 // Must be called on the UI thread. | 60 // Must be called on the UI thread. |
| 61 bool IsNigoriEnabled() const; | 61 bool IsNigoriEnabled() const; |
| 62 | 62 |
| 63 // Removes all types in |types_to_remove| from the routing info and | 63 // Removes all types in |types_to_remove| from the routing info and |
| 64 // adds all the types in |types_to_add| to the routing info that are | 64 // adds all the types in |types_to_add| to the routing info that are |
| 65 // not already there (initially put in the passive group). | 65 // not already there (initially put in the passive group). |
| 66 // |types_to_remove| and |types_to_add| must be disjoint. Returns | 66 // |types_to_remove| and |types_to_add| must be disjoint. Returns |
| 67 // the set of newly-added types. Must be called on the UI thread. | 67 // the set of newly-added types. Must be called on the UI thread. |
| 68 syncable::ModelTypeSet ConfigureDataTypes( | 68 syncer::ModelTypeSet ConfigureDataTypes( |
| 69 syncable::ModelTypeSet types_to_add, | 69 syncer::ModelTypeSet types_to_add, |
| 70 syncable::ModelTypeSet types_to_remove); | 70 syncer::ModelTypeSet types_to_remove); |
| 71 | 71 |
| 72 // Must be called from the UI thread. (See destructor comment.) | 72 // Must be called from the UI thread. (See destructor comment.) |
| 73 void StopOnUIThread(); | 73 void StopOnUIThread(); |
| 74 | 74 |
| 75 // Must be called from the sync thread. (See destructor comment.) | 75 // Must be called from the sync thread. (See destructor comment.) |
| 76 void OnSyncerShutdownComplete(); | 76 void OnSyncerShutdownComplete(); |
| 77 | 77 |
| 78 // Activates the given data type (which should belong to the given | 78 // Activates the given data type (which should belong to the given |
| 79 // group) and starts the given change processor. Must be called | 79 // group) and starts the given change processor. Must be called |
| 80 // from |group|'s native thread. | 80 // from |group|'s native thread. |
| 81 void ActivateDataType(syncable::ModelType type, | 81 void ActivateDataType(syncer::ModelType type, |
| 82 syncer::ModelSafeGroup group, | 82 syncer::ModelSafeGroup group, |
| 83 ChangeProcessor* change_processor, | 83 ChangeProcessor* change_processor, |
| 84 syncer::UserShare* user_share); | 84 syncer::UserShare* user_share); |
| 85 | 85 |
| 86 // Deactivates the given type if necessary. Must be called from the | 86 // Deactivates the given type if necessary. Must be called from the |
| 87 // UI thread and not |type|'s native thread. Yes, this is | 87 // UI thread and not |type|'s native thread. Yes, this is |
| 88 // surprising: see http://crbug.com/92804. | 88 // surprising: see http://crbug.com/92804. |
| 89 void DeactivateDataType(syncable::ModelType type); | 89 void DeactivateDataType(syncer::ModelType type); |
| 90 | 90 |
| 91 // Returns true only between calls to ActivateDataType(type, ...) | 91 // Returns true only between calls to ActivateDataType(type, ...) |
| 92 // and DeactivateDataType(type). Used only by tests. | 92 // and DeactivateDataType(type). Used only by tests. |
| 93 bool IsTypeActivatedForTest(syncable::ModelType type) const; | 93 bool IsTypeActivatedForTest(syncer::ModelType type) const; |
| 94 | 94 |
| 95 // SyncManager::ChangeDelegate implementation. May be called from | 95 // SyncManager::ChangeDelegate implementation. May be called from |
| 96 // any thread. | 96 // any thread. |
| 97 virtual void OnChangesApplied( | 97 virtual void OnChangesApplied( |
| 98 syncable::ModelType model_type, | 98 syncer::ModelType model_type, |
| 99 const syncer::BaseTransaction* trans, | 99 const syncer::BaseTransaction* trans, |
| 100 const syncer::ImmutableChangeRecordList& changes) OVERRIDE; | 100 const syncer::ImmutableChangeRecordList& changes) OVERRIDE; |
| 101 virtual void OnChangesComplete(syncable::ModelType model_type) OVERRIDE; | 101 virtual void OnChangesComplete(syncer::ModelType model_type) OVERRIDE; |
| 102 | 102 |
| 103 void GetWorkers(std::vector<syncer::ModelSafeWorker*>* out); | 103 void GetWorkers(std::vector<syncer::ModelSafeWorker*>* out); |
| 104 void GetModelSafeRoutingInfo(syncer::ModelSafeRoutingInfo* out); | 104 void GetModelSafeRoutingInfo(syncer::ModelSafeRoutingInfo* out); |
| 105 | 105 |
| 106 private: | 106 private: |
| 107 typedef std::map<syncer::ModelSafeGroup, | 107 typedef std::map<syncer::ModelSafeGroup, |
| 108 scoped_refptr<syncer::ModelSafeWorker> > WorkerMap; | 108 scoped_refptr<syncer::ModelSafeWorker> > WorkerMap; |
| 109 | 109 |
| 110 // Returns the change processor for the given model, or NULL if none | 110 // Returns the change processor for the given model, or NULL if none |
| 111 // exists. Must be called from |group|'s native thread. | 111 // exists. Must be called from |group|'s native thread. |
| 112 ChangeProcessor* GetProcessor(syncable::ModelType type) const; | 112 ChangeProcessor* GetProcessor(syncer::ModelType type) const; |
| 113 | 113 |
| 114 // Must be called with |lock_| held. Simply returns the change | 114 // Must be called with |lock_| held. Simply returns the change |
| 115 // processor for the given type, if it exists. May be called from | 115 // processor for the given type, if it exists. May be called from |
| 116 // any thread. | 116 // any thread. |
| 117 ChangeProcessor* GetProcessorUnsafe(syncable::ModelType type) const; | 117 ChangeProcessor* GetProcessorUnsafe(syncer::ModelType type) const; |
| 118 | 118 |
| 119 // Return true if |model_type| lives on the current thread. Must be | 119 // Return true if |model_type| lives on the current thread. Must be |
| 120 // called with |lock_| held. May be called on any thread. | 120 // called with |lock_| held. May be called on any thread. |
| 121 bool IsCurrentThreadSafeForModel( | 121 bool IsCurrentThreadSafeForModel( |
| 122 syncable::ModelType model_type) const; | 122 syncer::ModelType model_type) const; |
| 123 | 123 |
| 124 // Name used for debugging. | 124 // Name used for debugging. |
| 125 const std::string name_; | 125 const std::string name_; |
| 126 | 126 |
| 127 Profile* const profile_; | 127 Profile* const profile_; |
| 128 | 128 |
| 129 MessageLoop* const sync_loop_; | 129 MessageLoop* const sync_loop_; |
| 130 | 130 |
| 131 const scoped_refptr<UIModelWorker> ui_worker_; | 131 const scoped_refptr<UIModelWorker> ui_worker_; |
| 132 | 132 |
| 133 bool stopped_on_ui_thread_; | 133 bool stopped_on_ui_thread_; |
| 134 | 134 |
| 135 // Protects all variables below. | 135 // Protects all variables below. |
| 136 mutable base::Lock lock_; | 136 mutable base::Lock lock_; |
| 137 | 137 |
| 138 // We maintain ownership of all workers. In some cases, we need to | 138 // We maintain ownership of all workers. In some cases, we need to |
| 139 // ensure shutdown occurs in an expected sequence by Stop()ing | 139 // ensure shutdown occurs in an expected sequence by Stop()ing |
| 140 // certain workers. They are guaranteed to be valid because we only | 140 // certain workers. They are guaranteed to be valid because we only |
| 141 // destroy elements of |workers_| after the syncapi has been | 141 // destroy elements of |workers_| after the syncapi has been |
| 142 // destroyed. Unless a worker is no longer needed because all types | 142 // destroyed. Unless a worker is no longer needed because all types |
| 143 // that get routed to it have been disabled (from syncing). In that | 143 // that get routed to it have been disabled (from syncing). In that |
| 144 // case, we'll destroy on demand *after* routing any dependent types | 144 // case, we'll destroy on demand *after* routing any dependent types |
| 145 // to syncer::GROUP_PASSIVE, so that the syncapi doesn't call into garbage. | 145 // to syncer::GROUP_PASSIVE, so that the syncapi doesn't call into garbage. |
| 146 // If a key is present, it means at least one ModelType that routes | 146 // If a key is present, it means at least one ModelType that routes |
| 147 // to that model safe group is being synced. | 147 // to that model safe group is being synced. |
| 148 WorkerMap workers_; | 148 WorkerMap workers_; |
| 149 syncer::ModelSafeRoutingInfo routing_info_; | 149 syncer::ModelSafeRoutingInfo routing_info_; |
| 150 | 150 |
| 151 // The change processors that handle the different data types. | 151 // The change processors that handle the different data types. |
| 152 std::map<syncable::ModelType, ChangeProcessor*> processors_; | 152 std::map<syncer::ModelType, ChangeProcessor*> processors_; |
| 153 | 153 |
| 154 DISALLOW_COPY_AND_ASSIGN(SyncBackendRegistrar); | 154 DISALLOW_COPY_AND_ASSIGN(SyncBackendRegistrar); |
| 155 }; | 155 }; |
| 156 | 156 |
| 157 } // namespace browser_sync | 157 } // namespace browser_sync |
| 158 | 158 |
| 159 #endif // CHROME_BROWSER_SYNC_GLUE_SYNC_BACKEND_REGISTRAR_H_ | 159 #endif // CHROME_BROWSER_SYNC_GLUE_SYNC_BACKEND_REGISTRAR_H_ |
| OLD | NEW |