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 #include "chrome/browser/sync/glue/sync_backend_registrar.h" | 5 #include "chrome/browser/sync/glue/sync_backend_registrar.h" |
6 | 6 |
7 #include <algorithm> | |
8 #include <cstddef> | 7 #include <cstddef> |
9 | 8 |
10 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
11 #include "base/logging.h" | 10 #include "base/logging.h" |
12 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
13 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/browser/sync/engine/passive_model_worker.h" | 13 #include "chrome/browser/sync/engine/passive_model_worker.h" |
15 #include "chrome/browser/sync/glue/browser_thread_model_worker.h" | 14 #include "chrome/browser/sync/glue/browser_thread_model_worker.h" |
16 #include "chrome/browser/sync/glue/change_processor.h" | 15 #include "chrome/browser/sync/glue/change_processor.h" |
17 #include "chrome/browser/sync/glue/history_model_worker.h" | 16 #include "chrome/browser/sync/glue/history_model_worker.h" |
(...skipping 27 matching lines...) Expand all Loading... |
45 return true; | 44 return true; |
46 case MODEL_SAFE_GROUP_COUNT: | 45 case MODEL_SAFE_GROUP_COUNT: |
47 default: | 46 default: |
48 return false; | 47 return false; |
49 } | 48 } |
50 } | 49 } |
51 | 50 |
52 } // namespace | 51 } // namespace |
53 | 52 |
54 SyncBackendRegistrar::SyncBackendRegistrar( | 53 SyncBackendRegistrar::SyncBackendRegistrar( |
55 const syncable::ModelTypeSet& initial_types, | 54 syncable::ModelEnumSet initial_types, |
56 const std::string& name, Profile* profile, | 55 const std::string& name, Profile* profile, |
57 MessageLoop* sync_loop) : | 56 MessageLoop* sync_loop) : |
58 name_(name), | 57 name_(name), |
59 profile_(profile), | 58 profile_(profile), |
60 sync_loop_(sync_loop), | 59 sync_loop_(sync_loop), |
61 ui_worker_(new UIModelWorker()), | 60 ui_worker_(new UIModelWorker()), |
62 stopped_on_ui_thread_(false) { | 61 stopped_on_ui_thread_(false) { |
63 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 62 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
64 CHECK(profile_); | 63 CHECK(profile_); |
65 DCHECK(sync_loop_); | 64 DCHECK(sync_loop_); |
66 workers_[GROUP_DB] = new DatabaseModelWorker(); | 65 workers_[GROUP_DB] = new DatabaseModelWorker(); |
67 workers_[GROUP_FILE] = new FileModelWorker(); | 66 workers_[GROUP_FILE] = new FileModelWorker(); |
68 workers_[GROUP_UI] = ui_worker_; | 67 workers_[GROUP_UI] = ui_worker_; |
69 workers_[GROUP_PASSIVE] = new PassiveModelWorker(sync_loop_); | 68 workers_[GROUP_PASSIVE] = new PassiveModelWorker(sync_loop_); |
70 | 69 |
71 // Any datatypes that we want the syncer to pull down must be in the | 70 // Any datatypes that we want the syncer to pull down must be in the |
72 // routing_info map. We set them to group passive, meaning that | 71 // routing_info map. We set them to group passive, meaning that |
73 // updates will be applied to sync, but not dispatched to the native | 72 // updates will be applied to sync, but not dispatched to the native |
74 // models. | 73 // models. |
75 for (syncable::ModelTypeSet::const_iterator it = initial_types.begin(); | 74 for (syncable::ModelEnumSet::Iterator it = initial_types.First(); |
76 it != initial_types.end(); ++it) { | 75 it.Good(); it.Inc()) { |
77 routing_info_[*it] = GROUP_PASSIVE; | 76 routing_info_[it.Get()] = GROUP_PASSIVE; |
78 } | 77 } |
79 | 78 |
80 HistoryService* history_service = profile->GetHistoryService( | 79 HistoryService* history_service = profile->GetHistoryService( |
81 Profile::IMPLICIT_ACCESS); | 80 Profile::IMPLICIT_ACCESS); |
82 if (history_service) { | 81 if (history_service) { |
83 workers_[GROUP_HISTORY] = new HistoryModelWorker(history_service); | 82 workers_[GROUP_HISTORY] = new HistoryModelWorker(history_service); |
84 } else { | 83 } else { |
85 LOG_IF(WARNING, initial_types.count(syncable::TYPED_URLS) > 0) | 84 LOG_IF(WARNING, initial_types.Has(syncable::TYPED_URLS)) |
86 << "History store disabled, cannot sync Omnibox History"; | 85 << "History store disabled, cannot sync Omnibox History"; |
87 routing_info_.erase(syncable::TYPED_URLS); | 86 routing_info_.erase(syncable::TYPED_URLS); |
88 } | 87 } |
89 | 88 |
90 PasswordStore* password_store = | 89 PasswordStore* password_store = |
91 profile->GetPasswordStore(Profile::IMPLICIT_ACCESS); | 90 profile->GetPasswordStore(Profile::IMPLICIT_ACCESS); |
92 if (password_store) { | 91 if (password_store) { |
93 workers_[GROUP_PASSWORD] = new PasswordModelWorker(password_store); | 92 workers_[GROUP_PASSWORD] = new PasswordModelWorker(password_store); |
94 } else { | 93 } else { |
95 LOG_IF(WARNING, initial_types.count(syncable::PASSWORDS) > 0) | 94 LOG_IF(WARNING, initial_types.Has(syncable::PASSWORDS)) |
96 << "Password store not initialized, cannot sync passwords"; | 95 << "Password store not initialized, cannot sync passwords"; |
97 routing_info_.erase(syncable::PASSWORDS); | 96 routing_info_.erase(syncable::PASSWORDS); |
98 } | 97 } |
99 } | 98 } |
100 | 99 |
101 SyncBackendRegistrar::~SyncBackendRegistrar() { | 100 SyncBackendRegistrar::~SyncBackendRegistrar() { |
102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 101 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
103 DCHECK(stopped_on_ui_thread_); | 102 DCHECK(stopped_on_ui_thread_); |
104 } | 103 } |
105 | 104 |
106 bool SyncBackendRegistrar::IsNigoriEnabled() const { | 105 bool SyncBackendRegistrar::IsNigoriEnabled() const { |
107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 106 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
108 base::AutoLock lock(lock_); | 107 base::AutoLock lock(lock_); |
109 return routing_info_.find(syncable::NIGORI) != routing_info_.end(); | 108 return routing_info_.find(syncable::NIGORI) != routing_info_.end(); |
110 } | 109 } |
111 | 110 |
112 syncable::ModelTypeSet SyncBackendRegistrar::ConfigureDataTypes( | 111 syncable::ModelEnumSet SyncBackendRegistrar::ConfigureDataTypes( |
113 const syncable::ModelTypeSet& types_to_add, | 112 syncable::ModelEnumSet types_to_add, |
114 const syncable::ModelTypeSet& types_to_remove) { | 113 syncable::ModelEnumSet types_to_remove) { |
115 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 114 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
116 if (DCHECK_IS_ON()) { | 115 DCHECK(Intersection(types_to_add, types_to_remove).Empty()); |
117 syncable::ModelTypeSet intersection; | 116 syncable::ModelEnumSet filtered_types_to_add = types_to_add; |
118 std::set_intersection(types_to_add.begin(), types_to_add.end(), | |
119 types_to_remove.begin(), types_to_remove.end(), | |
120 std::inserter(intersection, intersection.end())); | |
121 DCHECK(intersection.empty()); | |
122 } | |
123 syncable::ModelTypeSet filtered_types_to_add = types_to_add; | |
124 if (workers_.count(GROUP_HISTORY) == 0) { | 117 if (workers_.count(GROUP_HISTORY) == 0) { |
125 LOG(WARNING) << "No history worker -- removing TYPED_URLS"; | 118 LOG(WARNING) << "No history worker -- removing TYPED_URLS"; |
126 filtered_types_to_add.erase(syncable::TYPED_URLS); | 119 filtered_types_to_add.Remove(syncable::TYPED_URLS); |
127 } | 120 } |
128 if (workers_.count(GROUP_PASSWORD) == 0) { | 121 if (workers_.count(GROUP_PASSWORD) == 0) { |
129 LOG(WARNING) << "No password worker -- removing PASSWORDS"; | 122 LOG(WARNING) << "No password worker -- removing PASSWORDS"; |
130 filtered_types_to_add.erase(syncable::PASSWORDS); | 123 filtered_types_to_add.Remove(syncable::PASSWORDS); |
131 } | 124 } |
132 | 125 |
133 base::AutoLock lock(lock_); | 126 base::AutoLock lock(lock_); |
134 syncable::ModelTypeSet newly_added_types; | 127 syncable::ModelEnumSet newly_added_types; |
135 for (syncable::ModelTypeSet::const_iterator it = | 128 for (syncable::ModelEnumSet::Iterator it = |
136 filtered_types_to_add.begin(); | 129 filtered_types_to_add.First(); |
137 it != filtered_types_to_add.end(); ++it) { | 130 it.Good(); it.Inc()) { |
138 // Add a newly specified data type as GROUP_PASSIVE into the | 131 // Add a newly specified data type as GROUP_PASSIVE into the |
139 // routing_info, if it does not already exist. | 132 // routing_info, if it does not already exist. |
140 if (routing_info_.count(*it) == 0) { | 133 if (routing_info_.count(it.Get()) == 0) { |
141 routing_info_[*it] = GROUP_PASSIVE; | 134 routing_info_[it.Get()] = GROUP_PASSIVE; |
142 newly_added_types.insert(*it); | 135 newly_added_types.Put(it.Get()); |
143 } | 136 } |
144 } | 137 } |
145 for (syncable::ModelTypeSet::const_iterator it = types_to_remove.begin(); | 138 for (syncable::ModelEnumSet::Iterator it = types_to_remove.First(); |
146 it != types_to_remove.end(); ++it) { | 139 it.Good(); it.Inc()) { |
147 routing_info_.erase(*it); | 140 routing_info_.erase(it.Get()); |
148 } | 141 } |
149 | 142 |
150 // TODO(akalin): Use SVLOG/SLOG if we add any more logging. | 143 // TODO(akalin): Use SVLOG/SLOG if we add any more logging. |
151 DVLOG(1) << name_ << ": Adding types " | 144 DVLOG(1) << name_ << ": Adding types " |
152 << syncable::ModelTypeSetToString(types_to_add) | 145 << syncable::ModelEnumSetToString(types_to_add) |
153 << " (with newly-added types " | 146 << " (with newly-added types " |
154 << syncable::ModelTypeSetToString(newly_added_types) | 147 << syncable::ModelEnumSetToString(newly_added_types) |
155 << ") and removing types " | 148 << ") and removing types " |
156 << syncable::ModelTypeSetToString(types_to_remove) | 149 << syncable::ModelEnumSetToString(types_to_remove) |
157 << " to get new routing info " | 150 << " to get new routing info " |
158 << ModelSafeRoutingInfoToString(routing_info_); | 151 << ModelSafeRoutingInfoToString(routing_info_); |
159 | 152 |
160 return newly_added_types; | 153 return newly_added_types; |
161 } | 154 } |
162 | 155 |
163 void SyncBackendRegistrar::StopOnUIThread() { | 156 void SyncBackendRegistrar::StopOnUIThread() { |
164 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 157 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
165 DCHECK(!stopped_on_ui_thread_); | 158 DCHECK(!stopped_on_ui_thread_); |
166 ui_worker_->Stop(); | 159 ui_worker_->Stop(); |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 return it->second; | 278 return it->second; |
286 } | 279 } |
287 | 280 |
288 bool SyncBackendRegistrar::IsCurrentThreadSafeForModel( | 281 bool SyncBackendRegistrar::IsCurrentThreadSafeForModel( |
289 syncable::ModelType model_type) const { | 282 syncable::ModelType model_type) const { |
290 lock_.AssertAcquired(); | 283 lock_.AssertAcquired(); |
291 return IsOnThreadForGroup(GetGroupForModelType(model_type, routing_info_)); | 284 return IsOnThreadForGroup(GetGroupForModelType(model_type, routing_info_)); |
292 } | 285 } |
293 | 286 |
294 } // namespace browser_sync | 287 } // namespace browser_sync |
OLD | NEW |