Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/media_galleries/fileapi/media_file_system_backend.h" | 5 #include "chrome/browser/media_galleries/fileapi/media_file_system_backend.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/message_loop/message_loop_proxy.h" | 12 #include "base/message_loop/message_loop_proxy.h" |
| 13 #include "base/platform_file.h" | 13 #include "base/platform_file.h" |
| 14 #include "base/sequenced_task_runner.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" | |
| 15 #include "base/threading/sequenced_worker_pool.h" | 18 #include "base/threading/sequenced_worker_pool.h" |
| 19 #include "chrome/browser/browser_process.h" | |
| 20 #include "chrome/browser/extensions/extension_service.h" | |
| 16 #include "chrome/browser/media_galleries/fileapi/device_media_async_file_util.h" | 21 #include "chrome/browser/media_galleries/fileapi/device_media_async_file_util.h" |
| 17 #include "chrome/browser/media_galleries/fileapi/media_file_validator_factory.h" | 22 #include "chrome/browser/media_galleries/fileapi/media_file_validator_factory.h" |
| 18 #include "chrome/browser/media_galleries/fileapi/media_path_filter.h" | 23 #include "chrome/browser/media_galleries/fileapi/media_path_filter.h" |
| 19 #include "chrome/browser/media_galleries/fileapi/native_media_file_util.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" | |
| 20 #include "content/public/browser/browser_thread.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" | |
| 21 #include "webkit/browser/blob/file_stream_reader.h" | 33 #include "webkit/browser/blob/file_stream_reader.h" |
| 22 #include "webkit/browser/fileapi/copy_or_move_file_validator.h" | 34 #include "webkit/browser/fileapi/copy_or_move_file_validator.h" |
| 23 #include "webkit/browser/fileapi/file_stream_writer.h" | 35 #include "webkit/browser/fileapi/file_stream_writer.h" |
| 24 #include "webkit/browser/fileapi/file_system_context.h" | 36 #include "webkit/browser/fileapi/file_system_context.h" |
| 25 #include "webkit/browser/fileapi/file_system_operation.h" | 37 #include "webkit/browser/fileapi/file_system_operation.h" |
| 26 #include "webkit/browser/fileapi/file_system_operation_context.h" | 38 #include "webkit/browser/fileapi/file_system_operation_context.h" |
| 27 #include "webkit/browser/fileapi/isolated_context.h" | 39 #include "webkit/browser/fileapi/file_system_url.h" |
| 28 #include "webkit/browser/fileapi/native_file_util.h" | 40 #include "webkit/browser/fileapi/native_file_util.h" |
| 29 #include "webkit/common/fileapi/file_system_types.h" | 41 #include "webkit/common/fileapi/file_system_types.h" |
| 30 #include "webkit/common/fileapi/file_system_util.h" | 42 #include "webkit/common/fileapi/file_system_util.h" |
| 31 | 43 |
| 32 #if defined(OS_WIN) || defined(OS_MACOSX) | 44 #if defined(OS_WIN) || defined(OS_MACOSX) |
| 33 #include "chrome/browser/media_galleries/fileapi/itunes_file_util.h" | 45 #include "chrome/browser/media_galleries/fileapi/itunes_file_util.h" |
| 34 #include "chrome/browser/media_galleries/fileapi/picasa_file_util.h" | 46 #include "chrome/browser/media_galleries/fileapi/picasa_file_util.h" |
| 35 #endif // defined(OS_WIN) || defined(OS_MACOSX) | 47 #endif // defined(OS_WIN) || defined(OS_MACOSX) |
| 36 | 48 |
| 37 #if defined(OS_MACOSX) | 49 #if defined(OS_MACOSX) |
| 38 #include "chrome/browser/media_galleries/fileapi/iphoto_file_util.h" | 50 #include "chrome/browser/media_galleries/fileapi/iphoto_file_util.h" |
| 39 #endif // defined(OS_MACOSX) | 51 #endif // defined(OS_MACOSX) |
| 40 | 52 |
| 41 using fileapi::FileSystemContext; | 53 using fileapi::FileSystemContext; |
| 42 using fileapi::FileSystemURL; | 54 using fileapi::FileSystemURL; |
| 43 | 55 |
| 56 namespace { | |
| 57 | |
| 58 const char kMediaGalleryMountPrefix[] = "media_galleries-"; | |
| 59 | |
| 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, | |
| 68 callback); | |
| 69 } | |
| 70 | |
| 71 void AttemptAutoMountOnUIThread( | |
| 72 int32 process_id, | |
| 73 int32 routing_id, | |
| 74 const std::string& storage_domain, | |
| 75 const std::string& mount_point, | |
| 76 const base::Callback<void(base::File::Error result)>& callback) { | |
| 77 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 78 | |
| 79 content::RenderViewHost* rvh = | |
| 80 content::RenderViewHost::FromID(process_id, routing_id); | |
| 81 if (rvh) { | |
| 82 Profile* profile = | |
| 83 Profile::FromBrowserContext(rvh->GetProcess()->GetBrowserContext()); | |
| 84 | |
| 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; | |
| 94 if (extension && | |
| 95 extension->id() == storage_domain && | |
| 96 mount_point.compare(0, expected_mount_prefix.size(), | |
|
tzik
2014/03/20 05:52:43
ditto: can be base::StartsWith()?
vandebo (ex-Chrome)
2014/03/20 18:08:53
Done.
| |
| 97 expected_mount_prefix) == 0 && | |
| 98 base::StringToUint64(mount_point.substr(expected_mount_prefix.size()), | |
| 99 &pref_id) && | |
| 100 pref_id != kInvalidMediaGalleryPrefId) { | |
| 101 MediaGalleriesPreferences* preferences = | |
| 102 g_browser_process->media_file_system_registry()->GetPreferences( | |
| 103 profile); | |
| 104 preferences->EnsureInitialized( | |
| 105 base::Bind(&OnPreferencesInit, rvh, extension, pref_id, callback)); | |
| 106 return; | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 content::BrowserThread::PostTask( | |
| 111 content::BrowserThread::IO, | |
| 112 FROM_HERE, | |
| 113 base::Bind(callback, base::File::FILE_ERROR_NOT_FOUND)); | |
| 114 } | |
| 115 | |
| 116 } // namespace | |
| 117 | |
| 44 const char MediaFileSystemBackend::kMediaTaskRunnerName[] = | 118 const char MediaFileSystemBackend::kMediaTaskRunnerName[] = |
| 45 "media-task-runner"; | 119 "media-task-runner"; |
| 46 | 120 |
| 47 MediaFileSystemBackend::MediaFileSystemBackend( | 121 MediaFileSystemBackend::MediaFileSystemBackend( |
| 48 const base::FilePath& profile_path, | 122 const base::FilePath& profile_path, |
| 49 base::SequencedTaskRunner* media_task_runner) | 123 base::SequencedTaskRunner* media_task_runner) |
| 50 : profile_path_(profile_path), | 124 : profile_path_(profile_path), |
| 51 media_task_runner_(media_task_runner), | 125 media_task_runner_(media_task_runner), |
| 52 media_path_filter_(new MediaPathFilter), | 126 media_path_filter_(new MediaPathFilter), |
| 53 media_copy_or_move_file_validator_factory_(new MediaFileValidatorFactory), | 127 media_copy_or_move_file_validator_factory_(new MediaFileValidatorFactory), |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 80 | 154 |
| 81 // static | 155 // static |
| 82 scoped_refptr<base::SequencedTaskRunner> | 156 scoped_refptr<base::SequencedTaskRunner> |
| 83 MediaFileSystemBackend::MediaTaskRunner() { | 157 MediaFileSystemBackend::MediaTaskRunner() { |
| 84 base::SequencedWorkerPool* pool = content::BrowserThread::GetBlockingPool(); | 158 base::SequencedWorkerPool* pool = content::BrowserThread::GetBlockingPool(); |
| 85 base::SequencedWorkerPool::SequenceToken media_sequence_token = | 159 base::SequencedWorkerPool::SequenceToken media_sequence_token = |
| 86 pool->GetNamedSequenceToken(kMediaTaskRunnerName); | 160 pool->GetNamedSequenceToken(kMediaTaskRunnerName); |
| 87 return pool->GetSequencedTaskRunner(media_sequence_token); | 161 return pool->GetSequencedTaskRunner(media_sequence_token); |
| 88 } | 162 } |
| 89 | 163 |
| 164 // static | |
| 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()); | |
| 171 name.append("-"); | |
| 172 name.append(extension_id); | |
| 173 name.append("-"); | |
| 174 if (pref_id != kInvalidMediaGalleryPrefId) | |
| 175 name.append(base::Uint64ToString(pref_id)); | |
| 176 base::ReplaceChars(name, " /", "_", &name); | |
| 177 return name; | |
| 178 } | |
| 179 | |
| 180 // static | |
| 181 bool MediaFileSystemBackend::AttemptAutoMountForURLRequest( | |
| 182 const net::URLRequest* url_request, | |
| 183 const fileapi::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() != fileapi::kFileSystemTypeExternal || | |
| 188 storage_domain != filesystem_url.origin().host()) { | |
| 189 return false; | |
| 190 } | |
| 191 | |
| 192 const base::FilePath& virtual_path = filesystem_url.path(); | |
| 193 if (virtual_path.ReferencesParent()) | |
| 194 return false; | |
| 195 std::vector<base::FilePath::StringType> components; | |
| 196 virtual_path.GetComponents(&components); | |
| 197 if (components.size() < 1) | |
|
Lei Zhang
2014/03/20 00:46:26
nit: .empty() instead
vandebo (ex-Chrome)
2014/03/20 18:08:53
Done.
| |
| 198 return false; | |
| 199 std::string mount_point = base::FilePath(components[0]).AsUTF8Unsafe(); | |
| 200 | |
| 201 const content::ResourceRequestInfo* request_info = | |
| 202 content::ResourceRequestInfo::ForRequest(url_request); | |
| 203 if (!request_info) | |
| 204 return false; | |
| 205 | |
| 206 std::string expected_mount_prefix(kMediaGalleryMountPrefix); | |
| 207 if (mount_point.compare(0, expected_mount_prefix.size(), | |
|
tzik
2014/03/20 05:52:43
can be base::StartsWith()?
vandebo (ex-Chrome)
2014/03/20 18:08:53
Done.
| |
| 208 expected_mount_prefix) != 0) { | |
| 209 return false; | |
| 210 } | |
| 211 | |
| 212 content::BrowserThread::PostTask( | |
| 213 content::BrowserThread::UI, | |
| 214 FROM_HERE, | |
| 215 base::Bind(&AttemptAutoMountOnUIThread, request_info->GetChildID(), | |
| 216 request_info->GetRouteID(), storage_domain, mount_point, | |
| 217 callback)); | |
| 218 return true; | |
| 219 } | |
| 220 | |
| 90 bool MediaFileSystemBackend::CanHandleType( | 221 bool MediaFileSystemBackend::CanHandleType( |
| 91 fileapi::FileSystemType type) const { | 222 fileapi::FileSystemType type) const { |
| 92 switch (type) { | 223 switch (type) { |
| 93 case fileapi::kFileSystemTypeNativeMedia: | 224 case fileapi::kFileSystemTypeNativeMedia: |
| 94 case fileapi::kFileSystemTypeDeviceMedia: | 225 case fileapi::kFileSystemTypeDeviceMedia: |
| 95 #if defined(OS_WIN) || defined(OS_MACOSX) | 226 #if defined(OS_WIN) || defined(OS_MACOSX) |
| 96 case fileapi::kFileSystemTypePicasa: | 227 case fileapi::kFileSystemTypePicasa: |
| 97 case fileapi::kFileSystemTypeItunes: | 228 case fileapi::kFileSystemTypeItunes: |
| 98 #endif // defined(OS_WIN) || defined(OS_MACOSX) | 229 #endif // defined(OS_WIN) || defined(OS_MACOSX) |
| 99 #if defined(OS_MACOSX) | 230 #if defined(OS_MACOSX) |
| 100 case fileapi::kFileSystemTypeIphoto: | 231 case fileapi::kFileSystemTypeIphoto: |
| 101 #endif // defined(OS_MACOSX) | 232 #endif // defined(OS_MACOSX) |
| 102 return true; | 233 return true; |
| 103 default: | 234 default: |
| 104 return false; | 235 return false; |
| 105 } | 236 } |
| 106 } | 237 } |
| 107 | 238 |
| 108 void MediaFileSystemBackend::Initialize(fileapi::FileSystemContext* context) { | 239 void MediaFileSystemBackend::Initialize(fileapi::FileSystemContext* context) { |
| 109 } | 240 } |
| 110 | 241 |
| 111 void MediaFileSystemBackend::ResolveURL( | 242 void MediaFileSystemBackend::ResolveURL( |
| 112 const FileSystemURL& url, | 243 const FileSystemURL& url, |
| 113 fileapi::OpenFileSystemMode mode, | 244 fileapi::OpenFileSystemMode mode, |
| 114 const OpenFileSystemCallback& callback) { | 245 const OpenFileSystemCallback& callback) { |
| 115 // We never allow opening a new isolated FileSystem via usual ResolveURL. | 246 // We never allow opening a new FileSystem via usual ResolveURL. |
| 116 base::MessageLoopProxy::current()->PostTask( | 247 base::MessageLoopProxy::current()->PostTask( |
| 117 FROM_HERE, | 248 FROM_HERE, |
| 118 base::Bind(callback, | 249 base::Bind(callback, |
| 119 GURL(), | 250 GURL(), |
| 120 std::string(), | 251 std::string(), |
| 121 base::File::FILE_ERROR_SECURITY)); | 252 base::File::FILE_ERROR_SECURITY)); |
| 122 } | 253 } |
| 123 | 254 |
| 124 fileapi::AsyncFileUtil* MediaFileSystemBackend::GetAsyncFileUtil( | 255 fileapi::AsyncFileUtil* MediaFileSystemBackend::GetAsyncFileUtil( |
| 125 fileapi::FileSystemType type) { | 256 fileapi::FileSystemType type) { |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 219 url.path(), | 350 url.path(), |
| 220 offset, | 351 offset, |
| 221 fileapi::FileStreamWriter::OPEN_EXISTING_FILE)); | 352 fileapi::FileStreamWriter::OPEN_EXISTING_FILE)); |
| 222 } | 353 } |
| 223 | 354 |
| 224 fileapi::FileSystemQuotaUtil* | 355 fileapi::FileSystemQuotaUtil* |
| 225 MediaFileSystemBackend::GetQuotaUtil() { | 356 MediaFileSystemBackend::GetQuotaUtil() { |
| 226 // No quota support. | 357 // No quota support. |
| 227 return NULL; | 358 return NULL; |
| 228 } | 359 } |
| OLD | NEW |