Chromium Code Reviews| Index: chrome/browser/chromeos/gdata/gdata_file_system.cc |
| =================================================================== |
| --- chrome/browser/chromeos/gdata/gdata_file_system.cc (revision 149486) |
| +++ chrome/browser/chromeos/gdata/gdata_file_system.cc (working copy) |
| @@ -8,6 +8,7 @@ |
| #include <utility> |
| #include "base/bind.h" |
| +#include "base/command_line.h" |
| #include "base/file_util.h" |
| #include "base/json/json_file_value_serializer.h" |
| #include "base/json/json_reader.h" |
| @@ -27,6 +28,7 @@ |
| #include "chrome/browser/chromeos/gdata/gdata_util.h" |
| #include "chrome/browser/prefs/pref_service.h" |
| #include "chrome/browser/profiles/profile.h" |
| +#include "chrome/common/chrome_switches.h" |
| #include "chrome/common/chrome_notification_types.h" |
| #include "chrome/common/pref_names.h" |
| #include "content/public/browser/browser_thread.h" |
| @@ -45,9 +47,15 @@ |
| FILE_PATH_LITERAL("account_metadata.json"); |
| const FilePath::CharType kFilesystemProtoFile[] = |
| FILE_PATH_LITERAL("file_system.pb"); |
| +const FilePath::CharType kResourceMetadataDBFile[] = |
| + FILE_PATH_LITERAL("resource_metadata.db"); |
| const char kEmptyFilePath[] = "/dev/null"; |
| +#ifndef NDEBUG |
| +static base::Time kTimerStart; |
|
satorux1
2012/08/02 17:24:03
this is not allowed
http://google-styleguide.goog
achuithb
2012/08/02 20:26:25
Done.
|
| +#endif |
| + |
| // GData update check interval (in seconds). |
| #ifndef NDEBUG |
| const int kGDataUpdateCheckIntervalInSec = 5; |
| @@ -75,28 +83,6 @@ |
| #endif |
| }; |
| -// Defines set of parameters sent to callback OnProtoLoaded(). |
| -struct LoadRootFeedParams { |
| - LoadRootFeedParams( |
| - FilePath search_file_path, |
| - bool should_load_from_server, |
| - const FindEntryCallback& callback) |
| - : search_file_path(search_file_path), |
| - should_load_from_server(should_load_from_server), |
| - load_error(GDATA_FILE_OK), |
| - callback(callback) { |
| - } |
| - ~LoadRootFeedParams() { |
| - } |
| - |
| - FilePath search_file_path; |
| - bool should_load_from_server; |
| - std::string proto; |
| - GDataFileError load_error; |
| - base::Time last_modified; |
| - const FindEntryCallback callback; |
| -}; |
| - |
| // Returns true if file system is due to be serialized on disk based on it |
| // |serialized_size| and |last_serialized| timestamp. |
| bool ShouldSerializeFileSystemNow(size_t serialized_size, |
| @@ -270,6 +256,11 @@ |
| } |
| } |
| +bool UseLevelDB() { |
| + return CommandLine::ForCurrentProcess()->HasSwitch( |
| + switches::kUseLevelDBForGData); |
| +} |
| + |
| // Gets the file size of |local_file|. |
| void GetLocalFileSizeOnBlockingPool(const FilePath& local_file, |
| GDataFileError* error, |
| @@ -914,6 +905,9 @@ |
| const FindEntryCallback& callback) { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + DVLOG(1) << "ReloadFeedFromServerIfNeeded local_changestamp=" |
| + << local_changestamp << ", initial_origin=" << initial_origin; |
| + |
| // First fetch the latest changestamp to see if there were any new changes |
| // there at all. |
| documents_service_->GetAccountMetadata( |
| @@ -1074,7 +1068,7 @@ |
| } |
| // Save file system metadata to disk. |
| - SaveFileSystemAsProto(); |
| + SaveFileSystem(); |
| // If we had someone to report this too, then this retrieval was done in a |
| // context of search... so continue search. |
| @@ -2832,17 +2826,29 @@ |
| const FindEntryCallback& callback) { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| - const FilePath path = |
| - cache_->GetCacheDirectoryPath(GDataCache::CACHE_TYPE_META).Append( |
| - kFilesystemProtoFile); |
| LoadRootFeedParams* params = new LoadRootFeedParams(search_file_path, |
| should_load_from_server, |
| callback); |
| - BrowserThread::GetBlockingPool()->PostTaskAndReply(FROM_HERE, |
| - base::Bind(&LoadProtoOnBlockingPool, path, params), |
| - base::Bind(&GDataFileSystem::OnProtoLoaded, |
| - ui_weak_ptr_, |
| - base::Owned(params))); |
| + |
| +#ifndef NDEBUG |
| + kTimerStart = base::Time::Now(); |
| +#endif |
|
satorux1
2012/08/02 17:24:03
Let's get rid of the #ifdef. Time object is cheap.
achuithb
2012/08/02 20:26:25
Done.
|
| + |
| + FilePath path = cache_->GetCacheDirectoryPath(GDataCache::CACHE_TYPE_META); |
| + if (UseLevelDB()) { |
| + path = path.Append(kResourceMetadataDBFile); |
| + directory_service_->InitFromDB(path, blocking_task_runner_, params, |
| + base::Bind(&GDataFileSystem::ContinueWithInitializedDirectoryService, |
| + ui_weak_ptr_, |
| + base::Owned(params))); |
| + } else { |
| + path = path.Append(kFilesystemProtoFile); |
| + BrowserThread::GetBlockingPool()->PostTaskAndReply(FROM_HERE, |
| + base::Bind(&LoadProtoOnBlockingPool, path, params), |
| + base::Bind(&GDataFileSystem::OnProtoLoaded, |
| + ui_weak_ptr_, |
| + base::Owned(params))); |
| + } |
| } |
| void GDataFileSystem::LoadRootFeedFromCacheForTesting() { |
| @@ -2862,7 +2868,6 @@ |
| if (directory_service_->origin() == FROM_SERVER) |
| return; |
| - int local_changestamp = 0; |
| // Update directory structure only if everything is OK and we haven't yet |
| // received the feed from the server yet. |
| if (params->load_error == GDATA_FILE_OK) { |
| @@ -2870,13 +2875,25 @@ |
| if (directory_service_->ParseFromString(params->proto)) { |
| directory_service_->set_last_serialized(params->last_modified); |
| directory_service_->set_serialized_size(params->proto.size()); |
| - local_changestamp = directory_service_->largest_changestamp(); |
| } else { |
| params->load_error = GDATA_FILE_ERROR_FAILED; |
| LOG(WARNING) << "Parse of cached proto file failed"; |
| } |
| } |
| + ContinueWithInitializedDirectoryService(params); |
| +} |
| + |
| +void GDataFileSystem::ContinueWithInitializedDirectoryService( |
| + LoadRootFeedParams* params) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + |
| +#ifndef NDEBUG |
| + DVLOG(1) << "Time elapsed to load directory service from disk=" |
| + << (base::Time::Now() - kTimerStart).InMilliseconds() |
| + << " milliseconds"; |
| +#endif |
|
satorux1
2012/08/02 17:24:03
let's remove #ifdef, and pass the start time nicel
achuithb
2012/08/02 20:26:25
Done.
|
| + |
| FindEntryCallback callback = params->callback; |
| // If we got feed content from cache, try search over it. |
| if (params->load_error == GDATA_FILE_OK && !callback.is_null()) { |
| @@ -2906,33 +2923,35 @@ |
| // |reported| to the original callback, then we just need to refresh the |
| // content without continuing search upon operation completion. |
| ReloadFeedFromServerIfNeeded(initial_origin, |
| - local_changestamp, |
| + directory_service_->largest_changestamp(), |
| params->search_file_path, |
| callback); |
| } |
| -void GDataFileSystem::SaveFileSystemAsProto() { |
| +void GDataFileSystem::SaveFileSystem() { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| - DVLOG(1) << "SaveFileSystemAsProto"; |
| - |
| if (!ShouldSerializeFileSystemNow(directory_service_->serialized_size(), |
| directory_service_->last_serialized())) { |
| return; |
| } |
| - const FilePath path = |
| - cache_->GetCacheDirectoryPath(GDataCache::CACHE_TYPE_META).Append( |
| - kFilesystemProtoFile); |
| - scoped_ptr<std::string> serialized_proto(new std::string()); |
| - directory_service_->SerializeToString(serialized_proto.get()); |
| - directory_service_->set_last_serialized(base::Time::Now()); |
| - directory_service_->set_serialized_size(serialized_proto->size()); |
| - PostBlockingPoolSequencedTask( |
| - FROM_HERE, |
| - blocking_task_runner_, |
| - base::Bind(&SaveProtoOnBlockingPool, path, |
| - base::Passed(serialized_proto.Pass()))); |
| + if (UseLevelDB()) { |
| + directory_service_->SaveToDB(); |
| + } else { |
| + const FilePath path = |
| + cache_->GetCacheDirectoryPath(GDataCache::CACHE_TYPE_META).Append( |
| + kFilesystemProtoFile); |
| + scoped_ptr<std::string> serialized_proto(new std::string()); |
| + directory_service_->SerializeToString(serialized_proto.get()); |
| + directory_service_->set_last_serialized(base::Time::Now()); |
| + directory_service_->set_serialized_size(serialized_proto->size()); |
| + PostBlockingPoolSequencedTask( |
| + FROM_HERE, |
| + blocking_task_runner_, |
| + base::Bind(&SaveProtoOnBlockingPool, path, |
| + base::Passed(serialized_proto.Pass()))); |
| + } |
| } |
| void GDataFileSystem::OnFilePathUpdated(const FileOperationCallback& callback, |
| @@ -3310,7 +3329,8 @@ |
| void GDataFileSystem::NotifyDocumentFeedFetched(int num_accumulated_entries) { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| - DVLOG(1) << "Document feed fetched: " << num_accumulated_entries; |
| + DVLOG(1) << "NotifyDocumentFeedFetched: " << num_accumulated_entries |
| + << ", origin: " << directory_service_->origin(); |
| // Notify the observers that a document feed is fetched. |
| FOR_EACH_OBSERVER(Observer, observers_, |
| OnDocumentFeedFetched(num_accumulated_entries)); |
| @@ -3322,10 +3342,11 @@ |
| GDataEntry* entry) { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| - DVLOG(1) << "Initial load finished"; |
| if (!callback.is_null()) |
| callback.Run(error, entry); |
| + DVLOG(1) << "RunAndNotifyInitialLoadFinished"; |
| + |
| // Notify the observers that root directory has been initialized. |
| FOR_EACH_OBSERVER(Observer, observers_, OnInitialLoadFinished()); |
| } |