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 PicasaDataProvider::DataType GetDataTypeForURL( |
| 53 const fileapi::FileSystemURL& url) { |
| 54 std::vector<std::string> components; |
| 55 fileapi::VirtualPath::GetComponentsUTF8Unsafe(url.path(), &components); |
| 56 |
| 57 if (components.size() >= 2 && components[0] == kPicasaDirAlbums) |
| 58 return PicasaDataProvider::ALBUMS_IMAGES_DATA; |
| 59 |
| 60 return PicasaDataProvider::LIST_OF_ALBUMS_AND_FOLDERS_DATA; |
| 61 } |
| 62 |
52 } // namespace | 63 } // namespace |
53 | 64 |
54 const char kPicasaDirAlbums[] = "albums"; | 65 const char kPicasaDirAlbums[] = "albums"; |
55 const char kPicasaDirFolders[] = "folders"; | 66 const char kPicasaDirFolders[] = "folders"; |
56 | 67 |
57 PicasaFileUtil::PicasaFileUtil(chrome::MediaPathFilter* media_path_filter) | 68 PicasaFileUtil::PicasaFileUtil(chrome::MediaPathFilter* media_path_filter) |
58 : chrome::NativeMediaFileUtil(media_path_filter), | 69 : chrome::NativeMediaFileUtil(media_path_filter), |
59 weak_factory_(this) { | 70 weak_factory_(this) { |
60 } | 71 } |
61 | 72 |
62 PicasaFileUtil::~PicasaFileUtil() {} | 73 PicasaFileUtil::~PicasaFileUtil() {} |
63 | 74 |
64 void PicasaFileUtil::GetFileInfoOnTaskRunnerThread( | 75 void PicasaFileUtil::GetFileInfoOnTaskRunnerThread( |
65 scoped_ptr<fileapi::FileSystemOperationContext> context, | 76 scoped_ptr<fileapi::FileSystemOperationContext> context, |
66 const fileapi::FileSystemURL& url, | 77 const fileapi::FileSystemURL& url, |
67 const GetFileInfoCallback& callback) { | 78 const GetFileInfoCallback& callback) { |
68 GetDataProvider()->RefreshData( | 79 GetDataProvider()->RefreshData( |
69 PicasaDataProvider::LIST_OF_ALBUMS_AND_FOLDERS_DATA, | 80 GetDataTypeForURL(url), |
70 base::Bind(&PicasaFileUtil::GetFileInfoWithFreshDataProvider, | 81 base::Bind(&PicasaFileUtil::GetFileInfoWithFreshDataProvider, |
71 weak_factory_.GetWeakPtr(), | 82 weak_factory_.GetWeakPtr(), |
72 base::Passed(&context), | 83 base::Passed(&context), |
73 url, | 84 url, |
74 callback)); | 85 callback)); |
75 } | 86 } |
76 | 87 |
77 void PicasaFileUtil::ReadDirectoryOnTaskRunnerThread( | 88 void PicasaFileUtil::ReadDirectoryOnTaskRunnerThread( |
78 scoped_ptr<fileapi::FileSystemOperationContext> context, | 89 scoped_ptr<fileapi::FileSystemOperationContext> context, |
79 const fileapi::FileSystemURL& url, | 90 const fileapi::FileSystemURL& url, |
80 const ReadDirectoryCallback& callback) { | 91 const ReadDirectoryCallback& callback) { |
81 GetDataProvider()->RefreshData( | 92 GetDataProvider()->RefreshData( |
82 PicasaDataProvider::LIST_OF_ALBUMS_AND_FOLDERS_DATA, | 93 GetDataTypeForURL(url), |
83 base::Bind(&PicasaFileUtil::ReadDirectoryWithFreshDataProvider, | 94 base::Bind(&PicasaFileUtil::ReadDirectoryWithFreshDataProvider, |
84 weak_factory_.GetWeakPtr(), | 95 weak_factory_.GetWeakPtr(), |
85 base::Passed(&context), | 96 base::Passed(&context), |
86 url, | 97 url, |
87 callback)); | 98 callback)); |
88 } | 99 } |
89 | 100 |
90 base::PlatformFileError PicasaFileUtil::GetFileInfoSync( | 101 base::PlatformFileError PicasaFileUtil::GetFileInfoSync( |
91 FileSystemOperationContext* context, const FileSystemURL& url, | 102 FileSystemOperationContext* context, const FileSystemURL& url, |
92 base::PlatformFileInfo* file_info, base::FilePath* platform_path) { | 103 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(); | 210 for (AlbumMap::const_iterator it = folders->begin(); |
200 it != folders->end(); ++it) { | 211 it != folders->end(); ++it) { |
201 file_list->push_back( | 212 file_list->push_back( |
202 DirectoryEntry(it->first, DirectoryEntry::DIRECTORY, 0, | 213 DirectoryEntry(it->first, DirectoryEntry::DIRECTORY, 0, |
203 it->second.timestamp)); | 214 it->second.timestamp)); |
204 } | 215 } |
205 } | 216 } |
206 break; | 217 break; |
207 case 2: | 218 case 2: |
208 if (components[0] == kPicasaDirAlbums) { | 219 if (components[0] == kPicasaDirAlbums) { |
209 // TODO(tommycli): Implement album contents. | 220 scoped_ptr<AlbumMap> album_map = GetDataProvider()->GetAlbums(); |
| 221 AlbumInfo album_info; |
| 222 base::PlatformFileError error = |
| 223 FindAlbumInfo(components[1], album_map.get(), &album_info); |
| 224 if (error != base::PLATFORM_FILE_OK) |
| 225 return error; |
| 226 |
| 227 scoped_ptr<AlbumImages> album_images = |
| 228 GetDataProvider()->FindAlbumImages(album_info.uid, &error); |
| 229 if (error != base::PLATFORM_FILE_OK) |
| 230 return error; |
| 231 |
| 232 for (AlbumImages::const_iterator it = album_images->begin(); |
| 233 it != album_images->end(); |
| 234 ++it) { |
| 235 fileapi::DirectoryEntry entry; |
| 236 base::PlatformFileInfo info; |
| 237 |
| 238 // Simply skip files that we can't get info on. |
| 239 if (fileapi::NativeFileUtil::GetFileInfo(it->second, &info) != |
| 240 base::PLATFORM_FILE_OK) { |
| 241 continue; |
| 242 } |
| 243 |
| 244 file_list->push_back(DirectoryEntry( |
| 245 it->first, DirectoryEntry::FILE, info.size, info.last_modified)); |
| 246 } |
210 } | 247 } |
211 | 248 |
212 if (components[0] == kPicasaDirFolders) { | 249 if (components[0] == kPicasaDirFolders) { |
213 EntryList super_list; | 250 EntryList super_list; |
214 base::PlatformFileError error = | 251 base::PlatformFileError error = |
215 NativeMediaFileUtil::ReadDirectorySync(context, url, &super_list); | 252 NativeMediaFileUtil::ReadDirectorySync(context, url, &super_list); |
216 if (error != base::PLATFORM_FILE_OK) | 253 if (error != base::PLATFORM_FILE_OK) |
217 return error; | 254 return error; |
218 | 255 |
219 for (EntryList::const_iterator it = super_list.begin(); | 256 for (EntryList::const_iterator it = super_list.begin(); |
(...skipping 27 matching lines...) Expand all Loading... |
247 if (error != base::PLATFORM_FILE_OK) | 284 if (error != base::PLATFORM_FILE_OK) |
248 return error; | 285 return error; |
249 | 286 |
250 *local_file_path = album_info.path; | 287 *local_file_path = album_info.path; |
251 return base::PLATFORM_FILE_OK; | 288 return base::PLATFORM_FILE_OK; |
252 } | 289 } |
253 break; | 290 break; |
254 case 3: | 291 case 3: |
255 if (components[0] == kPicasaDirAlbums) { | 292 if (components[0] == kPicasaDirAlbums) { |
256 scoped_ptr<AlbumMap> album_map = GetDataProvider()->GetAlbums(); | 293 scoped_ptr<AlbumMap> album_map = GetDataProvider()->GetAlbums(); |
| 294 AlbumInfo album_info; |
257 base::PlatformFileError error = | 295 base::PlatformFileError error = |
258 FindAlbumInfo(components[1], album_map.get(), NULL); | 296 FindAlbumInfo(components[1], album_map.get(), &album_info); |
259 if (error != base::PLATFORM_FILE_OK) | 297 if (error != base::PLATFORM_FILE_OK) |
260 return error; | 298 return error; |
261 | 299 |
262 // TODO(tommycli): Implement album contents. | 300 scoped_ptr<AlbumImages> album_images = |
263 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | 301 GetDataProvider()->FindAlbumImages(album_info.uid, &error); |
| 302 if (error != base::PLATFORM_FILE_OK) |
| 303 return error; |
| 304 |
| 305 AlbumImages::const_iterator it = album_images->find(components[2]); |
| 306 if (it == album_images->end()) |
| 307 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 308 |
| 309 *local_file_path = it->second; |
| 310 return base::PLATFORM_FILE_OK; |
264 } | 311 } |
265 | 312 |
266 if (components[0] == kPicasaDirFolders) { | 313 if (components[0] == kPicasaDirFolders) { |
267 scoped_ptr<AlbumMap> album_map = GetDataProvider()->GetFolders(); | 314 scoped_ptr<AlbumMap> album_map = GetDataProvider()->GetFolders(); |
268 AlbumInfo album_info; | 315 AlbumInfo album_info; |
269 base::PlatformFileError error = | 316 base::PlatformFileError error = |
270 FindAlbumInfo(components[1], album_map.get(), &album_info); | 317 FindAlbumInfo(components[1], album_map.get(), &album_info); |
271 if (error != base::PLATFORM_FILE_OK) | 318 if (error != base::PLATFORM_FILE_OK) |
272 return error; | 319 return error; |
273 | 320 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 } | 364 } |
318 NativeMediaFileUtil::ReadDirectoryOnTaskRunnerThread( | 365 NativeMediaFileUtil::ReadDirectoryOnTaskRunnerThread( |
319 context.Pass(), url, callback); | 366 context.Pass(), url, callback); |
320 } | 367 } |
321 | 368 |
322 PicasaDataProvider* PicasaFileUtil::GetDataProvider() { | 369 PicasaDataProvider* PicasaFileUtil::GetDataProvider() { |
323 return chrome::ImportedMediaGalleryRegistry::PicasaDataProvider(); | 370 return chrome::ImportedMediaGalleryRegistry::PicasaDataProvider(); |
324 } | 371 } |
325 | 372 |
326 } // namespace picasa | 373 } // namespace picasa |
OLD | NEW |