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 #include "chrome/browser/chromeos/drive/sync_client.h" | 5 #include "chrome/browser/chromeos/drive/sync_client.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/message_loop/message_loop_proxy.h" | 10 #include "base/message_loop/message_loop_proxy.h" |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 temporary_file_directory)), | 158 temporary_file_directory)), |
159 entry_update_performer_(new EntryUpdatePerformer(blocking_task_runner, | 159 entry_update_performer_(new EntryUpdatePerformer(blocking_task_runner, |
160 delegate, | 160 delegate, |
161 scheduler, | 161 scheduler, |
162 metadata, | 162 metadata, |
163 cache, | 163 cache, |
164 loader_controller)), | 164 loader_controller)), |
165 delay_(base::TimeDelta::FromSeconds(kDelaySeconds)), | 165 delay_(base::TimeDelta::FromSeconds(kDelaySeconds)), |
166 long_delay_(base::TimeDelta::FromSeconds(kLongDelaySeconds)), | 166 long_delay_(base::TimeDelta::FromSeconds(kLongDelaySeconds)), |
167 weak_ptr_factory_(this) { | 167 weak_ptr_factory_(this) { |
168 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 168 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
169 } | 169 } |
170 | 170 |
171 SyncClient::~SyncClient() { | 171 SyncClient::~SyncClient() { |
172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 172 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
173 } | 173 } |
174 | 174 |
175 void SyncClient::StartProcessingBacklog() { | 175 void SyncClient::StartProcessingBacklog() { |
176 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 176 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
177 | 177 |
178 std::vector<std::string>* to_fetch = new std::vector<std::string>; | 178 std::vector<std::string>* to_fetch = new std::vector<std::string>; |
179 std::vector<std::string>* to_update = new std::vector<std::string>; | 179 std::vector<std::string>* to_update = new std::vector<std::string>; |
180 blocking_task_runner_->PostTaskAndReply( | 180 blocking_task_runner_->PostTaskAndReply( |
181 FROM_HERE, | 181 FROM_HERE, |
182 base::Bind(&CollectBacklog, metadata_, to_fetch, to_update), | 182 base::Bind(&CollectBacklog, metadata_, to_fetch, to_update), |
183 base::Bind(&SyncClient::OnGetLocalIdsOfBacklog, | 183 base::Bind(&SyncClient::OnGetLocalIdsOfBacklog, |
184 weak_ptr_factory_.GetWeakPtr(), | 184 weak_ptr_factory_.GetWeakPtr(), |
185 base::Owned(to_fetch), | 185 base::Owned(to_fetch), |
186 base::Owned(to_update))); | 186 base::Owned(to_update))); |
187 } | 187 } |
188 | 188 |
189 void SyncClient::StartCheckingExistingPinnedFiles() { | 189 void SyncClient::StartCheckingExistingPinnedFiles() { |
190 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 190 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
191 | 191 |
192 std::vector<std::string>* local_ids = new std::vector<std::string>; | 192 std::vector<std::string>* local_ids = new std::vector<std::string>; |
193 blocking_task_runner_->PostTaskAndReply( | 193 blocking_task_runner_->PostTaskAndReply( |
194 FROM_HERE, | 194 FROM_HERE, |
195 base::Bind(&CheckExistingPinnedFiles, | 195 base::Bind(&CheckExistingPinnedFiles, |
196 metadata_, | 196 metadata_, |
197 cache_, | 197 cache_, |
198 local_ids), | 198 local_ids), |
199 base::Bind(&SyncClient::AddFetchTasks, | 199 base::Bind(&SyncClient::AddFetchTasks, |
200 weak_ptr_factory_.GetWeakPtr(), | 200 weak_ptr_factory_.GetWeakPtr(), |
201 base::Owned(local_ids))); | 201 base::Owned(local_ids))); |
202 } | 202 } |
203 | 203 |
204 void SyncClient::AddFetchTask(const std::string& local_id) { | 204 void SyncClient::AddFetchTask(const std::string& local_id) { |
205 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 205 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
206 AddFetchTaskInternal(local_id, delay_); | 206 AddFetchTaskInternal(local_id, delay_); |
207 } | 207 } |
208 | 208 |
209 void SyncClient::RemoveFetchTask(const std::string& local_id) { | 209 void SyncClient::RemoveFetchTask(const std::string& local_id) { |
210 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 210 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
211 | 211 |
212 SyncTasks::iterator it = tasks_.find(SyncTasks::key_type(FETCH, local_id)); | 212 SyncTasks::iterator it = tasks_.find(SyncTasks::key_type(FETCH, local_id)); |
213 if (it == tasks_.end()) | 213 if (it == tasks_.end()) |
214 return; | 214 return; |
215 | 215 |
216 SyncTask* task = &it->second; | 216 SyncTask* task = &it->second; |
217 switch (task->state) { | 217 switch (task->state) { |
218 case SUSPENDED: | 218 case SUSPENDED: |
219 case PENDING: | 219 case PENDING: |
220 OnTaskComplete(FETCH, local_id, FILE_ERROR_ABORT); | 220 OnTaskComplete(FETCH, local_id, FILE_ERROR_ABORT); |
221 break; | 221 break; |
222 case RUNNING: | 222 case RUNNING: |
223 if (!task->cancel_closure.is_null()) | 223 if (!task->cancel_closure.is_null()) |
224 task->cancel_closure.Run(); | 224 task->cancel_closure.Run(); |
225 break; | 225 break; |
226 } | 226 } |
227 } | 227 } |
228 | 228 |
229 void SyncClient::AddUpdateTask(const ClientContext& context, | 229 void SyncClient::AddUpdateTask(const ClientContext& context, |
230 const std::string& local_id) { | 230 const std::string& local_id) { |
231 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 231 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
232 AddUpdateTaskInternal(context, local_id, delay_); | 232 AddUpdateTaskInternal(context, local_id, delay_); |
233 } | 233 } |
234 | 234 |
235 bool SyncClient:: WaitForUpdateTaskToComplete( | 235 bool SyncClient:: WaitForUpdateTaskToComplete( |
236 const std::string& local_id, | 236 const std::string& local_id, |
237 const FileOperationCallback& callback) { | 237 const FileOperationCallback& callback) { |
238 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 238 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
239 | 239 |
240 SyncTasks::iterator it = tasks_.find(SyncTasks::key_type(UPDATE, local_id)); | 240 SyncTasks::iterator it = tasks_.find(SyncTasks::key_type(UPDATE, local_id)); |
241 if (it == tasks_.end()) | 241 if (it == tasks_.end()) |
242 return false; | 242 return false; |
243 | 243 |
244 SyncTask* task = &it->second; | 244 SyncTask* task = &it->second; |
245 task->waiting_callbacks.push_back(callback); | 245 task->waiting_callbacks.push_back(callback); |
246 return true; | 246 return true; |
247 } | 247 } |
248 | 248 |
249 base::Closure SyncClient::PerformFetchTask(const std::string& local_id, | 249 base::Closure SyncClient::PerformFetchTask(const std::string& local_id, |
250 const ClientContext& context) { | 250 const ClientContext& context) { |
251 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 251 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
252 return download_operation_->EnsureFileDownloadedByLocalId( | 252 return download_operation_->EnsureFileDownloadedByLocalId( |
253 local_id, | 253 local_id, |
254 context, | 254 context, |
255 GetFileContentInitializedCallback(), | 255 GetFileContentInitializedCallback(), |
256 google_apis::GetContentCallback(), | 256 google_apis::GetContentCallback(), |
257 base::Bind(&SyncClient::OnFetchFileComplete, | 257 base::Bind(&SyncClient::OnFetchFileComplete, |
258 weak_ptr_factory_.GetWeakPtr(), | 258 weak_ptr_factory_.GetWeakPtr(), |
259 local_id)); | 259 local_id)); |
260 } | 260 } |
261 | 261 |
262 void SyncClient::AddFetchTaskInternal(const std::string& local_id, | 262 void SyncClient::AddFetchTaskInternal(const std::string& local_id, |
263 const base::TimeDelta& delay) { | 263 const base::TimeDelta& delay) { |
264 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 264 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
265 | 265 |
266 SyncTask task; | 266 SyncTask task; |
267 task.state = PENDING; | 267 task.state = PENDING; |
268 task.context = ClientContext(BACKGROUND); | 268 task.context = ClientContext(BACKGROUND); |
269 task.task = base::Bind(&SyncClient::PerformFetchTask, | 269 task.task = base::Bind(&SyncClient::PerformFetchTask, |
270 base::Unretained(this), | 270 base::Unretained(this), |
271 local_id); | 271 local_id); |
272 AddTask(SyncTasks::key_type(FETCH, local_id), task, delay); | 272 AddTask(SyncTasks::key_type(FETCH, local_id), task, delay); |
273 } | 273 } |
274 | 274 |
275 base::Closure SyncClient::PerformUpdateTask(const std::string& local_id, | 275 base::Closure SyncClient::PerformUpdateTask(const std::string& local_id, |
276 const ClientContext& context) { | 276 const ClientContext& context) { |
277 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 277 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
278 entry_update_performer_->UpdateEntry( | 278 entry_update_performer_->UpdateEntry( |
279 local_id, | 279 local_id, |
280 context, | 280 context, |
281 base::Bind(&SyncClient::OnTaskComplete, | 281 base::Bind(&SyncClient::OnTaskComplete, |
282 weak_ptr_factory_.GetWeakPtr(), | 282 weak_ptr_factory_.GetWeakPtr(), |
283 UPDATE, | 283 UPDATE, |
284 local_id)); | 284 local_id)); |
285 return base::Closure(); | 285 return base::Closure(); |
286 } | 286 } |
287 | 287 |
288 void SyncClient::AddUpdateTaskInternal(const ClientContext& context, | 288 void SyncClient::AddUpdateTaskInternal(const ClientContext& context, |
289 const std::string& local_id, | 289 const std::string& local_id, |
290 const base::TimeDelta& delay) { | 290 const base::TimeDelta& delay) { |
291 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 291 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
292 | 292 |
293 SyncTask task; | 293 SyncTask task; |
294 task.state = PENDING; | 294 task.state = PENDING; |
295 task.context = context; | 295 task.context = context; |
296 task.task = base::Bind(&SyncClient::PerformUpdateTask, | 296 task.task = base::Bind(&SyncClient::PerformUpdateTask, |
297 base::Unretained(this), | 297 base::Unretained(this), |
298 local_id); | 298 local_id); |
299 AddTask(SyncTasks::key_type(UPDATE, local_id), task, delay); | 299 AddTask(SyncTasks::key_type(UPDATE, local_id), task, delay); |
300 } | 300 } |
301 | 301 |
302 void SyncClient::AddTask(const SyncTasks::key_type& key, | 302 void SyncClient::AddTask(const SyncTasks::key_type& key, |
303 const SyncTask& task, | 303 const SyncTask& task, |
304 const base::TimeDelta& delay) { | 304 const base::TimeDelta& delay) { |
305 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 305 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
306 | 306 |
307 SyncTasks::iterator it = tasks_.find(key); | 307 SyncTasks::iterator it = tasks_.find(key); |
308 if (it != tasks_.end()) { | 308 if (it != tasks_.end()) { |
309 switch (it->second.state) { | 309 switch (it->second.state) { |
310 case SUSPENDED: | 310 case SUSPENDED: |
311 // Activate the task. | 311 // Activate the task. |
312 it->second.state = PENDING; | 312 it->second.state = PENDING; |
313 break; | 313 break; |
314 case PENDING: | 314 case PENDING: |
315 // The same task will run, do nothing. | 315 // The same task will run, do nothing. |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
384 } | 384 } |
385 | 385 |
386 // Run the task. | 386 // Run the task. |
387 task->state = RUNNING; | 387 task->state = RUNNING; |
388 task->cancel_closure = task->task.Run(task->context); | 388 task->cancel_closure = task->task.Run(task->context); |
389 } | 389 } |
390 | 390 |
391 void SyncClient::OnGetLocalIdsOfBacklog( | 391 void SyncClient::OnGetLocalIdsOfBacklog( |
392 const std::vector<std::string>* to_fetch, | 392 const std::vector<std::string>* to_fetch, |
393 const std::vector<std::string>* to_update) { | 393 const std::vector<std::string>* to_update) { |
394 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 394 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
395 | 395 |
396 // Give priority to upload tasks over fetch tasks, so that dirty files are | 396 // Give priority to upload tasks over fetch tasks, so that dirty files are |
397 // uploaded as soon as possible. | 397 // uploaded as soon as possible. |
398 for (size_t i = 0; i < to_update->size(); ++i) { | 398 for (size_t i = 0; i < to_update->size(); ++i) { |
399 const std::string& local_id = (*to_update)[i]; | 399 const std::string& local_id = (*to_update)[i]; |
400 DVLOG(1) << "Queuing to update: " << local_id; | 400 DVLOG(1) << "Queuing to update: " << local_id; |
401 AddUpdateTask(ClientContext(BACKGROUND), local_id); | 401 AddUpdateTask(ClientContext(BACKGROUND), local_id); |
402 } | 402 } |
403 | 403 |
404 for (size_t i = 0; i < to_fetch->size(); ++i) { | 404 for (size_t i = 0; i < to_fetch->size(); ++i) { |
405 const std::string& local_id = (*to_fetch)[i]; | 405 const std::string& local_id = (*to_fetch)[i]; |
406 DVLOG(1) << "Queuing to fetch: " << local_id; | 406 DVLOG(1) << "Queuing to fetch: " << local_id; |
407 AddFetchTaskInternal(local_id, delay_); | 407 AddFetchTaskInternal(local_id, delay_); |
408 } | 408 } |
409 } | 409 } |
410 | 410 |
411 void SyncClient::AddFetchTasks(const std::vector<std::string>* local_ids) { | 411 void SyncClient::AddFetchTasks(const std::vector<std::string>* local_ids) { |
412 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 412 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
413 | 413 |
414 for (size_t i = 0; i < local_ids->size(); ++i) | 414 for (size_t i = 0; i < local_ids->size(); ++i) |
415 AddFetchTask((*local_ids)[i]); | 415 AddFetchTask((*local_ids)[i]); |
416 } | 416 } |
417 | 417 |
418 void SyncClient::OnTaskComplete(SyncType type, | 418 void SyncClient::OnTaskComplete(SyncType type, |
419 const std::string& local_id, | 419 const std::string& local_id, |
420 FileError error) { | 420 FileError error) { |
421 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 421 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
422 | 422 |
423 const SyncTasks::key_type key(type, local_id); | 423 const SyncTasks::key_type key(type, local_id); |
424 SyncTasks::iterator it = tasks_.find(key); | 424 SyncTasks::iterator it = tasks_.find(key); |
425 DCHECK(it != tasks_.end()); | 425 DCHECK(it != tasks_.end()); |
426 | 426 |
427 base::TimeDelta retry_delay = base::TimeDelta::FromSeconds(0); | 427 base::TimeDelta retry_delay = base::TimeDelta::FromSeconds(0); |
428 | 428 |
429 switch (error) { | 429 switch (error) { |
430 case FILE_ERROR_OK: | 430 case FILE_ERROR_OK: |
431 DVLOG(1) << "Completed: type = " << type << ", id = " << local_id; | 431 DVLOG(1) << "Completed: type = " << type << ", id = " << local_id; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
471 for (size_t i = 0; i < it->second.dependent_tasks.size(); ++i) | 471 for (size_t i = 0; i < it->second.dependent_tasks.size(); ++i) |
472 StartTask(it->second.dependent_tasks[i]); | 472 StartTask(it->second.dependent_tasks[i]); |
473 tasks_.erase(it); | 473 tasks_.erase(it); |
474 } | 474 } |
475 } | 475 } |
476 | 476 |
477 void SyncClient::OnFetchFileComplete(const std::string& local_id, | 477 void SyncClient::OnFetchFileComplete(const std::string& local_id, |
478 FileError error, | 478 FileError error, |
479 const base::FilePath& local_path, | 479 const base::FilePath& local_path, |
480 scoped_ptr<ResourceEntry> entry) { | 480 scoped_ptr<ResourceEntry> entry) { |
481 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 481 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
482 OnTaskComplete(FETCH, local_id, error); | 482 OnTaskComplete(FETCH, local_id, error); |
483 if (error == FILE_ERROR_ABORT) { | 483 if (error == FILE_ERROR_ABORT) { |
484 // If user cancels download, unpin the file so that we do not sync the file | 484 // If user cancels download, unpin the file so that we do not sync the file |
485 // again. | 485 // again. |
486 base::PostTaskAndReplyWithResult( | 486 base::PostTaskAndReplyWithResult( |
487 blocking_task_runner_.get(), | 487 blocking_task_runner_.get(), |
488 FROM_HERE, | 488 FROM_HERE, |
489 base::Bind(&FileCache::Unpin, base::Unretained(cache_), local_id), | 489 base::Bind(&FileCache::Unpin, base::Unretained(cache_), local_id), |
490 base::Bind(&util::EmptyFileOperationCallback)); | 490 base::Bind(&util::EmptyFileOperationCallback)); |
491 } | 491 } |
492 } | 492 } |
493 | 493 |
494 } // namespace internal | 494 } // namespace internal |
495 } // namespace drive | 495 } // namespace drive |
OLD | NEW |