Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/chromeos/file_manager/snapshot_manager.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/files/file_path.h" | |
| 9 #include "chrome/browser/chromeos/file_manager/app_id.h" | |
| 10 #include "chrome/browser/chromeos/file_manager/fileapi_util.h" | |
| 11 #include "chrome/browser/profiles/profile.h" | |
|
hashimoto
2014/06/19 06:16:51
nit: No need to include this? Seems this class is
kinaba
2014/06/20 01:17:08
Done.
| |
| 12 #include "content/public/browser/browser_thread.h" | |
| 13 #include "google_apis/drive/task_util.h" | |
| 14 | |
| 15 namespace file_manager { | |
| 16 namespace { | |
| 17 | |
| 18 // Part of CreateManagedSnapshot. Runs CreateSnapshotFile method of fileapi. | |
| 19 void CreateSnapshotFileOnIOThread( | |
| 20 scoped_refptr<fileapi::FileSystemContext> context, | |
| 21 const fileapi::FileSystemURL& url, | |
| 22 const fileapi::FileSystemOperation::SnapshotFileCallback& callback) { | |
|
hashimoto
2014/06/19 06:16:51
q: Are you going to implement a mechanism to evict
kinaba
2014/06/19 08:47:58
Yes. Filed Bug 386519.
| |
| 23 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 24 context->operation_runner()->CreateSnapshotFile(url, callback); | |
| 25 } | |
| 26 | |
| 27 // Utility for destructing the bound |file_refs| on IO thread. This is meant | |
| 28 // to be used together with base::Bind. After this function finishes, the | |
| 29 // Bind callback should destruct the bound argument. | |
| 30 void FreeReferenceOnIOThread(const std::vector< | |
| 31 scoped_refptr<webkit_blob::ShareableFileReference> >& file_refs) { | |
| 32 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 33 } | |
| 34 | |
| 35 } // namespace | |
| 36 | |
| 37 SnapshotManager::SnapshotManager(Profile* profile) | |
| 38 : profile_(profile), weak_ptr_factory_(this) { | |
| 39 } | |
| 40 | |
| 41 SnapshotManager::~SnapshotManager() { | |
| 42 if (!file_refs_.empty()) { | |
| 43 bool posted = content::BrowserThread::PostTask( | |
| 44 content::BrowserThread::IO, | |
| 45 FROM_HERE, | |
| 46 base::Bind(&FreeReferenceOnIOThread, file_refs_)); | |
| 47 DCHECK(posted); | |
| 48 } | |
| 49 } | |
| 50 | |
| 51 void SnapshotManager::CreateManagedSnapshot( | |
| 52 const base::FilePath& absolute_file_path, | |
| 53 const LocalPathCallback& callback) { | |
| 54 fileapi::FileSystemContext* context = | |
| 55 util::GetFileSystemContextForExtensionId(profile_, kFileManagerAppId); | |
| 56 DCHECK(context); | |
| 57 | |
| 58 GURL url; | |
| 59 if (!util::ConvertAbsoluteFilePathToFileSystemUrl( | |
| 60 profile_, absolute_file_path, kFileManagerAppId, &url)) { | |
| 61 callback.Run(base::FilePath()); | |
| 62 return; | |
| 63 } | |
| 64 | |
| 65 content::BrowserThread::PostTask( | |
| 66 content::BrowserThread::IO, | |
| 67 FROM_HERE, | |
| 68 base::Bind(&CreateSnapshotFileOnIOThread, | |
| 69 make_scoped_refptr(context), | |
| 70 context->CrackURL(url), | |
| 71 google_apis::CreateRelayCallback( | |
| 72 base::Bind(&SnapshotManager::OnCreateSnapshotFile, | |
| 73 weak_ptr_factory_.GetWeakPtr(), | |
| 74 callback)))); | |
| 75 } | |
| 76 | |
| 77 void SnapshotManager::OnCreateSnapshotFile( | |
| 78 const LocalPathCallback& callback, | |
| 79 base::File::Error result, | |
| 80 const base::File::Info& file_info, | |
| 81 const base::FilePath& platform_path, | |
| 82 const scoped_refptr<webkit_blob::ShareableFileReference>& file_ref) { | |
| 83 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 84 | |
| 85 if (result != base::File::FILE_OK) { | |
| 86 callback.Run(base::FilePath()); | |
| 87 return; | |
| 88 } | |
| 89 file_refs_.push_back(file_ref); | |
| 90 callback.Run(platform_path); | |
| 91 } | |
| 92 | |
| 93 } // namespace file_manager | |
| OLD | NEW |