Chromium Code Reviews| 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/picasa_file_util.h" | 5 #include "chrome/browser/media_galleries/fileapi/picasa/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/picasa_data_provider.h" | 17 #include "chrome/browser/media_galleries/fileapi/picasa/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/common/fileapi/file_system_util.h" | 24 #include "webkit/common/fileapi/file_system_util.h" |
| 24 | 25 |
| 25 using base::FilePath; | 26 using base::FilePath; |
| 26 using fileapi::DirectoryEntry; | 27 using fileapi::DirectoryEntry; |
| 27 using fileapi::FileSystemOperationContext; | 28 using fileapi::FileSystemOperationContext; |
| 28 using fileapi::FileSystemURL; | 29 using fileapi::FileSystemURL; |
| 29 | 30 |
| 30 namespace picasa { | 31 namespace picasa { |
| 31 | 32 |
| 32 namespace { | 33 namespace { |
| 33 | 34 |
| 34 // |error| is only set when the method fails and the return is NULL. | |
| 35 base::PlatformFileError FindAlbumInfo(const std::string& key, | 35 base::PlatformFileError 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::PLATFORM_FILE_ERROR_FAILED; | 39 return base::PLATFORM_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::PLATFORM_FILE_ERROR_NOT_FOUND; | 44 return base::PLATFORM_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::PLATFORM_FILE_OK; | 49 return base::PLATFORM_FILE_OK; |
| 50 } | 50 } |
| 51 | 51 |
| 52 // This method returns a pointer to the AlbumImages instead of a | |
|
Greg Billock
2013/08/21 17:13:17
You could have const AlbumImages** as an outparam.
tommycli
2013/08/21 21:37:06
I moved it to the data provider. Can't do AlbumIma
| |
| 53 // base::PlatformFileError because copying an AlbumImages is expensive. | |
| 54 const AlbumImages* FindAlbumImages(const std::string& key, | |
|
Greg Billock
2013/08/21 17:13:17
Should this be a method in the data provider?
tommycli
2013/08/21 21:37:06
Yes, that's actually much better. I put those chan
| |
| 55 const AlbumImagesMap* map, | |
| 56 base::PlatformFileError* error) { | |
| 57 DCHECK(error); | |
| 58 | |
| 59 if (!map) { | |
|
Greg Billock
2013/08/21 17:13:17
should this be DCHECK?
tommycli
2013/08/21 21:37:06
Done.
| |
| 60 *error = base::PLATFORM_FILE_ERROR_FAILED; | |
| 61 return NULL; | |
| 62 } | |
| 63 | |
| 64 AlbumImagesMap::const_iterator it = map->find(key); | |
| 65 | |
| 66 if (it == map->end()) { | |
| 67 *error = base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 68 return NULL; | |
| 69 } | |
| 70 | |
| 71 *error = base::PLATFORM_FILE_OK; | |
| 72 return &(it->second); | |
| 73 } | |
| 74 | |
| 75 PicasaDataProvider::DataType GetNeededDataType( | |
| 76 const fileapi::FileSystemURL& url) { | |
| 77 std::vector<std::string> components; | |
| 78 fileapi::VirtualPath::GetComponentsUTF8Unsafe(url.path(), &components); | |
| 79 | |
| 80 if (components.size() >= 2 && components[0] == kPicasaDirAlbums) | |
|
Greg Billock
2013/08/21 17:13:17
How about adding a comment to PicasaFileUtil expla
tommycli
2013/08/21 21:37:06
Done. Good idea.
| |
| 81 return PicasaDataProvider::ALBUMS_IMAGES_DATA; | |
| 82 | |
| 83 return PicasaDataProvider::LIST_OF_ALBUMS_AND_FOLDERS_DATA; | |
| 84 } | |
| 85 | |
| 52 } // namespace | 86 } // namespace |
| 53 | 87 |
| 54 const char kPicasaDirAlbums[] = "albums"; | 88 const char kPicasaDirAlbums[] = "albums"; |
| 55 const char kPicasaDirFolders[] = "folders"; | 89 const char kPicasaDirFolders[] = "folders"; |
| 56 | 90 |
| 57 PicasaFileUtil::PicasaFileUtil(chrome::MediaPathFilter* media_path_filter) | 91 PicasaFileUtil::PicasaFileUtil(chrome::MediaPathFilter* media_path_filter) |
| 58 : chrome::NativeMediaFileUtil(media_path_filter), | 92 : chrome::NativeMediaFileUtil(media_path_filter), |
| 59 weak_factory_(this) { | 93 weak_factory_(this) { |
| 60 } | 94 } |
| 61 | 95 |
| 62 PicasaFileUtil::~PicasaFileUtil() {} | 96 PicasaFileUtil::~PicasaFileUtil() {} |
| 63 | 97 |
| 64 void PicasaFileUtil::GetFileInfoOnTaskRunnerThread( | 98 void PicasaFileUtil::GetFileInfoOnTaskRunnerThread( |
| 65 scoped_ptr<fileapi::FileSystemOperationContext> context, | 99 scoped_ptr<fileapi::FileSystemOperationContext> context, |
| 66 const fileapi::FileSystemURL& url, | 100 const fileapi::FileSystemURL& url, |
| 67 const GetFileInfoCallback& callback) { | 101 const GetFileInfoCallback& callback) { |
| 68 GetDataProvider()->RefreshData( | 102 GetDataProvider()->RefreshData( |
| 69 PicasaDataProvider::LIST_OF_ALBUMS_AND_FOLDERS_DATA, | 103 GetNeededDataType(url), |
| 70 base::Bind(&PicasaFileUtil::GetFileInfoWithFreshDataProvider, | 104 base::Bind(&PicasaFileUtil::GetFileInfoWithFreshDataProvider, |
| 71 weak_factory_.GetWeakPtr(), | 105 weak_factory_.GetWeakPtr(), |
| 72 base::Passed(&context), | 106 base::Passed(&context), |
| 73 url, | 107 url, |
| 74 callback)); | 108 callback)); |
| 75 } | 109 } |
| 76 | 110 |
| 77 void PicasaFileUtil::ReadDirectoryOnTaskRunnerThread( | 111 void PicasaFileUtil::ReadDirectoryOnTaskRunnerThread( |
| 78 scoped_ptr<fileapi::FileSystemOperationContext> context, | 112 scoped_ptr<fileapi::FileSystemOperationContext> context, |
| 79 const fileapi::FileSystemURL& url, | 113 const fileapi::FileSystemURL& url, |
| 80 const ReadDirectoryCallback& callback) { | 114 const ReadDirectoryCallback& callback) { |
| 81 GetDataProvider()->RefreshData( | 115 GetDataProvider()->RefreshData( |
| 82 PicasaDataProvider::LIST_OF_ALBUMS_AND_FOLDERS_DATA, | 116 GetNeededDataType(url), |
|
Greg Billock
2013/08/21 17:13:17
GetDataTypeForUrl ?
tommycli
2013/08/21 21:37:06
Done.
| |
| 83 base::Bind(&PicasaFileUtil::ReadDirectoryWithFreshDataProvider, | 117 base::Bind(&PicasaFileUtil::ReadDirectoryWithFreshDataProvider, |
| 84 weak_factory_.GetWeakPtr(), | 118 weak_factory_.GetWeakPtr(), |
| 85 base::Passed(&context), | 119 base::Passed(&context), |
| 86 url, | 120 url, |
| 87 callback)); | 121 callback)); |
| 88 } | 122 } |
| 89 | 123 |
| 90 base::PlatformFileError PicasaFileUtil::GetFileInfoSync( | 124 base::PlatformFileError PicasaFileUtil::GetFileInfoSync( |
| 91 FileSystemOperationContext* context, const FileSystemURL& url, | 125 FileSystemOperationContext* context, const FileSystemURL& url, |
| 92 base::PlatformFileInfo* file_info, base::FilePath* platform_path) { | 126 base::PlatformFileInfo* file_info, base::FilePath* platform_path) { |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 199 for (AlbumMap::const_iterator it = folders->begin(); | 233 for (AlbumMap::const_iterator it = folders->begin(); |
| 200 it != folders->end(); ++it) { | 234 it != folders->end(); ++it) { |
| 201 file_list->push_back( | 235 file_list->push_back( |
| 202 DirectoryEntry(it->first, DirectoryEntry::DIRECTORY, 0, | 236 DirectoryEntry(it->first, DirectoryEntry::DIRECTORY, 0, |
| 203 it->second.timestamp)); | 237 it->second.timestamp)); |
| 204 } | 238 } |
| 205 } | 239 } |
| 206 break; | 240 break; |
| 207 case 2: | 241 case 2: |
| 208 if (components[0] == kPicasaDirAlbums) { | 242 if (components[0] == kPicasaDirAlbums) { |
| 209 // TODO(tommycli): Implement album contents. | 243 scoped_ptr<AlbumMap> album_map = GetDataProvider()->GetAlbums(); |
| 244 AlbumInfo album_info; | |
| 245 base::PlatformFileError error = | |
| 246 FindAlbumInfo(components[1], album_map.get(), &album_info); | |
| 247 if (error != base::PLATFORM_FILE_OK) | |
| 248 return error; | |
| 249 | |
| 250 scoped_ptr<AlbumImagesMap> album_images_map = | |
| 251 GetDataProvider()->GetAlbumsImages(); | |
| 252 const AlbumImages* album_images = | |
| 253 FindAlbumImages(album_info.uid, album_images_map.get(), &error); | |
| 254 if (error != base::PLATFORM_FILE_OK) | |
| 255 return error; | |
| 256 | |
| 257 for (AlbumImages::const_iterator it = album_images->begin(); | |
| 258 it != album_images->end(); | |
| 259 ++it) { | |
| 260 fileapi::DirectoryEntry entry; | |
| 261 base::PlatformFileInfo info; | |
| 262 | |
| 263 // Simply skip files that we can't get info on. | |
| 264 if (fileapi::NativeFileUtil::GetFileInfo(it->second, &info) != | |
| 265 base::PLATFORM_FILE_OK) { | |
| 266 continue; | |
| 267 } | |
| 268 | |
| 269 file_list->push_back(DirectoryEntry( | |
| 270 it->first, DirectoryEntry::FILE, info.size, info.last_modified)); | |
| 271 } | |
| 210 } | 272 } |
| 211 | 273 |
| 212 if (components[0] == kPicasaDirFolders) { | 274 if (components[0] == kPicasaDirFolders) { |
| 213 EntryList super_list; | 275 EntryList super_list; |
| 214 base::PlatformFileError error = | 276 base::PlatformFileError error = |
| 215 NativeMediaFileUtil::ReadDirectorySync(context, url, &super_list); | 277 NativeMediaFileUtil::ReadDirectorySync(context, url, &super_list); |
| 216 if (error != base::PLATFORM_FILE_OK) | 278 if (error != base::PLATFORM_FILE_OK) |
| 217 return error; | 279 return error; |
| 218 | 280 |
| 219 for (EntryList::const_iterator it = super_list.begin(); | 281 for (EntryList::const_iterator it = super_list.begin(); |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 247 if (error != base::PLATFORM_FILE_OK) | 309 if (error != base::PLATFORM_FILE_OK) |
| 248 return error; | 310 return error; |
| 249 | 311 |
| 250 *local_file_path = album_info.path; | 312 *local_file_path = album_info.path; |
| 251 return base::PLATFORM_FILE_OK; | 313 return base::PLATFORM_FILE_OK; |
| 252 } | 314 } |
| 253 break; | 315 break; |
| 254 case 3: | 316 case 3: |
| 255 if (components[0] == kPicasaDirAlbums) { | 317 if (components[0] == kPicasaDirAlbums) { |
| 256 scoped_ptr<AlbumMap> album_map = GetDataProvider()->GetAlbums(); | 318 scoped_ptr<AlbumMap> album_map = GetDataProvider()->GetAlbums(); |
| 319 AlbumInfo album_info; | |
| 257 base::PlatformFileError error = | 320 base::PlatformFileError error = |
| 258 FindAlbumInfo(components[1], album_map.get(), NULL); | 321 FindAlbumInfo(components[1], album_map.get(), &album_info); |
| 259 if (error != base::PLATFORM_FILE_OK) | 322 if (error != base::PLATFORM_FILE_OK) |
| 260 return error; | 323 return error; |
| 261 | 324 |
| 262 // TODO(tommycli): Implement album contents. | 325 scoped_ptr<AlbumImagesMap> album_images_map = |
| 263 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | 326 GetDataProvider()->GetAlbumsImages(); |
|
Greg Billock
2013/08/21 17:13:17
Is this file missing from the change? I don't see
tommycli
2013/08/21 21:37:06
Yes, this CL depends on https://codereview.chromiu
| |
| 327 const AlbumImages* album_images = | |
| 328 FindAlbumImages(album_info.uid, album_images_map.get(), &error); | |
| 329 if (error != base::PLATFORM_FILE_OK) | |
| 330 return error; | |
| 331 | |
| 332 AlbumImages::const_iterator it = album_images->find(components[2]); | |
| 333 if (it == album_images->end()) | |
| 334 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 335 | |
| 336 *local_file_path = it->second; | |
| 337 return base::PLATFORM_FILE_OK; | |
| 264 } | 338 } |
| 265 | 339 |
| 266 if (components[0] == kPicasaDirFolders) { | 340 if (components[0] == kPicasaDirFolders) { |
| 267 scoped_ptr<AlbumMap> album_map = GetDataProvider()->GetFolders(); | 341 scoped_ptr<AlbumMap> album_map = GetDataProvider()->GetFolders(); |
| 268 AlbumInfo album_info; | 342 AlbumInfo album_info; |
| 269 base::PlatformFileError error = | 343 base::PlatformFileError error = |
| 270 FindAlbumInfo(components[1], album_map.get(), &album_info); | 344 FindAlbumInfo(components[1], album_map.get(), &album_info); |
| 271 if (error != base::PLATFORM_FILE_OK) | 345 if (error != base::PLATFORM_FILE_OK) |
| 272 return error; | 346 return error; |
| 273 | 347 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 317 } | 391 } |
| 318 NativeMediaFileUtil::ReadDirectoryOnTaskRunnerThread( | 392 NativeMediaFileUtil::ReadDirectoryOnTaskRunnerThread( |
| 319 context.Pass(), url, callback); | 393 context.Pass(), url, callback); |
| 320 } | 394 } |
| 321 | 395 |
| 322 PicasaDataProvider* PicasaFileUtil::GetDataProvider() { | 396 PicasaDataProvider* PicasaFileUtil::GetDataProvider() { |
| 323 return chrome::ImportedMediaGalleryRegistry::PicasaDataProvider(); | 397 return chrome::ImportedMediaGalleryRegistry::PicasaDataProvider(); |
| 324 } | 398 } |
| 325 | 399 |
| 326 } // namespace picasa | 400 } // namespace picasa |
| OLD | NEW |