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/gdata/gdata_wapi_feed_loader.h" | 5 #include "chrome/browser/chromeos/gdata/gdata_wapi_feed_loader.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
| 10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| (...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 333 } | 333 } |
| 334 // If our cache holds the latest state from the server, change the | 334 // If our cache holds the latest state from the server, change the |
| 335 // state to FROM_SERVER. | 335 // state to FROM_SERVER. |
| 336 directory_service_->set_origin( | 336 directory_service_->set_origin( |
| 337 initial_origin == FROM_CACHE ? FROM_SERVER : initial_origin); | 337 initial_origin == FROM_CACHE ? FROM_SERVER : initial_origin); |
| 338 changes_detected = false; | 338 changes_detected = false; |
| 339 } | 339 } |
| 340 | 340 |
| 341 // No changes detected, continue with search as planned. | 341 // No changes detected, continue with search as planned. |
| 342 if (!changes_detected) { | 342 if (!changes_detected) { |
| 343 if (!callback.is_null()) { | 343 if (!callback.is_null()) { |
|
satorux1
2012/08/14 06:02:45
BTW, could you check if it's safe to remove this c
kochi
2012/08/14 07:16:56
Ok, I'll examine these checks in a separate CL.
O
| |
| 344 directory_service_->FindEntryByPathAndRunSync(search_file_path, | 344 directory_service_->FindEntryByPathAndRunSync(search_file_path, |
| 345 callback); | 345 callback); |
| 346 } | 346 } |
| 347 return; | 347 return; |
| 348 } | 348 } |
| 349 | 349 |
| 350 // Load changes from the server. | 350 // Load changes from the server. |
| 351 LoadFromServer(initial_origin, | 351 LoadFromServer(initial_origin, |
| 352 local_changestamp > 0 ? local_changestamp + 1 : 0, | 352 local_changestamp > 0 ? local_changestamp + 1 : 0, |
| 353 account_metadata->largest_changestamp(), | 353 account_metadata->largest_changestamp(), |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 457 const std::string& directory_resource_id, | 457 const std::string& directory_resource_id, |
| 458 const FindEntryCallback& entry_found_callback, | 458 const FindEntryCallback& entry_found_callback, |
| 459 const LoadDocumentFeedCallback& feed_load_callback) { | 459 const LoadDocumentFeedCallback& feed_load_callback) { |
| 460 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 460 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 461 | 461 |
| 462 // |feed_list| will contain the list of all collected feed updates that | 462 // |feed_list| will contain the list of all collected feed updates that |
| 463 // we will receive through calls of DocumentsService::GetDocuments(). | 463 // we will receive through calls of DocumentsService::GetDocuments(). |
| 464 scoped_ptr<std::vector<DocumentFeed*> > feed_list( | 464 scoped_ptr<std::vector<DocumentFeed*> > feed_list( |
| 465 new std::vector<DocumentFeed*>); | 465 new std::vector<DocumentFeed*>); |
| 466 const base::TimeTicks start_time = base::TimeTicks::Now(); | 466 const base::TimeTicks start_time = base::TimeTicks::Now(); |
| 467 | |
| 468 if (gdata::util::IsDriveV2ApiEnabled()) { | |
| 469 documents_service_->GetChangelist( | |
| 470 feed_to_load, | |
| 471 start_changestamp, | |
| 472 base::Bind(&GDataWapiFeedLoader::OnGetChangelist, | |
| 473 weak_ptr_factory_.GetWeakPtr(), | |
| 474 initial_origin, | |
| 475 feed_load_callback, | |
| 476 base::Owned(new GetDocumentsParams( | |
| 477 start_changestamp, | |
| 478 root_feed_changestamp, | |
| 479 feed_list.release(), | |
| 480 should_fetch_multiple_feeds, | |
| 481 search_file_path, | |
| 482 search_query, | |
| 483 directory_resource_id, | |
| 484 entry_found_callback, | |
| 485 NULL)), | |
| 486 start_time)); | |
| 487 return; | |
| 488 } | |
| 489 | |
| 467 documents_service_->GetDocuments( | 490 documents_service_->GetDocuments( |
| 468 feed_to_load, | 491 feed_to_load, |
| 469 start_changestamp, | 492 start_changestamp, |
| 470 search_query, | 493 search_query, |
| 471 directory_resource_id, | 494 directory_resource_id, |
| 472 base::Bind(&GDataWapiFeedLoader::OnGetDocuments, | 495 base::Bind(&GDataWapiFeedLoader::OnGetDocuments, |
| 473 weak_ptr_factory_.GetWeakPtr(), | 496 weak_ptr_factory_.GetWeakPtr(), |
| 474 initial_origin, | 497 initial_origin, |
| 475 feed_load_callback, | 498 feed_load_callback, |
| 476 base::Owned(new GetDocumentsParams(start_changestamp, | 499 base::Owned(new GetDocumentsParams(start_changestamp, |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 547 | 570 |
| 548 return; | 571 return; |
| 549 } | 572 } |
| 550 | 573 |
| 551 GURL next_feed_url; | 574 GURL next_feed_url; |
| 552 scoped_ptr<DocumentFeed> current_feed(DocumentFeed::ExtractAndParse(*data)); | 575 scoped_ptr<DocumentFeed> current_feed(DocumentFeed::ExtractAndParse(*data)); |
| 553 if (!current_feed.get()) { | 576 if (!current_feed.get()) { |
| 554 if (!callback.is_null()) { | 577 if (!callback.is_null()) { |
| 555 callback.Run(params, GDATA_FILE_ERROR_FAILED); | 578 callback.Run(params, GDATA_FILE_ERROR_FAILED); |
| 556 } | 579 } |
| 557 | |
| 558 return; | 580 return; |
| 559 } | 581 } |
| 560 const bool has_next_feed_url = current_feed->GetNextFeedURL(&next_feed_url); | 582 const bool has_next_feed_url = current_feed->GetNextFeedURL(&next_feed_url); |
| 561 | 583 |
| 562 #ifndef NDEBUG | 584 #ifndef NDEBUG |
| 563 // Save initial root feed for analysis. | 585 // Save initial root feed for analysis. |
| 564 std::string file_name = | 586 std::string file_name = |
| 565 base::StringPrintf("DEBUG_feed_%" PRId64 ".json", | 587 base::StringPrintf("DEBUG_feed_%" PRId64 ".json", |
| 566 params->start_changestamp); | 588 params->start_changestamp); |
| 567 util::PostBlockingPoolSequencedTask( | 589 util::PostBlockingPoolSequencedTask( |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 633 FOR_EACH_OBSERVER(Observer, observers_, | 655 FOR_EACH_OBSERVER(Observer, observers_, |
| 634 OnDocumentFeedFetched(num_accumulated_entries)); | 656 OnDocumentFeedFetched(num_accumulated_entries)); |
| 635 | 657 |
| 636 UMA_HISTOGRAM_TIMES("Gdata.EntireFeedLoadTime", | 658 UMA_HISTOGRAM_TIMES("Gdata.EntireFeedLoadTime", |
| 637 base::TimeTicks::Now() - start_time); | 659 base::TimeTicks::Now() - start_time); |
| 638 | 660 |
| 639 if (!callback.is_null()) | 661 if (!callback.is_null()) |
| 640 callback.Run(params, error); | 662 callback.Run(params, error); |
| 641 } | 663 } |
| 642 | 664 |
| 665 void GDataWapiFeedLoader::OnGetChangelist( | |
| 666 ContentOrigin initial_origin, | |
| 667 const LoadDocumentFeedCallback& callback, | |
| 668 GetDocumentsParams* params, | |
| 669 base::TimeTicks start_time, | |
| 670 GDataErrorCode status, | |
| 671 scoped_ptr<base::Value> data) { | |
| 672 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 673 | |
| 674 if (params->feed_list->empty()) { | |
| 675 UMA_HISTOGRAM_TIMES("Drive.InitialFeedLoadTime", | |
| 676 base::TimeTicks::Now() - start_time); | |
| 677 } | |
| 678 | |
| 679 GDataFileError error = util::GDataToGDataFileError(status); | |
| 680 if (error == GDATA_FILE_OK && | |
| 681 (!data.get() || data->GetType() != Value::TYPE_DICTIONARY)) { | |
| 682 error = GDATA_FILE_ERROR_FAILED; | |
| 683 } | |
| 684 | |
| 685 if (error != GDATA_FILE_OK) { | |
| 686 directory_service_->set_origin(initial_origin); | |
| 687 | |
| 688 if (!callback.is_null()) | |
| 689 callback.Run(params, error); | |
| 690 | |
| 691 return; | |
| 692 } | |
| 693 | |
| 694 GURL next_feed_url; | |
| 695 scoped_ptr<ChangeList> current_feed(ChangeList::CreateFrom(*data)); | |
| 696 if (!current_feed.get()) { | |
| 697 if (!callback.is_null()) { | |
| 698 callback.Run(params, GDATA_FILE_ERROR_FAILED); | |
| 699 } | |
| 700 return; | |
| 701 } | |
| 702 const bool has_next_feed = !current_feed->next_page_token().empty(); | |
| 703 | |
| 704 #ifndef NDEBUG | |
| 705 // Save initial root feed for analysis. | |
| 706 std::string file_name = | |
| 707 base::StringPrintf("DEBUG_changelist_%" PRId64 ".json", | |
| 708 params->start_changestamp); | |
| 709 util::PostBlockingPoolSequencedTask( | |
| 710 FROM_HERE, | |
| 711 blocking_task_runner_, | |
| 712 base::Bind(&SaveFeedOnBlockingPoolForDebugging, | |
| 713 cache_->GetCacheDirectoryPath( | |
| 714 GDataCache::CACHE_TYPE_META).Append(file_name), | |
| 715 base::Passed(&data))); | |
| 716 #endif | |
| 717 | |
| 718 // Add the current feed to the list of collected feeds for this directory. | |
| 719 scoped_ptr<DocumentFeed> feed = | |
| 720 DocumentFeed::CreateFromChangeList(*current_feed); | |
| 721 params->feed_list->push_back(feed.release()); | |
| 722 | |
| 723 // Compute and notify the number of entries fetched so far. | |
| 724 int num_accumulated_entries = 0; | |
| 725 for (size_t i = 0; i < params->feed_list->size(); ++i) | |
| 726 num_accumulated_entries += params->feed_list->at(i)->entries().size(); | |
| 727 | |
| 728 // Check if we need to collect more data to complete the directory list. | |
| 729 if (params->should_fetch_multiple_feeds && has_next_feed) { | |
| 730 | |
| 731 // Post an UI update event to make the UI smoother. | |
| 732 GetDocumentsUiState* ui_state = params->ui_state.get(); | |
| 733 if (ui_state == NULL) { | |
| 734 ui_state = new GetDocumentsUiState(base::TimeTicks::Now()); | |
| 735 params->ui_state.reset(ui_state); | |
| 736 } | |
| 737 DCHECK(ui_state); | |
| 738 | |
| 739 if ((ui_state->num_fetched_documents - ui_state->num_showing_documents) | |
| 740 < kFetchUiUpdateStep) { | |
| 741 // Currently the UI update is stopped. Start UI periodic callback. | |
| 742 MessageLoop::current()->PostTask( | |
| 743 FROM_HERE, | |
| 744 base::Bind(&GDataWapiFeedLoader::OnNotifyDocumentFeedFetched, | |
| 745 weak_ptr_factory_.GetWeakPtr(), | |
| 746 ui_state->weak_ptr_factory.GetWeakPtr())); | |
| 747 } | |
| 748 ui_state->num_fetched_documents = num_accumulated_entries; | |
| 749 ui_state->feed_fetching_elapsed_time = base::TimeTicks::Now() - start_time; | |
| 750 | |
| 751 // Kick of the remaining part of the feeds. | |
| 752 documents_service_->GetChangelist( | |
| 753 current_feed->next_link(), | |
| 754 params->start_changestamp, | |
| 755 base::Bind(&GDataWapiFeedLoader::OnGetChangelist, | |
| 756 weak_ptr_factory_.GetWeakPtr(), | |
| 757 initial_origin, | |
| 758 callback, | |
| 759 base::Owned( | |
| 760 new GetDocumentsParams( | |
| 761 params->start_changestamp, | |
| 762 params->root_feed_changestamp, | |
| 763 params->feed_list.release(), | |
| 764 params->should_fetch_multiple_feeds, | |
| 765 params->search_file_path, | |
| 766 params->search_query, | |
| 767 params->directory_resource_id, | |
| 768 params->callback, | |
| 769 NULL)), | |
| 770 start_time)); | |
| 771 return; | |
| 772 } | |
| 773 | |
| 774 // Notify the observers that a document feed is fetched. | |
| 775 FOR_EACH_OBSERVER(Observer, observers_, | |
| 776 OnDocumentFeedFetched(num_accumulated_entries)); | |
| 777 | |
| 778 UMA_HISTOGRAM_TIMES("Drive.EntireFeedLoadTime", | |
| 779 base::TimeTicks::Now() - start_time); | |
| 780 | |
| 781 if (!callback.is_null()) | |
| 782 callback.Run(params, error); | |
| 783 } | |
| 784 | |
| 643 void GDataWapiFeedLoader::OnNotifyDocumentFeedFetched( | 785 void GDataWapiFeedLoader::OnNotifyDocumentFeedFetched( |
| 644 base::WeakPtr<GetDocumentsUiState> ui_state) { | 786 base::WeakPtr<GetDocumentsUiState> ui_state) { |
| 645 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 787 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 646 | 788 |
| 647 if (!ui_state) { | 789 if (!ui_state) { |
| 648 // The ui state instance is already released, which means the fetching | 790 // The ui state instance is already released, which means the fetching |
| 649 // is done and we don't need to update any more. | 791 // is done and we don't need to update any more. |
| 650 return; | 792 return; |
| 651 } | 793 } |
| 652 | 794 |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 821 dir_iter != changed_dirs.end(); ++dir_iter) { | 963 dir_iter != changed_dirs.end(); ++dir_iter) { |
| 822 FOR_EACH_OBSERVER(Observer, observers_, | 964 FOR_EACH_OBSERVER(Observer, observers_, |
| 823 OnDirectoryChanged(*dir_iter)); | 965 OnDirectoryChanged(*dir_iter)); |
| 824 } | 966 } |
| 825 } | 967 } |
| 826 | 968 |
| 827 return error; | 969 return error; |
| 828 } | 970 } |
| 829 | 971 |
| 830 } // namespace gdata | 972 } // namespace gdata |
| OLD | NEW |