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_file_watches_(0), | |
931 hide_hosted_docs_(false), | 939 hide_hosted_docs_(false), |
932 ui_weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST( | 940 ui_weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST( |
933 new base::WeakPtrFactory<GDataFileSystem>(this))), | 941 new base::WeakPtrFactory<GDataFileSystem>(this))), |
934 ui_weak_ptr_(ui_weak_ptr_factory_->GetWeakPtr()), | 942 ui_weak_ptr_(ui_weak_ptr_factory_->GetWeakPtr()), |
935 sequence_token_(BrowserThread::GetBlockingPool()->GetSequenceToken()) { | 943 sequence_token_(BrowserThread::GetBlockingPool()->GetSequenceToken()) { |
936 // Should be created from the file browser extension API on UI thread. | 944 // Should be created from the file browser extension API on UI thread. |
937 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 945 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
938 } | 946 } |
939 | 947 |
940 void GDataFileSystem::Initialize() { | 948 void GDataFileSystem::Initialize() { |
(...skipping 13 matching lines...) Expand all Loading... | |
954 documents_service_->Initialize(profile_); | 962 documents_service_->Initialize(profile_); |
955 | 963 |
956 root_.reset(new GDataRootDirectory); | 964 root_.reset(new GDataRootDirectory); |
957 | 965 |
958 PrefService* pref_service = profile_->GetPrefs(); | 966 PrefService* pref_service = profile_->GetPrefs(); |
959 hide_hosted_docs_ = pref_service->GetBoolean(prefs::kDisableGDataHostedFiles); | 967 hide_hosted_docs_ = pref_service->GetBoolean(prefs::kDisableGDataHostedFiles); |
960 | 968 |
961 InitializePreferenceObserver(); | 969 InitializePreferenceObserver(); |
962 } | 970 } |
963 | 971 |
972 void GDataFileSystem::CheckForUpdates() { | |
973 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
974 ContentOrigin initial_origin = root_->origin(); | |
975 if (initial_origin == FROM_SERVER) { | |
976 root_->set_origin(REFRESHING); | |
977 ReloadFeedFromServerIfNeeded(initial_origin, | |
978 root_->largest_changestamp(), | |
979 root_->GetFilePath(), | |
980 gdata::FindEntryCallback()); | |
981 } | |
982 } | |
983 | |
964 bool GDataFileSystem::SetCacheRootPathForTesting(const FilePath& root_path) { | 984 bool GDataFileSystem::SetCacheRootPathForTesting(const FilePath& root_path) { |
965 if (cache_initialization_started_) | 985 if (cache_initialization_started_) |
966 return false; | 986 return false; |
967 cache_paths_.clear(); | 987 cache_paths_.clear(); |
968 SetCachePaths(root_path); | 988 SetCachePaths(root_path); |
969 return true; | 989 return true; |
970 } | 990 } |
971 | 991 |
972 GDataFileSystem::~GDataFileSystem() { | 992 GDataFileSystem::~GDataFileSystem() { |
973 // This should be called from UI thread, from GDataSystemService shutdown. | 993 // This should be called from UI thread, from GDataSystemService shutdown. |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1009 } | 1029 } |
1010 | 1030 |
1011 void GDataFileSystem::AddObserver(Observer* observer) { | 1031 void GDataFileSystem::AddObserver(Observer* observer) { |
1012 observers_.AddObserver(observer); | 1032 observers_.AddObserver(observer); |
1013 } | 1033 } |
1014 | 1034 |
1015 void GDataFileSystem::RemoveObserver(Observer* observer) { | 1035 void GDataFileSystem::RemoveObserver(Observer* observer) { |
1016 observers_.RemoveObserver(observer); | 1036 observers_.RemoveObserver(observer); |
1017 } | 1037 } |
1018 | 1038 |
1039 void GDataFileSystem::AddFileWatch(const FilePath&) { | |
1040 // Perform an immediate check for updates. | |
1041 CheckForUpdates(); | |
1042 // Start timer to periodically check for updates at the given intervals. | |
1043 if (!(num_file_watches_++)) { | |
satorux1
2012/05/03 16:49:29
This looks tricky. maybe:
++num_file_watches_;
if
hshi
2012/05/03 17:46:35
Done.
| |
1044 file_watch_timer_.Start(FROM_HERE, | |
1045 base::TimeDelta::FromSeconds( | |
1046 kGDataUpdateCheckIntervalInSec), | |
1047 this, &GDataFileSystem::CheckForUpdates); | |
zel
2012/05/03 03:41:07
this should be ui_weak_ptr_ instead of 'this'
satorux1
2012/05/03 16:49:29
Looking at base/timer.h, there is another Start()
hshi
2012/05/03 17:46:35
Zel & Satoru, the documentation in base/timer.h ac
satorux1
2012/05/03 18:09:29
This is worrisome. Is this safe? I don't know how
| |
1048 } | |
1049 } | |
1050 | |
1051 void GDataFileSystem::RemoveFileWatch(const FilePath&) { | |
1052 if (num_file_watches_ <= 0) { | |
1053 NOTREACHED(); | |
1054 } else if (!(--num_file_watches_)) { | |
satorux1
2012/05/03 16:49:29
This also looks tricky. maybe
else {
--num_fil
hshi
2012/05/03 17:46:35
Done.
| |
1055 file_watch_timer_.Stop(); | |
1056 } | |
1057 } | |
1058 | |
1019 void GDataFileSystem::Authenticate(const AuthStatusCallback& callback) { | 1059 void GDataFileSystem::Authenticate(const AuthStatusCallback& callback) { |
1020 // TokenFetcher, used in DocumentsService, must be run on UI thread. | 1060 // TokenFetcher, used in DocumentsService, must be run on UI thread. |
1021 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 1061 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
1022 | 1062 |
1023 documents_service_->Authenticate(callback); | 1063 documents_service_->Authenticate(callback); |
1024 } | 1064 } |
1025 | 1065 |
1026 void GDataFileSystem::FindEntryByResourceIdSync( | 1066 void GDataFileSystem::FindEntryByResourceIdSync( |
1027 const std::string& resource_id, | 1067 const std::string& resource_id, |
1028 FindEntryDelegate* delegate) { | 1068 FindEntryDelegate* delegate) { |
(...skipping 30 matching lines...) Expand all Loading... | |
1059 callback))); | 1099 callback))); |
1060 return; | 1100 return; |
1061 } else if (root_->origin() == UNINITIALIZED) { | 1101 } else if (root_->origin() == UNINITIALIZED) { |
1062 // Load root feed from this disk cache. Upon completion, kick off server | 1102 // Load root feed from this disk cache. Upon completion, kick off server |
1063 // fetching. | 1103 // fetching. |
1064 root_->set_origin(INITIALIZING); | 1104 root_->set_origin(INITIALIZING); |
1065 LoadRootFeedFromCache(true, // should_load_from_server | 1105 LoadRootFeedFromCache(true, // should_load_from_server |
1066 search_file_path, | 1106 search_file_path, |
1067 callback); | 1107 callback); |
1068 return; | 1108 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 } | 1109 } |
1080 | 1110 |
1081 // Post a task to the same thread, rather than calling it here, as | 1111 // Post a task to the same thread, rather than calling it here, as |
1082 // FindEntryByPathAsync() is asynchronous. | 1112 // FindEntryByPathAsync() is asynchronous. |
1083 base::MessageLoopProxy::current()->PostTask( | 1113 base::MessageLoopProxy::current()->PostTask( |
1084 FROM_HERE, | 1114 FROM_HERE, |
1085 base::Bind(&GDataFileSystem::FindEntryByPathSyncOnUIThread, | 1115 base::Bind(&GDataFileSystem::FindEntryByPathSyncOnUIThread, |
1086 ui_weak_ptr_, | 1116 ui_weak_ptr_, |
1087 search_file_path, | 1117 search_file_path, |
1088 callback)); | 1118 callback)); |
(...skipping 3682 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
4771 pref_registrar_->Init(profile_->GetPrefs()); | 4801 pref_registrar_->Init(profile_->GetPrefs()); |
4772 pref_registrar_->Add(prefs::kDisableGDataHostedFiles, this); | 4802 pref_registrar_->Add(prefs::kDisableGDataHostedFiles, this); |
4773 } | 4803 } |
4774 | 4804 |
4775 void SetFreeDiskSpaceGetterForTesting(FreeDiskSpaceGetterInterface* getter) { | 4805 void SetFreeDiskSpaceGetterForTesting(FreeDiskSpaceGetterInterface* getter) { |
4776 delete global_free_disk_getter_for_testing; // Safe to delete NULL; | 4806 delete global_free_disk_getter_for_testing; // Safe to delete NULL; |
4777 global_free_disk_getter_for_testing = getter; | 4807 global_free_disk_getter_for_testing = getter; |
4778 } | 4808 } |
4779 | 4809 |
4780 } // namespace gdata | 4810 } // namespace gdata |
OLD | NEW |