Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(650)

Side by Side Diff: chrome/browser/chromeos/gdata/gdata_file_system.cc

Issue 10828126: gdata: Introduce GDataWapiFeedLoader class (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase and fix Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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_file_system.h" 5 #include "chrome/browser/chromeos/gdata/gdata_file_system.h"
6 6
7 #include <set> 7 #include <set>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 540 matching lines...) Expand 10 before | Expand all | Expand 10 after
551 const GetEntryInfoWithFilePathCallback& callback, 551 const GetEntryInfoWithFilePathCallback& callback,
552 const FilePath& path, 552 const FilePath& path,
553 GDataFileError error, 553 GDataFileError error,
554 scoped_ptr<GDataEntryProto> entry_proto) { 554 scoped_ptr<GDataEntryProto> entry_proto) {
555 if (!callback.is_null()) 555 if (!callback.is_null())
556 callback.Run(error, path, entry_proto.Pass()); 556 callback.Run(error, path, entry_proto.Pass());
557 } 557 }
558 558
559 } // namespace 559 } // namespace
560 560
561 // GDataFileSystem::GetDocumentsParams struct implementation. 561 GDataWapiFeedLoader::GDataWapiFeedLoader(
562 struct GDataFileSystem::GetDocumentsParams { 562 GDataDirectoryService* directory_service,
563 DocumentsServiceInterface* documents_service,
564 DriveWebAppsRegistryInterface* webapps_registry,
565 GDataCache* cache,
566 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner)
567 : directory_service_(directory_service),
568 documents_service_(documents_service),
569 webapps_registry_(webapps_registry),
570 cache_(cache),
571 blocking_task_runner_(blocking_task_runner),
572 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
573 }
574
575 GDataWapiFeedLoader::~GDataWapiFeedLoader() {
576 }
577
578 void GDataWapiFeedLoader::AddObserver(Observer* observer) {
579 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
580 observers_.AddObserver(observer);
581 }
582
583 void GDataWapiFeedLoader::RemoveObserver(Observer* observer) {
584 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
585 observers_.RemoveObserver(observer);
586 }
587
588 // Defines set of parameters sent to callback OnGetDocuments().
589 // TODO(satorux): Move this to a new file: crbug.com/138268
590 struct GetDocumentsParams {
563 GetDocumentsParams(int start_changestamp, 591 GetDocumentsParams(int start_changestamp,
564 int root_feed_changestamp, 592 int root_feed_changestamp,
565 std::vector<DocumentFeed*>* feed_list, 593 std::vector<DocumentFeed*>* feed_list,
566 bool should_fetch_multiple_feeds, 594 bool should_fetch_multiple_feeds,
567 const FilePath& search_file_path, 595 const FilePath& search_file_path,
568 const std::string& search_query, 596 const std::string& search_query,
569 const std::string& directory_resource_id, 597 const std::string& directory_resource_id,
570 const FindEntryCallback& callback); 598 const FindEntryCallback& callback);
571 ~GetDocumentsParams(); 599 ~GetDocumentsParams();
572 600
573 // Changestamps are positive numbers in increasing order. The difference 601 // Changestamps are positive numbers in increasing order. The difference
574 // between two changestamps is proportional equal to number of items in 602 // between two changestamps is proportional equal to number of items in
575 // delta feed between them - bigger the difference, more likely bigger 603 // delta feed between them - bigger the difference, more likely bigger
576 // number of items in delta feeds. 604 // number of items in delta feeds.
577 int start_changestamp; 605 int start_changestamp;
578 int root_feed_changestamp; 606 int root_feed_changestamp;
579 scoped_ptr<std::vector<DocumentFeed*> > feed_list; 607 scoped_ptr<std::vector<DocumentFeed*> > feed_list;
580 // Should we stop after getting first feed chunk, even if there is more 608 // Should we stop after getting first feed chunk, even if there is more
581 // data. 609 // data.
582 bool should_fetch_multiple_feeds; 610 bool should_fetch_multiple_feeds;
583 FilePath search_file_path; 611 FilePath search_file_path;
584 std::string search_query; 612 std::string search_query;
585 std::string directory_resource_id; 613 std::string directory_resource_id;
586 FindEntryCallback callback; 614 FindEntryCallback callback;
587 }; 615 };
588 616
589 GDataFileSystem::GetDocumentsParams::GetDocumentsParams( 617 GetDocumentsParams::GetDocumentsParams(
590 int start_changestamp, 618 int start_changestamp,
591 int root_feed_changestamp, 619 int root_feed_changestamp,
592 std::vector<DocumentFeed*>* feed_list, 620 std::vector<DocumentFeed*>* feed_list,
593 bool should_fetch_multiple_feeds, 621 bool should_fetch_multiple_feeds,
594 const FilePath& search_file_path, 622 const FilePath& search_file_path,
595 const std::string& search_query, 623 const std::string& search_query,
596 const std::string& directory_resource_id, 624 const std::string& directory_resource_id,
597 const FindEntryCallback& callback) 625 const FindEntryCallback& callback)
598 : start_changestamp(start_changestamp), 626 : start_changestamp(start_changestamp),
599 root_feed_changestamp(root_feed_changestamp), 627 root_feed_changestamp(root_feed_changestamp),
600 feed_list(feed_list), 628 feed_list(feed_list),
601 should_fetch_multiple_feeds(should_fetch_multiple_feeds), 629 should_fetch_multiple_feeds(should_fetch_multiple_feeds),
602 search_file_path(search_file_path), 630 search_file_path(search_file_path),
603 search_query(search_query), 631 search_query(search_query),
604 directory_resource_id(directory_resource_id), 632 directory_resource_id(directory_resource_id),
605 callback(callback) { 633 callback(callback) {
606 } 634 }
607 635
608 GDataFileSystem::GetDocumentsParams::~GetDocumentsParams() { 636 GetDocumentsParams::~GetDocumentsParams() {
609 STLDeleteElements(feed_list.get()); 637 STLDeleteElements(feed_list.get());
610 } 638 }
611 639
612 // GDataFileSystem::CreateDirectoryParams struct implementation. 640 // GDataFileSystem::CreateDirectoryParams struct implementation.
613 struct GDataFileSystem::CreateDirectoryParams { 641 struct GDataFileSystem::CreateDirectoryParams {
614 CreateDirectoryParams(const FilePath& created_directory_path, 642 CreateDirectoryParams(const FilePath& created_directory_path,
615 const FilePath& target_directory_path, 643 const FilePath& target_directory_path,
616 bool is_exclusive, 644 bool is_exclusive,
617 bool is_recursive, 645 bool is_recursive,
618 const FileOperationCallback& callback); 646 const FileOperationCallback& callback);
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
741 // Should be created from the file browser extension API on UI thread. 769 // Should be created from the file browser extension API on UI thread.
742 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 770 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
743 } 771 }
744 772
745 void GDataFileSystem::Initialize() { 773 void GDataFileSystem::Initialize() {
746 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 774 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
747 775
748 documents_service_->Initialize(profile_); 776 documents_service_->Initialize(profile_);
749 777
750 directory_service_.reset(new GDataDirectoryService); 778 directory_service_.reset(new GDataDirectoryService);
779 feed_loader_.reset(new GDataWapiFeedLoader(directory_service_.get(),
780 documents_service_,
781 webapps_registry_,
782 cache_,
783 blocking_task_runner_));
784 feed_loader_->AddObserver(this);
751 785
752 PrefService* pref_service = profile_->GetPrefs(); 786 PrefService* pref_service = profile_->GetPrefs();
753 hide_hosted_docs_ = pref_service->GetBoolean(prefs::kDisableGDataHostedFiles); 787 hide_hosted_docs_ = pref_service->GetBoolean(prefs::kDisableGDataHostedFiles);
754 788
755 InitializePreferenceObserver(); 789 InitializePreferenceObserver();
756 } 790 }
757 791
758 void GDataFileSystem::CheckForUpdates() { 792 void GDataFileSystem::CheckForUpdates() {
759 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 793 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
760 ContentOrigin initial_origin = directory_service_->origin(); 794 ContentOrigin initial_origin = directory_service_->origin();
761 if (initial_origin == FROM_SERVER) { 795 if (initial_origin == FROM_SERVER) {
762 directory_service_->set_origin(REFRESHING); 796 directory_service_->set_origin(REFRESHING);
763 ReloadFeedFromServerIfNeeded( 797 feed_loader_->ReloadFromServerIfNeeded(
764 initial_origin, 798 initial_origin,
765 directory_service_->largest_changestamp(), 799 directory_service_->largest_changestamp(),
766 directory_service_->root()->GetFilePath(), 800 directory_service_->root()->GetFilePath(),
767 base::Bind(&GDataFileSystem::OnUpdateChecked, 801 base::Bind(&GDataFileSystem::OnUpdateChecked,
768 ui_weak_ptr_, 802 ui_weak_ptr_,
769 initial_origin)); 803 initial_origin));
770 } 804 }
771 } 805 }
772 806
773 void GDataFileSystem::OnUpdateChecked(ContentOrigin initial_origin, 807 void GDataFileSystem::OnUpdateChecked(ContentOrigin initial_origin,
774 GDataFileError error, 808 GDataFileError error,
775 GDataEntry* /* entry */) { 809 GDataEntry* /* entry */) {
776 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 810 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
777 811
778 if (error != GDATA_FILE_OK) { 812 if (error != GDATA_FILE_OK) {
779 directory_service_->set_origin(initial_origin); 813 directory_service_->set_origin(initial_origin);
780 } 814 }
781 } 815 }
782 816
783 GDataFileSystem::~GDataFileSystem() { 817 GDataFileSystem::~GDataFileSystem() {
784 // This should be called from UI thread, from GDataSystemService shutdown. 818 // This should be called from UI thread, from GDataSystemService shutdown.
785 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 819 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
786 820
821 feed_loader_->RemoveObserver(this);
822
787 // Cancel all the in-flight operations. 823 // Cancel all the in-flight operations.
788 // This asynchronously cancels the URL fetch operations. 824 // This asynchronously cancels the URL fetch operations.
789 documents_service_->CancelAll(); 825 documents_service_->CancelAll();
790 } 826 }
791 827
792 void GDataFileSystem::AddObserver(Observer* observer) { 828 void GDataFileSystem::AddObserver(
829 GDataFileSystemInterface::Observer* observer) {
793 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 830 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
794 observers_.AddObserver(observer); 831 observers_.AddObserver(observer);
795 } 832 }
796 833
797 void GDataFileSystem::RemoveObserver(Observer* observer) { 834 void GDataFileSystem::RemoveObserver(
835 GDataFileSystemInterface::Observer* observer) {
798 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 836 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
799 observers_.RemoveObserver(observer); 837 observers_.RemoveObserver(observer);
800 } 838 }
801 839
802 void GDataFileSystem::StartUpdates() { 840 void GDataFileSystem::StartUpdates() {
803 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 841 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
804 842
805 DCHECK(!update_timer_.IsRunning()); 843 DCHECK(!update_timer_.IsRunning());
806 update_timer_.Start(FROM_HERE, 844 update_timer_.Start(FROM_HERE,
807 base::TimeDelta::FromSeconds( 845 base::TimeDelta::FromSeconds(
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
870 this, 908 this,
871 base::Bind(&GDataFileSystem::FindEntryByPathSyncOnUIThread, 909 base::Bind(&GDataFileSystem::FindEntryByPathSyncOnUIThread,
872 ui_weak_ptr_, 910 ui_weak_ptr_,
873 search_file_path, 911 search_file_path,
874 callback))); 912 callback)));
875 return; 913 return;
876 } else if (directory_service_->origin() == UNINITIALIZED) { 914 } else if (directory_service_->origin() == UNINITIALIZED) {
877 // Load root feed from this disk cache. Upon completion, kick off server 915 // Load root feed from this disk cache. Upon completion, kick off server
878 // fetching. 916 // fetching.
879 directory_service_->set_origin(INITIALIZING); 917 directory_service_->set_origin(INITIALIZING);
880 LoadRootFeedFromCache( 918 feed_loader_->LoadFromCache(
881 true, // should_load_from_server 919 true, // should_load_from_server
882 search_file_path, 920 search_file_path,
883 // This is the initial load, hence we'll notify when it's done. 921 // This is the initial load, hence we'll notify when it's done.
884 base::Bind(&GDataFileSystem::RunAndNotifyInitialLoadFinished, 922 base::Bind(&GDataFileSystem::RunAndNotifyInitialLoadFinished,
885 ui_weak_ptr_, 923 ui_weak_ptr_,
886 callback)); 924 callback));
887 return; 925 return;
888 } 926 }
889 927
890 // Post a task to the same thread, rather than calling it here, as 928 // Post a task to the same thread, rather than calling it here, as
891 // FindEntryByPath() is asynchronous. 929 // FindEntryByPath() is asynchronous.
892 base::MessageLoopProxy::current()->PostTask( 930 base::MessageLoopProxy::current()->PostTask(
893 FROM_HERE, 931 FROM_HERE,
894 base::Bind(&GDataFileSystem::FindEntryByPathSyncOnUIThread, 932 base::Bind(&GDataFileSystem::FindEntryByPathSyncOnUIThread,
895 ui_weak_ptr_, 933 ui_weak_ptr_,
896 search_file_path, 934 search_file_path,
897 callback)); 935 callback));
898 } 936 }
899 937
900 void GDataFileSystem::FindEntryByPathSyncOnUIThread( 938 void GDataFileSystem::FindEntryByPathSyncOnUIThread(
901 const FilePath& search_file_path, 939 const FilePath& search_file_path,
902 const FindEntryCallback& callback) { 940 const FindEntryCallback& callback) {
903 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 941 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
904 942
905 GDataEntry* entry = directory_service_->FindEntryByPathSync( 943 directory_service_->FindEntryByPathAndRunSync(search_file_path, callback);
906 search_file_path);
907 callback.Run(entry ? GDATA_FILE_OK : GDATA_FILE_ERROR_NOT_FOUND, entry);
908 } 944 }
909 945
910 void GDataFileSystem::ReloadFeedFromServerIfNeeded( 946 void GDataWapiFeedLoader::ReloadFromServerIfNeeded(
911 ContentOrigin initial_origin, 947 ContentOrigin initial_origin,
912 int local_changestamp, 948 int local_changestamp,
913 const FilePath& search_file_path, 949 const FilePath& search_file_path,
914 const FindEntryCallback& callback) { 950 const FindEntryCallback& callback) {
915 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 951 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
916 952
917 // First fetch the latest changestamp to see if there were any new changes 953 // First fetch the latest changestamp to see if there were any new changes
918 // there at all. 954 // there at all.
919 documents_service_->GetAccountMetadata( 955 documents_service_->GetAccountMetadata(
920 base::Bind(&GDataFileSystem::OnGetAccountMetadata, 956 base::Bind(&GDataWapiFeedLoader::OnGetAccountMetadata,
921 ui_weak_ptr_, 957 weak_ptr_factory_.GetWeakPtr(),
922 initial_origin, 958 initial_origin,
923 local_changestamp, 959 local_changestamp,
924 search_file_path, 960 search_file_path,
925 callback)); 961 callback));
926 } 962 }
927 963
928 void GDataFileSystem::OnGetAccountMetadata( 964 void GDataWapiFeedLoader::OnGetAccountMetadata(
929 ContentOrigin initial_origin, 965 ContentOrigin initial_origin,
930 int local_changestamp, 966 int local_changestamp,
931 const FilePath& search_file_path, 967 const FilePath& search_file_path,
932 const FindEntryCallback& callback, 968 const FindEntryCallback& callback,
933 GDataErrorCode status, 969 GDataErrorCode status,
934 scoped_ptr<base::Value> feed_data) { 970 scoped_ptr<base::Value> feed_data) {
935 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 971 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
936 972
937 GDataFileError error = GDataToGDataFileError(status); 973 GDataFileError error = GDataToGDataFileError(status);
938 if (error != GDATA_FILE_OK) { 974 if (error != GDATA_FILE_OK) {
939 // Get changes starting from the next changestamp from what we have locally. 975 // Get changes starting from the next changestamp from what we have locally.
940 LoadFeedFromServer(initial_origin, 976 LoadFromServer(initial_origin,
941 local_changestamp + 1, 0, 977 local_changestamp + 1, 0,
942 true, /* should_fetch_multiple_feeds */ 978 true, /* should_fetch_multiple_feeds */
943 search_file_path, 979 search_file_path,
944 std::string() /* no search query */, 980 std::string() /* no search query */,
945 std::string() /* no directory resource ID */, 981 std::string() /* no directory resource ID */,
946 callback, 982 callback,
947 base::Bind(&GDataFileSystem::OnFeedFromServerLoaded, 983 base::Bind(&GDataWapiFeedLoader::OnFeedFromServerLoaded,
948 ui_weak_ptr_)); 984 weak_ptr_factory_.GetWeakPtr()));
949 return; 985 return;
950 } 986 }
951 987
952 scoped_ptr<AccountMetadataFeed> account_metadata; 988 scoped_ptr<AccountMetadataFeed> account_metadata;
953 if (feed_data.get()) { 989 if (feed_data.get()) {
954 account_metadata = AccountMetadataFeed::CreateFrom(*feed_data); 990 account_metadata = AccountMetadataFeed::CreateFrom(*feed_data);
955 #ifndef NDEBUG 991 #ifndef NDEBUG
956 // Save account metadata feed for analysis. 992 // Save account metadata feed for analysis.
957 const FilePath path = 993 const FilePath path =
958 cache_->GetCacheDirectoryPath(GDataCache::CACHE_TYPE_META).Append( 994 cache_->GetCacheDirectoryPath(GDataCache::CACHE_TYPE_META).Append(
959 kAccountMetadataFile); 995 kAccountMetadataFile);
960 PostBlockingPoolSequencedTask( 996 PostBlockingPoolSequencedTask(
961 FROM_HERE, 997 FROM_HERE,
962 blocking_task_runner_, 998 blocking_task_runner_,
963 base::Bind(&SaveFeedOnBlockingPoolForDebugging, 999 base::Bind(&SaveFeedOnBlockingPoolForDebugging,
964 path, base::Passed(&feed_data))); 1000 path, base::Passed(&feed_data)));
965 #endif 1001 #endif
966 } 1002 }
967 1003
968 if (!account_metadata.get()) { 1004 if (!account_metadata.get()) {
969 LoadFeedFromServer(initial_origin, 1005 LoadFromServer(initial_origin,
970 local_changestamp + 1, 0, 1006 local_changestamp + 1, 0,
971 true, /* should_fetch_multiple_feeds */ 1007 true, /* should_fetch_multiple_feeds */
972 search_file_path, 1008 search_file_path,
973 std::string() /* no search query */, 1009 std::string() /* no search query */,
974 std::string() /* no directory resource ID */, 1010 std::string() /* no directory resource ID */,
975 callback, 1011 callback,
976 base::Bind(&GDataFileSystem::OnFeedFromServerLoaded, 1012 base::Bind(&GDataWapiFeedLoader::OnFeedFromServerLoaded,
977 ui_weak_ptr_)); 1013 weak_ptr_factory_.GetWeakPtr()));
978 return; 1014 return;
979 } 1015 }
980 1016
981 webapps_registry_->UpdateFromFeed(account_metadata.get()); 1017 webapps_registry_->UpdateFromFeed(account_metadata.get());
982 1018
983 bool changes_detected = true; 1019 bool changes_detected = true;
984 if (local_changestamp >= account_metadata->largest_changestamp()) { 1020 if (local_changestamp >= account_metadata->largest_changestamp()) {
985 if (local_changestamp > account_metadata->largest_changestamp()) { 1021 if (local_changestamp > account_metadata->largest_changestamp()) {
986 LOG(WARNING) << "Cached client feed is fresher than server, client = " 1022 LOG(WARNING) << "Cached client feed is fresher than server, client = "
987 << local_changestamp 1023 << local_changestamp
988 << ", server = " 1024 << ", server = "
989 << account_metadata->largest_changestamp(); 1025 << account_metadata->largest_changestamp();
990 } 1026 }
991 // If our cache holds the latest state from the server, change the 1027 // If our cache holds the latest state from the server, change the
992 // state to FROM_SERVER. 1028 // state to FROM_SERVER.
993 directory_service_->set_origin( 1029 directory_service_->set_origin(
994 initial_origin == FROM_CACHE ? FROM_SERVER : initial_origin); 1030 initial_origin == FROM_CACHE ? FROM_SERVER : initial_origin);
995 changes_detected = false; 1031 changes_detected = false;
996 } 1032 }
997 1033
998 // No changes detected, continue with search as planned. 1034 // No changes detected, continue with search as planned.
999 if (!changes_detected) { 1035 if (!changes_detected) {
1000 if (!callback.is_null()) 1036 if (!callback.is_null()) {
1001 FindEntryByPathSyncOnUIThread(search_file_path, callback); 1037 directory_service_->FindEntryByPathAndRunSync(search_file_path,
1038 callback);
1039 }
1002 return; 1040 return;
1003 } 1041 }
1004 1042
1005 // Load changes from the server. 1043 // Load changes from the server.
1006 LoadFeedFromServer(initial_origin, 1044 LoadFromServer(initial_origin,
1007 local_changestamp > 0 ? local_changestamp + 1 : 0, 1045 local_changestamp > 0 ? local_changestamp + 1 : 0,
1008 account_metadata->largest_changestamp(), 1046 account_metadata->largest_changestamp(),
1009 true, /* should_fetch_multiple_feeds */ 1047 true, /* should_fetch_multiple_feeds */
1010 search_file_path, 1048 search_file_path,
1011 std::string() /* no search query */, 1049 std::string() /* no search query */,
1012 std::string() /* no directory resource ID */, 1050 std::string() /* no directory resource ID */,
1013 callback, 1051 callback,
1014 base::Bind(&GDataFileSystem::OnFeedFromServerLoaded, 1052 base::Bind(&GDataWapiFeedLoader::OnFeedFromServerLoaded,
1015 ui_weak_ptr_)); 1053 weak_ptr_factory_.GetWeakPtr()));
1016 } 1054 }
1017 1055
1018 void GDataFileSystem::LoadFeedFromServer( 1056 void GDataWapiFeedLoader::LoadFromServer(
1019 ContentOrigin initial_origin, 1057 ContentOrigin initial_origin,
1020 int start_changestamp, 1058 int start_changestamp,
1021 int root_feed_changestamp, 1059 int root_feed_changestamp,
1022 bool should_fetch_multiple_feeds, 1060 bool should_fetch_multiple_feeds,
1023 const FilePath& search_file_path, 1061 const FilePath& search_file_path,
1024 const std::string& search_query, 1062 const std::string& search_query,
1025 const std::string& directory_resource_id, 1063 const std::string& directory_resource_id,
1026 const FindEntryCallback& entry_found_callback, 1064 const FindEntryCallback& entry_found_callback,
1027 const LoadDocumentFeedCallback& feed_load_callback) { 1065 const LoadDocumentFeedCallback& feed_load_callback) {
1028 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 1066 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1029 1067
1030 // |feed_list| will contain the list of all collected feed updates that 1068 // |feed_list| will contain the list of all collected feed updates that
1031 // we will receive through calls of DocumentsService::GetDocuments(). 1069 // we will receive through calls of DocumentsService::GetDocuments().
1032 scoped_ptr<std::vector<DocumentFeed*> > feed_list( 1070 scoped_ptr<std::vector<DocumentFeed*> > feed_list(
1033 new std::vector<DocumentFeed*>); 1071 new std::vector<DocumentFeed*>);
1034 const base::TimeTicks start_time = base::TimeTicks::Now(); 1072 const base::TimeTicks start_time = base::TimeTicks::Now();
1035 documents_service_->GetDocuments( 1073 documents_service_->GetDocuments(
1036 GURL(), // root feed start. 1074 GURL(), // root feed start.
1037 start_changestamp, 1075 start_changestamp,
1038 search_query, 1076 search_query,
1039 directory_resource_id, 1077 directory_resource_id,
1040 base::Bind(&GDataFileSystem::OnGetDocuments, 1078 base::Bind(&GDataWapiFeedLoader::OnGetDocuments,
1041 ui_weak_ptr_, 1079 weak_ptr_factory_.GetWeakPtr(),
1042 initial_origin, 1080 initial_origin,
1043 feed_load_callback, 1081 feed_load_callback,
1044 base::Owned(new GetDocumentsParams(start_changestamp, 1082 base::Owned(new GetDocumentsParams(start_changestamp,
1045 root_feed_changestamp, 1083 root_feed_changestamp,
1046 feed_list.release(), 1084 feed_list.release(),
1047 should_fetch_multiple_feeds, 1085 should_fetch_multiple_feeds,
1048 search_file_path, 1086 search_file_path,
1049 search_query, 1087 search_query,
1050 directory_resource_id, 1088 directory_resource_id,
1051 entry_found_callback)), 1089 entry_found_callback)),
1052 start_time)); 1090 start_time));
1053 } 1091 }
1054 1092
1055 void GDataFileSystem::OnFeedFromServerLoaded(GetDocumentsParams* params, 1093 void GDataWapiFeedLoader::OnFeedFromServerLoaded(GetDocumentsParams* params,
1056 GDataFileError error) { 1094 GDataFileError error) {
1057 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 1095 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1058 1096
1059 if (error != GDATA_FILE_OK) { 1097 if (error != GDATA_FILE_OK) {
1060 if (!params->callback.is_null()) 1098 if (!params->callback.is_null())
1061 params->callback.Run(error, NULL); 1099 params->callback.Run(error, NULL);
1062 return; 1100 return;
1063 } 1101 }
1064 1102
1065 error = UpdateFromFeed(*params->feed_list, 1103 error = UpdateFromFeed(*params->feed_list,
1066 params->start_changestamp, 1104 params->start_changestamp,
1067 params->root_feed_changestamp); 1105 params->root_feed_changestamp);
1068 1106
1069 if (error != GDATA_FILE_OK) { 1107 if (error != GDATA_FILE_OK) {
1070 if (!params->callback.is_null()) 1108 if (!params->callback.is_null())
1071 params->callback.Run(error, NULL); 1109 params->callback.Run(error, NULL);
1072 1110
1073 return; 1111 return;
1074 } 1112 }
1075 1113
1076 // Save file system metadata to disk. 1114 // Save file system metadata to disk.
1077 SaveFileSystemAsProto(); 1115 SaveFileSystemAsProto();
1078 1116
1079 // If we had someone to report this too, then this retrieval was done in a 1117 // If we had someone to report this too, then this retrieval was done in a
1080 // context of search... so continue search. 1118 // context of search... so continue search.
1081 if (!params->callback.is_null()) 1119 if (!params->callback.is_null()) {
1082 FindEntryByPathSyncOnUIThread(params->search_file_path, params->callback); 1120 directory_service_->FindEntryByPathAndRunSync(params->search_file_path,
1121 params->callback);
1122 }
1083 1123
1084 FOR_EACH_OBSERVER(Observer, observers_, OnFeedFromServerLoaded()); 1124 FOR_EACH_OBSERVER(Observer, observers_, OnFeedFromServerLoaded());
1085 } 1125 }
1086 1126
1087 void GDataFileSystem::TransferFileFromRemoteToLocal( 1127 void GDataFileSystem::TransferFileFromRemoteToLocal(
1088 const FilePath& remote_src_file_path, 1128 const FilePath& remote_src_file_path,
1089 const FilePath& local_dest_file_path, 1129 const FilePath& local_dest_file_path,
1090 const FileOperationCallback& callback) { 1130 const FileOperationCallback& callback) {
1091 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 1131 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1092 1132
(...skipping 1215 matching lines...) Expand 10 before | Expand all | Expand 10 after
2308 GDataFileError error, 2348 GDataFileError error,
2309 scoped_ptr<GDataEntryProto> entry_proto) { 2349 scoped_ptr<GDataEntryProto> entry_proto) {
2310 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 2350 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2311 2351
2312 if (error != GDATA_FILE_OK || 2352 if (error != GDATA_FILE_OK ||
2313 !entry_proto->file_info().is_directory()) { 2353 !entry_proto->file_info().is_directory()) {
2314 LOG(ERROR) << "Directory entry not found: " << file_path.value(); 2354 LOG(ERROR) << "Directory entry not found: " << file_path.value();
2315 return; 2355 return;
2316 } 2356 }
2317 2357
2318 LoadFeedFromServer(directory_service_->origin(), 2358 feed_loader_->LoadFromServer(
2319 0, // Not delta feed. 2359 directory_service_->origin(),
2320 0, // Not used. 2360 0, // Not delta feed.
2321 true, // multiple feeds 2361 0, // Not used.
2322 file_path, 2362 true, // multiple feeds
2323 std::string(), // No search query 2363 file_path,
2324 entry_proto->resource_id(), 2364 std::string(), // No search query
2325 FindEntryCallback(), // Not used. 2365 entry_proto->resource_id(),
2326 base::Bind(&GDataFileSystem::OnRequestDirectoryRefresh, 2366 FindEntryCallback(), // Not used.
2327 ui_weak_ptr_)); 2367 base::Bind(&GDataFileSystem::OnRequestDirectoryRefresh,
2368 ui_weak_ptr_));
2328 } 2369 }
2329 2370
2330 void GDataFileSystem::OnRequestDirectoryRefresh( 2371 void GDataFileSystem::OnRequestDirectoryRefresh(
2331 GetDocumentsParams* params, 2372 GetDocumentsParams* params,
2332 GDataFileError error) { 2373 GDataFileError error) {
2333 DCHECK(params); 2374 DCHECK(params);
2334 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 2375 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2335 2376
2336 const FilePath& directory_path = params->search_file_path; 2377 const FilePath& directory_path = params->search_file_path;
2337 if (error != GDATA_FILE_OK) { 2378 if (error != GDATA_FILE_OK) {
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
2385 scoped_ptr<GDataEntry> entry(it->second); 2426 scoped_ptr<GDataEntry> entry(it->second);
2386 // Skip if it's not a file (i.e. directory). 2427 // Skip if it's not a file (i.e. directory).
2387 if (!entry->AsGDataFile()) 2428 if (!entry->AsGDataFile())
2388 continue; 2429 continue;
2389 directory->AddEntry(entry.release()); 2430 directory->AddEntry(entry.release());
2390 } 2431 }
2391 2432
2392 // Note that there may be no change in the directory, but it's expensive to 2433 // Note that there may be no change in the directory, but it's expensive to
2393 // check if the new metadata matches the existing one, so we just always 2434 // check if the new metadata matches the existing one, so we just always
2394 // notify that the directory is changed. 2435 // notify that the directory is changed.
2395 FOR_EACH_OBSERVER(Observer, observers_, OnDirectoryChanged(directory_path)); 2436 FOR_EACH_OBSERVER(GDataFileSystemInterface::Observer,
2437 observers_, OnDirectoryChanged(directory_path));
2396 DVLOG(1) << "Directory refreshed: " << directory_path.value(); 2438 DVLOG(1) << "Directory refreshed: " << directory_path.value();
2397 } 2439 }
2398 2440
2399 void GDataFileSystem::UpdateFileByResourceId( 2441 void GDataFileSystem::UpdateFileByResourceId(
2400 const std::string& resource_id, 2442 const std::string& resource_id,
2401 const FileOperationCallback& callback) { 2443 const FileOperationCallback& callback) {
2402 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || 2444 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
2403 BrowserThread::CurrentlyOn(BrowserThread::IO)); 2445 BrowserThread::CurrentlyOn(BrowserThread::IO));
2404 RunTaskOnUIThread( 2446 RunTaskOnUIThread(
2405 base::Bind(&GDataFileSystem::UpdateFileByResourceIdOnUIThread, 2447 base::Bind(&GDataFileSystem::UpdateFileByResourceIdOnUIThread,
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after
2709 } 2751 }
2710 2752
2711 void GDataFileSystem::SearchAsyncOnUIThread( 2753 void GDataFileSystem::SearchAsyncOnUIThread(
2712 const std::string& search_query, 2754 const std::string& search_query,
2713 const SearchCallback& callback) { 2755 const SearchCallback& callback) {
2714 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 2756 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2715 scoped_ptr<std::vector<DocumentFeed*> > feed_list( 2757 scoped_ptr<std::vector<DocumentFeed*> > feed_list(
2716 new std::vector<DocumentFeed*>); 2758 new std::vector<DocumentFeed*>);
2717 2759
2718 ContentOrigin initial_origin = directory_service_->origin(); 2760 ContentOrigin initial_origin = directory_service_->origin();
2719 LoadFeedFromServer(initial_origin, 2761 feed_loader_->LoadFromServer(
2720 0, 0, // We don't use change stamps when fetching search 2762 initial_origin,
2721 // data; we always fetch the whole result feed. 2763 0, 0, // We don't use change stamps when fetching search
2722 false, // Stop fetching search results after first feed 2764 // data; we always fetch the whole result feed.
2723 // chunk to avoid displaying huge number of search 2765 false, // Stop fetching search results after first feed
2724 // results (especially since we don't cache them). 2766 // chunk to avoid displaying huge number of search
2725 FilePath(), // Not used. 2767 // results (especially since we don't cache them).
2726 search_query, 2768 FilePath(), // Not used.
2727 std::string(), // No directory resource ID. 2769 search_query,
2728 FindEntryCallback(), // Not used. 2770 std::string(), // No directory resource ID.
2729 base::Bind(&GDataFileSystem::OnSearch, 2771 FindEntryCallback(), // Not used.
2730 ui_weak_ptr_, callback)); 2772 base::Bind(&GDataFileSystem::OnSearch, ui_weak_ptr_, callback));
2731 } 2773 }
2732 2774
2733 void GDataFileSystem::OnGetDocuments(ContentOrigin initial_origin, 2775 void GDataWapiFeedLoader::OnGetDocuments(
2734 const LoadDocumentFeedCallback& callback, 2776 ContentOrigin initial_origin,
2735 GetDocumentsParams* params, 2777 const LoadDocumentFeedCallback& callback,
2736 base::TimeTicks start_time, 2778 GetDocumentsParams* params,
2737 GDataErrorCode status, 2779 base::TimeTicks start_time,
2738 scoped_ptr<base::Value> data) { 2780 GDataErrorCode status,
2781 scoped_ptr<base::Value> data) {
2739 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 2782 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2740 2783
2741 if (params->feed_list->empty()) { 2784 if (params->feed_list->empty()) {
2742 UMA_HISTOGRAM_TIMES("Gdata.InitialFeedLoadTime", 2785 UMA_HISTOGRAM_TIMES("Gdata.InitialFeedLoadTime",
2743 base::TimeTicks::Now() - start_time); 2786 base::TimeTicks::Now() - start_time);
2744 } 2787 }
2745 2788
2746 GDataFileError error = GDataToGDataFileError(status); 2789 GDataFileError error = GDataToGDataFileError(status);
2747 if (error == GDATA_FILE_OK && 2790 if (error == GDATA_FILE_OK &&
2748 (!data.get() || data->GetType() != Value::TYPE_DICTIONARY)) { 2791 (!data.get() || data->GetType() != Value::TYPE_DICTIONARY)) {
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
2797 2840
2798 // Check if we need to collect more data to complete the directory list. 2841 // Check if we need to collect more data to complete the directory list.
2799 if (params->should_fetch_multiple_feeds && has_next_feed_url && 2842 if (params->should_fetch_multiple_feeds && has_next_feed_url &&
2800 !next_feed_url.is_empty()) { 2843 !next_feed_url.is_empty()) {
2801 // Kick of the remaining part of the feeds. 2844 // Kick of the remaining part of the feeds.
2802 documents_service_->GetDocuments( 2845 documents_service_->GetDocuments(
2803 next_feed_url, 2846 next_feed_url,
2804 params->start_changestamp, 2847 params->start_changestamp,
2805 params->search_query, 2848 params->search_query,
2806 params->directory_resource_id, 2849 params->directory_resource_id,
2807 base::Bind(&GDataFileSystem::OnGetDocuments, 2850 base::Bind(&GDataWapiFeedLoader::OnGetDocuments,
2808 ui_weak_ptr_, 2851 weak_ptr_factory_.GetWeakPtr(),
2809 initial_origin, 2852 initial_origin,
2810 callback, 2853 callback,
2811 base::Owned( 2854 base::Owned(
2812 new GetDocumentsParams( 2855 new GetDocumentsParams(
2813 params->start_changestamp, 2856 params->start_changestamp,
2814 params->root_feed_changestamp, 2857 params->root_feed_changestamp,
2815 params->feed_list.release(), 2858 params->feed_list.release(),
2816 params->should_fetch_multiple_feeds, 2859 params->should_fetch_multiple_feeds,
2817 params->search_file_path, 2860 params->search_file_path,
2818 params->search_query, 2861 params->search_query,
2819 params->directory_resource_id, 2862 params->directory_resource_id,
2820 params->callback)), 2863 params->callback)),
2821 start_time)); 2864 start_time));
2822 return; 2865 return;
2823 } 2866 }
2824 2867
2825 UMA_HISTOGRAM_TIMES("Gdata.EntireFeedLoadTime", 2868 UMA_HISTOGRAM_TIMES("Gdata.EntireFeedLoadTime",
2826 base::TimeTicks::Now() - start_time); 2869 base::TimeTicks::Now() - start_time);
2827 2870
2828 if (!callback.is_null()) 2871 if (!callback.is_null())
2829 callback.Run(params, error); 2872 callback.Run(params, error);
2830 } 2873 }
2831 2874
2832 void GDataFileSystem::LoadRootFeedFromCache( 2875 void GDataWapiFeedLoader::LoadFromCache(
2833 bool should_load_from_server, 2876 bool should_load_from_server,
2834 const FilePath& search_file_path, 2877 const FilePath& search_file_path,
2835 const FindEntryCallback& callback) { 2878 const FindEntryCallback& callback) {
2836 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 2879 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2837 2880
2838 const FilePath path = 2881 const FilePath path =
2839 cache_->GetCacheDirectoryPath(GDataCache::CACHE_TYPE_META).Append( 2882 cache_->GetCacheDirectoryPath(GDataCache::CACHE_TYPE_META).Append(
2840 kFilesystemProtoFile); 2883 kFilesystemProtoFile);
2841 LoadRootFeedParams* params = new LoadRootFeedParams(search_file_path, 2884 LoadRootFeedParams* params = new LoadRootFeedParams(search_file_path,
2842 should_load_from_server, 2885 should_load_from_server,
2843 callback); 2886 callback);
2844 BrowserThread::GetBlockingPool()->PostTaskAndReply(FROM_HERE, 2887 BrowserThread::GetBlockingPool()->PostTaskAndReply(FROM_HERE,
2845 base::Bind(&LoadProtoOnBlockingPool, path, params), 2888 base::Bind(&LoadProtoOnBlockingPool, path, params),
2846 base::Bind(&GDataFileSystem::OnProtoLoaded, 2889 base::Bind(&GDataWapiFeedLoader::OnProtoLoaded,
2847 ui_weak_ptr_, 2890 weak_ptr_factory_.GetWeakPtr(),
2848 base::Owned(params))); 2891 base::Owned(params)));
2849 } 2892 }
2850 2893
2894 void GDataFileSystem::OnDirectoryChanged(const FilePath& directory_path) {
2895 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2896
2897 FOR_EACH_OBSERVER(GDataFileSystemInterface::Observer, observers_,
2898 OnDirectoryChanged(directory_path));
2899 }
2900
2901 void GDataFileSystem::OnDocumentFeedFetched(int num_accumulated_entries) {
2902 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2903
2904 FOR_EACH_OBSERVER(GDataFileSystemInterface::Observer, observers_,
2905 OnDocumentFeedFetched(num_accumulated_entries));
2906 }
2907
2908 void GDataFileSystem::OnFeedFromServerLoaded() {
2909 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2910
2911 FOR_EACH_OBSERVER(GDataFileSystemInterface::Observer, observers_,
2912 OnFeedFromServerLoaded());
2913 }
2914
2851 void GDataFileSystem::LoadRootFeedFromCacheForTesting() { 2915 void GDataFileSystem::LoadRootFeedFromCacheForTesting() {
2852 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 2916 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2853 2917
2854 LoadRootFeedFromCache( 2918 feed_loader_->LoadFromCache(
2855 false, // should_load_from_server. 2919 false, // should_load_from_server.
2856 // search_path doesn't matter if FindEntryCallback parameter is null . 2920 // search_path doesn't matter if FindEntryCallback parameter is null .
2857 FilePath(), 2921 FilePath(),
2858 FindEntryCallback()); 2922 FindEntryCallback());
2859 } 2923 }
2860 2924
2861 void GDataFileSystem::OnProtoLoaded(LoadRootFeedParams* params) { 2925 GDataFileError GDataFileSystem::UpdateFromFeedForTesting(
2926 const std::vector<DocumentFeed*>& feed_list,
2927 int start_changestamp,
2928 int root_feed_changestamp) {
2929 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2930
2931 return feed_loader_->UpdateFromFeed(feed_list,
2932 start_changestamp,
2933 root_feed_changestamp);
2934 }
2935
2936 void GDataWapiFeedLoader::OnProtoLoaded(LoadRootFeedParams* params) {
2862 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 2937 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2863 2938
2864 // If we have already received updates from the server, bail out. 2939 // If we have already received updates from the server, bail out.
2865 if (directory_service_->origin() == FROM_SERVER) 2940 if (directory_service_->origin() == FROM_SERVER)
2866 return; 2941 return;
2867 2942
2868 int local_changestamp = 0; 2943 int local_changestamp = 0;
2869 // Update directory structure only if everything is OK and we haven't yet 2944 // Update directory structure only if everything is OK and we haven't yet
2870 // received the feed from the server yet. 2945 // received the feed from the server yet.
2871 if (params->load_error == GDATA_FILE_OK) { 2946 if (params->load_error == GDATA_FILE_OK) {
2872 DVLOG(1) << "ParseFromString"; 2947 DVLOG(1) << "ParseFromString";
2873 if (directory_service_->ParseFromString(params->proto)) { 2948 if (directory_service_->ParseFromString(params->proto)) {
2874 directory_service_->set_last_serialized(params->last_modified); 2949 directory_service_->set_last_serialized(params->last_modified);
2875 directory_service_->set_serialized_size(params->proto.size()); 2950 directory_service_->set_serialized_size(params->proto.size());
2876 local_changestamp = directory_service_->largest_changestamp(); 2951 local_changestamp = directory_service_->largest_changestamp();
2877 } else { 2952 } else {
2878 params->load_error = GDATA_FILE_ERROR_FAILED; 2953 params->load_error = GDATA_FILE_ERROR_FAILED;
2879 LOG(WARNING) << "Parse of cached proto file failed"; 2954 LOG(WARNING) << "Parse of cached proto file failed";
2880 } 2955 }
2881 } 2956 }
2882 2957
2883 FindEntryCallback callback = params->callback; 2958 FindEntryCallback callback = params->callback;
2884 // If we got feed content from cache, try search over it. 2959 // If we got feed content from cache, try search over it.
2885 if (params->load_error == GDATA_FILE_OK && !callback.is_null()) { 2960 if (params->load_error == GDATA_FILE_OK && !callback.is_null()) {
2886 // Continue file content search operation if the delegate hasn't terminated 2961 // Continue file content search operation if the delegate hasn't terminated
2887 // this search branch already. 2962 // this search branch already.
2888 FindEntryByPathSyncOnUIThread(params->search_file_path, callback); 2963 directory_service_->FindEntryByPathAndRunSync(params->search_file_path,
2964 callback);
2889 callback.Reset(); 2965 callback.Reset();
2890 } 2966 }
2891 2967
2892 if (!params->should_load_from_server) 2968 if (!params->should_load_from_server)
2893 return; 2969 return;
2894 2970
2895 // Decide the |initial_origin| to pass to ReloadFeedFromServerIfNeeded(). 2971 // Decide the |initial_origin| to pass to ReloadFromServerIfNeeded().
2896 // This is used to restore directory content origin to its initial value when 2972 // This is used to restore directory content origin to its initial value when
2897 // we fail to retrieve the feed from server. 2973 // we fail to retrieve the feed from server.
2898 // By default, if directory content is not yet initialized, restore content 2974 // By default, if directory content is not yet initialized, restore content
2899 // origin to UNINITIALIZED in case of failure. 2975 // origin to UNINITIALIZED in case of failure.
2900 ContentOrigin initial_origin = UNINITIALIZED; 2976 ContentOrigin initial_origin = UNINITIALIZED;
2901 if (directory_service_->origin() != INITIALIZING) { 2977 if (directory_service_->origin() != INITIALIZING) {
2902 // If directory content is already initialized, restore content origin 2978 // If directory content is already initialized, restore content origin
2903 // to FROM_CACHE in case of failure. 2979 // to FROM_CACHE in case of failure.
2904 initial_origin = FROM_CACHE; 2980 initial_origin = FROM_CACHE;
2905 directory_service_->set_origin(REFRESHING); 2981 directory_service_->set_origin(REFRESHING);
2906 } 2982 }
2907 2983
2908 // Kick of the retrieval of the feed from server. If we have previously 2984 // Kick of the retrieval of the feed from server. If we have previously
2909 // |reported| to the original callback, then we just need to refresh the 2985 // |reported| to the original callback, then we just need to refresh the
2910 // content without continuing search upon operation completion. 2986 // content without continuing search upon operation completion.
2911 ReloadFeedFromServerIfNeeded(initial_origin, 2987 ReloadFromServerIfNeeded(initial_origin,
2912 local_changestamp, 2988 local_changestamp,
2913 params->search_file_path, 2989 params->search_file_path,
2914 callback); 2990 callback);
2915 } 2991 }
2916 2992
2917 void GDataFileSystem::SaveFileSystemAsProto() { 2993 void GDataWapiFeedLoader::SaveFileSystemAsProto() {
2918 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 2994 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2919 2995
2920 DVLOG(1) << "SaveFileSystemAsProto"; 2996 DVLOG(1) << "SaveFileSystemAsProto";
2921 2997
2922 if (!ShouldSerializeFileSystemNow(directory_service_->serialized_size(), 2998 if (!ShouldSerializeFileSystemNow(directory_service_->serialized_size(),
2923 directory_service_->last_serialized())) { 2999 directory_service_->last_serialized())) {
2924 return; 3000 return;
2925 } 3001 }
2926 3002
2927 const FilePath path = 3003 const FilePath path =
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after
3184 // through the file name de-duplication. 3260 // through the file name de-duplication.
3185 // TODO(achuith/satorux/zel): This code is fragile. The title has been 3261 // TODO(achuith/satorux/zel): This code is fragile. The title has been
3186 // changed, but not the file_name. TakeEntry removes the child based on the 3262 // changed, but not the file_name. TakeEntry removes the child based on the
3187 // old file_name, and then re-adds the child by first assigning the new title 3263 // old file_name, and then re-adds the child by first assigning the new title
3188 // to file_name. http://crbug.com/30157 3264 // to file_name. http://crbug.com/30157
3189 if (!entry->parent()->TakeEntry(entry)) 3265 if (!entry->parent()->TakeEntry(entry))
3190 return GDATA_FILE_ERROR_FAILED; 3266 return GDATA_FILE_ERROR_FAILED;
3191 3267
3192 *updated_file_path = entry->GetFilePath(); 3268 *updated_file_path = entry->GetFilePath();
3193 3269
3194 FOR_EACH_OBSERVER(Observer, observers_, 3270 FOR_EACH_OBSERVER(GDataFileSystemInterface::Observer, observers_,
3195 OnDirectoryChanged(updated_file_path->DirName())); 3271 OnDirectoryChanged(updated_file_path->DirName()));
3196 return GDATA_FILE_OK; 3272 return GDATA_FILE_OK;
3197 } 3273 }
3198 3274
3199 GDataFileError GDataFileSystem::AddEntryToDirectoryOnFilesystem( 3275 GDataFileError GDataFileSystem::AddEntryToDirectoryOnFilesystem(
3200 GDataEntry* entry, const FilePath& dir_path) { 3276 GDataEntry* entry, const FilePath& dir_path) {
3201 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 3277 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
3202 DCHECK(entry); 3278 DCHECK(entry);
3203 3279
3204 GDataEntry* dir_entry = directory_service_->FindEntryByPathSync(dir_path); 3280 GDataEntry* dir_entry = directory_service_->FindEntryByPathSync(dir_path);
3205 if (!dir_entry) 3281 if (!dir_entry)
3206 return GDATA_FILE_ERROR_NOT_FOUND; 3282 return GDATA_FILE_ERROR_NOT_FOUND;
3207 3283
3208 GDataDirectory* dir = dir_entry->AsGDataDirectory(); 3284 GDataDirectory* dir = dir_entry->AsGDataDirectory();
3209 if (!dir) 3285 if (!dir)
3210 return GDATA_FILE_ERROR_NOT_A_DIRECTORY; 3286 return GDATA_FILE_ERROR_NOT_A_DIRECTORY;
3211 3287
3212 if (!dir->TakeEntry(entry)) 3288 if (!dir->TakeEntry(entry))
3213 return GDATA_FILE_ERROR_FAILED; 3289 return GDATA_FILE_ERROR_FAILED;
3214 3290
3215 FOR_EACH_OBSERVER(Observer, observers_, OnDirectoryChanged(dir_path)); 3291 FOR_EACH_OBSERVER(GDataFileSystemInterface::Observer, observers_,
3292 OnDirectoryChanged(dir_path));
3216 return GDATA_FILE_OK; 3293 return GDATA_FILE_OK;
3217 } 3294 }
3218 3295
3219 GDataFileError GDataFileSystem::RemoveEntryFromDirectoryOnFilesystem( 3296 GDataFileError GDataFileSystem::RemoveEntryFromDirectoryOnFilesystem(
3220 const FilePath& file_path, const FilePath& dir_path, 3297 const FilePath& file_path, const FilePath& dir_path,
3221 FilePath* updated_file_path) { 3298 FilePath* updated_file_path) {
3222 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 3299 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
3223 DCHECK(updated_file_path); 3300 DCHECK(updated_file_path);
3224 3301
3225 GDataEntry* entry = directory_service_->FindEntryByPathSync(file_path); 3302 GDataEntry* entry = directory_service_->FindEntryByPathSync(file_path);
3226 if (!entry) 3303 if (!entry)
3227 return GDATA_FILE_ERROR_NOT_FOUND; 3304 return GDATA_FILE_ERROR_NOT_FOUND;
3228 3305
3229 GDataEntry* dir = directory_service_->FindEntryByPathSync(dir_path); 3306 GDataEntry* dir = directory_service_->FindEntryByPathSync(dir_path);
3230 if (!dir) 3307 if (!dir)
3231 return GDATA_FILE_ERROR_NOT_FOUND; 3308 return GDATA_FILE_ERROR_NOT_FOUND;
3232 3309
3233 if (!dir->AsGDataDirectory()) 3310 if (!dir->AsGDataDirectory())
3234 return GDATA_FILE_ERROR_NOT_A_DIRECTORY; 3311 return GDATA_FILE_ERROR_NOT_A_DIRECTORY;
3235 3312
3236 DCHECK_EQ(dir->AsGDataDirectory(), entry->parent()); 3313 DCHECK_EQ(dir->AsGDataDirectory(), entry->parent());
3237 3314
3238 if (!directory_service_->root()->TakeEntry(entry)) 3315 if (!directory_service_->root()->TakeEntry(entry))
3239 return GDATA_FILE_ERROR_FAILED; 3316 return GDATA_FILE_ERROR_FAILED;
3240 3317
3241 *updated_file_path = entry->GetFilePath(); 3318 *updated_file_path = entry->GetFilePath();
3242 3319
3243 FOR_EACH_OBSERVER(Observer, observers_, 3320 FOR_EACH_OBSERVER(GDataFileSystemInterface::Observer, observers_,
3244 OnDirectoryChanged(updated_file_path->DirName())); 3321 OnDirectoryChanged(updated_file_path->DirName()));
3245 return GDATA_FILE_OK; 3322 return GDATA_FILE_OK;
3246 } 3323 }
3247 3324
3248 GDataFileError GDataFileSystem::RemoveEntryFromFileSystem( 3325 GDataFileError GDataFileSystem::RemoveEntryFromFileSystem(
3249 const FilePath& file_path) { 3326 const FilePath& file_path) {
3250 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 3327 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
3251 3328
3252 std::string resource_id; 3329 std::string resource_id;
3253 GDataFileError error = RemoveEntryFromGData(file_path, &resource_id); 3330 GDataFileError error = RemoveEntryFromGData(file_path, &resource_id);
3254 if (error != GDATA_FILE_OK) 3331 if (error != GDATA_FILE_OK)
3255 return error; 3332 return error;
3256 3333
3257 // If resource_id is not empty, remove its corresponding file from cache. 3334 // If resource_id is not empty, remove its corresponding file from cache.
3258 if (!resource_id.empty()) 3335 if (!resource_id.empty())
3259 cache_->RemoveOnUIThread(resource_id, CacheOperationCallback()); 3336 cache_->RemoveOnUIThread(resource_id, CacheOperationCallback());
3260 3337
3261 return GDATA_FILE_OK; 3338 return GDATA_FILE_OK;
3262 } 3339 }
3263 3340
3264 GDataFileError GDataFileSystem::UpdateFromFeed( 3341 GDataFileError GDataWapiFeedLoader::UpdateFromFeed(
3265 const std::vector<DocumentFeed*>& feed_list, 3342 const std::vector<DocumentFeed*>& feed_list,
3266 int start_changestamp, 3343 int start_changestamp,
3267 int root_feed_changestamp) { 3344 int root_feed_changestamp) {
3268 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 3345 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
3269 DVLOG(1) << "Updating directory with a feed"; 3346 DVLOG(1) << "Updating directory with a feed";
3270 3347
3271 std::set<FilePath> changed_dirs; 3348 std::set<FilePath> changed_dirs;
3272 3349
3273 GDataWapiFeedProcessor feed_processor(directory_service_.get()); 3350 GDataWapiFeedProcessor feed_processor(directory_service_);
3274 const GDataFileError error = feed_processor.ApplyFeeds( 3351 const GDataFileError error = feed_processor.ApplyFeeds(
3275 feed_list, 3352 feed_list,
3276 start_changestamp, 3353 start_changestamp,
3277 root_feed_changestamp, 3354 root_feed_changestamp,
3278 &changed_dirs); 3355 &changed_dirs);
3279 3356
3280 // Don't send directory content change notification while performing 3357 // Don't send directory content change notification while performing
3281 // the initial content retrieval. 3358 // the initial content retrieval.
3282 const bool should_notify_directory_changed = (start_changestamp != 0); 3359 const bool should_notify_directory_changed = (start_changestamp != 0);
3283 if (should_notify_directory_changed) { 3360 if (should_notify_directory_changed) {
3284 for (std::set<FilePath>::iterator dir_iter = changed_dirs.begin(); 3361 for (std::set<FilePath>::iterator dir_iter = changed_dirs.begin();
3285 dir_iter != changed_dirs.end(); ++dir_iter) { 3362 dir_iter != changed_dirs.end(); ++dir_iter) {
3286 FOR_EACH_OBSERVER(Observer, observers_, OnDirectoryChanged(*dir_iter)); 3363 FOR_EACH_OBSERVER(Observer, observers_,
3364 OnDirectoryChanged(*dir_iter));
3287 } 3365 }
3288 } 3366 }
3289 3367
3290 return error; 3368 return error;
3291 } 3369 }
3292 3370
3293 3371
3294 // static 3372 // static
3295 void GDataFileSystem::RemoveStaleEntryOnUpload(const std::string& resource_id, 3373 void GDataFileSystem::RemoveStaleEntryOnUpload(const std::string& resource_id,
3296 GDataDirectory* parent_dir, 3374 GDataDirectory* parent_dir,
(...skipping 11 matching lines...) Expand all
3308 const FindEntryCallback& callback, 3386 const FindEntryCallback& callback,
3309 GDataFileError error, 3387 GDataFileError error,
3310 GDataEntry* entry) { 3388 GDataEntry* entry) {
3311 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 3389 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
3312 3390
3313 DVLOG(1) << "Initial load finished"; 3391 DVLOG(1) << "Initial load finished";
3314 if (!callback.is_null()) 3392 if (!callback.is_null())
3315 callback.Run(error, entry); 3393 callback.Run(error, entry);
3316 3394
3317 // Notify the observers that root directory has been initialized. 3395 // Notify the observers that root directory has been initialized.
3318 FOR_EACH_OBSERVER(Observer, observers_, OnInitialLoadFinished()); 3396 FOR_EACH_OBSERVER(GDataFileSystemInterface::Observer, observers_,
3397 OnInitialLoadFinished());
3319 } 3398 }
3320 3399
3321 GDataFileError GDataFileSystem::AddNewDirectory( 3400 GDataFileError GDataFileSystem::AddNewDirectory(
3322 const FilePath& directory_path, base::Value* entry_value) { 3401 const FilePath& directory_path, base::Value* entry_value) {
3323 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 3402 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
3324 3403
3325 if (!entry_value) 3404 if (!entry_value)
3326 return GDATA_FILE_ERROR_FAILED; 3405 return GDATA_FILE_ERROR_FAILED;
3327 3406
3328 scoped_ptr<DocumentEntry> doc_entry(DocumentEntry::CreateFrom(*entry_value)); 3407 scoped_ptr<DocumentEntry> doc_entry(DocumentEntry::CreateFrom(*entry_value));
(...skipping 13 matching lines...) Expand all
3342 if (!parent_dir) 3421 if (!parent_dir)
3343 return GDATA_FILE_ERROR_FAILED; 3422 return GDATA_FILE_ERROR_FAILED;
3344 3423
3345 GDataEntry* new_entry = GDataEntry::FromDocumentEntry( 3424 GDataEntry* new_entry = GDataEntry::FromDocumentEntry(
3346 parent_dir, doc_entry.get(), directory_service_.get()); 3425 parent_dir, doc_entry.get(), directory_service_.get());
3347 if (!new_entry) 3426 if (!new_entry)
3348 return GDATA_FILE_ERROR_FAILED; 3427 return GDATA_FILE_ERROR_FAILED;
3349 3428
3350 parent_dir->AddEntry(new_entry); 3429 parent_dir->AddEntry(new_entry);
3351 3430
3352 FOR_EACH_OBSERVER(Observer, observers_, OnDirectoryChanged(directory_path)); 3431 FOR_EACH_OBSERVER(GDataFileSystemInterface::Observer, observers_,
3432 OnDirectoryChanged(directory_path));
3353 return GDATA_FILE_OK; 3433 return GDATA_FILE_OK;
3354 } 3434 }
3355 3435
3356 GDataFileSystem::FindMissingDirectoryResult 3436 GDataFileSystem::FindMissingDirectoryResult
3357 GDataFileSystem::FindFirstMissingParentDirectory( 3437 GDataFileSystem::FindFirstMissingParentDirectory(
3358 const FilePath& directory_path, 3438 const FilePath& directory_path,
3359 GURL* last_dir_content_url, 3439 GURL* last_dir_content_url,
3360 FilePath* first_missing_parent_path) { 3440 FilePath* first_missing_parent_path) {
3361 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 3441 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
3362 3442
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
3404 3484
3405 // If it's a file (only files have resource id), get its resource id so that 3485 // If it's a file (only files have resource id), get its resource id so that
3406 // we can remove it after releasing the auto lock. 3486 // we can remove it after releasing the auto lock.
3407 if (entry->AsGDataFile()) 3487 if (entry->AsGDataFile())
3408 *resource_id = entry->AsGDataFile()->resource_id(); 3488 *resource_id = entry->AsGDataFile()->resource_id();
3409 3489
3410 GDataDirectory* parent_dir = entry->parent(); 3490 GDataDirectory* parent_dir = entry->parent();
3411 if (!parent_dir->RemoveEntry(entry)) 3491 if (!parent_dir->RemoveEntry(entry))
3412 return GDATA_FILE_ERROR_NOT_FOUND; 3492 return GDATA_FILE_ERROR_NOT_FOUND;
3413 3493
3414 FOR_EACH_OBSERVER(Observer, observers_, 3494 FOR_EACH_OBSERVER(GDataFileSystemInterface::Observer, observers_,
3415 OnDirectoryChanged(parent_dir->GetFilePath())); 3495 OnDirectoryChanged(parent_dir->GetFilePath()));
3416 return GDATA_FILE_OK; 3496 return GDATA_FILE_OK;
3417 } 3497 }
3418 3498
3419 void GDataFileSystem::AddUploadedFile( 3499 void GDataFileSystem::AddUploadedFile(
3420 UploadMode upload_mode, 3500 UploadMode upload_mode,
3421 const FilePath& virtual_dir_path, 3501 const FilePath& virtual_dir_path,
3422 scoped_ptr<DocumentEntry> entry, 3502 scoped_ptr<DocumentEntry> entry,
3423 const FilePath& file_content_path, 3503 const FilePath& file_content_path,
3424 GDataCache::FileOperationType cache_operation, 3504 GDataCache::FileOperationType cache_operation,
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
3482 directory_service_->GetEntryByResourceIdAsync(resource_id, 3562 directory_service_->GetEntryByResourceIdAsync(resource_id,
3483 base::Bind(&RemoveStaleEntryOnUpload, resource_id, parent_dir)); 3563 base::Bind(&RemoveStaleEntryOnUpload, resource_id, parent_dir));
3484 } 3564 }
3485 3565
3486 GDataFile* file = new_entry->AsGDataFile(); 3566 GDataFile* file = new_entry->AsGDataFile();
3487 DCHECK(file); 3567 DCHECK(file);
3488 const std::string& resource_id = file->resource_id(); 3568 const std::string& resource_id = file->resource_id();
3489 const std::string& md5 = file->file_md5(); 3569 const std::string& md5 = file->file_md5();
3490 parent_dir->AddEntry(new_entry.release()); 3570 parent_dir->AddEntry(new_entry.release());
3491 3571
3492 FOR_EACH_OBSERVER(Observer, observers_, 3572 FOR_EACH_OBSERVER(GDataFileSystemInterface::Observer, observers_,
3493 OnDirectoryChanged(virtual_dir_path)); 3573 OnDirectoryChanged(virtual_dir_path));
3494 3574
3495 if (upload_mode == UPLOAD_NEW_FILE) { 3575 if (upload_mode == UPLOAD_NEW_FILE) {
3496 // Add the file to the cache if we have uploaded a new file. 3576 // Add the file to the cache if we have uploaded a new file.
3497 cache_->StoreOnUIThread(resource_id, 3577 cache_->StoreOnUIThread(resource_id,
3498 md5, 3578 md5,
3499 file_content_path, 3579 file_content_path,
3500 cache_operation, 3580 cache_operation,
3501 base::Bind(&OnCacheUpdatedForAddUploadedFile, 3581 base::Bind(&OnCacheUpdatedForAddUploadedFile,
3502 callback)); 3582 callback));
(...skipping 28 matching lines...) Expand all
3531 void GDataFileSystem::SetHideHostedDocuments(bool hide) { 3611 void GDataFileSystem::SetHideHostedDocuments(bool hide) {
3532 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 3612 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
3533 3613
3534 if (hide == hide_hosted_docs_) 3614 if (hide == hide_hosted_docs_)
3535 return; 3615 return;
3536 3616
3537 hide_hosted_docs_ = hide; 3617 hide_hosted_docs_ = hide;
3538 const FilePath root_path = directory_service_->root()->GetFilePath(); 3618 const FilePath root_path = directory_service_->root()->GetFilePath();
3539 3619
3540 // Kick off directory refresh when this setting changes. 3620 // Kick off directory refresh when this setting changes.
3541 FOR_EACH_OBSERVER(Observer, observers_, OnDirectoryChanged(root_path)); 3621 FOR_EACH_OBSERVER(GDataFileSystemInterface::Observer, observers_,
3622 OnDirectoryChanged(root_path));
3542 } 3623 }
3543 3624
3544 //============= GDataFileSystem: internal helper functions ===================== 3625 //============= GDataFileSystem: internal helper functions =====================
3545 3626
3546 void GDataFileSystem::InitializePreferenceObserver() { 3627 void GDataFileSystem::InitializePreferenceObserver() {
3547 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 3628 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
3548 3629
3549 pref_registrar_.reset(new PrefChangeRegistrar()); 3630 pref_registrar_.reset(new PrefChangeRegistrar());
3550 pref_registrar_->Init(profile_->GetPrefs()); 3631 pref_registrar_->Init(profile_->GetPrefs());
3551 pref_registrar_->Add(prefs::kDisableGDataHostedFiles, this); 3632 pref_registrar_->Add(prefs::kDisableGDataHostedFiles, this);
(...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after
3970 } 4051 }
3971 4052
3972 PlatformFileInfoProto entry_file_info; 4053 PlatformFileInfoProto entry_file_info;
3973 GDataEntry::ConvertPlatformFileInfoToProto(*file_info, &entry_file_info); 4054 GDataEntry::ConvertPlatformFileInfoToProto(*file_info, &entry_file_info);
3974 *entry_proto->mutable_file_info() = entry_file_info; 4055 *entry_proto->mutable_file_info() = entry_file_info;
3975 if (!callback.is_null()) 4056 if (!callback.is_null())
3976 callback.Run(GDATA_FILE_OK, entry_proto.Pass()); 4057 callback.Run(GDATA_FILE_OK, entry_proto.Pass());
3977 } 4058 }
3978 4059
3979 } // namespace gdata 4060 } // namespace gdata
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/gdata/gdata_file_system.h ('k') | chrome/browser/chromeos/gdata/gdata_file_system_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698