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 #include <sys/stat.h> | 8 #include <sys/stat.h> |
9 | 9 |
10 #include <set> | 10 #include <set> |
(...skipping 1669 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1680 | 1680 |
1681 base::PlatformFileError* error = | 1681 base::PlatformFileError* error = |
1682 new base::PlatformFileError(base::PLATFORM_FILE_OK); | 1682 new base::PlatformFileError(base::PLATFORM_FILE_OK); |
1683 FilePath* temp_file_path = new FilePath; | 1683 FilePath* temp_file_path = new FilePath; |
1684 std::string* mime_type = new std::string; | 1684 std::string* mime_type = new std::string; |
1685 GDataFileType* file_type = new GDataFileType(REGULAR_FILE); | 1685 GDataFileType* file_type = new GDataFileType(REGULAR_FILE); |
1686 PostBlockingPoolSequencedTaskAndReply( | 1686 PostBlockingPoolSequencedTaskAndReply( |
1687 kGDataFileSystemToken, | 1687 kGDataFileSystemToken, |
1688 FROM_HERE, | 1688 FROM_HERE, |
1689 base::Bind(&GDataFileSystem::CreateDocumentJsonFileOnIOThreadPool, | 1689 base::Bind(&GDataFileSystem::CreateDocumentJsonFileOnIOThreadPool, |
1690 GetGDataTempDocumentFolderPath(), | 1690 GetCacheDirectoryPath( |
1691 GDataRootDirectory::CACHE_TYPE_TMP_DOCUMENTS), | |
1691 file_properties.alternate_url, | 1692 file_properties.alternate_url, |
1692 file_properties.resource_id, | 1693 file_properties.resource_id, |
1693 error, | 1694 error, |
1694 temp_file_path, | 1695 temp_file_path, |
1695 mime_type, | 1696 mime_type, |
1696 file_type), | 1697 file_type), |
1697 base::Bind(&RunGetFileCallbackHelper, | 1698 base::Bind(&RunGetFileCallbackHelper, |
1698 callback, | 1699 callback, |
1699 base::Owned(error), | 1700 base::Owned(error), |
1700 base::Owned(temp_file_path), | 1701 base::Owned(temp_file_path), |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1810 *has_enough_space = HasEnoughSpaceFor(num_bytes); | 1811 *has_enough_space = HasEnoughSpaceFor(num_bytes); |
1811 if (*has_enough_space) | 1812 if (*has_enough_space) |
1812 return; | 1813 return; |
1813 | 1814 |
1814 // Otherwise, try to free up the disk space. | 1815 // Otherwise, try to free up the disk space. |
1815 DVLOG(1) << "Freeing up disk space for " << num_bytes; | 1816 DVLOG(1) << "Freeing up disk space for " << num_bytes; |
1816 base::AutoLock lock(lock_); // To access the cache map. | 1817 base::AutoLock lock(lock_); // To access the cache map. |
1817 // First remove temporary files from the cache map. | 1818 // First remove temporary files from the cache map. |
1818 root_->RemoveTemporaryFilesFromCacheMap(); | 1819 root_->RemoveTemporaryFilesFromCacheMap(); |
1819 // Then remove all files under "tmp" directory. | 1820 // Then remove all files under "tmp" directory. |
1820 RemoveAllFiles(GetGDataCacheTmpDirectory()); | 1821 RemoveAllFiles(GetCacheDirectoryPath(GDataRootDirectory::CACHE_TYPE_TMP)); |
1821 | 1822 |
1822 // Check the disk space again. | 1823 // Check the disk space again. |
1823 *has_enough_space = HasEnoughSpaceFor(num_bytes); | 1824 *has_enough_space = HasEnoughSpaceFor(num_bytes); |
1824 } | 1825 } |
1825 | 1826 |
1826 void GDataFileSystem::FreeDiskSpaceIfNeeded(bool* has_enough_space) { | 1827 void GDataFileSystem::FreeDiskSpaceIfNeeded(bool* has_enough_space) { |
1827 FreeDiskSpaceIfNeededFor(0, has_enough_space); | 1828 FreeDiskSpaceIfNeededFor(0, has_enough_space); |
1828 } | 1829 } |
1829 | 1830 |
1830 void GDataFileSystem::StartDownloadFileIfEnoughSpace( | 1831 void GDataFileSystem::StartDownloadFileIfEnoughSpace( |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2002 if (regular_file) { | 2003 if (regular_file) { |
2003 properties->file_md5 = regular_file->file_md5(); | 2004 properties->file_md5 = regular_file->file_md5(); |
2004 properties->mime_type = regular_file->content_mime_type(); | 2005 properties->mime_type = regular_file->content_mime_type(); |
2005 properties->content_url = regular_file->content_url(); | 2006 properties->content_url = regular_file->content_url(); |
2006 properties->alternate_url = regular_file->alternate_url(); | 2007 properties->alternate_url = regular_file->alternate_url(); |
2007 properties->is_hosted_document = regular_file->is_hosted_document(); | 2008 properties->is_hosted_document = regular_file->is_hosted_document(); |
2008 } | 2009 } |
2009 return true; | 2010 return true; |
2010 } | 2011 } |
2011 | 2012 |
2012 FilePath GDataFileSystem::GetGDataCacheTmpDirectory() const { | |
2013 return cache_paths_[GDataRootDirectory::CACHE_TYPE_TMP]; | |
2014 } | |
2015 | |
2016 FilePath GDataFileSystem::GetGDataTempDownloadFolderPath() const { | |
2017 return cache_paths_[GDataRootDirectory::CACHE_TYPE_TMP_DOWNLOADS]; | |
2018 } | |
2019 | |
2020 FilePath GDataFileSystem::GetGDataTempDocumentFolderPath() const { | |
2021 return cache_paths_[GDataRootDirectory::CACHE_TYPE_TMP_DOCUMENTS]; | |
2022 } | |
2023 | |
2024 FilePath GDataFileSystem::GetGDataCachePinnedDirectory() const { | |
2025 return cache_paths_[GDataRootDirectory::CACHE_TYPE_PINNED]; | |
2026 } | |
2027 | |
2028 FilePath GDataFileSystem::GetGDataCachePersistentDirectory() const { | |
2029 return cache_paths_[GDataRootDirectory::CACHE_TYPE_PERSISTENT]; | |
2030 } | |
2031 | |
2032 base::WeakPtr<GDataFileSystem> GDataFileSystem::GetWeakPtrForCurrentThread() { | 2013 base::WeakPtr<GDataFileSystem> GDataFileSystem::GetWeakPtrForCurrentThread() { |
2033 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 2014 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
2034 return ui_weak_ptr_factory_->GetWeakPtr(); | 2015 return ui_weak_ptr_factory_->GetWeakPtr(); |
2035 } else if (BrowserThread::CurrentlyOn(BrowserThread::IO)) { | 2016 } else if (BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
2036 if (!io_weak_ptr_factory_.get()) { | 2017 if (!io_weak_ptr_factory_.get()) { |
2037 io_weak_ptr_factory_.reset( | 2018 io_weak_ptr_factory_.reset( |
2038 new base::WeakPtrFactory<GDataFileSystem>(this)); | 2019 new base::WeakPtrFactory<GDataFileSystem>(this)); |
2039 } | 2020 } |
2040 return io_weak_ptr_factory_->GetWeakPtr(); | 2021 return io_weak_ptr_factory_->GetWeakPtr(); |
2041 } | 2022 } |
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2295 if (!params->callback.is_null()) { | 2276 if (!params->callback.is_null()) { |
2296 FindEntryByPathOnCallingThread(params->search_file_path, params->callback); | 2277 FindEntryByPathOnCallingThread(params->search_file_path, params->callback); |
2297 } | 2278 } |
2298 } | 2279 } |
2299 | 2280 |
2300 void GDataFileSystem::LoadRootFeedFromCache( | 2281 void GDataFileSystem::LoadRootFeedFromCache( |
2301 bool should_load_from_server, | 2282 bool should_load_from_server, |
2302 const FilePath& search_file_path, | 2283 const FilePath& search_file_path, |
2303 const FindEntryCallback& callback) { | 2284 const FindEntryCallback& callback) { |
2304 const FilePath path = | 2285 const FilePath path = |
2305 cache_paths_[GDataRootDirectory::CACHE_TYPE_META].Append( | 2286 GetCacheDirectoryPath(GDataRootDirectory::CACHE_TYPE_META).Append( |
2306 kFilesystemProtoFile); | 2287 kFilesystemProtoFile); |
2307 LoadRootFeedParams* params = new LoadRootFeedParams(search_file_path, | 2288 LoadRootFeedParams* params = new LoadRootFeedParams(search_file_path, |
2308 should_load_from_server, | 2289 should_load_from_server, |
2309 callback); | 2290 callback); |
2310 BrowserThread::GetBlockingPool()->PostTaskAndReply(FROM_HERE, | 2291 BrowserThread::GetBlockingPool()->PostTaskAndReply(FROM_HERE, |
2311 base::Bind(&LoadProtoOnIOThreadPool, path, params), | 2292 base::Bind(&LoadProtoOnIOThreadPool, path, params), |
2312 base::Bind(&GDataFileSystem::OnProtoLoaded, | 2293 base::Bind(&GDataFileSystem::OnProtoLoaded, |
2313 GetWeakPtrForCurrentThread(), | 2294 GetWeakPtrForCurrentThread(), |
2314 base::Owned(params))); | 2295 base::Owned(params))); |
2315 } | 2296 } |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2369 void GDataFileSystem::SaveFileSystemAsProto() { | 2350 void GDataFileSystem::SaveFileSystemAsProto() { |
2370 DVLOG(1) << "SaveFileSystemAsProto"; | 2351 DVLOG(1) << "SaveFileSystemAsProto"; |
2371 base::AutoLock lock(lock_); // To access root_. | 2352 base::AutoLock lock(lock_); // To access root_. |
2372 | 2353 |
2373 if (!ShouldSerializeFileSystemNow(root_->serialized_size(), | 2354 if (!ShouldSerializeFileSystemNow(root_->serialized_size(), |
2374 root_->last_serialized())) { | 2355 root_->last_serialized())) { |
2375 return; | 2356 return; |
2376 } | 2357 } |
2377 | 2358 |
2378 const FilePath path = | 2359 const FilePath path = |
2379 cache_paths_[GDataRootDirectory::CACHE_TYPE_META].Append( | 2360 GetCacheDirectoryPath(GDataRootDirectory::CACHE_TYPE_META).Append( |
2380 kFilesystemProtoFile); | 2361 kFilesystemProtoFile); |
2381 scoped_ptr<std::string> serialized_proto(new std::string()); | 2362 scoped_ptr<std::string> serialized_proto(new std::string()); |
2382 root_->SerializeToString(serialized_proto.get()); | 2363 root_->SerializeToString(serialized_proto.get()); |
2383 root_->set_last_serialized(base::Time::Now()); | 2364 root_->set_last_serialized(base::Time::Now()); |
2384 root_->set_serialized_size(serialized_proto->size()); | 2365 root_->set_serialized_size(serialized_proto->size()); |
2385 PostBlockingPoolSequencedTask(kGDataFileSystemToken, FROM_HERE, | 2366 PostBlockingPoolSequencedTask(kGDataFileSystemToken, FROM_HERE, |
2386 base::Bind(&SaveProtoOnIOThreadPool, path, | 2367 base::Bind(&SaveProtoOnIOThreadPool, path, |
2387 base::Passed(serialized_proto.Pass()))); | 2368 base::Passed(serialized_proto.Pass()))); |
2388 } | 2369 } |
2389 | 2370 |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2494 } | 2475 } |
2495 | 2476 |
2496 void GDataFileSystem::SaveFeed(scoped_ptr<base::Value> feed, | 2477 void GDataFileSystem::SaveFeed(scoped_ptr<base::Value> feed, |
2497 const FilePath& name) { | 2478 const FilePath& name) { |
2498 InitializeCacheIfNecessary(); | 2479 InitializeCacheIfNecessary(); |
2499 | 2480 |
2500 PostBlockingPoolSequencedTask( | 2481 PostBlockingPoolSequencedTask( |
2501 kGDataFileSystemToken, | 2482 kGDataFileSystemToken, |
2502 FROM_HERE, | 2483 FROM_HERE, |
2503 base::Bind(&SaveFeedOnIOThreadPool, | 2484 base::Bind(&SaveFeedOnIOThreadPool, |
2504 cache_paths_[GDataRootDirectory::CACHE_TYPE_META].Append( | 2485 GetCacheDirectoryPath( |
2505 name), | 2486 GDataRootDirectory::CACHE_TYPE_META).Append(name), |
2506 base::Passed(&feed))); | 2487 base::Passed(&feed))); |
2507 } | 2488 } |
2508 | 2489 |
2509 void GDataFileSystem::OnRemovedDocument( | 2490 void GDataFileSystem::OnRemovedDocument( |
2510 const FileOperationCallback& callback, | 2491 const FileOperationCallback& callback, |
2511 const FilePath& file_path, | 2492 const FilePath& file_path, |
2512 GDataErrorCode status, | 2493 GDataErrorCode status, |
2513 const GURL& document_url) { | 2494 const GURL& document_url) { |
2514 base::PlatformFileError error = GDataToPlatformError(status); | 2495 base::PlatformFileError error = GDataToPlatformError(status); |
2515 | 2496 |
(...skipping 680 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3196 hide_hosted_docs_ = hide; | 3177 hide_hosted_docs_ = hide; |
3197 root_path = root_->GetFilePath(); | 3178 root_path = root_->GetFilePath(); |
3198 } | 3179 } |
3199 | 3180 |
3200 // Kick of directory refresh when this setting changes. | 3181 // Kick of directory refresh when this setting changes. |
3201 NotifyDirectoryChanged(root_path); | 3182 NotifyDirectoryChanged(root_path); |
3202 } | 3183 } |
3203 | 3184 |
3204 //===================== GDataFileSystem: Cache entry points ==================== | 3185 //===================== GDataFileSystem: Cache entry points ==================== |
3205 | 3186 |
3187 FilePath GDataFileSystem::GetCacheDirectoryPath( | |
3188 GDataRootDirectory::CacheSubDirectoryType sub_dir_type) const { | |
3189 return cache_paths_[sub_dir_type]; | |
achuithb
2012/04/23 23:48:02
Are there any DCHECKs that might be appropriate he
satorux1
2012/04/24 00:04:36
Sounds good. Added.
| |
3190 } | |
3191 | |
3206 FilePath GDataFileSystem::GetCacheFilePath( | 3192 FilePath GDataFileSystem::GetCacheFilePath( |
3207 const std::string& resource_id, | 3193 const std::string& resource_id, |
3208 const std::string& md5, | 3194 const std::string& md5, |
3209 GDataRootDirectory::CacheSubDirectoryType sub_dir_type, | 3195 GDataRootDirectory::CacheSubDirectoryType sub_dir_type, |
3210 CachedFileOrigin file_origin) const { | 3196 CachedFileOrigin file_origin) const { |
3211 DCHECK(sub_dir_type != GDataRootDirectory::CACHE_TYPE_META); | 3197 DCHECK(sub_dir_type != GDataRootDirectory::CACHE_TYPE_META); |
3212 | 3198 |
3213 // Runs on any thread. | 3199 // Runs on any thread. |
3214 // Filename is formatted as resource_id.md5, i.e. resource_id is the base | 3200 // Filename is formatted as resource_id.md5, i.e. resource_id is the base |
3215 // name and md5 is the extension. | 3201 // name and md5 is the extension. |
3216 std::string base_name = GDataEntry::EscapeUtf8FileName(resource_id); | 3202 std::string base_name = GDataEntry::EscapeUtf8FileName(resource_id); |
3217 if (file_origin == CACHED_FILE_LOCALLY_MODIFIED) { | 3203 if (file_origin == CACHED_FILE_LOCALLY_MODIFIED) { |
3218 DCHECK(sub_dir_type == GDataRootDirectory::CACHE_TYPE_PERSISTENT); | 3204 DCHECK(sub_dir_type == GDataRootDirectory::CACHE_TYPE_PERSISTENT); |
3219 base_name += FilePath::kExtensionSeparator; | 3205 base_name += FilePath::kExtensionSeparator; |
3220 base_name += kLocallyModifiedFileExtension; | 3206 base_name += kLocallyModifiedFileExtension; |
3221 } else if (!md5.empty()) { | 3207 } else if (!md5.empty()) { |
3222 base_name += FilePath::kExtensionSeparator; | 3208 base_name += FilePath::kExtensionSeparator; |
3223 base_name += GDataEntry::EscapeUtf8FileName(md5); | 3209 base_name += GDataEntry::EscapeUtf8FileName(md5); |
3224 } | 3210 } |
3225 return cache_paths_[sub_dir_type].Append(base_name); | 3211 return GetCacheDirectoryPath(sub_dir_type).Append(base_name); |
3226 } | 3212 } |
3227 | 3213 |
3228 void GDataFileSystem::StoreToCache(const std::string& resource_id, | 3214 void GDataFileSystem::StoreToCache(const std::string& resource_id, |
3229 const std::string& md5, | 3215 const std::string& md5, |
3230 const FilePath& source_path, | 3216 const FilePath& source_path, |
3231 FileOperationType file_operation_type, | 3217 FileOperationType file_operation_type, |
3232 const CacheOperationCallback& callback) { | 3218 const CacheOperationCallback& callback) { |
3233 InitializeCacheIfNecessary(); | 3219 InitializeCacheIfNecessary(); |
3234 | 3220 |
3235 base::PlatformFileError* error = | 3221 base::PlatformFileError* error = |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3404 //========= GDataFileSystem: Cache tasks that ran on io thread pool ============ | 3390 //========= GDataFileSystem: Cache tasks that ran on io thread pool ============ |
3405 | 3391 |
3406 void GDataFileSystem::InitializeCacheOnIOThreadPool() { | 3392 void GDataFileSystem::InitializeCacheOnIOThreadPool() { |
3407 base::PlatformFileError error = CreateCacheDirectories(cache_paths_); | 3393 base::PlatformFileError error = CreateCacheDirectories(cache_paths_); |
3408 if (error != base::PLATFORM_FILE_OK) | 3394 if (error != base::PLATFORM_FILE_OK) |
3409 return; | 3395 return; |
3410 | 3396 |
3411 // Change permissions of cache persistent directory to u+rwx,og+x in order to | 3397 // Change permissions of cache persistent directory to u+rwx,og+x in order to |
3412 // allow archive files in that directory to be mounted by cros-disks. | 3398 // allow archive files in that directory to be mounted by cros-disks. |
3413 error = ChangeFilePermissions( | 3399 error = ChangeFilePermissions( |
3414 cache_paths_[GDataRootDirectory::CACHE_TYPE_PERSISTENT], | 3400 GetCacheDirectoryPath(GDataRootDirectory::CACHE_TYPE_PERSISTENT), |
3415 S_IRWXU | S_IXGRP | S_IXOTH); | 3401 S_IRWXU | S_IXGRP | S_IXOTH); |
3416 if (error != base::PLATFORM_FILE_OK) | 3402 if (error != base::PLATFORM_FILE_OK) |
3417 return; | 3403 return; |
3418 | 3404 |
3419 // Scan cache persistent and tmp directories to enumerate all files and create | 3405 // Scan cache persistent and tmp directories to enumerate all files and create |
3420 // corresponding entries for cache map. | 3406 // corresponding entries for cache map. |
3421 GDataRootDirectory::CacheMap cache_map; | 3407 GDataRootDirectory::CacheMap cache_map; |
3422 ScanCacheDirectory(GDataRootDirectory::CACHE_TYPE_PERSISTENT, &cache_map); | 3408 ScanCacheDirectory(GDataRootDirectory::CACHE_TYPE_PERSISTENT, &cache_map); |
3423 ScanCacheDirectory(GDataRootDirectory::CACHE_TYPE_TMP, &cache_map); | 3409 ScanCacheDirectory(GDataRootDirectory::CACHE_TYPE_TMP, &cache_map); |
3424 | 3410 |
(...skipping 718 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
4143 kGDataFileSystemToken, | 4129 kGDataFileSystemToken, |
4144 FROM_HERE, | 4130 FROM_HERE, |
4145 base::Bind(&GDataFileSystem::InitializeCacheOnIOThreadPool, | 4131 base::Bind(&GDataFileSystem::InitializeCacheOnIOThreadPool, |
4146 base::Unretained(this))); | 4132 base::Unretained(this))); |
4147 } | 4133 } |
4148 | 4134 |
4149 void GDataFileSystem::ScanCacheDirectory( | 4135 void GDataFileSystem::ScanCacheDirectory( |
4150 GDataRootDirectory::CacheSubDirectoryType sub_dir_type, | 4136 GDataRootDirectory::CacheSubDirectoryType sub_dir_type, |
4151 GDataRootDirectory::CacheMap* cache_map) { | 4137 GDataRootDirectory::CacheMap* cache_map) { |
4152 file_util::FileEnumerator enumerator( | 4138 file_util::FileEnumerator enumerator( |
4153 cache_paths_[sub_dir_type], | 4139 GetCacheDirectoryPath(sub_dir_type), |
4154 false, // not recursive | 4140 false, // not recursive |
4155 static_cast<file_util::FileEnumerator::FileType>( | 4141 static_cast<file_util::FileEnumerator::FileType>( |
4156 file_util::FileEnumerator::FILES | | 4142 file_util::FileEnumerator::FILES | |
4157 file_util::FileEnumerator::SHOW_SYM_LINKS), | 4143 file_util::FileEnumerator::SHOW_SYM_LINKS), |
4158 kWildCard); | 4144 kWildCard); |
4159 for (FilePath current = enumerator.Next(); !current.empty(); | 4145 for (FilePath current = enumerator.Next(); !current.empty(); |
4160 current = enumerator.Next()) { | 4146 current = enumerator.Next()) { |
4161 // Extract resource_id and md5 from filename. | 4147 // Extract resource_id and md5 from filename. |
4162 FilePath base_name = current.BaseName(); | 4148 FilePath base_name = current.BaseName(); |
4163 std::string resource_id; | 4149 std::string resource_id; |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
4306 pref_registrar_->Init(profile_->GetPrefs()); | 4292 pref_registrar_->Init(profile_->GetPrefs()); |
4307 pref_registrar_->Add(prefs::kDisableGDataHostedFiles, this); | 4293 pref_registrar_->Add(prefs::kDisableGDataHostedFiles, this); |
4308 } | 4294 } |
4309 | 4295 |
4310 void SetFreeDiskSpaceGetterForTesting(FreeDiskSpaceGetterInterface* getter) { | 4296 void SetFreeDiskSpaceGetterForTesting(FreeDiskSpaceGetterInterface* getter) { |
4311 delete global_free_disk_getter_for_testing; // Safe to delete NULL; | 4297 delete global_free_disk_getter_for_testing; // Safe to delete NULL; |
4312 global_free_disk_getter_for_testing = getter; | 4298 global_free_disk_getter_for_testing = getter; |
4313 } | 4299 } |
4314 | 4300 |
4315 } // namespace gdata | 4301 } // namespace gdata |
OLD | NEW |