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 <set> | 7 #include <set> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 // | 183 // |
184 // |callback| is run on the thread represented by |relay_proxy|. | 184 // |callback| is run on the thread represented by |relay_proxy|. |
185 void OnTransferRegularFileCompleteForCopy( | 185 void OnTransferRegularFileCompleteForCopy( |
186 const FileOperationCallback& callback, | 186 const FileOperationCallback& callback, |
187 scoped_refptr<base::MessageLoopProxy> relay_proxy, | 187 scoped_refptr<base::MessageLoopProxy> relay_proxy, |
188 base::PlatformFileError error) { | 188 base::PlatformFileError error) { |
189 if (!callback.is_null()) | 189 if (!callback.is_null()) |
190 relay_proxy->PostTask(FROM_HERE, base::Bind(callback, error)); | 190 relay_proxy->PostTask(FROM_HERE, base::Bind(callback, error)); |
191 } | 191 } |
192 | 192 |
193 // Gets a cache entry from a GDataCache, must be called on the blocking pool. | |
194 // The result value is copied to cache_entry on success. | |
195 void GetCacheEntryOnBlockingPool( | |
196 GDataCache* cache, | |
197 const std::string& resource_id, | |
198 const std::string& md5, | |
199 GDataCache::CacheEntry* cache_entry, | |
200 bool* success) { | |
201 scoped_ptr<GDataCache::CacheEntry> value( | |
202 cache->GetCacheEntry(resource_id, md5)); | |
203 *success = value.get(); | |
204 if (*success) | |
205 *cache_entry = *value; | |
206 } | |
207 | |
208 // Runs GetFileCallback with pointers dereferenced. | 193 // Runs GetFileCallback with pointers dereferenced. |
209 // Used for PostTaskAndReply(). | 194 // Used for PostTaskAndReply(). |
210 void RunGetFileCallbackHelper(const GetFileCallback& callback, | 195 void RunGetFileCallbackHelper(const GetFileCallback& callback, |
211 base::PlatformFileError* error, | 196 base::PlatformFileError* error, |
212 FilePath* file_path, | 197 FilePath* file_path, |
213 std::string* mime_type, | 198 std::string* mime_type, |
214 GDataFileType* file_type) { | 199 GDataFileType* file_type) { |
215 DCHECK(error); | 200 DCHECK(error); |
216 DCHECK(file_path); | 201 DCHECK(file_path); |
217 DCHECK(mime_type); | 202 DCHECK(mime_type); |
(...skipping 28 matching lines...) Expand all Loading... |
246 // OnTransferCompleted. | 231 // OnTransferCompleted. |
247 void OnAddUploadFileCompleted( | 232 void OnAddUploadFileCompleted( |
248 const FileOperationCallback& callback, | 233 const FileOperationCallback& callback, |
249 base::PlatformFileError error, | 234 base::PlatformFileError error, |
250 scoped_ptr<UploadFileInfo> /* upload_file_info */){ | 235 scoped_ptr<UploadFileInfo> /* upload_file_info */){ |
251 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 236 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
252 if (!callback.is_null()) | 237 if (!callback.is_null()) |
253 callback.Run(error); | 238 callback.Run(error); |
254 } | 239 } |
255 | 240 |
256 // Used to implement GetCacheState. | |
257 void RunGetCacheStateCallbackHelper( | |
258 const GetCacheStateCallback& callback, | |
259 GDataCache::CacheEntry* cache_entry, | |
260 bool* success) { | |
261 DCHECK(cache_entry); | |
262 DCHECK(success); | |
263 if (callback.is_null()) | |
264 return; | |
265 | |
266 callback.Run( | |
267 base::PLATFORM_FILE_OK, | |
268 *success ? cache_entry->cache_state : GDataCache::CACHE_STATE_NONE); | |
269 } | |
270 | |
271 // The class to wait for the initial load of root feed and runs the callback | 241 // The class to wait for the initial load of root feed and runs the callback |
272 // after the initialization. | 242 // after the initialization. |
273 class InitialLoadObserver : public GDataFileSystemInterface::Observer { | 243 class InitialLoadObserver : public GDataFileSystemInterface::Observer { |
274 public: | 244 public: |
275 InitialLoadObserver(GDataFileSystemInterface* file_system, | 245 InitialLoadObserver(GDataFileSystemInterface* file_system, |
276 const base::Closure& callback) | 246 const base::Closure& callback) |
277 : file_system_(file_system), callback_(callback) {} | 247 : file_system_(file_system), callback_(callback) {} |
278 | 248 |
279 virtual void OnInitialLoadFinished() OVERRIDE { | 249 virtual void OnInitialLoadFinished() OVERRIDE { |
280 if (!callback_.is_null()) | 250 if (!callback_.is_null()) |
(...skipping 2004 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2285 upload_file_info_ptr->gdata_path.DirName(), | 2255 upload_file_info_ptr->gdata_path.DirName(), |
2286 upload_file_info_ptr->entry.get(), | 2256 upload_file_info_ptr->entry.get(), |
2287 upload_file_info_ptr->file_path, | 2257 upload_file_info_ptr->file_path, |
2288 GDataCache::FILE_OPERATION_MOVE, | 2258 GDataCache::FILE_OPERATION_MOVE, |
2289 base::Bind(&OnAddUploadFileCompleted, | 2259 base::Bind(&OnAddUploadFileCompleted, |
2290 callback, | 2260 callback, |
2291 error, | 2261 error, |
2292 base::Passed(&upload_file_info))); | 2262 base::Passed(&upload_file_info))); |
2293 } | 2263 } |
2294 | 2264 |
2295 void GDataFileSystem::GetCacheState(const std::string& resource_id, | |
2296 const std::string& md5, | |
2297 const GetCacheStateCallback& callback) { | |
2298 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || | |
2299 BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
2300 | |
2301 // Always post a task to the UI thread to call GetCacheStateOnUIThread even if | |
2302 // GetCacheState is called on the UI thread. This ensures that, regardless of | |
2303 // whether GDataFileSystem is locked or not, GDataFileSystem is unlocked when | |
2304 // GetCacheStateOnUIThread is called. | |
2305 const bool posted = BrowserThread::PostTask( | |
2306 BrowserThread::UI, | |
2307 FROM_HERE, | |
2308 base::Bind(&GDataFileSystem::GetCacheStateOnUIThread, | |
2309 ui_weak_ptr_, | |
2310 resource_id, | |
2311 md5, | |
2312 CreateRelayCallback(callback))); | |
2313 DCHECK(posted); | |
2314 } | |
2315 | |
2316 void GDataFileSystem::GetCacheStateOnUIThread( | |
2317 const std::string& resource_id, | |
2318 const std::string& md5, | |
2319 const GetCacheStateCallback& callback) { | |
2320 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
2321 | |
2322 GDataEntry* entry = root_->GetEntryByResourceId(resource_id); | |
2323 if (!entry || !entry->AsGDataFile()) { | |
2324 const bool posted = BrowserThread::PostTask( | |
2325 BrowserThread::UI, | |
2326 FROM_HERE, | |
2327 base::Bind(callback, | |
2328 base::PLATFORM_FILE_ERROR_NOT_FOUND, | |
2329 GDataCache::CACHE_STATE_NONE)); | |
2330 DCHECK(posted); | |
2331 return; | |
2332 } | |
2333 | |
2334 GDataCache::CacheEntry* cache_entry = new GDataCache::CacheEntry; | |
2335 bool* success = new bool(false); | |
2336 PostBlockingPoolSequencedTaskAndReply( | |
2337 FROM_HERE, | |
2338 sequence_token_, | |
2339 base::Bind(&GetCacheEntryOnBlockingPool, | |
2340 cache_, | |
2341 resource_id, | |
2342 md5, | |
2343 cache_entry, | |
2344 success), | |
2345 base::Bind(&RunGetCacheStateCallbackHelper, | |
2346 callback, | |
2347 base::Owned(cache_entry), | |
2348 base::Owned(success))); | |
2349 } | |
2350 | |
2351 void GDataFileSystem::GetAvailableSpace( | 2265 void GDataFileSystem::GetAvailableSpace( |
2352 const GetAvailableSpaceCallback& callback) { | 2266 const GetAvailableSpaceCallback& callback) { |
2353 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || | 2267 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || |
2354 BrowserThread::CurrentlyOn(BrowserThread::IO)); | 2268 BrowserThread::CurrentlyOn(BrowserThread::IO)); |
2355 RunTaskOnUIThread(base::Bind(&GDataFileSystem::GetAvailableSpaceOnUIThread, | 2269 RunTaskOnUIThread(base::Bind(&GDataFileSystem::GetAvailableSpaceOnUIThread, |
2356 ui_weak_ptr_, | 2270 ui_weak_ptr_, |
2357 CreateRelayCallback(callback))); | 2271 CreateRelayCallback(callback))); |
2358 } | 2272 } |
2359 | 2273 |
2360 void GDataFileSystem::GetAvailableSpaceOnUIThread( | 2274 void GDataFileSystem::GetAvailableSpaceOnUIThread( |
(...skipping 479 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2840 void GDataFileSystem::OnFileDownloaded( | 2754 void GDataFileSystem::OnFileDownloaded( |
2841 const GetFileFromCacheParams& params, | 2755 const GetFileFromCacheParams& params, |
2842 GDataErrorCode status, | 2756 GDataErrorCode status, |
2843 const GURL& content_url, | 2757 const GURL& content_url, |
2844 const FilePath& downloaded_file_path) { | 2758 const FilePath& downloaded_file_path) { |
2845 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 2759 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
2846 | 2760 |
2847 // If user cancels download of a pinned-but-not-fetched file, mark file as | 2761 // If user cancels download of a pinned-but-not-fetched file, mark file as |
2848 // unpinned so that we do not sync the file again. | 2762 // unpinned so that we do not sync the file again. |
2849 if (status == GDATA_CANCELLED) { | 2763 if (status == GDATA_CANCELLED) { |
2850 GDataCache::CacheEntry* cache_entry = new GDataCache::CacheEntry; | 2764 cache_->GetCacheEntryOnUIThread( |
2851 bool* success = new bool(false); | 2765 params.resource_id, |
2852 PostBlockingPoolSequencedTaskAndReply( | 2766 params.md5, |
2853 FROM_HERE, | |
2854 sequence_token_, | |
2855 base::Bind(&GetCacheEntryOnBlockingPool, | |
2856 cache_, | |
2857 params.resource_id, | |
2858 params.md5, | |
2859 cache_entry, | |
2860 success), | |
2861 base::Bind(&GDataFileSystem::UnpinIfPinned, | 2767 base::Bind(&GDataFileSystem::UnpinIfPinned, |
2862 ui_weak_ptr_, | 2768 ui_weak_ptr_, |
2863 params.resource_id, | 2769 params.resource_id, |
2864 params.md5, | 2770 params.md5)); |
2865 base::Owned(cache_entry), | |
2866 base::Owned(success))); | |
2867 } | 2771 } |
2868 | 2772 |
2869 // At this point, the disk can be full or nearly full for several reasons: | 2773 // At this point, the disk can be full or nearly full for several reasons: |
2870 // - The expected file size was incorrect and the file was larger | 2774 // - The expected file size was incorrect and the file was larger |
2871 // - There was an in-flight download operation and it used up space | 2775 // - There was an in-flight download operation and it used up space |
2872 // - The disk became full for some user actions we cannot control | 2776 // - The disk became full for some user actions we cannot control |
2873 // (ex. the user might have downloaded a large file from a regular web site) | 2777 // (ex. the user might have downloaded a large file from a regular web site) |
2874 // | 2778 // |
2875 // If we don't have enough space, we return PLATFORM_FILE_ERROR_NO_SPACE, | 2779 // If we don't have enough space, we return PLATFORM_FILE_ERROR_NO_SPACE, |
2876 // and try to free up space, even if the file was downloaded successfully. | 2780 // and try to free up space, even if the file was downloaded successfully. |
2877 bool* has_enough_space = new bool(false); | 2781 bool* has_enough_space = new bool(false); |
2878 PostBlockingPoolSequencedTaskAndReply( | 2782 PostBlockingPoolSequencedTaskAndReply( |
2879 FROM_HERE, | 2783 FROM_HERE, |
2880 sequence_token_, | 2784 sequence_token_, |
2881 base::Bind(&GDataCache::FreeDiskSpaceIfNeededFor, | 2785 base::Bind(&GDataCache::FreeDiskSpaceIfNeededFor, |
2882 base::Unretained(cache_), | 2786 base::Unretained(cache_), |
2883 0, | 2787 0, |
2884 has_enough_space), | 2788 has_enough_space), |
2885 base::Bind(&GDataFileSystem::OnFileDownloadedAndSpaceChecked, | 2789 base::Bind(&GDataFileSystem::OnFileDownloadedAndSpaceChecked, |
2886 ui_weak_ptr_, | 2790 ui_weak_ptr_, |
2887 params, | 2791 params, |
2888 status, | 2792 status, |
2889 content_url, | 2793 content_url, |
2890 downloaded_file_path, | 2794 downloaded_file_path, |
2891 base::Owned(has_enough_space))); | 2795 base::Owned(has_enough_space))); |
2892 } | 2796 } |
2893 | 2797 |
2894 void GDataFileSystem::UnpinIfPinned(const std::string& resource_id, | 2798 void GDataFileSystem::UnpinIfPinned( |
2895 const std::string& md5, | 2799 const std::string& resource_id, |
2896 GDataCache::CacheEntry* cache_entry, | 2800 const std::string& md5, |
2897 bool* cache_entry_is_valid) { | 2801 bool success, |
| 2802 const GDataCache::CacheEntry& cache_entry) { |
2898 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 2803 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
2899 // TODO(hshi): http://crbug.com/127138 notify when file properties change. | 2804 // TODO(hshi): http://crbug.com/127138 notify when file properties change. |
2900 // This allows file manager to clear the "Available offline" checkbox. | 2805 // This allows file manager to clear the "Available offline" checkbox. |
2901 if (*cache_entry_is_valid && cache_entry->IsPinned()) | 2806 if (success && cache_entry.IsPinned()) |
2902 cache_->UnpinOnUIThread(resource_id, md5, CacheOperationCallback()); | 2807 cache_->UnpinOnUIThread(resource_id, md5, CacheOperationCallback()); |
2903 } | 2808 } |
2904 | 2809 |
2905 void GDataFileSystem::OnFileDownloadedAndSpaceChecked( | 2810 void GDataFileSystem::OnFileDownloadedAndSpaceChecked( |
2906 const GetFileFromCacheParams& params, | 2811 const GetFileFromCacheParams& params, |
2907 GDataErrorCode status, | 2812 GDataErrorCode status, |
2908 const GURL& content_url, | 2813 const GURL& content_url, |
2909 const FilePath& downloaded_file_path, | 2814 const FilePath& downloaded_file_path, |
2910 bool* has_enough_space) { | 2815 bool* has_enough_space) { |
2911 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 2816 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
(...skipping 947 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3859 // must go through here. Removes the |file_path| from the remembered set so | 3764 // must go through here. Removes the |file_path| from the remembered set so |
3860 // that subsequent operations can open the file again. | 3765 // that subsequent operations can open the file again. |
3861 open_files_.erase(file_path); | 3766 open_files_.erase(file_path); |
3862 | 3767 |
3863 // Then invokes the user-supplied callback function. | 3768 // Then invokes the user-supplied callback function. |
3864 if (!callback.is_null()) | 3769 if (!callback.is_null()) |
3865 callback.Run(result); | 3770 callback.Run(result); |
3866 } | 3771 } |
3867 | 3772 |
3868 } // namespace gdata | 3773 } // namespace gdata |
OLD | NEW |