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

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

Issue 10352004: gdata: Implement periodic file system update checks. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Bind the ui_weak_ptr_ in a callback to Timer::Start Created 8 years, 7 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
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 <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
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
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),
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
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
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::StartUpdates() {
1040 // Perform an immediate check for updates.
1041 CheckForUpdates();
1042 ++num_update_requests_;
1043 if (num_update_requests_ == 1) {
1044 // If this is the first StartUpdate request, start timer to periodically
1045 // check for updates at the specified intervals.
1046 update_timer_.Timer::Start(FROM_HERE,
1047 base::TimeDelta::FromSeconds(
1048 kGDataUpdateCheckIntervalInSec),
1049 base::Bind(&GDataFileSystem::CheckForUpdates,
1050 ui_weak_ptr_));
hshi 2012/05/03 18:26:37 Unfortunately I have to explicitly call base class
satorux1 2012/05/03 18:46:20 I just found this comment in timer.h: // OneShotT
hshi 2012/05/03 18:55:54 How about I just use the base::Timer class as Zel
satorux1 2012/05/03 18:59:54 Sounds good.
hshi 2012/05/03 19:09:19 Done.
1051 }
1052 }
1053
1054 void GDataFileSystem::StopUpdates() {
1055 if (num_update_requests_ <= 0) {
1056 NOTREACHED();
1057 return;
1058 }
satorux1 2012/05/03 18:46:20 you can replace the four lines with: DCHECK_GE(0,
hshi 2012/05/03 18:55:54 Did you mean DCHECK_GE(num_update_requests, 0)? O
satorux1 2012/05/03 18:59:54 oh I meant DCHECK_LE(). not a big deal at all, but
hshi 2012/05/03 19:09:19 Done.
1059 --num_update_requests_;
1060 if (num_update_requests_ == 0)
1061 update_timer_.Stop();
1062 }
1063
1019 void GDataFileSystem::Authenticate(const AuthStatusCallback& callback) { 1064 void GDataFileSystem::Authenticate(const AuthStatusCallback& callback) {
1020 // TokenFetcher, used in DocumentsService, must be run on UI thread. 1065 // TokenFetcher, used in DocumentsService, must be run on UI thread.
1021 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 1066 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1022 1067
1023 documents_service_->Authenticate(callback); 1068 documents_service_->Authenticate(callback);
1024 } 1069 }
1025 1070
1026 void GDataFileSystem::FindEntryByResourceIdSync( 1071 void GDataFileSystem::FindEntryByResourceIdSync(
1027 const std::string& resource_id, 1072 const std::string& resource_id,
1028 FindEntryDelegate* delegate) { 1073 FindEntryDelegate* delegate) {
(...skipping 30 matching lines...) Expand all
1059 callback))); 1104 callback)));
1060 return; 1105 return;
1061 } else if (root_->origin() == UNINITIALIZED) { 1106 } else if (root_->origin() == UNINITIALIZED) {
1062 // Load root feed from this disk cache. Upon completion, kick off server 1107 // Load root feed from this disk cache. Upon completion, kick off server
1063 // fetching. 1108 // fetching.
1064 root_->set_origin(INITIALIZING); 1109 root_->set_origin(INITIALIZING);
1065 LoadRootFeedFromCache(true, // should_load_from_server 1110 LoadRootFeedFromCache(true, // should_load_from_server
1066 search_file_path, 1111 search_file_path,
1067 callback); 1112 callback);
1068 return; 1113 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 } 1114 }
1080 1115
1081 // Post a task to the same thread, rather than calling it here, as 1116 // Post a task to the same thread, rather than calling it here, as
1082 // FindEntryByPathAsync() is asynchronous. 1117 // FindEntryByPathAsync() is asynchronous.
1083 base::MessageLoopProxy::current()->PostTask( 1118 base::MessageLoopProxy::current()->PostTask(
1084 FROM_HERE, 1119 FROM_HERE,
1085 base::Bind(&GDataFileSystem::FindEntryByPathSyncOnUIThread, 1120 base::Bind(&GDataFileSystem::FindEntryByPathSyncOnUIThread,
1086 ui_weak_ptr_, 1121 ui_weak_ptr_,
1087 search_file_path, 1122 search_file_path,
1088 callback)); 1123 callback));
(...skipping 3682 matching lines...) Expand 10 before | Expand all | Expand 10 after
4771 pref_registrar_->Init(profile_->GetPrefs()); 4806 pref_registrar_->Init(profile_->GetPrefs());
4772 pref_registrar_->Add(prefs::kDisableGDataHostedFiles, this); 4807 pref_registrar_->Add(prefs::kDisableGDataHostedFiles, this);
4773 } 4808 }
4774 4809
4775 void SetFreeDiskSpaceGetterForTesting(FreeDiskSpaceGetterInterface* getter) { 4810 void SetFreeDiskSpaceGetterForTesting(FreeDiskSpaceGetterInterface* getter) {
4776 delete global_free_disk_getter_for_testing; // Safe to delete NULL; 4811 delete global_free_disk_getter_for_testing; // Safe to delete NULL;
4777 global_free_disk_getter_for_testing = getter; 4812 global_free_disk_getter_for_testing = getter;
4778 } 4813 }
4779 4814
4780 } // namespace gdata 4815 } // namespace gdata
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698