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_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 |
| 22 PicasaDataProvider::PicasaDataProvider(const base::FilePath& database_path) | 23 PicasaDataProvider::PicasaDataProvider(const base::FilePath& database_path) |
| 23 : database_path_(database_path), | 24 : database_path_(database_path), |
| 24 needs_refresh_(true), | 25 data_state_(STALE_DATA_STATE), |
| 25 weak_factory_(this) { | 26 weak_factory_(this) { |
| 26 } | 27 } |
| 27 | 28 |
| 28 PicasaDataProvider::~PicasaDataProvider() {} | 29 PicasaDataProvider::~PicasaDataProvider() {} |
| 29 | 30 |
| 30 void PicasaDataProvider::RefreshData(const base::Closure& ready_callback) { | 31 void PicasaDataProvider::RefreshData( |
| 32 DataType needed_data, | |
| 33 const ReadyCallback& ready_callback) { | |
| 31 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | 34 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 32 // TODO(tommycli): Need to watch the database_path_ folder and handle | 35 // TODO(tommycli): Need to watch the database_path_ folder and handle |
| 33 // rereading the data when it changes. | 36 // rereading the data when it changes. |
| 34 if (!needs_refresh_) { | 37 |
| 35 ready_callback.Run(); | 38 switch (data_state_) { |
|
vandebo (ex-Chrome)
2013/07/12 18:29:59
An open coded version is a lot shorter and I think
tommycli
2013/07/12 21:37:19
Done.
| |
| 36 return; | 39 case STALE_DATA_STATE: |
| 40 StartAlbumListRefreshIfNecessary(); | |
| 41 switch (needed_data) { | |
| 42 case LIST_OF_ALBUMS_AND_FOLDERS_DATA: | |
| 43 album_list_ready_callbacks_.push(ready_callback); | |
| 44 break; | |
| 45 case ALBUMS_IMAGES_DATA: | |
| 46 albums_indexer_ready_callbacks_.push(ready_callback); | |
| 47 break; | |
| 48 default: | |
| 49 NOTREACHED(); | |
| 50 } | |
| 51 break; | |
| 52 case ALBUM_LIST_FRESH_STATE: | |
| 53 switch (needed_data) { | |
| 54 case LIST_OF_ALBUMS_AND_FOLDERS_DATA: | |
| 55 ready_callback.Run(true /* success */); | |
| 56 break; | |
| 57 case ALBUMS_IMAGES_DATA: | |
| 58 StartAlbumsImagesRefreshIfNecessary(); | |
| 59 albums_indexer_ready_callbacks_.push(ready_callback); | |
| 60 break; | |
| 61 default: | |
| 62 NOTREACHED(); | |
| 63 } | |
| 64 break; | |
| 65 case ALBUMS_IMAGES_FRESH_STATE: | |
| 66 switch (needed_data) { | |
| 67 case LIST_OF_ALBUMS_AND_FOLDERS_DATA: | |
| 68 case ALBUMS_IMAGES_DATA: | |
| 69 ready_callback.Run(true /* success */); | |
| 70 break; | |
| 71 default: | |
| 72 NOTREACHED(); | |
| 73 } | |
| 74 break; | |
| 75 default: | |
| 76 NOTREACHED(); | |
| 37 } | 77 } |
| 38 | |
| 39 needs_refresh_ = false; | |
| 40 album_table_reader_ = new SafePicasaAlbumTableReader( | |
| 41 AlbumTableFiles(database_path_), | |
| 42 base::Bind(&PicasaDataProvider::OnDataRefreshed, | |
| 43 weak_factory_.GetWeakPtr(), | |
| 44 ready_callback)); | |
| 45 album_table_reader_->Start(); | |
| 46 } | 78 } |
| 47 | 79 |
| 48 scoped_ptr<AlbumMap> PicasaDataProvider::GetFolders() { | 80 scoped_ptr<AlbumMap> PicasaDataProvider::GetFolders() { |
| 49 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | 81 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 50 return make_scoped_ptr(new AlbumMap(folder_map_)); | 82 return make_scoped_ptr(new AlbumMap(folder_map_)); |
| 51 } | 83 } |
| 52 | 84 |
| 53 scoped_ptr<AlbumMap> PicasaDataProvider::GetAlbums() { | 85 scoped_ptr<AlbumMap> PicasaDataProvider::GetAlbums() { |
| 54 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | 86 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 55 return make_scoped_ptr(new AlbumMap(album_map_)); | 87 return make_scoped_ptr(new AlbumMap(album_map_)); |
| 56 } | 88 } |
| 57 | 89 |
| 58 void PicasaDataProvider::OnDataRefreshed( | 90 scoped_ptr<AlbumImagesMap> PicasaDataProvider::GetAlbumsImages() { |
| 59 const base::Closure& ready_callback, | 91 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 92 return make_scoped_ptr(new AlbumImagesMap(albums_images_)); | |
| 93 } | |
| 94 | |
| 95 void PicasaDataProvider::InvalidateData() { | |
| 96 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | |
| 97 DCHECK(!(album_table_reader_ && albums_indexer_)); | |
| 98 | |
| 99 // Set data state to stale and ignore responses from any in-flight processes. | |
| 100 // TODO(tommycli): Implement and call Cancel function for these | |
| 101 // UtilityProcessHostClients to actually kill the in-flight processes. | |
| 102 data_state_ = STALE_DATA_STATE; | |
| 103 album_table_reader_ = NULL; | |
| 104 albums_indexer_ = NULL; | |
| 105 | |
| 106 if (!album_list_ready_callbacks_.empty() || | |
| 107 !albums_indexer_ready_callbacks_.empty()) { | |
| 108 StartAlbumListRefreshIfNecessary(); | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 void PicasaDataProvider::StartAlbumListRefreshIfNecessary() { | |
|
vandebo (ex-Chrome)
2013/07/12 18:29:59
It might be simpler to combine these two methods i
tommycli
2013/07/12 21:37:19
Done.
| |
| 113 if (!album_table_reader_) | |
|
vandebo (ex-Chrome)
2013/07/12 18:29:59
Is this negated?
tommycli
2013/07/12 21:37:19
Done.
| |
| 114 return; | |
| 115 album_table_reader_ = new SafePicasaAlbumTableReader( | |
| 116 AlbumTableFiles(database_path_), | |
| 117 base::Bind(&PicasaDataProvider::OnAlbumListRefreshed, | |
| 118 weak_factory_.GetWeakPtr())); | |
| 119 album_table_reader_->Start(); | |
| 120 } | |
| 121 | |
| 122 void PicasaDataProvider::StartAlbumsImagesRefreshIfNecessary() { | |
| 123 if (!albums_indexer_) | |
| 124 return; | |
| 125 albums_indexer_ = new SafePicasaAlbumsIndexer( | |
| 126 album_map_, | |
| 127 folder_map_, | |
| 128 base::Bind(&PicasaDataProvider::OnAlbumsIndexerDone, | |
| 129 weak_factory_.GetWeakPtr())); | |
| 130 albums_indexer_->Start(); | |
| 131 } | |
| 132 | |
| 133 void PicasaDataProvider::OnAlbumListRefreshed( | |
| 134 scoped_refptr<SafePicasaAlbumTableReader> reader, | |
| 60 bool parse_success, | 135 bool parse_success, |
| 61 const std::vector<AlbumInfo>& albums, | 136 const std::vector<AlbumInfo>& albums, |
| 62 const std::vector<AlbumInfo>& folders) { | 137 const std::vector<AlbumInfo>& folders) { |
| 63 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | 138 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 139 // If the reader has already been deemed stale, ignore the result. | |
| 140 if (reader != album_table_reader_) | |
| 141 return; | |
| 142 | |
| 64 if (parse_success) { | 143 if (parse_success) { |
|
vandebo (ex-Chrome)
2013/07/12 18:29:59
You may want to handle parsing failed first..
You
tommycli
2013/07/12 21:37:19
Done.
| |
| 144 data_state_ = ALBUM_LIST_FRESH_STATE; | |
| 145 | |
| 146 album_map_.clear(); | |
| 147 folder_map_.clear(); | |
| 65 UniquifyNames(albums, &album_map_); | 148 UniquifyNames(albums, &album_map_); |
| 66 UniquifyNames(folders, &folder_map_); | 149 UniquifyNames(folders, &folder_map_); |
| 67 } | 150 } |
| 68 ready_callback.Run(); | 151 |
| 152 while (!album_list_ready_callbacks_.empty()) { | |
|
vandebo (ex-Chrome)
2013/07/12 18:29:59
Probably want to pull this loop into a small helpe
tommycli
2013/07/12 21:37:19
Done.
| |
| 153 album_list_ready_callbacks_.front().Run(parse_success); | |
| 154 album_list_ready_callbacks_.pop(); | |
| 155 } | |
| 156 | |
| 157 if (parse_success) { | |
| 158 // Chain from this process onto refreshing the albums images if necessary. | |
| 159 if (!albums_indexer_ready_callbacks_.empty()) | |
| 160 StartAlbumsImagesRefreshIfNecessary(); | |
| 161 } else { | |
| 162 // If we didn't get the list successfully, fail all those waiting for | |
| 163 // the albums indexer also. | |
| 164 while (!albums_indexer_ready_callbacks_.empty()) { | |
| 165 albums_indexer_ready_callbacks_.front().Run(false /* success */); | |
| 166 albums_indexer_ready_callbacks_.pop(); | |
| 167 } | |
| 168 } | |
| 169 } | |
| 170 | |
| 171 void PicasaDataProvider::OnAlbumsIndexerDone( | |
| 172 scoped_refptr<SafePicasaAlbumsIndexer> indexer, | |
| 173 bool success, | |
| 174 const picasa::AlbumImagesMap& albums_images) { | |
| 175 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | |
| 176 // If the indexer has already been deemed stale, ignore the result. | |
| 177 if (indexer != albums_indexer_) | |
| 178 return; | |
| 179 | |
| 180 if (success) { | |
| 181 data_state_ = ALBUMS_IMAGES_FRESH_STATE; | |
| 182 | |
| 183 albums_images_ = albums_images; | |
| 184 } | |
| 185 | |
| 186 while (!albums_indexer_ready_callbacks_.empty()) { | |
| 187 albums_indexer_ready_callbacks_.front().Run(success); | |
| 188 albums_indexer_ready_callbacks_.pop(); | |
| 189 } | |
| 69 } | 190 } |
| 70 | 191 |
| 71 // static | 192 // static |
| 72 std::string PicasaDataProvider::DateToPathString(const base::Time& time) { | 193 std::string PicasaDataProvider::DateToPathString(const base::Time& time) { |
| 73 base::Time::Exploded exploded_time; | 194 base::Time::Exploded exploded_time; |
| 74 time.LocalExplode(&exploded_time); | 195 time.LocalExplode(&exploded_time); |
| 75 | 196 |
| 76 // TODO(tommycli): Investigate better localization and persisting which locale | 197 // TODO(tommycli): Investigate better localization and persisting which locale |
| 77 // we use to generate these unique names. | 198 // we use to generate these unique names. |
| 78 return base::StringPrintf("%04d-%02d-%02d", exploded_time.year, | 199 return base::StringPrintf("%04d-%02d-%02d", exploded_time.year, |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 102 if (total_counts[name] != 1) { | 223 if (total_counts[name] != 1) { |
| 103 name = base::StringPrintf("%s (%d)", name.c_str(), | 224 name = base::StringPrintf("%s (%d)", name.c_str(), |
| 104 ++current_counts[name]); | 225 ++current_counts[name]); |
| 105 } | 226 } |
| 106 | 227 |
| 107 result_map->insert(std::pair<std::string, AlbumInfo>(name, info_list[i])); | 228 result_map->insert(std::pair<std::string, AlbumInfo>(name, info_list[i])); |
| 108 } | 229 } |
| 109 } | 230 } |
| 110 | 231 |
| 111 } // namespace picasa | 232 } // namespace picasa |
| OLD | NEW |