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/gdata_wapi_feed_loader.h" | 5 #include "chrome/browser/chromeos/drive/gdata_wapi_feed_loader.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
198 start_time(start_time), | 198 start_time(start_time), |
199 weak_ptr_factory(this) { | 199 weak_ptr_factory(this) { |
200 } | 200 } |
201 | 201 |
202 // The number of fetched documents. | 202 // The number of fetched documents. |
203 int num_fetched_documents; | 203 int num_fetched_documents; |
204 | 204 |
205 // The number documents shown on UI. | 205 // The number documents shown on UI. |
206 int num_showing_documents; | 206 int num_showing_documents; |
207 | 207 |
208 // When the UI update has started. | 208 // When the loading started. |
209 base::TimeTicks start_time; | 209 base::TimeTicks start_time; |
210 | 210 |
211 // Time elapsed since the feed fetching was started. | 211 // When the last feed was fetched. |
212 base::TimeDelta feed_fetching_elapsed_time; | 212 base::TimeTicks last_fetch_time; |
213 | 213 |
214 base::WeakPtrFactory<GetDocumentsUiState> weak_ptr_factory; | 214 base::WeakPtrFactory<GetDocumentsUiState> weak_ptr_factory; |
215 }; | 215 }; |
216 | 216 |
217 GDataWapiFeedLoader::GDataWapiFeedLoader( | 217 GDataWapiFeedLoader::GDataWapiFeedLoader( |
218 DriveResourceMetadata* resource_metadata, | 218 DriveResourceMetadata* resource_metadata, |
219 DriveServiceInterface* drive_service, | 219 DriveServiceInterface* drive_service, |
220 DriveWebAppsRegistryInterface* webapps_registry, | 220 DriveWebAppsRegistryInterface* webapps_registry, |
221 DriveCache* cache, | 221 DriveCache* cache, |
222 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner) | 222 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner) |
(...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
577 // Compute and notify the number of entries fetched so far. | 577 // Compute and notify the number of entries fetched so far. |
578 int num_accumulated_entries = 0; | 578 int num_accumulated_entries = 0; |
579 for (size_t i = 0; i < params->feed_list.size(); ++i) | 579 for (size_t i = 0; i < params->feed_list.size(); ++i) |
580 num_accumulated_entries += params->feed_list[i]->entries().size(); | 580 num_accumulated_entries += params->feed_list[i]->entries().size(); |
581 | 581 |
582 // Check if we need to collect more data to complete the directory list. | 582 // Check if we need to collect more data to complete the directory list. |
583 if (has_next_feed_url && !next_feed_url.is_empty()) { | 583 if (has_next_feed_url && !next_feed_url.is_empty()) { |
584 // Post an UI update event to make the UI smoother. | 584 // Post an UI update event to make the UI smoother. |
585 GetDocumentsUiState* ui_state = params->ui_state.get(); | 585 GetDocumentsUiState* ui_state = params->ui_state.get(); |
586 if (ui_state == NULL) { | 586 if (ui_state == NULL) { |
587 ui_state = new GetDocumentsUiState(base::TimeTicks::Now()); | 587 ui_state = new GetDocumentsUiState(start_time); |
588 params->ui_state.reset(ui_state); | 588 params->ui_state.reset(ui_state); |
589 } | 589 } |
590 DCHECK(ui_state); | 590 DCHECK(ui_state); |
591 | 591 |
592 if ((ui_state->num_fetched_documents - ui_state->num_showing_documents) | 592 if ((ui_state->num_fetched_documents - ui_state->num_showing_documents) |
593 < kFetchUiUpdateStep) { | 593 < kFetchUiUpdateStep) { |
594 // Currently the UI update is stopped. Start UI periodic callback. | 594 // Currently the UI update is stopped. Start UI periodic callback. |
595 base::MessageLoopProxy::current()->PostTask( | 595 base::MessageLoopProxy::current()->PostTask( |
596 FROM_HERE, | 596 FROM_HERE, |
597 base::Bind(&GDataWapiFeedLoader::OnNotifyDocumentFeedFetched, | 597 base::Bind(&GDataWapiFeedLoader::OnNotifyDocumentFeedFetched, |
598 weak_ptr_factory_.GetWeakPtr(), | 598 weak_ptr_factory_.GetWeakPtr(), |
599 ui_state->weak_ptr_factory.GetWeakPtr())); | 599 ui_state->weak_ptr_factory.GetWeakPtr())); |
600 } | 600 } |
601 ui_state->num_fetched_documents = num_accumulated_entries; | 601 ui_state->num_fetched_documents = num_accumulated_entries; |
602 ui_state->feed_fetching_elapsed_time = base::TimeTicks::Now() - start_time; | 602 ui_state->last_fetch_time = base::TimeTicks::Now(); |
603 | 603 |
604 // |params| will be passed to the callback and thus nulled. Extract the | 604 // |params| will be passed to the callback and thus nulled. Extract the |
605 // pointer so we can use it bellow. | 605 // pointer so we can use it bellow. |
606 LoadFeedParams* params_ptr = params.get(); | 606 LoadFeedParams* params_ptr = params.get(); |
607 // Kick off the remaining part of the feeds. | 607 // Kick off the remaining part of the feeds. |
608 drive_service_->GetDocuments( | 608 drive_service_->GetDocuments( |
609 next_feed_url, | 609 next_feed_url, |
610 params_ptr->start_changestamp, | 610 params_ptr->start_changestamp, |
611 params_ptr->search_query, | 611 params_ptr->search_query, |
612 params_ptr->directory_resource_id, | 612 params_ptr->directory_resource_id, |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
683 // Compute and notify the number of entries fetched so far. | 683 // Compute and notify the number of entries fetched so far. |
684 int num_accumulated_entries = 0; | 684 int num_accumulated_entries = 0; |
685 for (size_t i = 0; i < params->feed_list.size(); ++i) | 685 for (size_t i = 0; i < params->feed_list.size(); ++i) |
686 num_accumulated_entries += params->feed_list[i]->entries().size(); | 686 num_accumulated_entries += params->feed_list[i]->entries().size(); |
687 | 687 |
688 // Check if we need to collect more data to complete the directory list. | 688 // Check if we need to collect more data to complete the directory list. |
689 if (has_next_feed) { | 689 if (has_next_feed) { |
690 // Post an UI update event to make the UI smoother. | 690 // Post an UI update event to make the UI smoother. |
691 GetDocumentsUiState* ui_state = params->ui_state.get(); | 691 GetDocumentsUiState* ui_state = params->ui_state.get(); |
692 if (ui_state == NULL) { | 692 if (ui_state == NULL) { |
693 ui_state = new GetDocumentsUiState(base::TimeTicks::Now()); | 693 ui_state = new GetDocumentsUiState(start_time); |
694 params->ui_state.reset(ui_state); | 694 params->ui_state.reset(ui_state); |
695 } | 695 } |
696 DCHECK(ui_state); | 696 DCHECK(ui_state); |
697 | 697 |
698 if ((ui_state->num_fetched_documents - ui_state->num_showing_documents) | 698 if ((ui_state->num_fetched_documents - ui_state->num_showing_documents) |
699 < kFetchUiUpdateStep) { | 699 < kFetchUiUpdateStep) { |
700 // Currently the UI update is stopped. Start UI periodic callback. | 700 // Currently the UI update is stopped. Start UI periodic callback. |
701 base::MessageLoopProxy::current()->PostTask( | 701 base::MessageLoopProxy::current()->PostTask( |
702 FROM_HERE, | 702 FROM_HERE, |
703 base::Bind(&GDataWapiFeedLoader::OnNotifyDocumentFeedFetched, | 703 base::Bind(&GDataWapiFeedLoader::OnNotifyDocumentFeedFetched, |
704 weak_ptr_factory_.GetWeakPtr(), | 704 weak_ptr_factory_.GetWeakPtr(), |
705 ui_state->weak_ptr_factory.GetWeakPtr())); | 705 ui_state->weak_ptr_factory.GetWeakPtr())); |
706 } | 706 } |
707 ui_state->num_fetched_documents = num_accumulated_entries; | 707 ui_state->num_fetched_documents = num_accumulated_entries; |
708 ui_state->feed_fetching_elapsed_time = base::TimeTicks::Now() - start_time; | 708 ui_state->last_fetch_time = base::TimeTicks::Now(); |
709 | 709 |
710 // Kick off the remaining part of the feeds. | 710 // Kick off the remaining part of the feeds. |
711 // Extract the pointer so we can use it bellow. | 711 // Extract the pointer so we can use it bellow. |
712 LoadFeedParams* params_ptr = params.get(); | 712 LoadFeedParams* params_ptr = params.get(); |
713 drive_service_->GetDocuments( | 713 drive_service_->GetDocuments( |
714 current_feed->next_link(), | 714 current_feed->next_link(), |
715 params_ptr->start_changestamp, | 715 params_ptr->start_changestamp, |
716 std::string(), // No search query. | 716 std::string(), // No search query. |
717 std::string(), // No directory resource ID. | 717 std::string(), // No directory resource ID. |
718 base::Bind(&GDataWapiFeedLoader::OnGetChangelist, | 718 base::Bind(&GDataWapiFeedLoader::OnGetChangelist, |
(...skipping 17 matching lines...) Expand all Loading... | |
736 void GDataWapiFeedLoader::OnNotifyDocumentFeedFetched( | 736 void GDataWapiFeedLoader::OnNotifyDocumentFeedFetched( |
737 base::WeakPtr<GetDocumentsUiState> ui_state) { | 737 base::WeakPtr<GetDocumentsUiState> ui_state) { |
738 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 738 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
739 | 739 |
740 if (!ui_state) { | 740 if (!ui_state) { |
741 // The ui state instance is already released, which means the fetching | 741 // The ui state instance is already released, which means the fetching |
742 // is done and we don't need to update any more. | 742 // is done and we don't need to update any more. |
743 return; | 743 return; |
744 } | 744 } |
745 | 745 |
746 base::TimeDelta elapsed_time = | 746 // Compute the number of documents to be shown in UI. |
747 base::TimeTicks::Now() - ui_state->start_time; | 747 ui_state->num_showing_documents = std::min( |
748 ui_state->num_showing_documents + kFetchUiUpdateStep, | |
749 ui_state->num_fetched_documents); | |
750 // Update UI via observers. | |
751 FOR_EACH_OBSERVER(GDataWapiFeedLoaderObserver, observers_, | |
752 OnDocumentFeedFetched(ui_state->num_showing_documents)); | |
748 | 753 |
749 if (ui_state->num_showing_documents + kFetchUiUpdateStep <= | 754 // UI should be updated if num_showing_documents is behind |
750 ui_state->num_fetched_documents) { | 755 // num_fetched_documents. |
751 ui_state->num_showing_documents += kFetchUiUpdateStep; | 756 if (ui_state->num_showing_documents < ui_state->num_fetched_documents) { |
752 FOR_EACH_OBSERVER(GDataWapiFeedLoaderObserver, observers_, | 757 // Time elapsed from the beginning to the last fetch; |
753 OnDocumentFeedFetched(ui_state->num_showing_documents)); | 758 const base::TimeDelta elapsed_time = |
754 | 759 ui_state->last_fetch_time - ui_state ->start_time; |
hidehiko
2012/10/17 06:26:41
s/ui_state ->/ui_state->/
satorux1
2012/10/17 08:00:43
Done.
| |
755 int num_remaining_ui_updates = | 760 // How long did it take to fetch one document? |
756 (ui_state->num_fetched_documents - ui_state->num_showing_documents) | 761 const base::TimeDelta rate = |
757 / kFetchUiUpdateStep; | 762 elapsed_time / ui_state->num_fetched_documents; |
758 if (num_remaining_ui_updates > 0) { | 763 // How long would it take to fetch the next kFetchUiUpdateStep documents? |
759 // Heuristically, we use fetched time duration to calculate the next | 764 const base::TimeDelta interval = rate * kFetchUiUpdateStep; |
hidehiko
2012/10/17 06:26:41
By this expression's change, UI update duration ma
satorux1
2012/10/17 08:00:43
Chatted off-line. Here's the summary of your conce
| |
760 // UI update timing. | 765 base::MessageLoopProxy::current()->PostDelayedTask( |
761 base::TimeDelta remaining_duration = | 766 FROM_HERE, |
762 ui_state->feed_fetching_elapsed_time - elapsed_time; | 767 base::Bind(&GDataWapiFeedLoader::OnNotifyDocumentFeedFetched, |
763 base::MessageLoopProxy::current()->PostDelayedTask( | 768 weak_ptr_factory_.GetWeakPtr(), |
764 FROM_HERE, | 769 ui_state->weak_ptr_factory.GetWeakPtr()), |
765 base::Bind(&GDataWapiFeedLoader::OnNotifyDocumentFeedFetched, | 770 interval); |
766 weak_ptr_factory_.GetWeakPtr(), | |
767 ui_state->weak_ptr_factory.GetWeakPtr()), | |
768 remaining_duration / num_remaining_ui_updates); | |
769 } | |
770 } | 771 } |
771 } | 772 } |
772 | 773 |
773 void GDataWapiFeedLoader::LoadFromCache( | 774 void GDataWapiFeedLoader::LoadFromCache( |
774 bool should_load_from_server, | 775 bool should_load_from_server, |
775 const FileOperationCallback& callback) { | 776 const FileOperationCallback& callback) { |
776 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 777 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
777 | 778 |
778 LoadRootFeedParams* params = new LoadRootFeedParams(should_load_from_server, | 779 LoadRootFeedParams* params = new LoadRootFeedParams(should_load_from_server, |
779 callback); | 780 callback); |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
912 dir_iter != changed_dirs.end(); ++dir_iter) { | 913 dir_iter != changed_dirs.end(); ++dir_iter) { |
913 FOR_EACH_OBSERVER(GDataWapiFeedLoaderObserver, observers_, | 914 FOR_EACH_OBSERVER(GDataWapiFeedLoaderObserver, observers_, |
914 OnDirectoryChanged(*dir_iter)); | 915 OnDirectoryChanged(*dir_iter)); |
915 } | 916 } |
916 } | 917 } |
917 | 918 |
918 return error; | 919 return error; |
919 } | 920 } |
920 | 921 |
921 } // namespace drive | 922 } // namespace drive |
OLD | NEW |