| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/picasa_file_util.h" | 5 #include "chrome/browser/media_galleries/fileapi/picasa_file_util.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 14 #include "base/strings/sys_string_conversions.h" | 14 #include "base/strings/sys_string_conversions.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "chrome/browser/media_galleries/fileapi/media_file_system_backend.h" | 16 #include "chrome/browser/media_galleries/fileapi/media_file_system_backend.h" |
| 17 #include "chrome/browser/media_galleries/fileapi/picasa_data_provider.h" | 17 #include "chrome/browser/media_galleries/fileapi/picasa_data_provider.h" |
| 18 #include "chrome/browser/media_galleries/imported_media_gallery_registry.h" | 18 #include "chrome/browser/media_galleries/imported_media_gallery_registry.h" |
| 19 #include "chrome/common/media_galleries/picasa_types.h" | 19 #include "chrome/common/media_galleries/picasa_types.h" |
| 20 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
| 21 #include "webkit/browser/fileapi/file_system_operation_context.h" | 21 #include "webkit/browser/fileapi/file_system_operation_context.h" |
| 22 #include "webkit/browser/fileapi/file_system_url.h" | 22 #include "webkit/browser/fileapi/file_system_url.h" |
| 23 #include "webkit/browser/fileapi/native_file_util.h" | 23 #include "webkit/browser/fileapi/native_file_util.h" |
| 24 #include "webkit/common/fileapi/file_system_util.h" | 24 #include "webkit/common/fileapi/file_system_util.h" |
| 25 | 25 |
| 26 using base::FilePath; | 26 using base::FilePath; |
| 27 using fileapi::DirectoryEntry; | 27 using storage::DirectoryEntry; |
| 28 using fileapi::FileSystemOperationContext; | 28 using storage::FileSystemOperationContext; |
| 29 using fileapi::FileSystemURL; | 29 using storage::FileSystemURL; |
| 30 | 30 |
| 31 namespace picasa { | 31 namespace picasa { |
| 32 | 32 |
| 33 namespace { | 33 namespace { |
| 34 | 34 |
| 35 base::File::Error FindAlbumInfo(const std::string& key, | 35 base::File::Error FindAlbumInfo(const std::string& key, |
| 36 const AlbumMap* map, | 36 const AlbumMap* map, |
| 37 AlbumInfo* album_info) { | 37 AlbumInfo* album_info) { |
| 38 if (!map) | 38 if (!map) |
| 39 return base::File::FILE_ERROR_FAILED; | 39 return base::File::FILE_ERROR_FAILED; |
| 40 | 40 |
| 41 AlbumMap::const_iterator it = map->find(key); | 41 AlbumMap::const_iterator it = map->find(key); |
| 42 | 42 |
| 43 if (it == map->end()) | 43 if (it == map->end()) |
| 44 return base::File::FILE_ERROR_NOT_FOUND; | 44 return base::File::FILE_ERROR_NOT_FOUND; |
| 45 | 45 |
| 46 if (album_info != NULL) | 46 if (album_info != NULL) |
| 47 *album_info = it->second; | 47 *album_info = it->second; |
| 48 | 48 |
| 49 return base::File::FILE_OK; | 49 return base::File::FILE_OK; |
| 50 } | 50 } |
| 51 | 51 |
| 52 std::vector<std::string> GetVirtualPathComponents( | 52 std::vector<std::string> GetVirtualPathComponents( |
| 53 const fileapi::FileSystemURL& url) { | 53 const storage::FileSystemURL& url) { |
| 54 ImportedMediaGalleryRegistry* imported_registry = | 54 ImportedMediaGalleryRegistry* imported_registry = |
| 55 ImportedMediaGalleryRegistry::GetInstance(); | 55 ImportedMediaGalleryRegistry::GetInstance(); |
| 56 base::FilePath root = imported_registry->ImportedRoot().AppendASCII("picasa"); | 56 base::FilePath root = imported_registry->ImportedRoot().AppendASCII("picasa"); |
| 57 | 57 |
| 58 DCHECK(root.IsParent(url.path()) || root == url.path()); | 58 DCHECK(root.IsParent(url.path()) || root == url.path()); |
| 59 base::FilePath virtual_path; | 59 base::FilePath virtual_path; |
| 60 root.AppendRelativePath(url.path(), &virtual_path); | 60 root.AppendRelativePath(url.path(), &virtual_path); |
| 61 | 61 |
| 62 std::vector<std::string> result; | 62 std::vector<std::string> result; |
| 63 fileapi::VirtualPath::GetComponentsUTF8Unsafe(virtual_path, &result); | 63 storage::VirtualPath::GetComponentsUTF8Unsafe(virtual_path, &result); |
| 64 return result; | 64 return result; |
| 65 } | 65 } |
| 66 | 66 |
| 67 PicasaDataProvider::DataType GetDataTypeForURL( | 67 PicasaDataProvider::DataType GetDataTypeForURL( |
| 68 const fileapi::FileSystemURL& url) { | 68 const storage::FileSystemURL& url) { |
| 69 std::vector<std::string> components = GetVirtualPathComponents(url); | 69 std::vector<std::string> components = GetVirtualPathComponents(url); |
| 70 if (components.size() >= 2 && components[0] == kPicasaDirAlbums) | 70 if (components.size() >= 2 && components[0] == kPicasaDirAlbums) |
| 71 return PicasaDataProvider::ALBUMS_IMAGES_DATA; | 71 return PicasaDataProvider::ALBUMS_IMAGES_DATA; |
| 72 | 72 |
| 73 return PicasaDataProvider::LIST_OF_ALBUMS_AND_FOLDERS_DATA; | 73 return PicasaDataProvider::LIST_OF_ALBUMS_AND_FOLDERS_DATA; |
| 74 } | 74 } |
| 75 | 75 |
| 76 } // namespace | 76 } // namespace |
| 77 | 77 |
| 78 const char kPicasaDirAlbums[] = "albums"; | 78 const char kPicasaDirAlbums[] = "albums"; |
| 79 const char kPicasaDirFolders[] = "folders"; | 79 const char kPicasaDirFolders[] = "folders"; |
| 80 | 80 |
| 81 PicasaFileUtil::PicasaFileUtil(MediaPathFilter* media_path_filter) | 81 PicasaFileUtil::PicasaFileUtil(MediaPathFilter* media_path_filter) |
| 82 : NativeMediaFileUtil(media_path_filter), | 82 : NativeMediaFileUtil(media_path_filter), |
| 83 weak_factory_(this) { | 83 weak_factory_(this) { |
| 84 } | 84 } |
| 85 | 85 |
| 86 PicasaFileUtil::~PicasaFileUtil() {} | 86 PicasaFileUtil::~PicasaFileUtil() {} |
| 87 | 87 |
| 88 void PicasaFileUtil::GetFileInfoOnTaskRunnerThread( | 88 void PicasaFileUtil::GetFileInfoOnTaskRunnerThread( |
| 89 scoped_ptr<fileapi::FileSystemOperationContext> context, | 89 scoped_ptr<storage::FileSystemOperationContext> context, |
| 90 const fileapi::FileSystemURL& url, | 90 const storage::FileSystemURL& url, |
| 91 const GetFileInfoCallback& callback) { | 91 const GetFileInfoCallback& callback) { |
| 92 PicasaDataProvider* data_provider = GetDataProvider(); | 92 PicasaDataProvider* data_provider = GetDataProvider(); |
| 93 // |data_provider| may be NULL if the file system was revoked before this | 93 // |data_provider| may be NULL if the file system was revoked before this |
| 94 // operation had a chance to run. | 94 // operation had a chance to run. |
| 95 if (!data_provider) { | 95 if (!data_provider) { |
| 96 GetFileInfoWithFreshDataProvider(context.Pass(), url, callback, false); | 96 GetFileInfoWithFreshDataProvider(context.Pass(), url, callback, false); |
| 97 } else { | 97 } else { |
| 98 data_provider->RefreshData( | 98 data_provider->RefreshData( |
| 99 GetDataTypeForURL(url), | 99 GetDataTypeForURL(url), |
| 100 base::Bind(&PicasaFileUtil::GetFileInfoWithFreshDataProvider, | 100 base::Bind(&PicasaFileUtil::GetFileInfoWithFreshDataProvider, |
| 101 weak_factory_.GetWeakPtr(), | 101 weak_factory_.GetWeakPtr(), |
| 102 base::Passed(&context), | 102 base::Passed(&context), |
| 103 url, | 103 url, |
| 104 callback)); | 104 callback)); |
| 105 } | 105 } |
| 106 } | 106 } |
| 107 | 107 |
| 108 void PicasaFileUtil::ReadDirectoryOnTaskRunnerThread( | 108 void PicasaFileUtil::ReadDirectoryOnTaskRunnerThread( |
| 109 scoped_ptr<fileapi::FileSystemOperationContext> context, | 109 scoped_ptr<storage::FileSystemOperationContext> context, |
| 110 const fileapi::FileSystemURL& url, | 110 const storage::FileSystemURL& url, |
| 111 const ReadDirectoryCallback& callback) { | 111 const ReadDirectoryCallback& callback) { |
| 112 PicasaDataProvider* data_provider = GetDataProvider(); | 112 PicasaDataProvider* data_provider = GetDataProvider(); |
| 113 // |data_provider| may be NULL if the file system was revoked before this | 113 // |data_provider| may be NULL if the file system was revoked before this |
| 114 // operation had a chance to run. | 114 // operation had a chance to run. |
| 115 if (!data_provider) { | 115 if (!data_provider) { |
| 116 ReadDirectoryWithFreshDataProvider(context.Pass(), url, callback, false); | 116 ReadDirectoryWithFreshDataProvider(context.Pass(), url, callback, false); |
| 117 } else { | 117 } else { |
| 118 data_provider->RefreshData( | 118 data_provider->RefreshData( |
| 119 GetDataTypeForURL(url), | 119 GetDataTypeForURL(url), |
| 120 base::Bind(&PicasaFileUtil::ReadDirectoryWithFreshDataProvider, | 120 base::Bind(&PicasaFileUtil::ReadDirectoryWithFreshDataProvider, |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 components[0] == kPicasaDirFolders || | 177 components[0] == kPicasaDirFolders || |
| 178 result == base::File::FILE_ERROR_NOT_FOUND); | 178 result == base::File::FILE_ERROR_NOT_FOUND); |
| 179 | 179 |
| 180 return result; | 180 return result; |
| 181 } | 181 } |
| 182 | 182 |
| 183 return base::File::FILE_ERROR_NOT_FOUND; | 183 return base::File::FILE_ERROR_NOT_FOUND; |
| 184 } | 184 } |
| 185 | 185 |
| 186 base::File::Error PicasaFileUtil::ReadDirectorySync( | 186 base::File::Error PicasaFileUtil::ReadDirectorySync( |
| 187 fileapi::FileSystemOperationContext* context, | 187 storage::FileSystemOperationContext* context, |
| 188 const fileapi::FileSystemURL& url, | 188 const storage::FileSystemURL& url, |
| 189 EntryList* file_list) { | 189 EntryList* file_list) { |
| 190 DCHECK(context); | 190 DCHECK(context); |
| 191 DCHECK(file_list); | 191 DCHECK(file_list); |
| 192 DCHECK(file_list->empty()); | 192 DCHECK(file_list->empty()); |
| 193 | 193 |
| 194 base::File::Info file_info; | 194 base::File::Info file_info; |
| 195 base::FilePath platform_directory_path; | 195 base::FilePath platform_directory_path; |
| 196 base::File::Error error = GetFileInfoSync( | 196 base::File::Error error = GetFileInfoSync( |
| 197 context, url, &file_info, &platform_directory_path); | 197 context, url, &file_info, &platform_directory_path); |
| 198 | 198 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 return error; | 249 return error; |
| 250 | 250 |
| 251 scoped_ptr<AlbumImages> album_images = | 251 scoped_ptr<AlbumImages> album_images = |
| 252 GetDataProvider()->FindAlbumImages(album_info.uid, &error); | 252 GetDataProvider()->FindAlbumImages(album_info.uid, &error); |
| 253 if (error != base::File::FILE_OK) | 253 if (error != base::File::FILE_OK) |
| 254 return error; | 254 return error; |
| 255 | 255 |
| 256 for (AlbumImages::const_iterator it = album_images->begin(); | 256 for (AlbumImages::const_iterator it = album_images->begin(); |
| 257 it != album_images->end(); | 257 it != album_images->end(); |
| 258 ++it) { | 258 ++it) { |
| 259 fileapi::DirectoryEntry entry; | 259 storage::DirectoryEntry entry; |
| 260 base::File::Info info; | 260 base::File::Info info; |
| 261 | 261 |
| 262 // Simply skip files that we can't get info on. | 262 // Simply skip files that we can't get info on. |
| 263 if (fileapi::NativeFileUtil::GetFileInfo(it->second, &info) != | 263 if (storage::NativeFileUtil::GetFileInfo(it->second, &info) != |
| 264 base::File::FILE_OK) { | 264 base::File::FILE_OK) { |
| 265 continue; | 265 continue; |
| 266 } | 266 } |
| 267 | 267 |
| 268 file_list->push_back(DirectoryEntry( | 268 file_list->push_back(DirectoryEntry( |
| 269 it->first, DirectoryEntry::FILE, info.size, info.last_modified)); | 269 it->first, DirectoryEntry::FILE, info.size, info.last_modified)); |
| 270 } | 270 } |
| 271 } | 271 } |
| 272 | 272 |
| 273 if (components[0] == kPicasaDirFolders) { | 273 if (components[0] == kPicasaDirFolders) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 284 } | 284 } |
| 285 } | 285 } |
| 286 | 286 |
| 287 break; | 287 break; |
| 288 } | 288 } |
| 289 | 289 |
| 290 return base::File::FILE_OK; | 290 return base::File::FILE_OK; |
| 291 } | 291 } |
| 292 | 292 |
| 293 base::File::Error PicasaFileUtil::DeleteDirectorySync( | 293 base::File::Error PicasaFileUtil::DeleteDirectorySync( |
| 294 fileapi::FileSystemOperationContext* context, | 294 storage::FileSystemOperationContext* context, |
| 295 const fileapi::FileSystemURL& url) { | 295 const storage::FileSystemURL& url) { |
| 296 return base::File::FILE_ERROR_SECURITY; | 296 return base::File::FILE_ERROR_SECURITY; |
| 297 } | 297 } |
| 298 | 298 |
| 299 base::File::Error PicasaFileUtil::DeleteFileSync( | 299 base::File::Error PicasaFileUtil::DeleteFileSync( |
| 300 fileapi::FileSystemOperationContext* context, | 300 storage::FileSystemOperationContext* context, |
| 301 const fileapi::FileSystemURL& url) { | 301 const storage::FileSystemURL& url) { |
| 302 return base::File::FILE_ERROR_SECURITY; | 302 return base::File::FILE_ERROR_SECURITY; |
| 303 } | 303 } |
| 304 | 304 |
| 305 base::File::Error PicasaFileUtil::GetLocalFilePath( | 305 base::File::Error PicasaFileUtil::GetLocalFilePath( |
| 306 FileSystemOperationContext* context, const FileSystemURL& url, | 306 FileSystemOperationContext* context, const FileSystemURL& url, |
| 307 base::FilePath* local_file_path) { | 307 base::FilePath* local_file_path) { |
| 308 DCHECK(local_file_path); | 308 DCHECK(local_file_path); |
| 309 DCHECK(url.is_valid()); | 309 DCHECK(url.is_valid()); |
| 310 std::vector<std::string> components = GetVirtualPathComponents(url); | 310 std::vector<std::string> components = GetVirtualPathComponents(url); |
| 311 | 311 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 362 break; | 362 break; |
| 363 } | 363 } |
| 364 | 364 |
| 365 // All other cases don't have a local path. The valid cases should be | 365 // All other cases don't have a local path. The valid cases should be |
| 366 // intercepted by GetFileInfo()/CreateFileEnumerator(). Invalid cases | 366 // intercepted by GetFileInfo()/CreateFileEnumerator(). Invalid cases |
| 367 // return a NOT_FOUND error. | 367 // return a NOT_FOUND error. |
| 368 return base::File::FILE_ERROR_NOT_FOUND; | 368 return base::File::FILE_ERROR_NOT_FOUND; |
| 369 } | 369 } |
| 370 | 370 |
| 371 void PicasaFileUtil::GetFileInfoWithFreshDataProvider( | 371 void PicasaFileUtil::GetFileInfoWithFreshDataProvider( |
| 372 scoped_ptr<fileapi::FileSystemOperationContext> context, | 372 scoped_ptr<storage::FileSystemOperationContext> context, |
| 373 const fileapi::FileSystemURL& url, | 373 const storage::FileSystemURL& url, |
| 374 const GetFileInfoCallback& callback, | 374 const GetFileInfoCallback& callback, |
| 375 bool success) { | 375 bool success) { |
| 376 if (!success) { | 376 if (!success) { |
| 377 content::BrowserThread::PostTask( | 377 content::BrowserThread::PostTask( |
| 378 content::BrowserThread::IO, | 378 content::BrowserThread::IO, |
| 379 FROM_HERE, | 379 FROM_HERE, |
| 380 base::Bind(callback, base::File::FILE_ERROR_IO, base::File::Info())); | 380 base::Bind(callback, base::File::FILE_ERROR_IO, base::File::Info())); |
| 381 return; | 381 return; |
| 382 } | 382 } |
| 383 NativeMediaFileUtil::GetFileInfoOnTaskRunnerThread( | 383 NativeMediaFileUtil::GetFileInfoOnTaskRunnerThread( |
| 384 context.Pass(), url, callback); | 384 context.Pass(), url, callback); |
| 385 } | 385 } |
| 386 | 386 |
| 387 void PicasaFileUtil::ReadDirectoryWithFreshDataProvider( | 387 void PicasaFileUtil::ReadDirectoryWithFreshDataProvider( |
| 388 scoped_ptr<fileapi::FileSystemOperationContext> context, | 388 scoped_ptr<storage::FileSystemOperationContext> context, |
| 389 const fileapi::FileSystemURL& url, | 389 const storage::FileSystemURL& url, |
| 390 const ReadDirectoryCallback& callback, | 390 const ReadDirectoryCallback& callback, |
| 391 bool success) { | 391 bool success) { |
| 392 if (!success) { | 392 if (!success) { |
| 393 content::BrowserThread::PostTask( | 393 content::BrowserThread::PostTask( |
| 394 content::BrowserThread::IO, | 394 content::BrowserThread::IO, |
| 395 FROM_HERE, | 395 FROM_HERE, |
| 396 base::Bind(callback, base::File::FILE_ERROR_IO, EntryList(), false)); | 396 base::Bind(callback, base::File::FILE_ERROR_IO, EntryList(), false)); |
| 397 return; | 397 return; |
| 398 } | 398 } |
| 399 NativeMediaFileUtil::ReadDirectoryOnTaskRunnerThread( | 399 NativeMediaFileUtil::ReadDirectoryOnTaskRunnerThread( |
| 400 context.Pass(), url, callback); | 400 context.Pass(), url, callback); |
| 401 } | 401 } |
| 402 | 402 |
| 403 PicasaDataProvider* PicasaFileUtil::GetDataProvider() { | 403 PicasaDataProvider* PicasaFileUtil::GetDataProvider() { |
| 404 return ImportedMediaGalleryRegistry::PicasaDataProvider(); | 404 return ImportedMediaGalleryRegistry::PicasaDataProvider(); |
| 405 } | 405 } |
| 406 | 406 |
| 407 } // namespace picasa | 407 } // namespace picasa |
| OLD | NEW |