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 2120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2131 lock_.AssertAcquired(); | 2131 lock_.AssertAcquired(); |
2132 // Find directory element within the cached file system snapshot. | 2132 // Find directory element within the cached file system snapshot. |
2133 ReadOnlyFindEntryDelegate find_delegate; | 2133 ReadOnlyFindEntryDelegate find_delegate; |
2134 root_->FindEntryByPath(file_path, &find_delegate); | 2134 root_->FindEntryByPath(file_path, &find_delegate); |
2135 return find_delegate.entry(); | 2135 return find_delegate.entry(); |
2136 } | 2136 } |
2137 | 2137 |
2138 void GDataFileSystem::GetCacheState(const std::string& resource_id, | 2138 void GDataFileSystem::GetCacheState(const std::string& resource_id, |
2139 const std::string& md5, | 2139 const std::string& md5, |
2140 const GetCacheStateCallback& callback) { | 2140 const GetCacheStateCallback& callback) { |
2141 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 2141 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || |
2142 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 2142 BrowserThread::CurrentlyOn(BrowserThread::IO)); |
2143 const bool posted = BrowserThread::PostTask( | |
2144 BrowserThread::UI, | |
2145 FROM_HERE, | |
2146 base::Bind(&GDataFileSystem::GetCacheStateOnUIThread, | |
2147 ui_weak_ptr_, | |
2148 resource_id, | |
2149 md5, | |
2150 base::Bind(&RelayGetCacheStateCallback, | |
2151 base::MessageLoopProxy::current(), | |
2152 callback))); | |
2153 DCHECK(posted); | |
2154 return; | |
2155 } | |
2156 | 2143 |
2157 GetCacheStateOnUIThread(resource_id, md5, callback); | 2144 // Always post a task to the UI thread to call GetCacheStateOnUIThread even if |
| 2145 // GetCacheState is called on the UI thread. This ensures that, regardless of |
| 2146 // whether GDataFileSystem is locked or not, GDataFileSystem is unlocked when |
| 2147 // GetCacheStateOnUIThread is called. |
| 2148 const bool posted = BrowserThread::PostTask( |
| 2149 BrowserThread::UI, |
| 2150 FROM_HERE, |
| 2151 base::Bind(&GDataFileSystem::GetCacheStateOnUIThread, |
| 2152 ui_weak_ptr_, |
| 2153 resource_id, |
| 2154 md5, |
| 2155 base::Bind(&RelayGetCacheStateCallback, |
| 2156 base::MessageLoopProxy::current(), |
| 2157 callback))); |
| 2158 DCHECK(posted); |
2158 } | 2159 } |
2159 | 2160 |
2160 void GDataFileSystem::GetCacheStateOnUIThread( | 2161 void GDataFileSystem::GetCacheStateOnUIThread( |
2161 const std::string& resource_id, | 2162 const std::string& resource_id, |
2162 const std::string& md5, | 2163 const std::string& md5, |
2163 const GetCacheStateCallback& callback) { | 2164 const GetCacheStateCallback& callback) { |
2164 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 2165 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
2165 | 2166 |
2166 // This method originates from GDataFile::GetCacheState, which already locks, | 2167 InitializeCacheIfNecessary(); |
2167 // so we shouldn't lock here. | |
2168 UnsafeInitializeCacheIfNecessary(); | |
2169 | 2168 |
2170 base::PlatformFileError* error = | 2169 base::PlatformFileError* error = |
2171 new base::PlatformFileError(base::PLATFORM_FILE_OK); | 2170 new base::PlatformFileError(base::PLATFORM_FILE_OK); |
2172 int* cache_state = new int(GDataFile::CACHE_STATE_NONE); | 2171 int* cache_state = new int(GDataFile::CACHE_STATE_NONE); |
2173 | 2172 |
2174 // GetCacheStateOnIOThreadPool won't do file IO, but post it to the thread | 2173 // GetCacheStateOnIOThreadPool won't do file IO, but post it to the thread |
2175 // pool, as it must be performed after the cache is initialized. | 2174 // pool, as it must be performed after the cache is initialized. |
2176 PostBlockingPoolSequencedTaskAndReply( | 2175 PostBlockingPoolSequencedTaskAndReply( |
2177 FROM_HERE, | 2176 FROM_HERE, |
2178 base::Bind(&GDataFileSystem::GetCacheStateOnIOThreadPool, | 2177 base::Bind(&GDataFileSystem::GetCacheStateOnIOThreadPool, |
(...skipping 1487 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3666 resource_id, | 3665 resource_id, |
3667 error), | 3666 error), |
3668 base::Bind(&RunCacheOperationCallbackHelper, | 3667 base::Bind(&RunCacheOperationCallbackHelper, |
3669 callback, | 3668 callback, |
3670 base::Owned(error), | 3669 base::Owned(error), |
3671 resource_id, | 3670 resource_id, |
3672 "" /* md5 */)); | 3671 "" /* md5 */)); |
3673 } | 3672 } |
3674 | 3673 |
3675 void GDataFileSystem::InitializeCacheIfNecessary() { | 3674 void GDataFileSystem::InitializeCacheIfNecessary() { |
3676 // Lock to access cache_initialization_started_; | 3675 // Lock to access cache_initialization_started_. |
3677 base::AutoLock lock(lock_); | 3676 base::AutoLock lock(lock_); |
3678 UnsafeInitializeCacheIfNecessary(); | 3677 |
| 3678 // Cache initialization is either in progress or has completed. |
| 3679 if (cache_initialization_started_) |
| 3680 return; |
| 3681 |
| 3682 // Need to initialize cache. |
| 3683 cache_initialization_started_ = true; |
| 3684 PostBlockingPoolSequencedTask( |
| 3685 FROM_HERE, |
| 3686 base::Bind(&GDataFileSystem::InitializeCacheOnIOThreadPool, |
| 3687 base::Unretained(this))); |
3679 } | 3688 } |
3680 | 3689 |
3681 //========= GDataFileSystem: Cache tasks that ran on io thread pool ============ | 3690 //========= GDataFileSystem: Cache tasks that ran on io thread pool ============ |
3682 | 3691 |
3683 void GDataFileSystem::InitializeCacheOnIOThreadPool() { | 3692 void GDataFileSystem::InitializeCacheOnIOThreadPool() { |
3684 base::PlatformFileError error = CreateCacheDirectories(cache_paths_); | 3693 base::PlatformFileError error = CreateCacheDirectories(cache_paths_); |
3685 if (error != base::PLATFORM_FILE_OK) | 3694 if (error != base::PLATFORM_FILE_OK) |
3686 return; | 3695 return; |
3687 | 3696 |
3688 // Change permissions of cache persistent directory to u+rwx,og+x in order to | 3697 // Change permissions of cache persistent directory to u+rwx,og+x in order to |
(...skipping 717 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4406 bool* has_enough_space = new bool(false); | 4415 bool* has_enough_space = new bool(false); |
4407 PostBlockingPoolSequencedTask( | 4416 PostBlockingPoolSequencedTask( |
4408 FROM_HERE, | 4417 FROM_HERE, |
4409 base::Bind(&GDataFileSystem::FreeDiskSpaceIfNeeded, | 4418 base::Bind(&GDataFileSystem::FreeDiskSpaceIfNeeded, |
4410 base::Unretained(this), | 4419 base::Unretained(this), |
4411 base::Owned(has_enough_space))); | 4420 base::Owned(has_enough_space))); |
4412 } | 4421 } |
4413 | 4422 |
4414 //============= GDataFileSystem: internal helper functions ===================== | 4423 //============= GDataFileSystem: internal helper functions ===================== |
4415 | 4424 |
4416 void GDataFileSystem::UnsafeInitializeCacheIfNecessary() { | |
4417 lock_.AssertAcquired(); | |
4418 | |
4419 if (cache_initialization_started_) { | |
4420 // Cache initialization is either in progress or has completed. | |
4421 return; | |
4422 } | |
4423 | |
4424 // Need to initialize cache. | |
4425 | |
4426 cache_initialization_started_ = true; | |
4427 | |
4428 PostBlockingPoolSequencedTask( | |
4429 FROM_HERE, | |
4430 base::Bind(&GDataFileSystem::InitializeCacheOnIOThreadPool, | |
4431 base::Unretained(this))); | |
4432 } | |
4433 | |
4434 void GDataFileSystem::ScanCacheDirectory( | 4425 void GDataFileSystem::ScanCacheDirectory( |
4435 GDataRootDirectory::CacheSubDirectoryType sub_dir_type, | 4426 GDataRootDirectory::CacheSubDirectoryType sub_dir_type, |
4436 GDataRootDirectory::CacheMap* cache_map) { | 4427 GDataRootDirectory::CacheMap* cache_map) { |
4437 file_util::FileEnumerator enumerator( | 4428 file_util::FileEnumerator enumerator( |
4438 GetCacheDirectoryPath(sub_dir_type), | 4429 GetCacheDirectoryPath(sub_dir_type), |
4439 false, // not recursive | 4430 false, // not recursive |
4440 static_cast<file_util::FileEnumerator::FileType>( | 4431 static_cast<file_util::FileEnumerator::FileType>( |
4441 file_util::FileEnumerator::FILES | | 4432 file_util::FileEnumerator::FILES | |
4442 file_util::FileEnumerator::SHOW_SYM_LINKS), | 4433 file_util::FileEnumerator::SHOW_SYM_LINKS), |
4443 kWildCard); | 4434 kWildCard); |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4581 pref_registrar_->Init(profile_->GetPrefs()); | 4572 pref_registrar_->Init(profile_->GetPrefs()); |
4582 pref_registrar_->Add(prefs::kDisableGDataHostedFiles, this); | 4573 pref_registrar_->Add(prefs::kDisableGDataHostedFiles, this); |
4583 } | 4574 } |
4584 | 4575 |
4585 void SetFreeDiskSpaceGetterForTesting(FreeDiskSpaceGetterInterface* getter) { | 4576 void SetFreeDiskSpaceGetterForTesting(FreeDiskSpaceGetterInterface* getter) { |
4586 delete global_free_disk_getter_for_testing; // Safe to delete NULL; | 4577 delete global_free_disk_getter_for_testing; // Safe to delete NULL; |
4587 global_free_disk_getter_for_testing = getter; | 4578 global_free_disk_getter_for_testing = getter; |
4588 } | 4579 } |
4589 | 4580 |
4590 } // namespace gdata | 4581 } // namespace gdata |
OLD | NEW |