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

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

Issue 9742002: Wired GDataFileSystem::GetFile() method with internal cache. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 9 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 | Annotate | Revision Log
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 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/eintr_wrapper.h" 10 #include "base/eintr_wrapper.h"
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 source_path(in_source_path), 190 source_path(in_source_path),
191 operation_callback(in_operation_callback), 191 operation_callback(in_operation_callback),
192 dest_path(in_dest_path), 192 dest_path(in_dest_path),
193 modification_callback(in_modification_callback), 193 modification_callback(in_modification_callback),
194 relay_proxy(in_relay_proxy) { 194 relay_proxy(in_relay_proxy) {
195 } 195 }
196 196
197 StoreToCacheParams::~StoreToCacheParams() { 197 StoreToCacheParams::~StoreToCacheParams() {
198 } 198 }
199 199
200
200 //=================== ModifyCacheStatusParams implementations ================== 201 //=================== ModifyCacheStatusParams implementations ==================
201 202
202 ModifyCacheStatusParams::ModifyCacheStatusParams( 203 ModifyCacheStatusParams::ModifyCacheStatusParams(
203 const std::string& in_resource_id, 204 const std::string& in_resource_id,
204 const std::string& in_md5, 205 const std::string& in_md5,
205 const gdata::CacheOperationCallback& in_operation_callback, 206 const gdata::CacheOperationCallback& in_operation_callback,
206 gdata::GDataRootDirectory::CacheStatusFlags in_flags, 207 gdata::GDataRootDirectory::CacheStatusFlags in_flags,
207 bool in_enable, 208 bool in_enable,
208 const FilePath& in_file_path, 209 const FilePath& in_file_path,
209 const CacheStatusModificationCallback& in_modification_callback, 210 const CacheStatusModificationCallback& in_modification_callback,
(...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after
531 : created_directory_path(created_directory_path), 532 : created_directory_path(created_directory_path),
532 target_directory_path(target_directory_path), 533 target_directory_path(target_directory_path),
533 is_exclusive(is_exclusive), 534 is_exclusive(is_exclusive),
534 is_recursive(is_recursive), 535 is_recursive(is_recursive),
535 callback(callback) { 536 callback(callback) {
536 } 537 }
537 538
538 GDataFileSystem::CreateDirectoryParams::~CreateDirectoryParams() { 539 GDataFileSystem::CreateDirectoryParams::~CreateDirectoryParams() {
539 } 540 }
540 541
542 //=================== GetFileFromCacheParams implementation ===================
543
544 GDataFileSystem::GetFileFromCacheParams::GetFileFromCacheParams(
545 const FilePath& virtual_file_path,
546 const FilePath& local_tmp_path,
547 const GURL& content_url,
548 const std::string& resource_id,
549 const std::string& md5,
550 scoped_refptr<base::MessageLoopProxy> proxy,
551 const GetFileCallback& callback)
552 : virtual_file_path(virtual_file_path),
553 local_tmp_path(local_tmp_path),
554 content_url(content_url),
555 resource_id(resource_id),
556 md5(md5),
557 proxy(proxy),
558 callback(callback) {
559 }
560
561 GDataFileSystem::GetFileFromCacheParams::~GetFileFromCacheParams() {
562 }
563
541 // GDataFileSystem class implementatsion. 564 // GDataFileSystem class implementatsion.
542 565
543 GDataFileSystem::GDataFileSystem(Profile* profile, 566 GDataFileSystem::GDataFileSystem(Profile* profile,
544 DocumentsServiceInterface* documents_service) 567 DocumentsServiceInterface* documents_service)
545 : profile_(profile), 568 : profile_(profile),
546 documents_service_(documents_service), 569 documents_service_(documents_service),
547 gdata_uploader_(new GDataUploader(ALLOW_THIS_IN_INITIALIZER_LIST(this))), 570 gdata_uploader_(new GDataUploader(ALLOW_THIS_IN_INITIALIZER_LIST(this))),
548 gdata_download_observer_(new GDataDownloadObserver()), 571 gdata_download_observer_(new GDataDownloadObserver()),
549 on_cache_initialized_(new base::WaitableEvent( 572 on_cache_initialized_(new base::WaitableEvent(
550 true /* manual reset*/, false /* initially not signaled*/)), 573 true /* manual reset*/, false /* initially not signaled*/)),
(...skipping 442 matching lines...) Expand 10 before | Expand all | Expand 10 after
993 CreateDirectoryParams( 1016 CreateDirectoryParams(
994 first_missing_path, 1017 first_missing_path,
995 directory_path, 1018 directory_path,
996 is_exclusive, 1019 is_exclusive,
997 is_recursive, 1020 is_recursive,
998 callback))); 1021 callback)));
999 } 1022 }
1000 1023
1001 void GDataFileSystem::GetFile(const FilePath& file_path, 1024 void GDataFileSystem::GetFile(const FilePath& file_path,
1002 const GetFileCallback& callback) { 1025 const GetFileCallback& callback) {
1003 base::AutoLock lock(lock_); 1026 std::string resource_id;
1004 GDataFileBase* file_info = GetGDataFileInfoFromPath(file_path); 1027 std::string md5;
1005 if (!file_info) { 1028 GURL content_url;
1029 base::PlatformFileInfo file_info;
1030 if (!GetFileInfoFromPath(file_path, &file_info,
1031 &resource_id, &md5, &content_url)) {
1006 if (!callback.is_null()) { 1032 if (!callback.is_null()) {
1007 MessageLoop::current()->PostTask( 1033 MessageLoop::current()->PostTask(
1008 FROM_HERE, 1034 FROM_HERE,
1009 base::Bind(callback, 1035 base::Bind(callback,
1010 base::PLATFORM_FILE_ERROR_NOT_FOUND, 1036 base::PLATFORM_FILE_ERROR_NOT_FOUND,
1011 FilePath())); 1037 FilePath()));
1012 } 1038 }
1013 return; 1039 return;
1014 } 1040 }
1015 1041
1016 // TODO(satorux): We should get a file from the cache if it's present, but 1042 // Returns absolute path of the file if it were cached or to be cached.
1017 // the caching layer is not implemented yet. For now, always download from 1043 FilePath local_tmp_path = GetCacheFilePath(resource_id, md5, CACHE_TYPE_TMP,
1018 // the cloud. 1044 false /* is_local */);
kuan 2012/03/20 03:22:19 nit: align "false" with "resource_id"
zel 2012/03/20 04:31:17 Done.
1045 // Checks if file corresponding to |resource_id| and |md5| exist on disk and
1046 // can be accessed i.e. not corrupted by previous file operations that didn't
1047 // complete for whatever reasons.
1048 // Initializes cache if it has not been initialized.
1049 // Upon completion, |callback| is invoked on the thread where this method was
1050 // called with the cache file path if it exists and is accessible or empty
1051 // 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.
1052 GetFromCache(resource_id, md5,
1053 base::Bind(
1054 &GDataFileSystem::OnGetFileFromCache,
1055 weak_ptr_factory_.GetWeakPtr(),
1056 GetFileFromCacheParams(file_path,
1057 local_tmp_path,
1058 content_url,
1059 resource_id,
1060 md5,
1061 base::MessageLoopProxy::current(),
1062 callback)));
1063 }
1064
1065 void GDataFileSystem::OnGetFileFromCache(
1066 const GetFileFromCacheParams& params,
1067 base::PlatformFileError error,
1068 const std::string& resource_id,
1069 const std::string& md5,
1070 const FilePath& gdata_file_path,
1071 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.
1072 // Have we found the file in cache? If so, return it back to the caller.
1073 if (error == base::PLATFORM_FILE_OK) {
1074 if (!params.callback.is_null()) {
1075 params.proxy->PostTask(FROM_HERE,
1076 base::Bind(params.callback,
1077 error,
1078 cache_file_path));
1079 }
1080
1081 return;
1082 }
1083
1084 // If cache file is not found, try to download it from the server instead.
1019 documents_service_->DownloadFile( 1085 documents_service_->DownloadFile(
1020 file_info->GetFilePath(), 1086 params.virtual_file_path,
1021 file_info->content_url(), 1087 params.local_tmp_path,
1088 params.content_url,
1022 base::Bind(&GDataFileSystem::OnFileDownloaded, 1089 base::Bind(&GDataFileSystem::OnFileDownloaded,
1023 weak_ptr_factory_.GetWeakPtr(), 1090 weak_ptr_factory_.GetWeakPtr(),
1024 callback)); 1091 params));
1025 } 1092 }
1026 1093
1027 void GDataFileSystem::InitiateUpload( 1094 void GDataFileSystem::InitiateUpload(
1028 const std::string& file_name, 1095 const std::string& file_name,
1029 const std::string& content_type, 1096 const std::string& content_type,
1030 int64 content_length, 1097 int64 content_length,
1031 const FilePath& destination_directory, 1098 const FilePath& destination_directory,
1032 const FilePath& virtual_path, 1099 const FilePath& virtual_path,
1033 const InitiateUploadCallback& callback) { 1100 const InitiateUploadCallback& callback) {
1034 GURL destination_directory_url = 1101 GURL destination_directory_url =
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
1141 delegate->OnDone(base::PLATFORM_FILE_ERROR_NOT_FOUND, FilePath(), NULL); 1208 delegate->OnDone(base::PLATFORM_FILE_ERROR_NOT_FOUND, FilePath(), NULL);
1142 } 1209 }
1143 1210
1144 return; 1211 return;
1145 } 1212 }
1146 } 1213 }
1147 delegate->OnDone(base::PLATFORM_FILE_ERROR_NOT_FOUND, FilePath(), NULL); 1214 delegate->OnDone(base::PLATFORM_FILE_ERROR_NOT_FOUND, FilePath(), NULL);
1148 } 1215 }
1149 1216
1150 bool GDataFileSystem::GetFileInfoFromPath( 1217 bool GDataFileSystem::GetFileInfoFromPath(
1151 const FilePath& file_path, base::PlatformFileInfo* file_info) { 1218 const FilePath& file_path, base::PlatformFileInfo* file_info,
1219 std::string* resource_id, std::string* md5, GURL* content_url) {
1220 DCHECK(file_info);
1152 base::AutoLock lock(lock_); 1221 base::AutoLock lock(lock_);
1153 GDataFileBase* file = GetGDataFileInfoFromPath(file_path); 1222 GDataFileBase* file = GetGDataFileInfoFromPath(file_path);
1154 if (!file) 1223 if (!file)
1155 return false; 1224 return false;
1156 1225
1157 *file_info = file->file_info(); 1226 *file_info = file->file_info();
1227 if (resource_id)
1228 *resource_id = file->resource_id();
1229
1230 GDataFile* regular_file = file->AsGDataFile();
1231 if (regular_file) {
1232 if (md5)
1233 *md5 = regular_file->file_md5();
1234
1235 if (content_url)
1236 *content_url = regular_file->content_url();
1237 }
1158 return true; 1238 return true;
1159 } 1239 }
1160 1240
1161 GDataFileBase* GDataFileSystem::GetGDataFileInfoFromPath( 1241 GDataFileBase* GDataFileSystem::GetGDataFileInfoFromPath(
1162 const FilePath& file_path) { 1242 const FilePath& file_path) {
1163 lock_.AssertAcquired(); 1243 lock_.AssertAcquired();
1164 // Find directory element within the cached file system snapshot. 1244 // Find directory element within the cached file system snapshot.
1165 ReadOnlyFindFileDelegate find_delegate; 1245 ReadOnlyFindFileDelegate find_delegate;
1166 UnsafeFindFileByPath(file_path, &find_delegate); 1246 UnsafeFindFileByPath(file_path, &find_delegate);
1167 return find_delegate.file(); 1247 return find_delegate.file();
(...skipping 478 matching lines...) Expand 10 before | Expand all | Expand 10 after
1646 1726
1647 if (error == base::PLATFORM_FILE_OK) 1727 if (error == base::PLATFORM_FILE_OK)
1648 error = RemoveFileFromFileSystem(file_path); 1728 error = RemoveFileFromFileSystem(file_path);
1649 1729
1650 if (!callback.is_null()) { 1730 if (!callback.is_null()) {
1651 callback.Run(error); 1731 callback.Run(error);
1652 } 1732 }
1653 } 1733 }
1654 1734
1655 void GDataFileSystem::OnFileDownloaded( 1735 void GDataFileSystem::OnFileDownloaded(
1656 const GetFileCallback& callback, 1736 const GetFileFromCacheParams& params,
1657 GDataErrorCode status, 1737 GDataErrorCode status,
1658 const GURL& content_url, 1738 const GURL& content_url,
1659 const FilePath& file_path) { 1739 const FilePath& downloaded_file_path) {
1660 base::PlatformFileError error = GDataToPlatformError(status); 1740 base::PlatformFileError error = GDataToPlatformError(status);
1661 1741
1662 if (!callback.is_null()) { 1742 // Make sure that downloaded file is properly stored in cache. We don't have
1663 callback.Run(error, file_path); 1743 // to wait for this operation to finish since the user can already use the
1744 // downloaded file.
1745 if (error == base::PLATFORM_FILE_OK) {
1746 StoreToCache(params.resource_id,
1747 params.md5,
1748 downloaded_file_path,
1749 base::Bind(&GDataFileSystem::OnStoreToCache,
1750 weak_ptr_factory_.GetWeakPtr()));
1751
1752 }
1753 if (!params.callback.is_null()) {
1754 params.proxy->PostTask(FROM_HERE,
1755 base::Bind(params.callback,
1756 error,
1757 downloaded_file_path));
1664 } 1758 }
1665 } 1759 }
1666 1760
1761 void GDataFileSystem::OnStoreToCache(base::PlatformFileError error,
1762 const std::string& resource_id,
1763 const std::string& md5) {
1764 // Nothing much to do here for now.
1765 }
1766
1667 base::PlatformFileError GDataFileSystem::RenameFileOnFilesystem( 1767 base::PlatformFileError GDataFileSystem::RenameFileOnFilesystem(
1668 const FilePath& file_path, 1768 const FilePath& file_path,
1669 const FilePath::StringType& new_name, 1769 const FilePath::StringType& new_name,
1670 FilePath* updated_file_path) { 1770 FilePath* updated_file_path) {
1671 DCHECK(updated_file_path); 1771 DCHECK(updated_file_path);
1672 1772
1673 base::AutoLock lock(lock_); 1773 base::AutoLock lock(lock_);
1674 GDataFileBase* file = GetGDataFileInfoFromPath(file_path); 1774 GDataFileBase* file = GetGDataFileInfoFromPath(file_path);
1675 if (!file) 1775 if (!file)
1676 return base::PLATFORM_FILE_ERROR_NOT_FOUND; 1776 return base::PLATFORM_FILE_ERROR_NOT_FOUND;
(...skipping 661 matching lines...) Expand 10 before | Expand all | Expand 10 after
2338 2438
2339 GDataFileSystemFactory::~GDataFileSystemFactory() { 2439 GDataFileSystemFactory::~GDataFileSystemFactory() {
2340 } 2440 }
2341 2441
2342 ProfileKeyedService* GDataFileSystemFactory::BuildServiceInstanceFor( 2442 ProfileKeyedService* GDataFileSystemFactory::BuildServiceInstanceFor(
2343 Profile* profile) const { 2443 Profile* profile) const {
2344 return new GDataFileSystem(profile, new DocumentsService); 2444 return new GDataFileSystem(profile, new DocumentsService);
2345 } 2445 }
2346 2446
2347 } // namespace gdata 2447 } // namespace gdata
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698