1 // Copyright 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.
5 #include "chrome/browser/media_galleries/fileapi/iphoto_file_util.h"
11 #include "base/bind_helpers.h"
12 #include "base/file_util.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/browser/media_galleries/fileapi/iphoto_data_provider.h"
15 #include "chrome/browser/media_galleries/fileapi/media_path_filter.h"
16 #include "chrome/browser/media_galleries/imported_media_gallery_registry.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "webkit/browser/fileapi/file_system_operation_context.h"
19 #include "webkit/browser/fileapi/file_system_url.h"
20 #include "webkit/browser/fileapi/native_file_util.h"
21 #include "webkit/common/blob/shareable_file_reference.h"
22 #include "webkit/common/fileapi/file_system_util.h"
28 base::PlatformFileError MakeDirectoryFileInfo(
29 base::PlatformFileInfo* file_info) {
30 base::PlatformFileInfo result;
31 result.is_directory = true;
33 return base::PLATFORM_FILE_OK;
38 IPhotoFileUtil::IPhotoFileUtil(MediaPathFilter* media_path_filter)
39 : NativeMediaFileUtil(media_path_filter),
41 imported_registry_(NULL) {
44 IPhotoFileUtil::~IPhotoFileUtil() {
47 void IPhotoFileUtil::GetFileInfoOnTaskRunnerThread(
48 scoped_ptr<fileapi::FileSystemOperationContext> context,
49 const fileapi::FileSystemURL& url,
50 const GetFileInfoCallback& callback) {
51 GetDataProvider()->RefreshData(
52 base::Bind(&IPhotoFileUtil::GetFileInfoWithFreshDataProvider,
53 weak_factory_.GetWeakPtr(), base::Passed(&context), url,
57 void IPhotoFileUtil::ReadDirectoryOnTaskRunnerThread(
58 scoped_ptr<fileapi::FileSystemOperationContext> context,
59 const fileapi::FileSystemURL& url,
60 const ReadDirectoryCallback& callback) {
61 GetDataProvider()->RefreshData(
62 base::Bind(&IPhotoFileUtil::ReadDirectoryWithFreshDataProvider,
63 weak_factory_.GetWeakPtr(), base::Passed(&context), url,
67 void IPhotoFileUtil::CreateSnapshotFileOnTaskRunnerThread(
68 scoped_ptr<fileapi::FileSystemOperationContext> context,
69 const fileapi::FileSystemURL& url,
70 const CreateSnapshotFileCallback& callback) {
71 GetDataProvider()->RefreshData(
72 base::Bind(&IPhotoFileUtil::CreateSnapshotFileWithFreshDataProvider,
73 weak_factory_.GetWeakPtr(), base::Passed(&context), url,
77 void IPhotoFileUtil::GetFileInfoWithFreshDataProvider(
78 scoped_ptr<fileapi::FileSystemOperationContext> context,
79 const fileapi::FileSystemURL& url,
80 const GetFileInfoCallback& callback,
83 if (!callback.is_null()) {
84 content::BrowserThread::PostTask(
85 content::BrowserThread::IO,
87 base::Bind(callback, base::PLATFORM_FILE_ERROR_IO,
88 base::PlatformFileInfo()));
92 NativeMediaFileUtil::GetFileInfoOnTaskRunnerThread(context.Pass(), url,
96 void IPhotoFileUtil::ReadDirectoryWithFreshDataProvider(
97 scoped_ptr<fileapi::FileSystemOperationContext> context,
98 const fileapi::FileSystemURL& url,
99 const ReadDirectoryCallback& callback,
102 if (!callback.is_null()) {
103 content::BrowserThread::PostTask(
104 content::BrowserThread::IO,
106 base::Bind(callback, base::PLATFORM_FILE_ERROR_IO, EntryList(),
111 NativeMediaFileUtil::ReadDirectoryOnTaskRunnerThread(context.Pass(), url,
115 void IPhotoFileUtil::CreateSnapshotFileWithFreshDataProvider(
116 scoped_ptr<fileapi::FileSystemOperationContext> context,
117 const fileapi::FileSystemURL& url,
118 const CreateSnapshotFileCallback& callback,
121 if (!callback.is_null()) {
122 base::PlatformFileInfo file_info;
123 base::FilePath platform_path;
124 scoped_refptr<webkit_blob::ShareableFileReference> file_ref;
125 content::BrowserThread::PostTask(
126 content::BrowserThread::IO,
128 base::Bind(callback, base::PLATFORM_FILE_ERROR_IO, file_info,
129 platform_path, file_ref));
133 NativeMediaFileUtil::CreateSnapshotFileOnTaskRunnerThread(context.Pass(), url,
137 // TODO(gbillock): Right now, returns an always-empty FS. Document virtual FS.
138 base::PlatformFileError IPhotoFileUtil::GetFileInfoSync(
139 fileapi::FileSystemOperationContext* context,
140 const fileapi::FileSystemURL& url,
141 base::PlatformFileInfo* file_info,
142 base::FilePath* platform_path) {
143 std::vector<std::string> components;
144 fileapi::VirtualPath::GetComponentsUTF8Unsafe(url.path(), &components);
146 if (components.size() == 0)
147 return MakeDirectoryFileInfo(file_info);
149 return base::PLATFORM_FILE_ERROR_NOT_FOUND;
152 base::PlatformFileError IPhotoFileUtil::ReadDirectorySync(
153 fileapi::FileSystemOperationContext* context,
154 const fileapi::FileSystemURL& url,
155 EntryList* file_list) {
156 DCHECK(file_list->empty());
157 std::vector<std::string> components;
158 fileapi::VirtualPath::GetComponentsUTF8Unsafe(url.path(), &components);
160 if (components.size() == 0) {
161 return base::PLATFORM_FILE_OK;
164 return base::PLATFORM_FILE_ERROR_NOT_FOUND;
167 base::PlatformFileError IPhotoFileUtil::CreateSnapshotFileSync(
168 fileapi::FileSystemOperationContext* context,
169 const fileapi::FileSystemURL& url,
170 base::PlatformFileInfo* file_info,
171 base::FilePath* platform_path,
172 scoped_refptr<webkit_blob::ShareableFileReference>* file_ref) {
173 DCHECK(!url.path().IsAbsolute());
175 return base::PLATFORM_FILE_ERROR_NOT_FOUND;
178 base::PlatformFileError IPhotoFileUtil::GetLocalFilePath(
179 fileapi::FileSystemOperationContext* context,
180 const fileapi::FileSystemURL& url,
181 base::FilePath* local_file_path) {
182 return base::PLATFORM_FILE_ERROR_NOT_FOUND;
185 IPhotoDataProvider* IPhotoFileUtil::GetDataProvider() {
186 if (!imported_registry_)
187 imported_registry_ = ImportedMediaGalleryRegistry::GetInstance();
188 return imported_registry_->IPhotoDataProvider();
191 } // namespace iphoto