| 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_data_provider.h" | 5 #include "chrome/browser/media_galleries/fileapi/picasa/picasa_data_provider.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/callback.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 12 #include "chrome/browser/media_galleries/fileapi/media_file_system_backend.h" | 12 #include "chrome/browser/media_galleries/fileapi/media_file_system_backend.h" |
| 13 #include "chrome/browser/media_galleries/fileapi/safe_picasa_album_table_reader.
h" | 13 #include "chrome/browser/media_galleries/fileapi/safe_picasa_album_table_reader.
h" |
| 14 #include "chrome/browser/media_galleries/fileapi/safe_picasa_albums_indexer.h" |
| 14 #include "chrome/browser/media_galleries/imported_media_gallery_registry.h" | 15 #include "chrome/browser/media_galleries/imported_media_gallery_registry.h" |
| 15 #include "webkit/browser/fileapi/file_system_operation_context.h" | 16 #include "webkit/browser/fileapi/file_system_operation_context.h" |
| 16 #include "webkit/browser/fileapi/file_system_url.h" | 17 #include "webkit/browser/fileapi/file_system_url.h" |
| 17 | 18 |
| 18 using chrome::MediaFileSystemBackend; | 19 using chrome::MediaFileSystemBackend; |
| 19 | 20 |
| 20 namespace picasa { | 21 namespace picasa { |
| 21 | 22 |
| 23 namespace { |
| 24 |
| 25 void RunAllCallbacks( |
| 26 std::queue<PicasaDataProvider::ReadyCallback>* ready_callbacks_queue, |
| 27 bool success) { |
| 28 while (!ready_callbacks_queue->empty()) { |
| 29 ready_callbacks_queue->front().Run(success); |
| 30 ready_callbacks_queue->pop(); |
| 31 } |
| 32 } |
| 33 |
| 34 } // namespace |
| 35 |
| 22 PicasaDataProvider::PicasaDataProvider(const base::FilePath& database_path) | 36 PicasaDataProvider::PicasaDataProvider(const base::FilePath& database_path) |
| 23 : database_path_(database_path), | 37 : database_path_(database_path), |
| 24 needs_refresh_(true), | 38 state_(STALE_DATA_STATE), |
| 25 weak_factory_(this) { | 39 weak_factory_(this) { |
| 40 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 26 } | 41 } |
| 27 | 42 |
| 28 PicasaDataProvider::~PicasaDataProvider() {} | 43 PicasaDataProvider::~PicasaDataProvider() {} |
| 29 | 44 |
| 30 void PicasaDataProvider::RefreshData(const base::Closure& ready_callback) { | 45 void PicasaDataProvider::RefreshData(DataType needed_data, |
| 46 const ReadyCallback& ready_callback) { |
| 31 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | 47 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 32 // TODO(tommycli): Need to watch the database_path_ folder and handle | 48 // TODO(tommycli): Need to watch the database_path_ folder and handle |
| 33 // rereading the data when it changes. | 49 // rereading the data when it changes. |
| 34 if (!needs_refresh_) { | 50 |
| 35 ready_callback.Run(); | 51 if (state_ == INVALID_DATA_STATE) { |
| 52 ready_callback.Run(false /* success */); |
| 36 return; | 53 return; |
| 37 } | 54 } |
| 38 | 55 |
| 39 needs_refresh_ = false; | 56 if (needed_data == LIST_OF_ALBUMS_AND_FOLDERS_DATA) { |
| 40 album_table_reader_ = new SafePicasaAlbumTableReader( | 57 if (state_ == LIST_OF_ALBUMS_AND_FOLDERS_FRESH_STATE || |
| 41 AlbumTableFiles(database_path_), | 58 state_ == ALBUMS_IMAGES_FRESH_STATE) { |
| 42 base::Bind(&PicasaDataProvider::OnDataRefreshed, | 59 ready_callback.Run(true /* success */); |
| 43 weak_factory_.GetWeakPtr(), | 60 return; |
| 44 ready_callback)); | 61 } |
| 45 album_table_reader_->Start(); | 62 album_list_ready_callbacks_.push(ready_callback); |
| 63 } else { |
| 64 if (state_ == ALBUMS_IMAGES_FRESH_STATE) { |
| 65 ready_callback.Run(true /* success */); |
| 66 return; |
| 67 } |
| 68 albums_index_ready_callbacks_.push(ready_callback); |
| 69 } |
| 70 DoRefreshIfNecessary(); |
| 46 } | 71 } |
| 47 | 72 |
| 48 scoped_ptr<AlbumMap> PicasaDataProvider::GetFolders() { | 73 scoped_ptr<AlbumMap> PicasaDataProvider::GetFolders() { |
| 49 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | 74 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 75 DCHECK(state_ == LIST_OF_ALBUMS_AND_FOLDERS_FRESH_STATE || |
| 76 state_ == ALBUMS_IMAGES_FRESH_STATE); |
| 50 return make_scoped_ptr(new AlbumMap(folder_map_)); | 77 return make_scoped_ptr(new AlbumMap(folder_map_)); |
| 51 } | 78 } |
| 52 | 79 |
| 53 scoped_ptr<AlbumMap> PicasaDataProvider::GetAlbums() { | 80 scoped_ptr<AlbumMap> PicasaDataProvider::GetAlbums() { |
| 54 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | 81 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 82 DCHECK(state_ == LIST_OF_ALBUMS_AND_FOLDERS_FRESH_STATE || |
| 83 state_ == ALBUMS_IMAGES_FRESH_STATE); |
| 55 return make_scoped_ptr(new AlbumMap(album_map_)); | 84 return make_scoped_ptr(new AlbumMap(album_map_)); |
| 56 } | 85 } |
| 57 | 86 |
| 58 void PicasaDataProvider::OnDataRefreshed( | 87 scoped_ptr<AlbumImages> PicasaDataProvider::FindAlbumImages( |
| 59 const base::Closure& ready_callback, | 88 const std::string& key, |
| 89 base::PlatformFileError* error) { |
| 90 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 91 DCHECK(state_ == ALBUMS_IMAGES_FRESH_STATE); |
| 92 DCHECK(error); |
| 93 |
| 94 AlbumImagesMap::const_iterator it = albums_images_.find(key); |
| 95 |
| 96 if (it == albums_images_.end()) { |
| 97 *error = base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 98 return scoped_ptr<AlbumImages>(); |
| 99 } |
| 100 |
| 101 *error = base::PLATFORM_FILE_OK; |
| 102 return make_scoped_ptr(new AlbumImages(it->second)); |
| 103 } |
| 104 |
| 105 void PicasaDataProvider::InvalidateData() { |
| 106 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 107 |
| 108 // Set data state to stale and ignore responses from any in-flight processes. |
| 109 // TODO(tommycli): Implement and call Cancel function for these |
| 110 // UtilityProcessHostClients to actually kill the in-flight processes. |
| 111 state_ = STALE_DATA_STATE; |
| 112 album_table_reader_ = NULL; |
| 113 albums_indexer_ = NULL; |
| 114 |
| 115 DoRefreshIfNecessary(); |
| 116 } |
| 117 |
| 118 void PicasaDataProvider::DoRefreshIfNecessary() { |
| 119 DCHECK(state_ != INVALID_DATA_STATE); |
| 120 DCHECK(state_ != ALBUMS_IMAGES_FRESH_STATE); |
| 121 DCHECK(!(album_table_reader_ && albums_indexer_)); |
| 122 |
| 123 if (album_list_ready_callbacks_.empty() && |
| 124 albums_index_ready_callbacks_.empty()) { |
| 125 return; |
| 126 } |
| 127 |
| 128 if (state_ == STALE_DATA_STATE) { |
| 129 if (album_table_reader_) |
| 130 return; |
| 131 album_table_reader_ = |
| 132 new SafePicasaAlbumTableReader(AlbumTableFiles(database_path_)); |
| 133 album_table_reader_->Start( |
| 134 base::Bind(&PicasaDataProvider::OnAlbumTableReaderDone, |
| 135 weak_factory_.GetWeakPtr(), |
| 136 album_table_reader_)); |
| 137 } else { |
| 138 DCHECK(state_ == LIST_OF_ALBUMS_AND_FOLDERS_FRESH_STATE); |
| 139 if (albums_indexer_) |
| 140 return; |
| 141 albums_indexer_ = new SafePicasaAlbumsIndexer(album_map_, folder_map_); |
| 142 albums_indexer_->Start(base::Bind(&PicasaDataProvider::OnAlbumsIndexerDone, |
| 143 weak_factory_.GetWeakPtr(), |
| 144 albums_indexer_)); |
| 145 } |
| 146 } |
| 147 |
| 148 void PicasaDataProvider::OnAlbumTableReaderDone( |
| 149 scoped_refptr<SafePicasaAlbumTableReader> reader, |
| 60 bool parse_success, | 150 bool parse_success, |
| 61 const std::vector<AlbumInfo>& albums, | 151 const std::vector<AlbumInfo>& albums, |
| 62 const std::vector<AlbumInfo>& folders) { | 152 const std::vector<AlbumInfo>& folders) { |
| 63 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | 153 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 64 if (parse_success) { | 154 // If the reader has already been deemed stale, ignore the result. |
| 65 UniquifyNames(albums, &album_map_); | 155 if (reader != album_table_reader_) |
| 66 UniquifyNames(folders, &folder_map_); | 156 return; |
| 157 album_table_reader_ = NULL; |
| 158 |
| 159 DCHECK(state_ == STALE_DATA_STATE); |
| 160 |
| 161 if (!parse_success) { |
| 162 // If we didn't get the list successfully, fail all those waiting for |
| 163 // the albums indexer also. |
| 164 state_ = INVALID_DATA_STATE; |
| 165 RunAllCallbacks(&album_list_ready_callbacks_, false /* success */); |
| 166 RunAllCallbacks(&albums_index_ready_callbacks_, false /* success */); |
| 167 return; |
| 67 } | 168 } |
| 68 ready_callback.Run(); | 169 |
| 170 album_map_.clear(); |
| 171 folder_map_.clear(); |
| 172 UniquifyNames(albums, &album_map_); |
| 173 UniquifyNames(folders, &folder_map_); |
| 174 |
| 175 state_ = LIST_OF_ALBUMS_AND_FOLDERS_FRESH_STATE; |
| 176 RunAllCallbacks(&album_list_ready_callbacks_, parse_success); |
| 177 |
| 178 // Chain from this process onto refreshing the albums images if necessary. |
| 179 DoRefreshIfNecessary(); |
| 180 } |
| 181 |
| 182 void PicasaDataProvider::OnAlbumsIndexerDone( |
| 183 scoped_refptr<SafePicasaAlbumsIndexer> indexer, |
| 184 bool success, |
| 185 const picasa::AlbumImagesMap& albums_images) { |
| 186 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 187 // If the indexer has already been deemed stale, ignore the result. |
| 188 if (indexer != albums_indexer_) |
| 189 return; |
| 190 albums_indexer_ = NULL; |
| 191 |
| 192 DCHECK(state_ == LIST_OF_ALBUMS_AND_FOLDERS_FRESH_STATE); |
| 193 |
| 194 if (success) { |
| 195 state_ = ALBUMS_IMAGES_FRESH_STATE; |
| 196 |
| 197 albums_images_ = albums_images; |
| 198 } |
| 199 |
| 200 RunAllCallbacks(&albums_index_ready_callbacks_, success); |
| 69 } | 201 } |
| 70 | 202 |
| 71 // static | 203 // static |
| 72 std::string PicasaDataProvider::DateToPathString(const base::Time& time) { | 204 std::string PicasaDataProvider::DateToPathString(const base::Time& time) { |
| 73 base::Time::Exploded exploded_time; | 205 base::Time::Exploded exploded_time; |
| 74 time.LocalExplode(&exploded_time); | 206 time.LocalExplode(&exploded_time); |
| 75 | 207 |
| 76 // TODO(tommycli): Investigate better localization and persisting which locale | 208 // TODO(tommycli): Investigate better localization and persisting which locale |
| 77 // we use to generate these unique names. | 209 // we use to generate these unique names. |
| 78 return base::StringPrintf("%04d-%02d-%02d", exploded_time.year, | 210 return base::StringPrintf("%04d-%02d-%02d", exploded_time.year, |
| (...skipping 22 matching lines...) Expand all Loading... |
| 101 | 233 |
| 102 if (total_counts[name] != 1) { | 234 if (total_counts[name] != 1) { |
| 103 name = base::StringPrintf("%s (%d)", name.c_str(), | 235 name = base::StringPrintf("%s (%d)", name.c_str(), |
| 104 ++current_counts[name]); | 236 ++current_counts[name]); |
| 105 } | 237 } |
| 106 | 238 |
| 107 result_map->insert(std::pair<std::string, AlbumInfo>(name, info_list[i])); | 239 result_map->insert(std::pair<std::string, AlbumInfo>(name, info_list[i])); |
| 108 } | 240 } |
| 109 } | 241 } |
| 110 | 242 |
| 243 void PicasaDataProvider::SetDatabasePathForTesting( |
| 244 const base::FilePath& database_path) { |
| 245 database_path_ = database_path; |
| 246 } |
| 247 |
| 248 void PicasaDataProvider::SetAlbumMapsForTesting(const AlbumMap& album_map, |
| 249 const AlbumMap& folder_map) { |
| 250 album_map_ = album_map; |
| 251 folder_map_ = folder_map; |
| 252 } |
| 253 |
| 111 } // namespace picasa | 254 } // namespace picasa |
| OLD | NEW |