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.
5 #include "chrome/browser/media_galleries/fileapi/media_file_system_backend.h"
10 #include "base/bind.h"
11 #include "base/files/file_path.h"
12 #include "base/logging.h"
13 #include "base/message_loop/message_loop_proxy.h"
14 #include "base/sequenced_task_runner.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "base/strings/string_util.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "base/threading/sequenced_worker_pool.h"
19 #include "chrome/browser/browser_process.h"
20 #include "chrome/browser/extensions/extension_service.h"
21 #include "chrome/browser/media_galleries/fileapi/device_media_async_file_util.h"
22 #include "chrome/browser/media_galleries/fileapi/media_file_validator_factory.h"
23 #include "chrome/browser/media_galleries/fileapi/media_path_filter.h"
24 #include "chrome/browser/media_galleries/fileapi/native_media_file_util.h"
25 #include "chrome/browser/media_galleries/media_file_system_registry.h"
26 #include "chrome/browser/profiles/profile.h"
27 #include "content/public/browser/browser_thread.h"
28 #include "content/public/browser/render_process_host.h"
29 #include "content/public/browser/render_view_host.h"
30 #include "content/public/browser/resource_request_info.h"
31 #include "extensions/browser/extension_system.h"
32 #include "net/url_request/url_request.h"
33 #include "storage/browser/fileapi/copy_or_move_file_validator.h"
34 #include "storage/browser/fileapi/file_stream_reader.h"
35 #include "storage/browser/fileapi/file_stream_writer.h"
36 #include "storage/browser/fileapi/file_system_context.h"
37 #include "storage/browser/fileapi/file_system_operation.h"
38 #include "storage/browser/fileapi/file_system_operation_context.h"
39 #include "storage/browser/fileapi/file_system_url.h"
40 #include "storage/browser/fileapi/native_file_util.h"
41 #include "storage/common/fileapi/file_system_types.h"
42 #include "storage/common/fileapi/file_system_util.h"
44 #if defined(OS_WIN) || defined(OS_MACOSX)
45 #include "chrome/browser/media_galleries/fileapi/itunes_file_util.h"
46 #include "chrome/browser/media_galleries/fileapi/picasa_file_util.h"
47 #endif // defined(OS_WIN) || defined(OS_MACOSX)
49 #if defined(OS_MACOSX)
50 #include "chrome/browser/media_galleries/fileapi/iphoto_file_util.h"
51 #endif // defined(OS_MACOSX)
53 using storage::FileSystemContext
;
54 using storage::FileSystemURL
;
58 const char kMediaGalleryMountPrefix
[] = "media_galleries-";
60 void OnPreferencesInit(
61 const content::RenderViewHost
* rvh
,
62 const extensions::Extension
* extension
,
63 MediaGalleryPrefId pref_id
,
64 const base::Callback
<void(base::File::Error result
)>& callback
) {
65 MediaFileSystemRegistry
* registry
=
66 g_browser_process
->media_file_system_registry();
67 registry
->RegisterMediaFileSystemForExtension(rvh
, extension
, pref_id
,
71 void AttemptAutoMountOnUIThread(
74 const std::string
& storage_domain
,
75 const std::string
& mount_point
,
76 const base::Callback
<void(base::File::Error result
)>& callback
) {
77 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
79 content::RenderViewHost
* rvh
=
80 content::RenderViewHost::FromID(process_id
, routing_id
);
83 Profile::FromBrowserContext(rvh
->GetProcess()->GetBrowserContext());
85 ExtensionService
* extension_service
=
86 extensions::ExtensionSystem::Get(profile
)->extension_service();
87 const extensions::Extension
* extension
=
88 extension_service
->GetExtensionById(storage_domain
,
89 false /*include disabled*/);
90 std::string expected_mount_prefix
=
91 MediaFileSystemBackend::ConstructMountName(
92 profile
->GetPath(), storage_domain
, kInvalidMediaGalleryPrefId
);
93 MediaGalleryPrefId pref_id
= kInvalidMediaGalleryPrefId
;
95 extension
->id() == storage_domain
&&
96 StartsWithASCII(mount_point
, expected_mount_prefix
, true) &&
97 base::StringToUint64(mount_point
.substr(expected_mount_prefix
.size()),
99 pref_id
!= kInvalidMediaGalleryPrefId
) {
100 MediaGalleriesPreferences
* preferences
=
101 g_browser_process
->media_file_system_registry()->GetPreferences(
103 preferences
->EnsureInitialized(
104 base::Bind(&OnPreferencesInit
, rvh
, extension
, pref_id
, callback
));
109 content::BrowserThread::PostTask(
110 content::BrowserThread::IO
,
112 base::Bind(callback
, base::File::FILE_ERROR_NOT_FOUND
));
117 const char MediaFileSystemBackend::kMediaTaskRunnerName
[] =
120 MediaFileSystemBackend::MediaFileSystemBackend(
121 const base::FilePath
& profile_path
,
122 base::SequencedTaskRunner
* media_task_runner
)
123 : profile_path_(profile_path
),
124 media_task_runner_(media_task_runner
),
125 media_path_filter_(new MediaPathFilter
),
126 media_copy_or_move_file_validator_factory_(new MediaFileValidatorFactory
),
127 native_media_file_util_(
128 new NativeMediaFileUtil(media_path_filter_
.get())),
129 device_media_async_file_util_(
130 DeviceMediaAsyncFileUtil::Create(profile_path_
,
131 APPLY_MEDIA_FILE_VALIDATION
))
132 #if defined(OS_WIN) || defined(OS_MACOSX)
134 picasa_file_util_(new picasa::PicasaFileUtil(media_path_filter_
.get())),
135 itunes_file_util_(new itunes::ITunesFileUtil(media_path_filter_
.get()))
136 #endif // defined(OS_WIN) || defined(OS_MACOSX)
137 #if defined(OS_MACOSX)
139 iphoto_file_util_(new iphoto::IPhotoFileUtil(media_path_filter_
.get()))
140 #endif // defined(OS_MACOSX)
144 MediaFileSystemBackend::~MediaFileSystemBackend() {
148 bool MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread() {
149 base::SequencedWorkerPool
* pool
= content::BrowserThread::GetBlockingPool();
150 base::SequencedWorkerPool::SequenceToken media_sequence_token
=
151 pool
->GetNamedSequenceToken(kMediaTaskRunnerName
);
152 return pool
->IsRunningSequenceOnCurrentThread(media_sequence_token
);
156 scoped_refptr
<base::SequencedTaskRunner
>
157 MediaFileSystemBackend::MediaTaskRunner() {
158 base::SequencedWorkerPool
* pool
= content::BrowserThread::GetBlockingPool();
159 base::SequencedWorkerPool::SequenceToken media_sequence_token
=
160 pool
->GetNamedSequenceToken(kMediaTaskRunnerName
);
161 return pool
->GetSequencedTaskRunner(media_sequence_token
);
165 std::string
MediaFileSystemBackend::ConstructMountName(
166 const base::FilePath
& profile_path
,
167 const std::string
& extension_id
,
168 MediaGalleryPrefId pref_id
) {
169 std::string
name(kMediaGalleryMountPrefix
);
170 name
.append(profile_path
.BaseName().MaybeAsASCII());
172 name
.append(extension_id
);
174 if (pref_id
!= kInvalidMediaGalleryPrefId
)
175 name
.append(base::Uint64ToString(pref_id
));
176 base::ReplaceChars(name
, " /", "_", &name
);
181 bool MediaFileSystemBackend::AttemptAutoMountForURLRequest(
182 const net::URLRequest
* url_request
,
183 const storage::FileSystemURL
& filesystem_url
,
184 const std::string
& storage_domain
,
185 const base::Callback
<void(base::File::Error result
)>& callback
) {
186 if (storage_domain
.empty() ||
187 filesystem_url
.type() != storage::kFileSystemTypeExternal
||
188 storage_domain
!= filesystem_url
.origin().host()) {
192 const base::FilePath
& virtual_path
= filesystem_url
.path();
193 if (virtual_path
.ReferencesParent())
195 std::vector
<base::FilePath::StringType
> components
;
196 virtual_path
.GetComponents(&components
);
197 if (components
.empty())
199 std::string mount_point
= base::FilePath(components
[0]).AsUTF8Unsafe();
200 if (!StartsWithASCII(mount_point
, kMediaGalleryMountPrefix
, true))
203 const content::ResourceRequestInfo
* request_info
=
204 content::ResourceRequestInfo::ForRequest(url_request
);
208 content::BrowserThread::PostTask(
209 content::BrowserThread::UI
,
211 base::Bind(&AttemptAutoMountOnUIThread
, request_info
->GetChildID(),
212 request_info
->GetRouteID(), storage_domain
, mount_point
,
217 bool MediaFileSystemBackend::CanHandleType(storage::FileSystemType type
) const {
219 case storage::kFileSystemTypeNativeMedia
:
220 case storage::kFileSystemTypeDeviceMedia
:
221 #if defined(OS_WIN) || defined(OS_MACOSX)
222 case storage::kFileSystemTypePicasa
:
223 case storage::kFileSystemTypeItunes
:
224 #endif // defined(OS_WIN) || defined(OS_MACOSX)
225 #if defined(OS_MACOSX)
226 case storage::kFileSystemTypeIphoto
:
227 #endif // defined(OS_MACOSX)
234 void MediaFileSystemBackend::Initialize(storage::FileSystemContext
* context
) {
237 void MediaFileSystemBackend::ResolveURL(
238 const FileSystemURL
& url
,
239 storage::OpenFileSystemMode mode
,
240 const OpenFileSystemCallback
& callback
) {
241 // We never allow opening a new FileSystem via usual ResolveURL.
242 base::MessageLoopProxy::current()->PostTask(
247 base::File::FILE_ERROR_SECURITY
));
250 storage::AsyncFileUtil
* MediaFileSystemBackend::GetAsyncFileUtil(
251 storage::FileSystemType type
) {
253 case storage::kFileSystemTypeNativeMedia
:
254 return native_media_file_util_
.get();
255 case storage::kFileSystemTypeDeviceMedia
:
256 return device_media_async_file_util_
.get();
257 #if defined(OS_WIN) || defined(OS_MACOSX)
258 case storage::kFileSystemTypeItunes
:
259 return itunes_file_util_
.get();
260 case storage::kFileSystemTypePicasa
:
261 return picasa_file_util_
.get();
262 #endif // defined(OS_WIN) || defined(OS_MACOSX)
263 #if defined(OS_MACOSX)
264 case storage::kFileSystemTypeIphoto
:
265 return iphoto_file_util_
.get();
266 #endif // defined(OS_MACOSX)
273 storage::WatcherManager
* MediaFileSystemBackend::GetWatcherManager(
274 storage::FileSystemType type
) {
278 storage::CopyOrMoveFileValidatorFactory
*
279 MediaFileSystemBackend::GetCopyOrMoveFileValidatorFactory(
280 storage::FileSystemType type
,
281 base::File::Error
* error_code
) {
283 *error_code
= base::File::FILE_OK
;
285 case storage::kFileSystemTypeNativeMedia
:
286 case storage::kFileSystemTypeDeviceMedia
:
287 case storage::kFileSystemTypeIphoto
:
288 case storage::kFileSystemTypeItunes
:
289 if (!media_copy_or_move_file_validator_factory_
) {
290 *error_code
= base::File::FILE_ERROR_SECURITY
;
293 return media_copy_or_move_file_validator_factory_
.get();
300 storage::FileSystemOperation
* MediaFileSystemBackend::CreateFileSystemOperation(
301 const FileSystemURL
& url
,
302 FileSystemContext
* context
,
303 base::File::Error
* error_code
) const {
304 scoped_ptr
<storage::FileSystemOperationContext
> operation_context(
305 new storage::FileSystemOperationContext(context
,
306 media_task_runner_
.get()));
307 return storage::FileSystemOperation::Create(
308 url
, context
, operation_context
.Pass());
311 bool MediaFileSystemBackend::SupportsStreaming(
312 const storage::FileSystemURL
& url
) const {
313 if (url
.type() == storage::kFileSystemTypeDeviceMedia
) {
314 DCHECK(device_media_async_file_util_
);
315 return device_media_async_file_util_
->SupportsStreaming(url
);
321 bool MediaFileSystemBackend::HasInplaceCopyImplementation(
322 storage::FileSystemType type
) const {
323 DCHECK(type
== storage::kFileSystemTypeNativeMedia
||
324 type
== storage::kFileSystemTypeDeviceMedia
||
325 type
== storage::kFileSystemTypeItunes
||
326 type
== storage::kFileSystemTypePicasa
||
327 type
== storage::kFileSystemTypeIphoto
);
331 scoped_ptr
<storage::FileStreamReader
>
332 MediaFileSystemBackend::CreateFileStreamReader(
333 const FileSystemURL
& url
,
335 int64 max_bytes_to_read
,
336 const base::Time
& expected_modification_time
,
337 FileSystemContext
* context
) const {
338 if (url
.type() == storage::kFileSystemTypeDeviceMedia
) {
339 DCHECK(device_media_async_file_util_
);
340 scoped_ptr
<storage::FileStreamReader
> reader
=
341 device_media_async_file_util_
->GetFileStreamReader(
342 url
, offset
, expected_modification_time
, context
);
344 return reader
.Pass();
347 return scoped_ptr
<storage::FileStreamReader
>(
348 storage::FileStreamReader::CreateForLocalFile(
349 context
->default_file_task_runner(),
352 expected_modification_time
));
355 scoped_ptr
<storage::FileStreamWriter
>
356 MediaFileSystemBackend::CreateFileStreamWriter(
357 const FileSystemURL
& url
,
359 FileSystemContext
* context
) const {
360 return scoped_ptr
<storage::FileStreamWriter
>(
361 storage::FileStreamWriter::CreateForLocalFile(
362 context
->default_file_task_runner(),
365 storage::FileStreamWriter::OPEN_EXISTING_FILE
));
368 storage::FileSystemQuotaUtil
* MediaFileSystemBackend::GetQuotaUtil() {
373 const storage::UpdateObserverList
* MediaFileSystemBackend::GetUpdateObservers(
374 storage::FileSystemType type
) const {
378 const storage::ChangeObserverList
* MediaFileSystemBackend::GetChangeObservers(
379 storage::FileSystemType type
) const {
383 const storage::AccessObserverList
* MediaFileSystemBackend::GetAccessObservers(
384 storage::FileSystemType type
) const {