Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1 | // Copyright (c) 2013 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/media_galleries/fileapi/device_media_async_file_util.h" |
| 6 | |
| 7 | #include "base/callback.h" |
| 8 | #include "base/file_util.h" |
| 9 | #include "base/single_thread_task_runner.h" |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 10 | #include "base/task_runner_util.h" |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 11 | #include "chrome/browser/media_galleries/fileapi/media_path_filter.h" |
| 12 | #include "chrome/browser/media_galleries/fileapi/mtp_device_async_delegate.h" |
| 13 | #include "chrome/browser/media_galleries/fileapi/mtp_device_map_service.h" |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 14 | #include "chrome/browser/media_galleries/fileapi/native_media_file_util.h" |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 15 | #include "content/public/browser/browser_thread.h" |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 16 | #include "webkit/browser/fileapi/file_system_operation_context.h" |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 17 | #include "webkit/browser/fileapi/file_system_url.h" |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 18 | #include "webkit/common/blob/shareable_file_reference.h" |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 19 | |
| 20 | using fileapi::FileSystemOperationContext; |
| 21 | using fileapi::FileSystemURL; |
| 22 | using webkit_blob::ShareableFileReference; |
| 23 | |
| 24 | namespace chrome { |
| 25 | |
| 26 | namespace { |
| 27 | |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 28 | const char kDeviceMediaAsyncFileUtilTempDir[] = "DeviceMediaFileSystem"; |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 29 | |
| 30 | // Called on the IO thread. |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 31 | MTPDeviceAsyncDelegate* GetMTPDeviceDelegate(const FileSystemURL& url) { |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 32 | DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 33 | return MTPDeviceMapService::GetInstance()->GetMTPDeviceAsyncDelegate( |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 34 | url.filesystem_id()); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | // Called on a blocking pool thread to create a snapshot file to hold the |
| 38 | // contents of |device_file_path|. The snapshot file is created in |
| 39 | // "profile_path/kDeviceMediaAsyncFileUtilTempDir" directory. If the snapshot |
| 40 | // file is created successfully, |snapshot_file_path| will be a non-empty file |
| 41 | // path. In case of failure, the |snapshot_file_path| will be an empty file |
| 42 | // path. |
| 43 | void CreateSnapshotFileOnBlockingPool( |
| 44 | const base::FilePath& device_file_path, |
| 45 | const base::FilePath& profile_path, |
| 46 | base::FilePath* snapshot_file_path) { |
| 47 | DCHECK(snapshot_file_path); |
| 48 | base::FilePath isolated_media_file_system_dir_path = |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 49 | profile_path.AppendASCII(kDeviceMediaAsyncFileUtilTempDir); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 50 | if (!file_util::CreateDirectory(isolated_media_file_system_dir_path) || |
| 51 | !file_util::CreateTemporaryFileInDir(isolated_media_file_system_dir_path, |
| 52 | snapshot_file_path)) { |
| 53 | LOG(WARNING) << "Could not create media snapshot file " |
| 54 | << isolated_media_file_system_dir_path.value(); |
| 55 | *snapshot_file_path = base::FilePath(); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | } // namespace |
| 60 | |
| 61 | DeviceMediaAsyncFileUtil::~DeviceMediaAsyncFileUtil() { |
| 62 | } |
| 63 | |
| 64 | // static |
| 65 | DeviceMediaAsyncFileUtil* DeviceMediaAsyncFileUtil::Create( |
| 66 | const base::FilePath& profile_path) { |
| 67 | DCHECK(!profile_path.empty()); |
| 68 | return new DeviceMediaAsyncFileUtil(profile_path); |
| 69 | } |
| 70 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 71 | void DeviceMediaAsyncFileUtil::CreateOrOpen( |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 72 | scoped_ptr<FileSystemOperationContext> context, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 73 | const FileSystemURL& url, |
| 74 | int file_flags, |
| 75 | const CreateOrOpenCallback& callback) { |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 76 | DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 77 | NOTIMPLEMENTED(); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 78 | base::PlatformFile invalid_file = base::kInvalidPlatformFileValue; |
| 79 | callback.Run(base::PLATFORM_FILE_ERROR_SECURITY, |
| 80 | base::PassPlatformFile(&invalid_file), |
| 81 | base::Closure()); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 82 | } |
| 83 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 84 | void DeviceMediaAsyncFileUtil::EnsureFileExists( |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 85 | scoped_ptr<FileSystemOperationContext> context, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 86 | const FileSystemURL& url, |
| 87 | const EnsureFileExistsCallback& callback) { |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 88 | DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 89 | NOTIMPLEMENTED(); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 90 | callback.Run(base::PLATFORM_FILE_ERROR_SECURITY, false); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 91 | } |
| 92 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 93 | void DeviceMediaAsyncFileUtil::CreateDirectory( |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 94 | scoped_ptr<FileSystemOperationContext> context, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 95 | const FileSystemURL& url, |
| 96 | bool exclusive, |
| 97 | bool recursive, |
| 98 | const StatusCallback& callback) { |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 99 | DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 100 | NOTIMPLEMENTED(); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 101 | callback.Run(base::PLATFORM_FILE_ERROR_SECURITY); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 102 | } |
| 103 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 104 | void DeviceMediaAsyncFileUtil::GetFileInfo( |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 105 | scoped_ptr<FileSystemOperationContext> context, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 106 | const FileSystemURL& url, |
| 107 | const GetFileInfoCallback& callback) { |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 108 | DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 109 | MTPDeviceAsyncDelegate* delegate = GetMTPDeviceDelegate(url); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 110 | if (!delegate) { |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 111 | OnGetFileInfoError(callback, base::PLATFORM_FILE_ERROR_NOT_FOUND); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 112 | return; |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 113 | } |
| 114 | delegate->GetFileInfo( |
| 115 | url.path(), |
| 116 | base::Bind(&DeviceMediaAsyncFileUtil::OnDidGetFileInfo, |
| 117 | weak_ptr_factory_.GetWeakPtr(), |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 118 | callback), |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 119 | base::Bind(&DeviceMediaAsyncFileUtil::OnGetFileInfoError, |
| 120 | weak_ptr_factory_.GetWeakPtr(), |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 121 | callback)); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 122 | } |
| 123 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 124 | void DeviceMediaAsyncFileUtil::ReadDirectory( |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 125 | scoped_ptr<FileSystemOperationContext> context, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 126 | const FileSystemURL& url, |
| 127 | const ReadDirectoryCallback& callback) { |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 128 | DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 129 | MTPDeviceAsyncDelegate* delegate = GetMTPDeviceDelegate(url); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 130 | if (!delegate) { |
| 131 | OnReadDirectoryError(callback, base::PLATFORM_FILE_ERROR_NOT_FOUND); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 132 | return; |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 133 | } |
| 134 | delegate->ReadDirectory( |
| 135 | url.path(), |
| 136 | base::Bind(&DeviceMediaAsyncFileUtil::OnDidReadDirectory, |
| 137 | weak_ptr_factory_.GetWeakPtr(), |
| 138 | callback), |
| 139 | base::Bind(&DeviceMediaAsyncFileUtil::OnReadDirectoryError, |
| 140 | weak_ptr_factory_.GetWeakPtr(), |
| 141 | callback)); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 142 | } |
| 143 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 144 | void DeviceMediaAsyncFileUtil::Touch( |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 145 | scoped_ptr<FileSystemOperationContext> context, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 146 | const FileSystemURL& url, |
| 147 | const base::Time& last_access_time, |
| 148 | const base::Time& last_modified_time, |
| 149 | const StatusCallback& callback) { |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 150 | DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 151 | NOTIMPLEMENTED(); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 152 | callback.Run(base::PLATFORM_FILE_ERROR_SECURITY); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 153 | } |
| 154 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 155 | void DeviceMediaAsyncFileUtil::Truncate( |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 156 | scoped_ptr<FileSystemOperationContext> context, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 157 | const FileSystemURL& url, |
| 158 | int64 length, |
| 159 | const StatusCallback& callback) { |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 160 | DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 161 | NOTIMPLEMENTED(); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 162 | callback.Run(base::PLATFORM_FILE_ERROR_SECURITY); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 163 | } |
| 164 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 165 | void DeviceMediaAsyncFileUtil::CopyFileLocal( |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 166 | scoped_ptr<FileSystemOperationContext> context, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 167 | const FileSystemURL& src_url, |
| 168 | const FileSystemURL& dest_url, |
| 169 | const StatusCallback& callback) { |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 170 | DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 171 | NOTIMPLEMENTED(); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 172 | callback.Run(base::PLATFORM_FILE_ERROR_SECURITY); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 173 | } |
| 174 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 175 | void DeviceMediaAsyncFileUtil::MoveFileLocal( |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 176 | scoped_ptr<FileSystemOperationContext> context, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 177 | const FileSystemURL& src_url, |
| 178 | const FileSystemURL& dest_url, |
| 179 | const StatusCallback& callback) { |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 180 | DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 181 | NOTIMPLEMENTED(); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 182 | callback.Run(base::PLATFORM_FILE_ERROR_SECURITY); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 183 | } |
| 184 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 185 | void DeviceMediaAsyncFileUtil::CopyInForeignFile( |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 186 | scoped_ptr<FileSystemOperationContext> context, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 187 | const base::FilePath& src_file_path, |
| 188 | const FileSystemURL& dest_url, |
| 189 | const StatusCallback& callback) { |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 190 | DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 191 | NOTIMPLEMENTED(); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 192 | callback.Run(base::PLATFORM_FILE_ERROR_SECURITY); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 193 | } |
| 194 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 195 | void DeviceMediaAsyncFileUtil::DeleteFile( |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 196 | scoped_ptr<FileSystemOperationContext> context, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 197 | const FileSystemURL& url, |
| 198 | const StatusCallback& callback) { |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 199 | DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 200 | NOTIMPLEMENTED(); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 201 | callback.Run(base::PLATFORM_FILE_ERROR_SECURITY); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 202 | } |
| 203 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 204 | void DeviceMediaAsyncFileUtil::DeleteDirectory( |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 205 | scoped_ptr<FileSystemOperationContext> context, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 206 | const FileSystemURL& url, |
| 207 | const StatusCallback& callback) { |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 208 | DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 209 | NOTIMPLEMENTED(); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 210 | callback.Run(base::PLATFORM_FILE_ERROR_SECURITY); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 211 | } |
| 212 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 213 | void DeviceMediaAsyncFileUtil::DeleteRecursively( |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 214 | scoped_ptr<FileSystemOperationContext> context, |
| 215 | const FileSystemURL& url, |
| 216 | const StatusCallback& callback) { |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 217 | DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 218 | callback.Run(base::PLATFORM_FILE_ERROR_INVALID_OPERATION); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 219 | } |
| 220 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 221 | void DeviceMediaAsyncFileUtil::CreateSnapshotFile( |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 222 | scoped_ptr<FileSystemOperationContext> context, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 223 | const FileSystemURL& url, |
| 224 | const CreateSnapshotFileCallback& callback) { |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 225 | DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 226 | MTPDeviceAsyncDelegate* delegate = GetMTPDeviceDelegate(url); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 227 | if (!delegate) { |
| 228 | OnCreateSnapshotFileError(callback, base::PLATFORM_FILE_ERROR_NOT_FOUND); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 229 | return; |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 230 | } |
| 231 | base::FilePath* snapshot_file_path = new base::FilePath; |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 232 | base::SequencedTaskRunner* task_runner = context->task_runner(); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 233 | const bool success = task_runner->PostTaskAndReply( |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 234 | FROM_HERE, |
| 235 | base::Bind(&CreateSnapshotFileOnBlockingPool, |
| 236 | url.path(), |
| 237 | profile_path_, |
| 238 | base::Unretained(snapshot_file_path)), |
| 239 | base::Bind(&DeviceMediaAsyncFileUtil::OnSnapshotFileCreatedRunTask, |
| 240 | weak_ptr_factory_.GetWeakPtr(), |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 241 | base::Passed(&context), |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 242 | callback, |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 243 | url, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 244 | base::Owned(snapshot_file_path))); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 245 | DCHECK(success); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | DeviceMediaAsyncFileUtil::DeviceMediaAsyncFileUtil( |
| 249 | const base::FilePath& profile_path) |
| 250 | : profile_path_(profile_path), |
| 251 | weak_ptr_factory_(this) { |
| 252 | } |
| 253 | |
| 254 | void DeviceMediaAsyncFileUtil::OnDidGetFileInfo( |
| 255 | const AsyncFileUtil::GetFileInfoCallback& callback, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 256 | const base::PlatformFileInfo& file_info) { |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 257 | callback.Run(base::PLATFORM_FILE_OK, file_info); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | void DeviceMediaAsyncFileUtil::OnGetFileInfoError( |
| 261 | const AsyncFileUtil::GetFileInfoCallback& callback, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 262 | base::PlatformFileError error) { |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 263 | callback.Run(error, base::PlatformFileInfo()); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | void DeviceMediaAsyncFileUtil::OnDidReadDirectory( |
| 267 | const AsyncFileUtil::ReadDirectoryCallback& callback, |
| 268 | const AsyncFileUtil::EntryList& file_list, |
| 269 | bool has_more) { |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 270 | callback.Run(base::PLATFORM_FILE_OK, file_list, has_more); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | void DeviceMediaAsyncFileUtil::OnReadDirectoryError( |
| 274 | const AsyncFileUtil::ReadDirectoryCallback& callback, |
| 275 | base::PlatformFileError error) { |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 276 | callback.Run(error, AsyncFileUtil::EntryList(), false /*no more*/); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | void DeviceMediaAsyncFileUtil::OnDidCreateSnapshotFile( |
| 280 | const AsyncFileUtil::CreateSnapshotFileCallback& callback, |
| 281 | base::SequencedTaskRunner* media_task_runner, |
| 282 | const base::PlatformFileInfo& file_info, |
| 283 | const base::FilePath& platform_path) { |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 284 | base::PostTaskAndReplyWithResult( |
| 285 | media_task_runner, |
| 286 | FROM_HERE, |
| 287 | base::Bind(&NativeMediaFileUtil::IsMediaFile, platform_path), |
| 288 | base::Bind(&DeviceMediaAsyncFileUtil::OnDidCheckMedia, |
| 289 | weak_ptr_factory_.GetWeakPtr(), |
| 290 | callback, |
| 291 | file_info, |
| 292 | ShareableFileReference::GetOrCreate( |
| 293 | platform_path, |
| 294 | ShareableFileReference::DELETE_ON_FINAL_RELEASE, |
| 295 | media_task_runner))); |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | void DeviceMediaAsyncFileUtil::OnDidCheckMedia( |
| 299 | const AsyncFileUtil::CreateSnapshotFileCallback& callback, |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 300 | const base::PlatformFileInfo& file_info, |
| 301 | scoped_refptr<webkit_blob::ShareableFileReference> platform_file, |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 302 | base::PlatformFileError error) { |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 303 | base::FilePath platform_path(platform_file.get()->path()); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 304 | if (error != base::PLATFORM_FILE_OK) |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 305 | platform_file = NULL; |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 306 | callback.Run(error, file_info, platform_path, platform_file); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | void DeviceMediaAsyncFileUtil::OnCreateSnapshotFileError( |
| 310 | const AsyncFileUtil::CreateSnapshotFileCallback& callback, |
| 311 | base::PlatformFileError error) { |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 312 | callback.Run(error, base::PlatformFileInfo(), base::FilePath(), |
| 313 | scoped_refptr<ShareableFileReference>()); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | void DeviceMediaAsyncFileUtil::OnSnapshotFileCreatedRunTask( |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 317 | scoped_ptr<FileSystemOperationContext> context, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 318 | const AsyncFileUtil::CreateSnapshotFileCallback& callback, |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 319 | const FileSystemURL& url, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 320 | base::FilePath* snapshot_file_path) { |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 321 | DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 322 | if (!snapshot_file_path || snapshot_file_path->empty()) { |
| 323 | OnCreateSnapshotFileError(callback, base::PLATFORM_FILE_ERROR_FAILED); |
| 324 | return; |
| 325 | } |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 326 | MTPDeviceAsyncDelegate* delegate = GetMTPDeviceDelegate(url); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 327 | if (!delegate) { |
| 328 | OnCreateSnapshotFileError(callback, base::PLATFORM_FILE_ERROR_NOT_FOUND); |
| 329 | return; |
| 330 | } |
| 331 | delegate->CreateSnapshotFile( |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 332 | url.path(), // device file path |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 333 | *snapshot_file_path, |
| 334 | base::Bind(&DeviceMediaAsyncFileUtil::OnDidCreateSnapshotFile, |
| 335 | weak_ptr_factory_.GetWeakPtr(), |
| 336 | callback, |
| 337 | make_scoped_refptr(context->task_runner())), |
| 338 | base::Bind(&DeviceMediaAsyncFileUtil::OnCreateSnapshotFileError, |
| 339 | weak_ptr_factory_.GetWeakPtr(), |
| 340 | callback)); |
| 341 | } |
| 342 | |
| 343 | } // namespace chrome |