Index: chrome/browser/chromeos/gdata/gdata_file_system.cc |
diff --git a/chrome/browser/chromeos/gdata/gdata_file_system.cc b/chrome/browser/chromeos/gdata/gdata_file_system.cc |
index 4d0539528b71375b7292ed89fcbed87530af78d4..0bbeb5a82d3b863a773fb2df43ca684eb884870e 100644 |
--- a/chrome/browser/chromeos/gdata/gdata_file_system.cc |
+++ b/chrome/browser/chromeos/gdata/gdata_file_system.cc |
@@ -197,6 +197,7 @@ StoreToCacheParams::StoreToCacheParams( |
StoreToCacheParams::~StoreToCacheParams() { |
} |
+ |
//=================== ModifyCacheStatusParams implementations ================== |
ModifyCacheStatusParams::ModifyCacheStatusParams( |
@@ -538,6 +539,28 @@ GDataFileSystem::CreateDirectoryParams::CreateDirectoryParams( |
GDataFileSystem::CreateDirectoryParams::~CreateDirectoryParams() { |
} |
+//=================== GetFileFromCacheParams implementation =================== |
+ |
+GDataFileSystem::GetFileFromCacheParams::GetFileFromCacheParams( |
+ const FilePath& virtual_file_path, |
+ const FilePath& local_tmp_path, |
+ const GURL& content_url, |
+ const std::string& resource_id, |
+ const std::string& md5, |
+ scoped_refptr<base::MessageLoopProxy> proxy, |
+ const GetFileCallback& callback) |
+ : virtual_file_path(virtual_file_path), |
+ local_tmp_path(local_tmp_path), |
+ content_url(content_url), |
+ resource_id(resource_id), |
+ md5(md5), |
+ proxy(proxy), |
+ callback(callback) { |
+} |
+ |
+GDataFileSystem::GetFileFromCacheParams::~GetFileFromCacheParams() { |
+} |
+ |
// GDataFileSystem class implementatsion. |
GDataFileSystem::GDataFileSystem(Profile* profile, |
@@ -1000,9 +1023,12 @@ void GDataFileSystem::CreateDirectory( |
void GDataFileSystem::GetFile(const FilePath& file_path, |
const GetFileCallback& callback) { |
- base::AutoLock lock(lock_); |
- GDataFileBase* file_info = GetGDataFileInfoFromPath(file_path); |
- if (!file_info) { |
+ std::string resource_id; |
+ std::string md5; |
+ GURL content_url; |
+ base::PlatformFileInfo file_info; |
+ if (!GetFileInfoFromPath(file_path, &file_info, |
+ &resource_id, &md5, &content_url)) { |
if (!callback.is_null()) { |
MessageLoop::current()->PostTask( |
FROM_HERE, |
@@ -1013,15 +1039,56 @@ void GDataFileSystem::GetFile(const FilePath& file_path, |
return; |
} |
- // TODO(satorux): We should get a file from the cache if it's present, but |
- // the caching layer is not implemented yet. For now, always download from |
- // the cloud. |
+ // Returns absolute path of the file if it were cached or to be cached. |
+ FilePath local_tmp_path = GetCacheFilePath(resource_id, md5, CACHE_TYPE_TMP, |
+ false /* is_local */); |
kuan
2012/03/20 03:22:19
nit: align "false" with "resource_id"
zel
2012/03/20 04:31:17
Done.
|
+ // Checks if file corresponding to |resource_id| and |md5| exist on disk and |
+ // can be accessed i.e. not corrupted by previous file operations that didn't |
+ // complete for whatever reasons. |
+ // Initializes cache if it has not been initialized. |
+ // Upon completion, |callback| is invoked on the thread where this method was |
+ // called with the cache file path if it exists and is accessible or empty |
+ // otherwise. |
kuan
2012/03/20 03:22:19
not sure if u really mean to copy and paste the co
zel
2012/03/20 04:31:17
Removed.
|
+ GetFromCache(resource_id, md5, |
+ base::Bind( |
+ &GDataFileSystem::OnGetFileFromCache, |
+ weak_ptr_factory_.GetWeakPtr(), |
+ GetFileFromCacheParams(file_path, |
+ local_tmp_path, |
+ content_url, |
+ resource_id, |
+ md5, |
+ base::MessageLoopProxy::current(), |
+ callback))); |
+} |
+ |
+void GDataFileSystem::OnGetFileFromCache( |
+ const GetFileFromCacheParams& params, |
+ base::PlatformFileError error, |
+ const std::string& resource_id, |
+ const std::string& md5, |
+ const FilePath& gdata_file_path, |
+ const FilePath& cache_file_path) { |
kuan
2012/03/20 03:22:19
nit: only 4 spaces needed for indentation
zel
2012/03/20 04:31:17
Done.
|
+ // Have we found the file in cache? If so, return it back to the caller. |
+ if (error == base::PLATFORM_FILE_OK) { |
+ if (!params.callback.is_null()) { |
+ params.proxy->PostTask(FROM_HERE, |
+ base::Bind(params.callback, |
+ error, |
+ cache_file_path)); |
+ } |
+ |
+ return; |
+ } |
+ |
+ // If cache file is not found, try to download it from the server instead. |
documents_service_->DownloadFile( |
- file_info->GetFilePath(), |
- file_info->content_url(), |
+ params.virtual_file_path, |
+ params.local_tmp_path, |
+ params.content_url, |
base::Bind(&GDataFileSystem::OnFileDownloaded, |
weak_ptr_factory_.GetWeakPtr(), |
- callback)); |
+ params)); |
} |
void GDataFileSystem::InitiateUpload( |
@@ -1148,13 +1215,26 @@ void GDataFileSystem::UnsafeFindFileByPath( |
} |
bool GDataFileSystem::GetFileInfoFromPath( |
- const FilePath& file_path, base::PlatformFileInfo* file_info) { |
+ const FilePath& file_path, base::PlatformFileInfo* file_info, |
+ std::string* resource_id, std::string* md5, GURL* content_url) { |
+ DCHECK(file_info); |
base::AutoLock lock(lock_); |
GDataFileBase* file = GetGDataFileInfoFromPath(file_path); |
if (!file) |
return false; |
*file_info = file->file_info(); |
+ if (resource_id) |
+ *resource_id = file->resource_id(); |
+ |
+ GDataFile* regular_file = file->AsGDataFile(); |
+ if (regular_file) { |
+ if (md5) |
+ *md5 = regular_file->file_md5(); |
+ |
+ if (content_url) |
+ *content_url = regular_file->content_url(); |
+ } |
return true; |
} |
@@ -1653,15 +1733,35 @@ void GDataFileSystem::OnRemovedDocument( |
} |
void GDataFileSystem::OnFileDownloaded( |
- const GetFileCallback& callback, |
+ const GetFileFromCacheParams& params, |
GDataErrorCode status, |
const GURL& content_url, |
- const FilePath& file_path) { |
+ const FilePath& downloaded_file_path) { |
base::PlatformFileError error = GDataToPlatformError(status); |
- if (!callback.is_null()) { |
- callback.Run(error, file_path); |
+ // Make sure that downloaded file is properly stored in cache. We don't have |
+ // to wait for this operation to finish since the user can already use the |
+ // downloaded file. |
+ if (error == base::PLATFORM_FILE_OK) { |
+ StoreToCache(params.resource_id, |
+ params.md5, |
+ downloaded_file_path, |
+ base::Bind(&GDataFileSystem::OnStoreToCache, |
+ weak_ptr_factory_.GetWeakPtr())); |
+ |
} |
+ if (!params.callback.is_null()) { |
+ params.proxy->PostTask(FROM_HERE, |
+ base::Bind(params.callback, |
+ error, |
+ downloaded_file_path)); |
+ } |
+} |
+ |
+void GDataFileSystem::OnStoreToCache(base::PlatformFileError error, |
+ const std::string& resource_id, |
+ const std::string& md5) { |
+ // Nothing much to do here for now. |
} |
base::PlatformFileError GDataFileSystem::RenameFileOnFilesystem( |