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_file_system.h" | 5 #include "chrome/browser/chromeos/gdata/gdata_file_system.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <sys/stat.h> | 8 #include <sys/stat.h> |
9 | 9 |
10 #include <set> | 10 #include <set> |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
70 const FilePath::CharType kGDataCacheTmpDownloadsDir[] = | 70 const FilePath::CharType kGDataCacheTmpDownloadsDir[] = |
71 FILE_PATH_LITERAL("tmp/downloads"); | 71 FILE_PATH_LITERAL("tmp/downloads"); |
72 const FilePath::CharType kGDataCacheTmpDocumentsDir[] = | 72 const FilePath::CharType kGDataCacheTmpDocumentsDir[] = |
73 FILE_PATH_LITERAL("tmp/documents"); | 73 FILE_PATH_LITERAL("tmp/documents"); |
74 const FilePath::CharType kAccountMetadataFile[] = | 74 const FilePath::CharType kAccountMetadataFile[] = |
75 FILE_PATH_LITERAL("account_metadata.json"); | 75 FILE_PATH_LITERAL("account_metadata.json"); |
76 const FilePath::CharType kFilesystemProtoFile[] = | 76 const FilePath::CharType kFilesystemProtoFile[] = |
77 FILE_PATH_LITERAL("file_system.pb"); | 77 FILE_PATH_LITERAL("file_system.pb"); |
78 const FilePath::CharType kSymLinkToDevNull[] = FILE_PATH_LITERAL("/dev/null"); | 78 const FilePath::CharType kSymLinkToDevNull[] = FILE_PATH_LITERAL("/dev/null"); |
79 | 79 |
80 // GData update check interval (in seconds). | |
81 #ifndef NDEBUG | |
82 const int kGDataUpdateCheckIntervalInSec = 5; | |
83 #else | |
84 const int kGDataUpdateCheckIntervalInSec = 60; | |
85 #endif | |
86 | |
80 // Schedule for dumping root file system proto buffers to disk depending its | 87 // Schedule for dumping root file system proto buffers to disk depending its |
81 // total protobuffer size in MB. | 88 // total protobuffer size in MB. |
82 typedef struct { | 89 typedef struct { |
83 double size; | 90 double size; |
84 int timeout; | 91 int timeout; |
85 } SerializationTimetable; | 92 } SerializationTimetable; |
86 | 93 |
87 SerializationTimetable kSerializeTimetable[] = { | 94 SerializationTimetable kSerializeTimetable[] = { |
88 #ifndef NDEBUG | 95 #ifndef NDEBUG |
89 {0.5, 0}, // Less than 0.5MB, dump immediately. | 96 {0.5, 0}, // Less than 0.5MB, dump immediately. |
(...skipping 831 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
921 // GDataFileSystem class implementatsion. | 928 // GDataFileSystem class implementatsion. |
922 | 929 |
923 GDataFileSystem::GDataFileSystem(Profile* profile, | 930 GDataFileSystem::GDataFileSystem(Profile* profile, |
924 DocumentsServiceInterface* documents_service) | 931 DocumentsServiceInterface* documents_service) |
925 : profile_(profile), | 932 : profile_(profile), |
926 documents_service_(documents_service), | 933 documents_service_(documents_service), |
927 on_io_completed_(new base::WaitableEvent( | 934 on_io_completed_(new base::WaitableEvent( |
928 true /* manual reset */, true /* initially signaled */)), | 935 true /* manual reset */, true /* initially signaled */)), |
929 cache_initialization_started_(false), | 936 cache_initialization_started_(false), |
930 num_pending_tasks_(0), | 937 num_pending_tasks_(0), |
938 num_update_requests_(0), | |
939 update_timer_(true /* retain_user_task */, true /* is_repeating */), | |
931 hide_hosted_docs_(false), | 940 hide_hosted_docs_(false), |
932 ui_weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST( | 941 ui_weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST( |
933 new base::WeakPtrFactory<GDataFileSystem>(this))), | 942 new base::WeakPtrFactory<GDataFileSystem>(this))), |
934 ui_weak_ptr_(ui_weak_ptr_factory_->GetWeakPtr()), | 943 ui_weak_ptr_(ui_weak_ptr_factory_->GetWeakPtr()), |
935 sequence_token_(BrowserThread::GetBlockingPool()->GetSequenceToken()) { | 944 sequence_token_(BrowserThread::GetBlockingPool()->GetSequenceToken()) { |
936 // Should be created from the file browser extension API on UI thread. | 945 // Should be created from the file browser extension API on UI thread. |
937 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 946 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
938 } | 947 } |
939 | 948 |
940 void GDataFileSystem::Initialize() { | 949 void GDataFileSystem::Initialize() { |
(...skipping 13 matching lines...) Expand all Loading... | |
954 documents_service_->Initialize(profile_); | 963 documents_service_->Initialize(profile_); |
955 | 964 |
956 root_.reset(new GDataRootDirectory); | 965 root_.reset(new GDataRootDirectory); |
957 | 966 |
958 PrefService* pref_service = profile_->GetPrefs(); | 967 PrefService* pref_service = profile_->GetPrefs(); |
959 hide_hosted_docs_ = pref_service->GetBoolean(prefs::kDisableGDataHostedFiles); | 968 hide_hosted_docs_ = pref_service->GetBoolean(prefs::kDisableGDataHostedFiles); |
960 | 969 |
961 InitializePreferenceObserver(); | 970 InitializePreferenceObserver(); |
962 } | 971 } |
963 | 972 |
973 void GDataFileSystem::CheckForUpdates() { | |
974 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
975 ContentOrigin initial_origin = root_->origin(); | |
976 if (initial_origin == FROM_SERVER) { | |
977 root_->set_origin(REFRESHING); | |
978 ReloadFeedFromServerIfNeeded(initial_origin, | |
979 root_->largest_changestamp(), | |
980 root_->GetFilePath(), | |
981 gdata::FindEntryCallback()); | |
982 } | |
983 } | |
984 | |
964 bool GDataFileSystem::SetCacheRootPathForTesting(const FilePath& root_path) { | 985 bool GDataFileSystem::SetCacheRootPathForTesting(const FilePath& root_path) { |
965 if (cache_initialization_started_) | 986 if (cache_initialization_started_) |
966 return false; | 987 return false; |
967 cache_paths_.clear(); | 988 cache_paths_.clear(); |
968 SetCachePaths(root_path); | 989 SetCachePaths(root_path); |
969 return true; | 990 return true; |
970 } | 991 } |
971 | 992 |
972 GDataFileSystem::~GDataFileSystem() { | 993 GDataFileSystem::~GDataFileSystem() { |
973 // This should be called from UI thread, from GDataSystemService shutdown. | 994 // This should be called from UI thread, from GDataSystemService shutdown. |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1009 } | 1030 } |
1010 | 1031 |
1011 void GDataFileSystem::AddObserver(Observer* observer) { | 1032 void GDataFileSystem::AddObserver(Observer* observer) { |
1012 observers_.AddObserver(observer); | 1033 observers_.AddObserver(observer); |
1013 } | 1034 } |
1014 | 1035 |
1015 void GDataFileSystem::RemoveObserver(Observer* observer) { | 1036 void GDataFileSystem::RemoveObserver(Observer* observer) { |
1016 observers_.RemoveObserver(observer); | 1037 observers_.RemoveObserver(observer); |
1017 } | 1038 } |
1018 | 1039 |
1040 void GDataFileSystem::RequestStartUpdates() { | |
1041 // Perform an immediate check for updates. | |
1042 CheckForUpdates(); | |
1043 ++num_update_requests_; | |
zel
2012/05/03 19:54:30
You should move num_update_requests_ logic to File
hshi
2012/05/03 21:17:49
Done.
| |
1044 if (num_update_requests_ == 1) { | |
1045 // If this is the first StartUpdate request, start timer to periodically | |
1046 // check for updates at the specified intervals. | |
1047 update_timer_.Start(FROM_HERE, | |
1048 base::TimeDelta::FromSeconds( | |
1049 kGDataUpdateCheckIntervalInSec), | |
1050 base::Bind(&GDataFileSystem::CheckForUpdates, | |
1051 ui_weak_ptr_)); | |
1052 } | |
1053 } | |
1054 | |
1055 void GDataFileSystem::RequestStopUpdates() { | |
1056 DCHECK_LE(0, num_update_requests_); | |
1057 --num_update_requests_; | |
1058 if (num_update_requests_ == 0) | |
1059 update_timer_.Stop(); | |
1060 } | |
1061 | |
1019 void GDataFileSystem::Authenticate(const AuthStatusCallback& callback) { | 1062 void GDataFileSystem::Authenticate(const AuthStatusCallback& callback) { |
1020 // TokenFetcher, used in DocumentsService, must be run on UI thread. | 1063 // TokenFetcher, used in DocumentsService, must be run on UI thread. |
1021 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 1064 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
1022 | 1065 |
1023 documents_service_->Authenticate(callback); | 1066 documents_service_->Authenticate(callback); |
1024 } | 1067 } |
1025 | 1068 |
1026 void GDataFileSystem::FindEntryByResourceIdSync( | 1069 void GDataFileSystem::FindEntryByResourceIdSync( |
1027 const std::string& resource_id, | 1070 const std::string& resource_id, |
1028 FindEntryDelegate* delegate) { | 1071 FindEntryDelegate* delegate) { |
(...skipping 30 matching lines...) Expand all Loading... | |
1059 callback))); | 1102 callback))); |
1060 return; | 1103 return; |
1061 } else if (root_->origin() == UNINITIALIZED) { | 1104 } else if (root_->origin() == UNINITIALIZED) { |
1062 // Load root feed from this disk cache. Upon completion, kick off server | 1105 // Load root feed from this disk cache. Upon completion, kick off server |
1063 // fetching. | 1106 // fetching. |
1064 root_->set_origin(INITIALIZING); | 1107 root_->set_origin(INITIALIZING); |
1065 LoadRootFeedFromCache(true, // should_load_from_server | 1108 LoadRootFeedFromCache(true, // should_load_from_server |
1066 search_file_path, | 1109 search_file_path, |
1067 callback); | 1110 callback); |
1068 return; | 1111 return; |
1069 } else if (root_->NeedsRefresh()) { | |
1070 // If content is stale or from disk from cache, fetch content from | |
1071 // the server. | |
1072 ContentOrigin initial_origin = root_->origin(); | |
1073 root_->set_origin(REFRESHING); | |
1074 ReloadFeedFromServerIfNeeded(initial_origin, | |
1075 root_->largest_changestamp(), | |
1076 search_file_path, | |
1077 callback); | |
1078 return; | |
1079 } | 1112 } |
1080 | 1113 |
1081 // Post a task to the same thread, rather than calling it here, as | 1114 // Post a task to the same thread, rather than calling it here, as |
1082 // FindEntryByPathAsync() is asynchronous. | 1115 // FindEntryByPathAsync() is asynchronous. |
1083 base::MessageLoopProxy::current()->PostTask( | 1116 base::MessageLoopProxy::current()->PostTask( |
1084 FROM_HERE, | 1117 FROM_HERE, |
1085 base::Bind(&GDataFileSystem::FindEntryByPathSyncOnUIThread, | 1118 base::Bind(&GDataFileSystem::FindEntryByPathSyncOnUIThread, |
1086 ui_weak_ptr_, | 1119 ui_weak_ptr_, |
1087 search_file_path, | 1120 search_file_path, |
1088 callback)); | 1121 callback)); |
(...skipping 3682 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
4771 pref_registrar_->Init(profile_->GetPrefs()); | 4804 pref_registrar_->Init(profile_->GetPrefs()); |
4772 pref_registrar_->Add(prefs::kDisableGDataHostedFiles, this); | 4805 pref_registrar_->Add(prefs::kDisableGDataHostedFiles, this); |
4773 } | 4806 } |
4774 | 4807 |
4775 void SetFreeDiskSpaceGetterForTesting(FreeDiskSpaceGetterInterface* getter) { | 4808 void SetFreeDiskSpaceGetterForTesting(FreeDiskSpaceGetterInterface* getter) { |
4776 delete global_free_disk_getter_for_testing; // Safe to delete NULL; | 4809 delete global_free_disk_getter_for_testing; // Safe to delete NULL; |
4777 global_free_disk_getter_for_testing = getter; | 4810 global_free_disk_getter_for_testing = getter; |
4778 } | 4811 } |
4779 | 4812 |
4780 } // namespace gdata | 4813 } // namespace gdata |
OLD | NEW |