| OLD | NEW |
| (Empty) |
| 1 // Copyright 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 COMPONENTS_SYNC_DRIVER_GLUE_SYNC_BACKEND_REGISTRAR_H_ | |
| 6 #define COMPONENTS_SYNC_DRIVER_GLUE_SYNC_BACKEND_REGISTRAR_H_ | |
| 7 | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include <map> | |
| 11 #include <memory> | |
| 12 #include <string> | |
| 13 #include <vector> | |
| 14 | |
| 15 #include "base/callback.h" | |
| 16 #include "base/macros.h" | |
| 17 #include "base/memory/ref_counted.h" | |
| 18 #include "base/synchronization/lock.h" | |
| 19 #include "base/threading/thread_checker.h" | |
| 20 #include "components/sync/base/model_type.h" | |
| 21 #include "components/sync/engine/model_safe_worker.h" | |
| 22 #include "components/sync/engine/sync_manager.h" | |
| 23 | |
| 24 namespace syncer { | |
| 25 | |
| 26 class ChangeProcessor; | |
| 27 struct UserShare; | |
| 28 | |
| 29 // A class that keep track of the workers, change processors, and | |
| 30 // routing info for the enabled sync types, and also routes change | |
| 31 // events to the right processors. | |
| 32 class SyncBackendRegistrar : public SyncManager::ChangeDelegate { | |
| 33 public: | |
| 34 using ModelSafeWorkerFactory = | |
| 35 base::Callback<scoped_refptr<ModelSafeWorker>(ModelSafeGroup)>; | |
| 36 | |
| 37 // |name| is used for debugging. Must be created on the UI thread. | |
| 38 SyncBackendRegistrar(const std::string& name, | |
| 39 ModelSafeWorkerFactory worker_factory); | |
| 40 | |
| 41 // A SyncBackendRegistrar is owned by a SyncBackendHostImpl. It is destroyed | |
| 42 // by SyncBackendHostImpl::Shutdown() which performs the following operations | |
| 43 // on the UI thread: | |
| 44 // | |
| 45 // 1) Call SyncBackendRegistrar::RequestWorkerStopOnUIThread(). | |
| 46 // 2) Post a SyncBackendHostCore::DoShutdown() task to the sync thread. This | |
| 47 // task destroys SyncManager which holds a SyncBackendRegistrar pointer. | |
| 48 // 3) Take ownership of the sync thread. | |
| 49 // 4) Post a task to delete the SyncBackendRegistrar on the sync thread. | |
| 50 // When this task runs, there are no remaining pointers to the | |
| 51 // SyncBackendRegistrar. | |
| 52 ~SyncBackendRegistrar() override; | |
| 53 | |
| 54 // Adds |type| to set of non-blocking types. These types are assigned to | |
| 55 // GROUP_NON_BLOCKING model safe group and will be treated differently in | |
| 56 // ModelTypeRegistry. Unlike directory types, non-blocking types always stay | |
| 57 // assigned to GROUP_NON_BLOCKING group. | |
| 58 void RegisterNonBlockingType(ModelType type); | |
| 59 | |
| 60 // Informs the SyncBackendRegistrar of the currently enabled set of types. | |
| 61 // These types will be placed in the passive group. This function should be | |
| 62 // called exactly once during startup. | |
| 63 void SetInitialTypes(ModelTypeSet initial_types); | |
| 64 | |
| 65 // Informs SyncBackendRegistrar about non-blocking type loaded from local | |
| 66 // storage. Initial sync was already performed for this type, therefore its | |
| 67 // data shouldn't be downloaded as part of configuration. | |
| 68 void AddRestoredNonBlockingType(ModelType type); | |
| 69 | |
| 70 // Returns whether or not we are currently syncing encryption keys. | |
| 71 // Must be called on the UI thread. | |
| 72 bool IsNigoriEnabled() const; | |
| 73 | |
| 74 // Removes all types in |types_to_remove| from the routing info and | |
| 75 // adds all the types in |types_to_add| to the routing info that are | |
| 76 // not already there (initially put in the passive group). | |
| 77 // |types_to_remove| and |types_to_add| must be disjoint. Returns | |
| 78 // the set of newly-added types. Must be called on the UI thread. | |
| 79 ModelTypeSet ConfigureDataTypes(ModelTypeSet types_to_add, | |
| 80 ModelTypeSet types_to_remove); | |
| 81 | |
| 82 // Returns the set of enabled types as of the last configuration. Note that | |
| 83 // this might be different from the current types in the routing info due | |
| 84 // to DeactiveDataType being called separately from ConfigureDataTypes. | |
| 85 ModelTypeSet GetLastConfiguredTypes() const; | |
| 86 | |
| 87 // Must be called from the UI thread. (See destructor comment.) | |
| 88 void RequestWorkerStopOnUIThread(); | |
| 89 | |
| 90 // Activates the given data type (which should belong to the given | |
| 91 // group) and starts the given change processor. Must be called | |
| 92 // from |group|'s native thread. | |
| 93 void ActivateDataType(ModelType type, | |
| 94 ModelSafeGroup group, | |
| 95 ChangeProcessor* change_processor, | |
| 96 UserShare* user_share); | |
| 97 | |
| 98 // Deactivates the given type if necessary. Must be called from the | |
| 99 // UI thread and not |type|'s native thread. Yes, this is | |
| 100 // surprising: see http://crbug.com/92804. | |
| 101 void DeactivateDataType(ModelType type); | |
| 102 | |
| 103 // Returns true only between calls to ActivateDataType(type, ...) | |
| 104 // and DeactivateDataType(type). Used only by tests. | |
| 105 bool IsTypeActivatedForTest(ModelType type) const; | |
| 106 | |
| 107 // SyncManager::ChangeDelegate implementation. May be called from | |
| 108 // any thread. | |
| 109 void OnChangesApplied(ModelType model_type, | |
| 110 int64_t model_version, | |
| 111 const BaseTransaction* trans, | |
| 112 const ImmutableChangeRecordList& changes) override; | |
| 113 void OnChangesComplete(ModelType model_type) override; | |
| 114 | |
| 115 void GetWorkers(std::vector<scoped_refptr<ModelSafeWorker>>* out); | |
| 116 void GetModelSafeRoutingInfo(ModelSafeRoutingInfo* out); | |
| 117 | |
| 118 private: | |
| 119 // Add a worker for |group| to the worker map if one is successfully created | |
| 120 // by |worker_factory|. | |
| 121 void MaybeAddWorker(ModelSafeWorkerFactory worker_factory, | |
| 122 ModelSafeGroup group); | |
| 123 | |
| 124 // Returns the change processor for the given model, or null if none | |
| 125 // exists. Must be called from |group|'s native thread. | |
| 126 ChangeProcessor* GetProcessor(ModelType type) const; | |
| 127 | |
| 128 // Must be called with |lock_| held. Simply returns the change | |
| 129 // processor for the given type, if it exists. May be called from | |
| 130 // any thread. | |
| 131 ChangeProcessor* GetProcessorUnsafe(ModelType type) const; | |
| 132 | |
| 133 // Return true if |model_type| lives on the current thread. Must be | |
| 134 // called with |lock_| held. May be called on any thread. | |
| 135 bool IsCurrentThreadSafeForModel(ModelType model_type) const; | |
| 136 | |
| 137 // Returns model safe group that should be assigned to type when it is first | |
| 138 // configured (before activation). Returns GROUP_PASSIVE for directory types | |
| 139 // and GROUP_NON_BLOCKING for non-blocking types. | |
| 140 ModelSafeGroup GetInitialGroupForType(ModelType type) const; | |
| 141 | |
| 142 // Name used for debugging. | |
| 143 const std::string name_; | |
| 144 | |
| 145 // Checker for the UI thread (where this object is constructed). | |
| 146 base::ThreadChecker ui_thread_checker_; | |
| 147 | |
| 148 // Protects all variables below. | |
| 149 mutable base::Lock lock_; | |
| 150 | |
| 151 // Workers created by this SyncBackendRegistrar. | |
| 152 std::map<ModelSafeGroup, scoped_refptr<ModelSafeWorker>> workers_; | |
| 153 | |
| 154 // The change processors that handle the different data types. | |
| 155 std::map<ModelType, ChangeProcessor*> processors_; | |
| 156 | |
| 157 // Maps ModelType to ModelSafeGroup. | |
| 158 ModelSafeRoutingInfo routing_info_; | |
| 159 | |
| 160 // The types that were enabled as of the last configuration. Updated on each | |
| 161 // call to ConfigureDataTypes as well as SetInitialTypes. | |
| 162 ModelTypeSet last_configured_types_; | |
| 163 | |
| 164 // Set of types with non-blocking implementation (as opposed to directory | |
| 165 // based). | |
| 166 ModelTypeSet non_blocking_types_; | |
| 167 | |
| 168 DISALLOW_COPY_AND_ASSIGN(SyncBackendRegistrar); | |
| 169 }; | |
| 170 | |
| 171 } // namespace syncer | |
| 172 | |
| 173 #endif // COMPONENTS_SYNC_DRIVER_GLUE_SYNC_BACKEND_REGISTRAR_H_ | |
| OLD | NEW |