Chromium Code Reviews| 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 // If the metadata is not yet loaded, CheckForUpdates does nothing. | |
| 331 if (!loaded_ && !IsRefreshing()) | |
| 332 return; | |
| 333 | |
| 334 // For each CheckForUpdates() request, always refresh the changestamp info. | |
| 335 about_resource_loader_->UpdateAboutResource( | |
| 336 base::Bind(&ChangeListLoader::OnAboutResourceUpdated, | |
| 337 weak_ptr_factory_.GetWeakPtr())); | |
| 338 | |
| 330 if (IsRefreshing()) { | 339 if (IsRefreshing()) { |
| 331 // There is in-flight loading. So keep the callback here, and check for | 340 // There is in-flight loading. So keep the callback here, and check for |
| 332 // updates when the in-flight loading is completed. | 341 // updates when the in-flight loading is completed. |
| 333 pending_update_check_callback_ = callback; | 342 pending_update_check_callback_ = callback; |
| 334 return; | 343 return; |
| 335 } | 344 } |
| 336 | 345 |
| 337 if (loaded_) { | 346 if (loaded_) { |
|
hashimoto
2014/08/06 05:55:43
nit: Can't we replace this "if" with DCHECK?
kinaba
2014/08/06 06:16:54
Done.
| |
| 338 // We only start to check for updates iff the load is done. | 347 // We only start to check for updates iff the load is done. |
| 339 // I.e., we ignore checking updates if not loaded to avoid starting the | 348 // I.e., we ignore checking updates if not loaded to avoid starting the |
| 340 // load without user's explicit interaction (such as opening Drive). | 349 // load without user's explicit interaction (such as opening Drive). |
| 341 logger_->Log(logging::LOG_INFO, "Checking for updates"); | 350 logger_->Log(logging::LOG_INFO, "Checking for updates"); |
| 342 Load(callback); | 351 Load(callback); |
| 343 } | 352 } |
| 344 } | 353 } |
| 345 | 354 |
| 346 void ChangeListLoader::LoadIfNeeded(const FileOperationCallback& callback) { | 355 void ChangeListLoader::LoadIfNeeded(const FileOperationCallback& callback) { |
| 347 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 356 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 348 DCHECK(!callback.is_null()); | 357 DCHECK(!callback.is_null()); |
| 349 | 358 |
| 350 // If the metadata is not yet loaded, start loading. | 359 // If the metadata is not yet loaded, start loading. |
| 351 if (!loaded_) | 360 if (!loaded_ && !IsRefreshing()) { |
| 361 about_resource_loader_->UpdateAboutResource( | |
|
hashimoto
2014/08/06 05:55:43
GetAboutResource() requests the resource if not av
kinaba
2014/08/06 06:16:54
Good point. Done.
| |
| 362 base::Bind(&ChangeListLoader::OnAboutResourceUpdated, | |
| 363 weak_ptr_factory_.GetWeakPtr())); | |
| 352 Load(callback); | 364 Load(callback); |
| 365 } | |
| 353 } | 366 } |
| 354 | 367 |
| 355 void ChangeListLoader::Load(const FileOperationCallback& callback) { | 368 void ChangeListLoader::Load(const FileOperationCallback& callback) { |
| 356 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 369 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 357 DCHECK(!callback.is_null()); | 370 DCHECK(!callback.is_null()); |
| 358 | 371 |
| 359 // Check if this is the first time this ChangeListLoader do loading. | 372 // Check if this is the first time this ChangeListLoader do loading. |
| 360 // Note: IsRefreshing() depends on pending_load_callback_ so check in advance. | 373 // Note: IsRefreshing() depends on pending_load_callback_ so check in advance. |
| 361 const bool is_initial_load = (!loaded_ && !IsRefreshing()); | 374 const bool is_initial_load = (!loaded_ && !IsRefreshing()); |
| 362 | 375 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 395 if (is_initial_load && *local_changestamp > 0) { | 408 if (is_initial_load && *local_changestamp > 0) { |
| 396 // The local data is usable. Flush callbacks to tell loading was successful. | 409 // The local data is usable. Flush callbacks to tell loading was successful. |
| 397 OnChangeListLoadComplete(FILE_ERROR_OK); | 410 OnChangeListLoadComplete(FILE_ERROR_OK); |
| 398 | 411 |
| 399 // Continues to load from server in background. | 412 // Continues to load from server in background. |
| 400 // Put dummy callbacks to indicate that fetching is still continuing. | 413 // Put dummy callbacks to indicate that fetching is still continuing. |
| 401 pending_load_callback_.push_back( | 414 pending_load_callback_.push_back( |
| 402 base::Bind(&util::EmptyFileOperationCallback)); | 415 base::Bind(&util::EmptyFileOperationCallback)); |
| 403 } | 416 } |
| 404 | 417 |
| 405 about_resource_loader_->UpdateAboutResource( | 418 about_resource_loader_->GetAboutResource( |
| 406 base::Bind(&ChangeListLoader::LoadAfterGetAboutResource, | 419 base::Bind(&ChangeListLoader::LoadAfterGetAboutResource, |
| 407 weak_ptr_factory_.GetWeakPtr(), | 420 weak_ptr_factory_.GetWeakPtr(), |
| 408 *local_changestamp)); | 421 *local_changestamp)); |
| 409 } | 422 } |
| 410 | 423 |
| 411 void ChangeListLoader::LoadAfterGetAboutResource( | 424 void ChangeListLoader::LoadAfterGetAboutResource( |
| 412 int64 local_changestamp, | 425 int64 local_changestamp, |
| 413 google_apis::GDataErrorCode status, | 426 google_apis::GDataErrorCode status, |
| 414 scoped_ptr<google_apis::AboutResource> about_resource) { | 427 scoped_ptr<google_apis::AboutResource> about_resource) { |
| 415 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 428 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 456 } | 469 } |
| 457 pending_load_callback_.clear(); | 470 pending_load_callback_.clear(); |
| 458 | 471 |
| 459 // If there is pending update check, try to load the change from the server | 472 // 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. | 473 // again, because there may exist an update during the completed loading. |
| 461 if (!pending_update_check_callback_.is_null()) { | 474 if (!pending_update_check_callback_.is_null()) { |
| 462 Load(base::ResetAndReturn(&pending_update_check_callback_)); | 475 Load(base::ResetAndReturn(&pending_update_check_callback_)); |
| 463 } | 476 } |
| 464 } | 477 } |
| 465 | 478 |
| 479 void ChangeListLoader::OnAboutResourceUpdated( | |
| 480 google_apis::GDataErrorCode error, | |
| 481 scoped_ptr<google_apis::AboutResource> resource) { | |
| 482 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 483 | |
| 484 if (drive::GDataToFileError(error) != drive::FILE_ERROR_OK) { | |
| 485 logger_->Log(logging::LOG_ERROR, | |
| 486 "Failed to update the about resource: %s", | |
| 487 google_apis::GDataErrorCodeToString(error).c_str()); | |
| 488 return; | |
| 489 } | |
| 490 logger_->Log(logging::LOG_INFO, | |
| 491 "About resource updated to: %s", | |
| 492 base::Int64ToString(resource->largest_change_id()).c_str()); | |
| 493 } | |
| 494 | |
| 466 void ChangeListLoader::LoadChangeListFromServer(int64 start_changestamp) { | 495 void ChangeListLoader::LoadChangeListFromServer(int64 start_changestamp) { |
| 467 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 496 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 468 DCHECK(!change_feed_fetcher_); | 497 DCHECK(!change_feed_fetcher_); |
| 469 DCHECK(about_resource_loader_->cached_about_resource()); | 498 DCHECK(about_resource_loader_->cached_about_resource()); |
| 470 | 499 |
| 471 bool is_delta_update = start_changestamp != 0; | 500 bool is_delta_update = start_changestamp != 0; |
| 472 | 501 |
| 473 // Set up feed fetcher. | 502 // Set up feed fetcher. |
| 474 if (is_delta_update) { | 503 if (is_delta_update) { |
| 475 change_feed_fetcher_.reset( | 504 change_feed_fetcher_.reset( |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 550 | 579 |
| 551 OnChangeListLoadComplete(error); | 580 OnChangeListLoadComplete(error); |
| 552 | 581 |
| 553 FOR_EACH_OBSERVER(ChangeListLoaderObserver, | 582 FOR_EACH_OBSERVER(ChangeListLoaderObserver, |
| 554 observers_, | 583 observers_, |
| 555 OnLoadFromServerComplete()); | 584 OnLoadFromServerComplete()); |
| 556 } | 585 } |
| 557 | 586 |
| 558 } // namespace internal | 587 } // namespace internal |
| 559 } // namespace drive | 588 } // namespace drive |
| OLD | NEW |