OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_file_system/drive_backend/sync_worker.h" | 5 #include "chrome/browser/sync_file_system/drive_backend/sync_worker.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 (*status)[app_id] = SyncWorker::APP_STATUS_DISABLED; | 78 (*status)[app_id] = SyncWorker::APP_STATUS_DISABLED; |
79 else | 79 else |
80 (*status)[app_id] = SyncWorker::APP_STATUS_ENABLED; | 80 (*status)[app_id] = SyncWorker::APP_STATUS_ENABLED; |
81 } | 81 } |
82 | 82 |
83 callback.Run(); | 83 callback.Run(); |
84 } | 84 } |
85 | 85 |
86 } // namespace | 86 } // namespace |
87 | 87 |
88 scoped_ptr<SyncWorker> SyncWorker::CreateOnWorker( | 88 SyncWorker::SyncWorker( |
89 const base::FilePath& base_dir, | 89 const base::FilePath& base_dir, |
90 Observer* observer, | |
91 const base::WeakPtr<ExtensionServiceInterface>& extension_service, | 90 const base::WeakPtr<ExtensionServiceInterface>& extension_service, |
92 scoped_ptr<SyncEngineContext> sync_engine_context, | 91 scoped_ptr<SyncEngineContext> sync_engine_context, |
93 leveldb::Env* env_override) { | 92 leveldb::Env* env_override) |
94 scoped_ptr<SyncWorker> sync_worker( | 93 : base_dir_(base_dir), |
95 new SyncWorker(base_dir, | 94 env_override_(env_override), |
96 extension_service, | 95 service_state_(REMOTE_SERVICE_TEMPORARY_UNAVAILABLE), |
97 sync_engine_context.Pass(), | 96 should_check_conflict_(true), |
98 env_override)); | 97 should_check_remote_change_(true), |
99 sync_worker->AddObserver(observer); | 98 listing_remote_changes_(false), |
100 sync_worker->Initialize(); | 99 sync_enabled_(false), |
101 | 100 default_conflict_resolution_policy_( |
102 return sync_worker.Pass(); | 101 CONFLICT_RESOLUTION_POLICY_LAST_WRITE_WIN), |
103 } | 102 network_available_(false), |
| 103 extension_service_(extension_service), |
| 104 context_(sync_engine_context.Pass()), |
| 105 weak_ptr_factory_(this) {} |
104 | 106 |
105 SyncWorker::~SyncWorker() {} | 107 SyncWorker::~SyncWorker() {} |
106 | 108 |
107 void SyncWorker::Initialize() { | 109 void SyncWorker::Initialize() { |
108 DCHECK(!task_manager_); | 110 DCHECK(!task_manager_); |
109 | 111 |
110 task_manager_.reset(new SyncTaskManager( | 112 task_manager_.reset(new SyncTaskManager( |
111 weak_ptr_factory_.GetWeakPtr(), 0 /* maximum_background_task */)); | 113 weak_ptr_factory_.GetWeakPtr(), 0 /* maximum_background_task */)); |
112 task_manager_->Initialize(SYNC_STATUS_OK); | 114 task_manager_->Initialize(SYNC_STATUS_OK); |
113 | 115 |
(...skipping 12 matching lines...) Expand all Loading... |
126 PostInitializeTask(); | 128 PostInitializeTask(); |
127 | 129 |
128 scoped_ptr<RegisterAppTask> task( | 130 scoped_ptr<RegisterAppTask> task( |
129 new RegisterAppTask(context_.get(), origin.host())); | 131 new RegisterAppTask(context_.get(), origin.host())); |
130 if (task->CanFinishImmediately()) { | 132 if (task->CanFinishImmediately()) { |
131 context_->GetUITaskRunner()->PostTask( | 133 context_->GetUITaskRunner()->PostTask( |
132 FROM_HERE, base::Bind(callback, SYNC_STATUS_OK)); | 134 FROM_HERE, base::Bind(callback, SYNC_STATUS_OK)); |
133 return; | 135 return; |
134 } | 136 } |
135 | 137 |
136 // TODO(peria): Forward |callback| to UI thread. | |
137 task_manager_->ScheduleSyncTask( | 138 task_manager_->ScheduleSyncTask( |
138 FROM_HERE, | 139 FROM_HERE, |
139 task.PassAs<SyncTask>(), | 140 task.PassAs<SyncTask>(), |
140 SyncTaskManager::PRIORITY_HIGH, | 141 SyncTaskManager::PRIORITY_HIGH, |
141 callback); | 142 callback); |
142 } | 143 } |
143 | 144 |
144 void SyncWorker::EnableOrigin( | 145 void SyncWorker::EnableOrigin( |
145 const GURL& origin, | 146 const GURL& origin, |
146 const SyncStatusCallback& callback) { | 147 const SyncStatusCallback& callback) { |
147 // TODO(peria): Forward |callback| to UI thread. | |
148 task_manager_->ScheduleTask( | 148 task_manager_->ScheduleTask( |
149 FROM_HERE, | 149 FROM_HERE, |
150 base::Bind(&SyncWorker::DoEnableApp, | 150 base::Bind(&SyncWorker::DoEnableApp, |
151 weak_ptr_factory_.GetWeakPtr(), | 151 weak_ptr_factory_.GetWeakPtr(), |
152 origin.host()), | 152 origin.host()), |
153 SyncTaskManager::PRIORITY_HIGH, | 153 SyncTaskManager::PRIORITY_HIGH, |
154 callback); | 154 callback); |
155 } | 155 } |
156 | 156 |
157 void SyncWorker::DisableOrigin( | 157 void SyncWorker::DisableOrigin( |
158 const GURL& origin, | 158 const GURL& origin, |
159 const SyncStatusCallback& callback) { | 159 const SyncStatusCallback& callback) { |
160 // TODO(peria): Forward |callback| to UI thread. | |
161 task_manager_->ScheduleTask( | 160 task_manager_->ScheduleTask( |
162 FROM_HERE, | 161 FROM_HERE, |
163 base::Bind(&SyncWorker::DoDisableApp, | 162 base::Bind(&SyncWorker::DoDisableApp, |
164 weak_ptr_factory_.GetWeakPtr(), | 163 weak_ptr_factory_.GetWeakPtr(), |
165 origin.host()), | 164 origin.host()), |
166 SyncTaskManager::PRIORITY_HIGH, | 165 SyncTaskManager::PRIORITY_HIGH, |
167 callback); | 166 callback); |
168 } | 167 } |
169 | 168 |
170 void SyncWorker::UninstallOrigin( | 169 void SyncWorker::UninstallOrigin( |
171 const GURL& origin, | 170 const GURL& origin, |
172 RemoteFileSyncService::UninstallFlag flag, | 171 RemoteFileSyncService::UninstallFlag flag, |
173 const SyncStatusCallback& callback) { | 172 const SyncStatusCallback& callback) { |
174 // TODO(peria): Forward |callback| to UI thread. | |
175 task_manager_->ScheduleSyncTask( | 173 task_manager_->ScheduleSyncTask( |
176 FROM_HERE, | 174 FROM_HERE, |
177 scoped_ptr<SyncTask>( | 175 scoped_ptr<SyncTask>( |
178 new UninstallAppTask(context_.get(), origin.host(), flag)), | 176 new UninstallAppTask(context_.get(), origin.host(), flag)), |
179 SyncTaskManager::PRIORITY_HIGH, | 177 SyncTaskManager::PRIORITY_HIGH, |
180 callback); | 178 callback); |
181 } | 179 } |
182 | 180 |
183 void SyncWorker::ProcessRemoteChange( | 181 void SyncWorker::ProcessRemoteChange( |
184 const SyncFileCallback& callback) { | 182 const SyncFileCallback& callback) { |
185 RemoteToLocalSyncer* syncer = new RemoteToLocalSyncer(context_.get()); | 183 RemoteToLocalSyncer* syncer = new RemoteToLocalSyncer(context_.get()); |
186 task_manager_->ScheduleSyncTask( | 184 task_manager_->ScheduleSyncTask( |
187 FROM_HERE, | 185 FROM_HERE, |
188 scoped_ptr<SyncTask>(syncer), | 186 scoped_ptr<SyncTask>(syncer), |
189 SyncTaskManager::PRIORITY_MED, | 187 SyncTaskManager::PRIORITY_MED, |
190 base::Bind(&SyncWorker::DidProcessRemoteChange, | 188 base::Bind(&SyncWorker::DidProcessRemoteChange, |
191 weak_ptr_factory_.GetWeakPtr(), | 189 weak_ptr_factory_.GetWeakPtr(), |
192 syncer, callback)); | 190 syncer, |
| 191 callback)); |
193 } | 192 } |
194 | 193 |
195 void SyncWorker::SetRemoteChangeProcessor( | 194 void SyncWorker::SetRemoteChangeProcessor( |
196 RemoteChangeProcessorOnWorker* remote_change_processor_on_worker) { | 195 RemoteChangeProcessorOnWorker* remote_change_processor_on_worker) { |
197 context_->SetRemoteChangeProcessor(remote_change_processor_on_worker); | 196 context_->SetRemoteChangeProcessor(remote_change_processor_on_worker); |
198 } | 197 } |
199 | 198 |
200 RemoteServiceState SyncWorker::GetCurrentState() const { | 199 RemoteServiceState SyncWorker::GetCurrentState() const { |
201 if (!sync_enabled_) | 200 if (!sync_enabled_) |
202 return REMOTE_SERVICE_DISABLED; | 201 return REMOTE_SERVICE_DISABLED; |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 const fileapi::FileSystemURL& url, | 283 const fileapi::FileSystemURL& url, |
285 const SyncStatusCallback& callback) { | 284 const SyncStatusCallback& callback) { |
286 LocalToRemoteSyncer* syncer = new LocalToRemoteSyncer( | 285 LocalToRemoteSyncer* syncer = new LocalToRemoteSyncer( |
287 context_.get(), local_metadata, local_change, local_path, url); | 286 context_.get(), local_metadata, local_change, local_path, url); |
288 task_manager_->ScheduleSyncTask( | 287 task_manager_->ScheduleSyncTask( |
289 FROM_HERE, | 288 FROM_HERE, |
290 scoped_ptr<SyncTask>(syncer), | 289 scoped_ptr<SyncTask>(syncer), |
291 SyncTaskManager::PRIORITY_MED, | 290 SyncTaskManager::PRIORITY_MED, |
292 base::Bind(&SyncWorker::DidApplyLocalChange, | 291 base::Bind(&SyncWorker::DidApplyLocalChange, |
293 weak_ptr_factory_.GetWeakPtr(), | 292 weak_ptr_factory_.GetWeakPtr(), |
294 syncer, callback)); | 293 syncer, |
| 294 callback)); |
295 } | 295 } |
296 | 296 |
297 void SyncWorker::MaybeScheduleNextTask() { | 297 void SyncWorker::MaybeScheduleNextTask() { |
298 if (GetCurrentState() == REMOTE_SERVICE_DISABLED) | 298 if (GetCurrentState() == REMOTE_SERVICE_DISABLED) |
299 return; | 299 return; |
300 | 300 |
301 // TODO(tzik): Notify observer of OnRemoteChangeQueueUpdated. | 301 // TODO(tzik): Notify observer of OnRemoteChangeQueueUpdated. |
302 // TODO(tzik): Add an interface to get the number of dirty trackers to | 302 // TODO(tzik): Add an interface to get the number of dirty trackers to |
303 // MetadataDatabase. | 303 // MetadataDatabase. |
304 | 304 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
374 } | 374 } |
375 | 375 |
376 SyncTaskManager* SyncWorker::GetSyncTaskManager() { | 376 SyncTaskManager* SyncWorker::GetSyncTaskManager() { |
377 return task_manager_.get(); | 377 return task_manager_.get(); |
378 } | 378 } |
379 | 379 |
380 void SyncWorker::AddObserver(Observer* observer) { | 380 void SyncWorker::AddObserver(Observer* observer) { |
381 observers_.AddObserver(observer); | 381 observers_.AddObserver(observer); |
382 } | 382 } |
383 | 383 |
384 SyncWorker::SyncWorker( | |
385 const base::FilePath& base_dir, | |
386 const base::WeakPtr<ExtensionServiceInterface>& extension_service, | |
387 scoped_ptr<SyncEngineContext> sync_engine_context, | |
388 leveldb::Env* env_override) | |
389 : base_dir_(base_dir), | |
390 env_override_(env_override), | |
391 service_state_(REMOTE_SERVICE_TEMPORARY_UNAVAILABLE), | |
392 should_check_conflict_(true), | |
393 should_check_remote_change_(true), | |
394 listing_remote_changes_(false), | |
395 sync_enabled_(false), | |
396 default_conflict_resolution_policy_( | |
397 CONFLICT_RESOLUTION_POLICY_LAST_WRITE_WIN), | |
398 network_available_(false), | |
399 extension_service_(extension_service), | |
400 context_(sync_engine_context.Pass()), | |
401 weak_ptr_factory_(this) {} | |
402 | |
403 void SyncWorker::DoDisableApp(const std::string& app_id, | 384 void SyncWorker::DoDisableApp(const std::string& app_id, |
404 const SyncStatusCallback& callback) { | 385 const SyncStatusCallback& callback) { |
405 if (GetMetadataDatabase()) { | 386 if (GetMetadataDatabase()) { |
406 GetMetadataDatabase()->DisableApp(app_id, callback); | 387 GetMetadataDatabase()->DisableApp(app_id, callback); |
407 } else { | 388 } else { |
408 context_->GetUITaskRunner()->PostTask( | 389 context_->GetUITaskRunner()->PostTask( |
409 FROM_HERE, base::Bind(callback, SYNC_STATUS_OK)); | 390 FROM_HERE, base::Bind(callback, SYNC_STATUS_OK)); |
410 } | 391 } |
411 } | 392 } |
412 | 393 |
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
705 "Service state changed: %d->%d: %s", | 686 "Service state changed: %d->%d: %s", |
706 old_state, GetCurrentState(), description.c_str()); | 687 old_state, GetCurrentState(), description.c_str()); |
707 | 688 |
708 FOR_EACH_OBSERVER( | 689 FOR_EACH_OBSERVER( |
709 Observer, observers_, | 690 Observer, observers_, |
710 UpdateServiceState(GetCurrentState(), description)); | 691 UpdateServiceState(GetCurrentState(), description)); |
711 } | 692 } |
712 | 693 |
713 } // namespace drive_backend | 694 } // namespace drive_backend |
714 } // namespace sync_file_system | 695 } // namespace sync_file_system |
OLD | NEW |