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 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/eintr_wrapper.h" | 10 #include "base/eintr_wrapper.h" |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
189 md5(in_md5), | 189 md5(in_md5), |
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 |
satorux1
2012/03/20 04:51:17
nit: remove the blank line.
zel
2012/03/20 05:24:25
Done.
| |
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 Loading... | |
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 Loading... | |
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 */); |
satorux1
2012/03/20 04:51:17
Would be easier to read if it's enum like PINNED_F
zel
2012/03/20 05:24:25
Done.
| |
1045 GetFromCache(resource_id, md5, | |
1046 base::Bind( | |
1047 &GDataFileSystem::OnGetFileFromCache, | |
1048 weak_ptr_factory_.GetWeakPtr(), | |
1049 GetFileFromCacheParams(file_path, | |
1050 local_tmp_path, | |
1051 content_url, | |
1052 resource_id, | |
1053 md5, | |
1054 base::MessageLoopProxy::current(), | |
1055 callback))); | |
1056 } | |
1057 | |
1058 void GDataFileSystem::OnGetFileFromCache(const GetFileFromCacheParams& params, | |
1059 base::PlatformFileError error, | |
1060 const std::string& resource_id, | |
1061 const std::string& md5, | |
1062 const FilePath& gdata_file_path, | |
1063 const FilePath& cache_file_path) { | |
1064 // Have we found the file in cache? If so, return it back to the caller. | |
1065 if (error == base::PLATFORM_FILE_OK) { | |
1066 if (!params.callback.is_null()) { | |
1067 params.proxy->PostTask(FROM_HERE, | |
1068 base::Bind(params.callback, | |
1069 error, | |
1070 cache_file_path)); | |
1071 } | |
1072 | |
1073 return; | |
1074 } | |
1075 | |
1076 // If cache file is not found, try to download it from the server instead. | |
1019 documents_service_->DownloadFile( | 1077 documents_service_->DownloadFile( |
1020 file_info->GetFilePath(), | 1078 params.virtual_file_path, |
1021 file_info->content_url(), | 1079 params.local_tmp_path, |
1080 params.content_url, | |
1022 base::Bind(&GDataFileSystem::OnFileDownloaded, | 1081 base::Bind(&GDataFileSystem::OnFileDownloaded, |
1023 weak_ptr_factory_.GetWeakPtr(), | 1082 weak_ptr_factory_.GetWeakPtr(), |
1024 callback)); | 1083 params)); |
1025 } | 1084 } |
1026 | 1085 |
1027 void GDataFileSystem::InitiateUpload( | 1086 void GDataFileSystem::InitiateUpload( |
1028 const std::string& file_name, | 1087 const std::string& file_name, |
1029 const std::string& content_type, | 1088 const std::string& content_type, |
1030 int64 content_length, | 1089 int64 content_length, |
1031 const FilePath& destination_directory, | 1090 const FilePath& destination_directory, |
1032 const FilePath& virtual_path, | 1091 const FilePath& virtual_path, |
1033 const InitiateUploadCallback& callback) { | 1092 const InitiateUploadCallback& callback) { |
1034 GURL destination_directory_url = | 1093 GURL destination_directory_url = |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1141 delegate->OnDone(base::PLATFORM_FILE_ERROR_NOT_FOUND, FilePath(), NULL); | 1200 delegate->OnDone(base::PLATFORM_FILE_ERROR_NOT_FOUND, FilePath(), NULL); |
1142 } | 1201 } |
1143 | 1202 |
1144 return; | 1203 return; |
1145 } | 1204 } |
1146 } | 1205 } |
1147 delegate->OnDone(base::PLATFORM_FILE_ERROR_NOT_FOUND, FilePath(), NULL); | 1206 delegate->OnDone(base::PLATFORM_FILE_ERROR_NOT_FOUND, FilePath(), NULL); |
1148 } | 1207 } |
1149 | 1208 |
1150 bool GDataFileSystem::GetFileInfoFromPath( | 1209 bool GDataFileSystem::GetFileInfoFromPath( |
1151 const FilePath& file_path, base::PlatformFileInfo* file_info) { | 1210 const FilePath& file_path, base::PlatformFileInfo* file_info, |
1211 std::string* resource_id, std::string* md5, GURL* content_url) { | |
1212 DCHECK(file_info); | |
1152 base::AutoLock lock(lock_); | 1213 base::AutoLock lock(lock_); |
1153 GDataFileBase* file = GetGDataFileInfoFromPath(file_path); | 1214 GDataFileBase* file = GetGDataFileInfoFromPath(file_path); |
1154 if (!file) | 1215 if (!file) |
1155 return false; | 1216 return false; |
1156 | 1217 |
1157 *file_info = file->file_info(); | 1218 *file_info = file->file_info(); |
1219 if (resource_id) | |
1220 *resource_id = file->resource_id(); | |
1221 | |
1222 GDataFile* regular_file = file->AsGDataFile(); | |
1223 if (regular_file) { | |
1224 if (md5) | |
1225 *md5 = regular_file->file_md5(); | |
1226 | |
1227 if (content_url) | |
1228 *content_url = regular_file->content_url(); | |
1229 } | |
1158 return true; | 1230 return true; |
1159 } | 1231 } |
1160 | 1232 |
1161 GDataFileBase* GDataFileSystem::GetGDataFileInfoFromPath( | 1233 GDataFileBase* GDataFileSystem::GetGDataFileInfoFromPath( |
1162 const FilePath& file_path) { | 1234 const FilePath& file_path) { |
1163 lock_.AssertAcquired(); | 1235 lock_.AssertAcquired(); |
1164 // Find directory element within the cached file system snapshot. | 1236 // Find directory element within the cached file system snapshot. |
1165 ReadOnlyFindFileDelegate find_delegate; | 1237 ReadOnlyFindFileDelegate find_delegate; |
1166 UnsafeFindFileByPath(file_path, &find_delegate); | 1238 UnsafeFindFileByPath(file_path, &find_delegate); |
1167 return find_delegate.file(); | 1239 return find_delegate.file(); |
(...skipping 478 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1646 | 1718 |
1647 if (error == base::PLATFORM_FILE_OK) | 1719 if (error == base::PLATFORM_FILE_OK) |
1648 error = RemoveFileFromFileSystem(file_path); | 1720 error = RemoveFileFromFileSystem(file_path); |
1649 | 1721 |
1650 if (!callback.is_null()) { | 1722 if (!callback.is_null()) { |
1651 callback.Run(error); | 1723 callback.Run(error); |
1652 } | 1724 } |
1653 } | 1725 } |
1654 | 1726 |
1655 void GDataFileSystem::OnFileDownloaded( | 1727 void GDataFileSystem::OnFileDownloaded( |
1656 const GetFileCallback& callback, | 1728 const GetFileFromCacheParams& params, |
1657 GDataErrorCode status, | 1729 GDataErrorCode status, |
1658 const GURL& content_url, | 1730 const GURL& content_url, |
1659 const FilePath& file_path) { | 1731 const FilePath& downloaded_file_path) { |
1660 base::PlatformFileError error = GDataToPlatformError(status); | 1732 base::PlatformFileError error = GDataToPlatformError(status); |
1661 | 1733 |
1662 if (!callback.is_null()) { | 1734 // Make sure that downloaded file is properly stored in cache. We don't have |
1663 callback.Run(error, file_path); | 1735 // to wait for this operation to finish since the user can already use the |
1736 // downloaded file. | |
1737 if (error == base::PLATFORM_FILE_OK) { | |
1738 StoreToCache(params.resource_id, | |
1739 params.md5, | |
1740 downloaded_file_path, | |
1741 base::Bind(&GDataFileSystem::OnDownloadStoredToCache, | |
1742 weak_ptr_factory_.GetWeakPtr())); | |
1743 | |
1664 } | 1744 } |
1745 if (!params.callback.is_null()) { | |
1746 params.proxy->PostTask(FROM_HERE, | |
1747 base::Bind(params.callback, | |
1748 error, | |
1749 downloaded_file_path)); | |
1750 } | |
1751 } | |
1752 | |
1753 void GDataFileSystem::OnDownloadStoredToCache(base::PlatformFileError error, | |
1754 const std::string& resource_id, | |
1755 const std::string& md5) { | |
1756 // Nothing much to do here for now. | |
1665 } | 1757 } |
1666 | 1758 |
1667 base::PlatformFileError GDataFileSystem::RenameFileOnFilesystem( | 1759 base::PlatformFileError GDataFileSystem::RenameFileOnFilesystem( |
1668 const FilePath& file_path, | 1760 const FilePath& file_path, |
1669 const FilePath::StringType& new_name, | 1761 const FilePath::StringType& new_name, |
1670 FilePath* updated_file_path) { | 1762 FilePath* updated_file_path) { |
1671 DCHECK(updated_file_path); | 1763 DCHECK(updated_file_path); |
1672 | 1764 |
1673 base::AutoLock lock(lock_); | 1765 base::AutoLock lock(lock_); |
1674 GDataFileBase* file = GetGDataFileInfoFromPath(file_path); | 1766 GDataFileBase* file = GetGDataFileInfoFromPath(file_path); |
(...skipping 663 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2338 | 2430 |
2339 GDataFileSystemFactory::~GDataFileSystemFactory() { | 2431 GDataFileSystemFactory::~GDataFileSystemFactory() { |
2340 } | 2432 } |
2341 | 2433 |
2342 ProfileKeyedService* GDataFileSystemFactory::BuildServiceInstanceFor( | 2434 ProfileKeyedService* GDataFileSystemFactory::BuildServiceInstanceFor( |
2343 Profile* profile) const { | 2435 Profile* profile) const { |
2344 return new GDataFileSystem(profile, new DocumentsService); | 2436 return new GDataFileSystem(profile, new DocumentsService); |
2345 } | 2437 } |
2346 | 2438 |
2347 } // namespace gdata | 2439 } // namespace gdata |
OLD | NEW |