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/change_list_loader.h" | 5 #include "chrome/browser/chromeos/drive/change_list_loader.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/callback_helpers.h" | 10 #include "base/callback_helpers.h" |
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 | 320 |
321 void ChangeListLoader::RemoveObserver(ChangeListLoaderObserver* observer) { | 321 void ChangeListLoader::RemoveObserver(ChangeListLoaderObserver* observer) { |
322 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 322 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
323 observers_.RemoveObserver(observer); | 323 observers_.RemoveObserver(observer); |
324 } | 324 } |
325 | 325 |
326 void ChangeListLoader::CheckForUpdates(const FileOperationCallback& callback) { | 326 void ChangeListLoader::CheckForUpdates(const FileOperationCallback& callback) { |
327 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 327 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
328 DCHECK(!callback.is_null()); | 328 DCHECK(!callback.is_null()); |
329 | 329 |
| 330 // We only start to check for updates iff the load is done. |
| 331 // I.e., we ignore checking updates if not loaded to avoid starting the |
| 332 // load without user's explicit interaction (such as opening Drive). |
| 333 if (!loaded_ && !IsRefreshing()) |
| 334 return; |
| 335 |
| 336 // For each CheckForUpdates() request, always refresh the changestamp info. |
| 337 about_resource_loader_->UpdateAboutResource( |
| 338 base::Bind(&ChangeListLoader::OnAboutResourceUpdated, |
| 339 weak_ptr_factory_.GetWeakPtr())); |
| 340 |
330 if (IsRefreshing()) { | 341 if (IsRefreshing()) { |
331 // There is in-flight loading. So keep the callback here, and check for | 342 // There is in-flight loading. So keep the callback here, and check for |
332 // updates when the in-flight loading is completed. | 343 // updates when the in-flight loading is completed. |
333 pending_update_check_callback_ = callback; | 344 pending_update_check_callback_ = callback; |
334 return; | 345 return; |
335 } | 346 } |
336 | 347 |
337 if (loaded_) { | 348 DCHECK(loaded_); |
338 // We only start to check for updates iff the load is done. | 349 logger_->Log(logging::LOG_INFO, "Checking for updates"); |
339 // I.e., we ignore checking updates if not loaded to avoid starting the | 350 Load(callback); |
340 // load without user's explicit interaction (such as opening Drive). | |
341 logger_->Log(logging::LOG_INFO, "Checking for updates"); | |
342 Load(callback); | |
343 } | |
344 } | 351 } |
345 | 352 |
346 void ChangeListLoader::LoadIfNeeded(const FileOperationCallback& callback) { | 353 void ChangeListLoader::LoadIfNeeded(const FileOperationCallback& callback) { |
347 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 354 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
348 DCHECK(!callback.is_null()); | 355 DCHECK(!callback.is_null()); |
349 | 356 |
350 // If the metadata is not yet loaded, start loading. | 357 // If the metadata is not yet loaded, start loading. |
351 if (!loaded_) | 358 if (!loaded_ && !IsRefreshing()) |
352 Load(callback); | 359 Load(callback); |
353 } | 360 } |
354 | 361 |
355 void ChangeListLoader::Load(const FileOperationCallback& callback) { | 362 void ChangeListLoader::Load(const FileOperationCallback& callback) { |
356 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 363 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
357 DCHECK(!callback.is_null()); | 364 DCHECK(!callback.is_null()); |
358 | 365 |
359 // Check if this is the first time this ChangeListLoader do loading. | 366 // Check if this is the first time this ChangeListLoader do loading. |
360 // Note: IsRefreshing() depends on pending_load_callback_ so check in advance. | 367 // Note: IsRefreshing() depends on pending_load_callback_ so check in advance. |
361 const bool is_initial_load = (!loaded_ && !IsRefreshing()); | 368 const bool is_initial_load = (!loaded_ && !IsRefreshing()); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
395 if (is_initial_load && *local_changestamp > 0) { | 402 if (is_initial_load && *local_changestamp > 0) { |
396 // The local data is usable. Flush callbacks to tell loading was successful. | 403 // The local data is usable. Flush callbacks to tell loading was successful. |
397 OnChangeListLoadComplete(FILE_ERROR_OK); | 404 OnChangeListLoadComplete(FILE_ERROR_OK); |
398 | 405 |
399 // Continues to load from server in background. | 406 // Continues to load from server in background. |
400 // Put dummy callbacks to indicate that fetching is still continuing. | 407 // Put dummy callbacks to indicate that fetching is still continuing. |
401 pending_load_callback_.push_back( | 408 pending_load_callback_.push_back( |
402 base::Bind(&util::EmptyFileOperationCallback)); | 409 base::Bind(&util::EmptyFileOperationCallback)); |
403 } | 410 } |
404 | 411 |
405 about_resource_loader_->UpdateAboutResource( | 412 about_resource_loader_->GetAboutResource( |
406 base::Bind(&ChangeListLoader::LoadAfterGetAboutResource, | 413 base::Bind(&ChangeListLoader::LoadAfterGetAboutResource, |
407 weak_ptr_factory_.GetWeakPtr(), | 414 weak_ptr_factory_.GetWeakPtr(), |
408 *local_changestamp)); | 415 *local_changestamp)); |
409 } | 416 } |
410 | 417 |
411 void ChangeListLoader::LoadAfterGetAboutResource( | 418 void ChangeListLoader::LoadAfterGetAboutResource( |
412 int64 local_changestamp, | 419 int64 local_changestamp, |
413 google_apis::GDataErrorCode status, | 420 google_apis::GDataErrorCode status, |
414 scoped_ptr<google_apis::AboutResource> about_resource) { | 421 scoped_ptr<google_apis::AboutResource> about_resource) { |
415 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 422 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
456 } | 463 } |
457 pending_load_callback_.clear(); | 464 pending_load_callback_.clear(); |
458 | 465 |
459 // If there is pending update check, try to load the change from the server | 466 // If there is pending update check, try to load the change from the server |
460 // again, because there may exist an update during the completed loading. | 467 // again, because there may exist an update during the completed loading. |
461 if (!pending_update_check_callback_.is_null()) { | 468 if (!pending_update_check_callback_.is_null()) { |
462 Load(base::ResetAndReturn(&pending_update_check_callback_)); | 469 Load(base::ResetAndReturn(&pending_update_check_callback_)); |
463 } | 470 } |
464 } | 471 } |
465 | 472 |
| 473 void ChangeListLoader::OnAboutResourceUpdated( |
| 474 google_apis::GDataErrorCode error, |
| 475 scoped_ptr<google_apis::AboutResource> resource) { |
| 476 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 477 |
| 478 if (drive::GDataToFileError(error) != drive::FILE_ERROR_OK) { |
| 479 logger_->Log(logging::LOG_ERROR, |
| 480 "Failed to update the about resource: %s", |
| 481 google_apis::GDataErrorCodeToString(error).c_str()); |
| 482 return; |
| 483 } |
| 484 logger_->Log(logging::LOG_INFO, |
| 485 "About resource updated to: %s", |
| 486 base::Int64ToString(resource->largest_change_id()).c_str()); |
| 487 } |
| 488 |
466 void ChangeListLoader::LoadChangeListFromServer(int64 start_changestamp) { | 489 void ChangeListLoader::LoadChangeListFromServer(int64 start_changestamp) { |
467 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 490 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
468 DCHECK(!change_feed_fetcher_); | 491 DCHECK(!change_feed_fetcher_); |
469 DCHECK(about_resource_loader_->cached_about_resource()); | 492 DCHECK(about_resource_loader_->cached_about_resource()); |
470 | 493 |
471 bool is_delta_update = start_changestamp != 0; | 494 bool is_delta_update = start_changestamp != 0; |
472 | 495 |
473 // Set up feed fetcher. | 496 // Set up feed fetcher. |
474 if (is_delta_update) { | 497 if (is_delta_update) { |
475 change_feed_fetcher_.reset( | 498 change_feed_fetcher_.reset( |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
550 | 573 |
551 OnChangeListLoadComplete(error); | 574 OnChangeListLoadComplete(error); |
552 | 575 |
553 FOR_EACH_OBSERVER(ChangeListLoaderObserver, | 576 FOR_EACH_OBSERVER(ChangeListLoaderObserver, |
554 observers_, | 577 observers_, |
555 OnLoadFromServerComplete()); | 578 OnLoadFromServerComplete()); |
556 } | 579 } |
557 | 580 |
558 } // namespace internal | 581 } // namespace internal |
559 } // namespace drive | 582 } // namespace drive |
OLD | NEW |