1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
|
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/media_galleries/fileapi/device_media_async_file_util.h"
#include "base/callback.h"
#include "base/files/file_util.h"
#include "base/single_thread_task_runner.h"
#include "base/task_runner_util.h"
#include "chrome/browser/media_galleries/fileapi/media_path_filter.h"
#include "chrome/browser/media_galleries/fileapi/mtp_device_async_delegate.h"
#include "chrome/browser/media_galleries/fileapi/mtp_device_map_service.h"
#include "chrome/browser/media_galleries/fileapi/mtp_file_stream_reader.h"
#include "chrome/browser/media_galleries/fileapi/native_media_file_util.h"
#include "chrome/browser/media_galleries/fileapi/readahead_file_stream_reader.h"
#include "content/public/browser/browser_thread.h"
#include "storage/browser/blob/file_stream_reader.h"
#include "storage/browser/fileapi/file_system_context.h"
#include "storage/browser/fileapi/file_system_operation_context.h"
#include "storage/browser/fileapi/file_system_url.h"
#include "storage/browser/fileapi/native_file_util.h"
#include "storage/common/blob/shareable_file_reference.h"
using storage::AsyncFileUtil;
using storage::FileSystemOperationContext;
using storage::FileSystemURL;
using storage::ShareableFileReference;
namespace {
const char kDeviceMediaAsyncFileUtilTempDir[] = "DeviceMediaFileSystem";
MTPDeviceAsyncDelegate* GetMTPDeviceDelegate(const FileSystemURL& url) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
return MTPDeviceMapService::GetInstance()->GetMTPDeviceAsyncDelegate(
url.filesystem_id());
}
// Called when GetFileInfo method call failed to get the details of file
// specified by the requested url. |callback| is invoked to notify the
// caller about the file |error|.
void OnGetFileInfoError(const AsyncFileUtil::GetFileInfoCallback& callback,
base::File::Error error) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
callback.Run(error, base::File::Info());
}
// Called after OnDidGetFileInfo finishes media check.
// |callback| is invoked to complete the GetFileInfo request.
void OnDidCheckMediaForGetFileInfo(
const AsyncFileUtil::GetFileInfoCallback& callback,
const base::File::Info& file_info,
bool is_valid_file) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
if (!is_valid_file) {
OnGetFileInfoError(callback, base::File::FILE_ERROR_NOT_FOUND);
return;
}
callback.Run(base::File::FILE_OK, file_info);
}
// Called after OnDidReadDirectory finishes media check.
// |callback| is invoked to complete the ReadDirectory request.
void OnDidCheckMediaForReadDirectory(
const AsyncFileUtil::ReadDirectoryCallback& callback,
bool has_more,
const AsyncFileUtil::EntryList& file_list) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
callback.Run(base::File::FILE_OK, file_list, has_more);
}
// Called when ReadDirectory method call failed to enumerate the directory
// objects. |callback| is invoked to notify the caller about the |error|
// that occured while reading the directory objects.
void OnReadDirectoryError(const AsyncFileUtil::ReadDirectoryCallback& callback,
base::File::Error error) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
callback.Run(error, AsyncFileUtil::EntryList(), false /*no more*/);
}
// Called on a blocking pool thread to create a snapshot file to hold the
// contents of |device_file_path|. The snapshot file is created in the
// "profile_path/kDeviceMediaAsyncFileUtilTempDir" directory. Return the
// snapshot file path or an empty path on failure.
base::FilePath CreateSnapshotFileOnBlockingPool(
const base::FilePath& device_file_path,
const base::FilePath& profile_path) {
base::FilePath snapshot_file_path;
base::FilePath media_file_system_dir_path =
profile_path.AppendASCII(kDeviceMediaAsyncFileUtilTempDir);
if (!base::CreateDirectory(media_file_system_dir_path) ||
!base::CreateTemporaryFileInDir(media_file_system_dir_path,
&snapshot_file_path)) {
LOG(WARNING) << "Could not create media snapshot file "
<< media_file_system_dir_path.value();
snapshot_file_path = base::FilePath();
}
return snapshot_file_path;
}
// Called after OnDidCreateSnapshotFile finishes media check.
// |callback| is invoked to complete the CreateSnapshotFile request.
void OnDidCheckMediaForCreateSnapshotFile(
const AsyncFileUtil::CreateSnapshotFileCallback& callback,
const base::File::Info& file_info,
scoped_refptr<storage::ShareableFileReference> platform_file,
base::File::Error error) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
base::FilePath platform_path(platform_file.get()->path());
if (error != base::File::FILE_OK)
platform_file = NULL;
callback.Run(error, file_info, platform_path, platform_file);
}
// Called when the snapshot file specified by the |platform_path| is
// successfully created. |file_info| contains the device media file details
// for which the snapshot file is created.
void OnDidCreateSnapshotFile(
const AsyncFileUtil::CreateSnapshotFileCallback& callback,
base::SequencedTaskRunner* media_task_runner,
bool validate_media_files,
const base::File::Info& file_info,
const base::FilePath& platform_path) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
scoped_refptr<storage::ShareableFileReference> file =
ShareableFileReference::GetOrCreate(
platform_path,
ShareableFileReference::DELETE_ON_FINAL_RELEASE,
media_task_runner);
if (validate_media_files) {
base::PostTaskAndReplyWithResult(
media_task_runner,
FROM_HERE,
base::Bind(&NativeMediaFileUtil::IsMediaFile, platform_path),
base::Bind(&OnDidCheckMediaForCreateSnapshotFile,
callback,
file_info,
file));
} else {
OnDidCheckMediaForCreateSnapshotFile(callback, file_info, file,
base::File::FILE_OK);
}
}
// Called when CreateSnapshotFile method call fails. |callback| is invoked to
// notify the caller about the |error|.
void OnCreateSnapshotFileError(
const AsyncFileUtil::CreateSnapshotFileCallback& callback,
base::File::Error error) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
callback.Run(error, base::File::Info(), base::FilePath(),
scoped_refptr<ShareableFileReference>());
}
// Called when the snapshot file specified by the |snapshot_file_path| is
// created to hold the contents of the url.path(). If the snapshot
// file is successfully created, |snapshot_file_path| will be an non-empty
// file path. In case of failure, |snapshot_file_path| will be an empty file
// path. Forwards the CreateSnapshot request to the delegate to copy the
// contents of url.path() to |snapshot_file_path|.
void OnSnapshotFileCreatedRunTask(
scoped_ptr<FileSystemOperationContext> context,
const AsyncFileUtil::CreateSnapshotFileCallback& callback,
const FileSystemURL& url,
bool validate_media_files,
const base::FilePath& snapshot_file_path) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
if (snapshot_file_path.empty()) {
OnCreateSnapshotFileError(callback, base::File::FILE_ERROR_FAILED);
return;
}
MTPDeviceAsyncDelegate* delegate = GetMTPDeviceDelegate(url);
if (!delegate) {
OnCreateSnapshotFileError(callback, base::File::FILE_ERROR_NOT_FOUND);
return;
}
delegate->CreateSnapshotFile(
url.path(), // device file path
snapshot_file_path,
base::Bind(&OnDidCreateSnapshotFile,
callback,
make_scoped_refptr(context->task_runner()),
validate_media_files),
base::Bind(&OnCreateSnapshotFileError, callback));
}
} // namespace
class DeviceMediaAsyncFileUtil::MediaPathFilterWrapper
: public base::RefCountedThreadSafe<MediaPathFilterWrapper> {
public:
MediaPathFilterWrapper();
// Check if entries in |file_list| look like media files.
// Append the ones that look like media files to |results|.
// Should run on a media task runner.
AsyncFileUtil::EntryList FilterMediaEntries(
const AsyncFileUtil::EntryList& file_list);
// Check if |path| looks like a media file.
bool CheckFilePath(const base::FilePath& path);
private:
friend class base::RefCountedThreadSafe<MediaPathFilterWrapper>;
virtual ~MediaPathFilterWrapper();
scoped_ptr<MediaPathFilter> media_path_filter_;
DISALLOW_COPY_AND_ASSIGN(MediaPathFilterWrapper);
};
DeviceMediaAsyncFileUtil::MediaPathFilterWrapper::MediaPathFilterWrapper()
: media_path_filter_(new MediaPathFilter) {
}
DeviceMediaAsyncFileUtil::MediaPathFilterWrapper::~MediaPathFilterWrapper() {
}
AsyncFileUtil::EntryList
DeviceMediaAsyncFileUtil::MediaPathFilterWrapper::FilterMediaEntries(
const AsyncFileUtil::EntryList& file_list) {
AsyncFileUtil::EntryList results;
for (size_t i = 0; i < file_list.size(); ++i) {
const storage::DirectoryEntry& entry = file_list[i];
if (entry.is_directory || CheckFilePath(base::FilePath(entry.name))) {
results.push_back(entry);
}
}
return results;
}
bool DeviceMediaAsyncFileUtil::MediaPathFilterWrapper::CheckFilePath(
const base::FilePath& path) {
return media_path_filter_->Match(path);
}
DeviceMediaAsyncFileUtil::~DeviceMediaAsyncFileUtil() {
}
// static
scoped_ptr<DeviceMediaAsyncFileUtil> DeviceMediaAsyncFileUtil::Create(
const base::FilePath& profile_path,
MediaFileValidationType validation_type) {
DCHECK(!profile_path.empty());
return make_scoped_ptr(
new DeviceMediaAsyncFileUtil(profile_path, validation_type));
}
bool DeviceMediaAsyncFileUtil::SupportsStreaming(
const storage::FileSystemURL& url) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
MTPDeviceAsyncDelegate* delegate = GetMTPDeviceDelegate(url);
if (!delegate)
return false;
return delegate->IsStreaming();
}
void DeviceMediaAsyncFileUtil::CreateOrOpen(
scoped_ptr<FileSystemOperationContext> context,
const FileSystemURL& url,
int file_flags,
const CreateOrOpenCallback& callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
// Returns an error if any unsupported flag is found.
if (file_flags & ~(base::File::FLAG_OPEN |
base::File::FLAG_READ |
base::File::FLAG_WRITE_ATTRIBUTES)) {
callback.Run(base::File(base::File::FILE_ERROR_SECURITY), base::Closure());
return;
}
CreateSnapshotFile(
context.Pass(),
url,
base::Bind(&NativeMediaFileUtil::CreatedSnapshotFileForCreateOrOpen,
make_scoped_refptr(context->task_runner()),
file_flags,
callback));
}
void DeviceMediaAsyncFileUtil::EnsureFileExists(
scoped_ptr<FileSystemOperationContext> context,
const FileSystemURL& url,
const EnsureFileExistsCallback& callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
NOTIMPLEMENTED();
callback.Run(base::File::FILE_ERROR_SECURITY, false);
}
void DeviceMediaAsyncFileUtil::CreateDirectory(
scoped_ptr<FileSystemOperationContext> context,
const FileSystemURL& url,
bool exclusive,
bool recursive,
const StatusCallback& callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
NOTIMPLEMENTED();
callback.Run(base::File::FILE_ERROR_SECURITY);
}
void DeviceMediaAsyncFileUtil::GetFileInfo(
scoped_ptr<FileSystemOperationContext> context,
const FileSystemURL& url,
const GetFileInfoCallback& callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
MTPDeviceAsyncDelegate* delegate = GetMTPDeviceDelegate(url);
if (!delegate) {
OnGetFileInfoError(callback, base::File::FILE_ERROR_NOT_FOUND);
return;
}
delegate->GetFileInfo(
url.path(),
base::Bind(&DeviceMediaAsyncFileUtil::OnDidGetFileInfo,
weak_ptr_factory_.GetWeakPtr(),
make_scoped_refptr(context->task_runner()),
url.path(),
callback),
base::Bind(&OnGetFileInfoError, callback));
}
void DeviceMediaAsyncFileUtil::ReadDirectory(
scoped_ptr<FileSystemOperationContext> context,
const FileSystemURL& url,
const ReadDirectoryCallback& callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
MTPDeviceAsyncDelegate* delegate = GetMTPDeviceDelegate(url);
if (!delegate) {
OnReadDirectoryError(callback, base::File::FILE_ERROR_NOT_FOUND);
return;
}
delegate->ReadDirectory(
url.path(),
base::Bind(&DeviceMediaAsyncFileUtil::OnDidReadDirectory,
weak_ptr_factory_.GetWeakPtr(),
make_scoped_refptr(context->task_runner()),
callback),
base::Bind(&OnReadDirectoryError, callback));
}
void DeviceMediaAsyncFileUtil::Touch(
scoped_ptr<FileSystemOperationContext> context,
const FileSystemURL& url,
const base::Time& last_access_time,
const base::Time& last_modified_time,
const StatusCallback& callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
NOTIMPLEMENTED();
callback.Run(base::File::FILE_ERROR_SECURITY);
}
void DeviceMediaAsyncFileUtil::Truncate(
scoped_ptr<FileSystemOperationContext> context,
const FileSystemURL& url,
int64 length,
const StatusCallback& callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
NOTIMPLEMENTED();
callback.Run(base::File::FILE_ERROR_SECURITY);
}
void DeviceMediaAsyncFileUtil::CopyFileLocal(
scoped_ptr<FileSystemOperationContext> context,
const FileSystemURL& src_url,
const FileSystemURL& dest_url,
CopyOrMoveOption option,
const CopyFileProgressCallback& progress_callback,
const StatusCallback& callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
NOTIMPLEMENTED();
callback.Run(base::File::FILE_ERROR_SECURITY);
}
void DeviceMediaAsyncFileUtil::MoveFileLocal(
scoped_ptr<FileSystemOperationContext> context,
const FileSystemURL& src_url,
const FileSystemURL& dest_url,
CopyOrMoveOption option,
const StatusCallback& callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
NOTIMPLEMENTED();
callback.Run(base::File::FILE_ERROR_SECURITY);
}
void DeviceMediaAsyncFileUtil::CopyInForeignFile(
scoped_ptr<FileSystemOperationContext> context,
const base::FilePath& src_file_path,
const FileSystemURL& dest_url,
const StatusCallback& callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
NOTIMPLEMENTED();
callback.Run(base::File::FILE_ERROR_SECURITY);
}
void DeviceMediaAsyncFileUtil::DeleteFile(
scoped_ptr<FileSystemOperationContext> context,
const FileSystemURL& url,
const StatusCallback& callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
NOTIMPLEMENTED();
callback.Run(base::File::FILE_ERROR_SECURITY);
}
void DeviceMediaAsyncFileUtil::DeleteDirectory(
scoped_ptr<FileSystemOperationContext> context,
const FileSystemURL& url,
const StatusCallback& callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
NOTIMPLEMENTED();
callback.Run(base::File::FILE_ERROR_SECURITY);
}
void DeviceMediaAsyncFileUtil::DeleteRecursively(
scoped_ptr<FileSystemOperationContext> context,
const FileSystemURL& url,
const StatusCallback& callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
callback.Run(base::File::FILE_ERROR_INVALID_OPERATION);
}
void DeviceMediaAsyncFileUtil::CreateSnapshotFile(
scoped_ptr<FileSystemOperationContext> context,
const FileSystemURL& url,
const CreateSnapshotFileCallback& callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
MTPDeviceAsyncDelegate* delegate = GetMTPDeviceDelegate(url);
if (!delegate) {
OnCreateSnapshotFileError(callback, base::File::FILE_ERROR_NOT_FOUND);
return;
}
scoped_refptr<base::SequencedTaskRunner> task_runner(context->task_runner());
base::PostTaskAndReplyWithResult(
task_runner.get(),
FROM_HERE,
base::Bind(&CreateSnapshotFileOnBlockingPool, url.path(), profile_path_),
base::Bind(&OnSnapshotFileCreatedRunTask,
base::Passed(&context),
callback,
url,
validate_media_files()));
}
scoped_ptr<storage::FileStreamReader>
DeviceMediaAsyncFileUtil::GetFileStreamReader(
const FileSystemURL& url,
int64 offset,
const base::Time& expected_modification_time,
storage::FileSystemContext* context) {
MTPDeviceAsyncDelegate* delegate = GetMTPDeviceDelegate(url);
if (!delegate)
return scoped_ptr<storage::FileStreamReader>();
DCHECK(delegate->IsStreaming());
return scoped_ptr<storage::FileStreamReader>(new ReadaheadFileStreamReader(
new MTPFileStreamReader(context,
url,
offset,
expected_modification_time,
validate_media_files())));
}
DeviceMediaAsyncFileUtil::DeviceMediaAsyncFileUtil(
const base::FilePath& profile_path,
MediaFileValidationType validation_type)
: profile_path_(profile_path),
weak_ptr_factory_(this) {
if (validation_type == APPLY_MEDIA_FILE_VALIDATION) {
media_path_filter_wrapper_ = new MediaPathFilterWrapper;
}
}
void DeviceMediaAsyncFileUtil::OnDidGetFileInfo(
base::SequencedTaskRunner* task_runner,
const base::FilePath& path,
const AsyncFileUtil::GetFileInfoCallback& callback,
const base::File::Info& file_info) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
if (file_info.is_directory || !validate_media_files()) {
OnDidCheckMediaForGetFileInfo(callback, file_info, true /* valid */);
return;
}
base::PostTaskAndReplyWithResult(
task_runner,
FROM_HERE,
base::Bind(&MediaPathFilterWrapper::CheckFilePath,
media_path_filter_wrapper_,
path),
base::Bind(&OnDidCheckMediaForGetFileInfo, callback, file_info));
}
void DeviceMediaAsyncFileUtil::OnDidReadDirectory(
base::SequencedTaskRunner* task_runner,
const AsyncFileUtil::ReadDirectoryCallback& callback,
const AsyncFileUtil::EntryList& file_list,
bool has_more) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
if (!validate_media_files()) {
OnDidCheckMediaForReadDirectory(callback, has_more, file_list);
return;
}
base::PostTaskAndReplyWithResult(
task_runner,
FROM_HERE,
base::Bind(&MediaPathFilterWrapper::FilterMediaEntries,
media_path_filter_wrapper_,
file_list),
base::Bind(&OnDidCheckMediaForReadDirectory, callback, has_more));
}
bool DeviceMediaAsyncFileUtil::validate_media_files() const {
return media_path_filter_wrapper_.get() != NULL;
}
|