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_(NO_FRESH_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 bool need_albums_images, | |
|
vandebo (ex-Chrome)
2013/07/11 16:56:27
Better to have this as an enum than a bool; it mak
tommycli
2013/07/11 21:19:17
Done. This is kind of a strange case:
Any time we
vandebo (ex-Chrome)
2013/07/11 23:22:37
I think it's ok the way it is. If you want the in
tommycli
2013/07/12 01:37:48
Done.
| |
| 33 const base::Closure& 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 |
|
vandebo (ex-Chrome)
2013/07/11 16:56:27
Do an early exit here. If state is already good e
tommycli
2013/07/11 21:19:17
The Ensure* functions perform the early exits them
vandebo (ex-Chrome)
2013/07/11 23:22:37
I really think that things will end up being a lot
tommycli
2013/07/12 01:37:48
Done.
| |
| 35 ready_callback.Run(); | 38 if (need_albums_images) |
| 39 EnsureAlbumListRefreshed(ready_callback); | |
| 40 else | |
| 41 EnsureAlbumsImagesRefreshed(ready_callback); | |
| 42 } | |
| 43 | |
| 44 void PicasaDataProvider::InvalidateData() { | |
| 45 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | |
| 46 DCHECK(!(album_table_reader_ && albums_indexer_)); | |
| 47 | |
| 48 if (album_table_reader_) { | |
|
vandebo (ex-Chrome)
2013/07/11 16:56:27
This should first set the state to NO_FRESH_DATA_S
tommycli
2013/07/11 21:19:17
Done.
| |
| 49 album_table_reader_ = NULL; | |
| 50 EnsureAlbumListRefreshed(base::Bind(&base::DoNothing)); | |
|
vandebo (ex-Chrome)
2013/07/11 16:56:27
These DoNothing callbacks could possibly accumulat
tommycli
2013/07/11 21:19:17
Done.
| |
| 36 return; | 51 return; |
| 37 } | 52 } |
| 38 | 53 |
| 39 needs_refresh_ = false; | 54 if (albums_indexer_) { |
| 40 album_table_reader_ = new SafePicasaAlbumTableReader( | 55 albums_indexer_ = NULL; |
| 41 AlbumTableFiles(database_path_), | 56 EnsureAlbumsImagesRefreshed(base::Bind(&base::DoNothing)); |
| 42 base::Bind(&PicasaDataProvider::OnDataRefreshed, | 57 return; |
| 43 weak_factory_.GetWeakPtr(), | 58 } |
| 44 ready_callback)); | |
| 45 album_table_reader_->Start(); | |
| 46 } | 59 } |
| 47 | 60 |
| 48 scoped_ptr<AlbumMap> PicasaDataProvider::GetFolders() { | 61 scoped_ptr<AlbumMap> PicasaDataProvider::GetFolders() { |
| 49 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | 62 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 50 return make_scoped_ptr(new AlbumMap(folder_map_)); | 63 return make_scoped_ptr(new AlbumMap(folder_map_)); |
| 51 } | 64 } |
| 52 | 65 |
| 53 scoped_ptr<AlbumMap> PicasaDataProvider::GetAlbums() { | 66 scoped_ptr<AlbumMap> PicasaDataProvider::GetAlbums() { |
| 54 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | 67 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 55 return make_scoped_ptr(new AlbumMap(album_map_)); | 68 return make_scoped_ptr(new AlbumMap(album_map_)); |
| 56 } | 69 } |
| 57 | 70 |
| 58 void PicasaDataProvider::OnDataRefreshed( | 71 scoped_ptr<AlbumImagesMap> PicasaDataProvider::GetAlbumsImages() { |
| 59 const base::Closure& ready_callback, | 72 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 73 return make_scoped_ptr(new AlbumImagesMap(albums_images_)); | |
| 74 } | |
| 75 | |
| 76 void PicasaDataProvider::EnsureAlbumListRefreshed( | |
| 77 const base::Closure& ready_callback) { | |
| 78 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | |
| 79 switch (data_state_) { | |
| 80 case NO_FRESH_DATA_STATE: | |
| 81 if (!album_table_reader_) { | |
| 82 album_table_reader_ = new SafePicasaAlbumTableReader( | |
| 83 AlbumTableFiles(database_path_), | |
| 84 base::Bind(&PicasaDataProvider::OnAlbumListRefreshed, | |
| 85 weak_factory_.GetWeakPtr())); | |
| 86 album_table_reader_->Start(); | |
| 87 } | |
| 88 album_list_ready_callbacks_.push(ready_callback); | |
| 89 break; | |
| 90 case ALBUM_LIST_FRESH_STATE: | |
| 91 case ALBUMS_IMAGES_FRESH_STATE: | |
| 92 ready_callback.Run(); | |
| 93 break; | |
| 94 default: | |
| 95 NOTREACHED(); | |
| 96 } | |
| 97 } | |
| 98 | |
| 99 void PicasaDataProvider::EnsureAlbumsImagesRefreshed( | |
| 100 const base::Closure& ready_callback) { | |
| 101 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | |
| 102 switch (data_state_) { | |
| 103 case NO_FRESH_DATA_STATE: | |
| 104 // Album list not ready. Refresh that first, then call this again. | |
| 105 EnsureAlbumListRefreshed( | |
| 106 base::Bind(&PicasaDataProvider::EnsureAlbumsImagesRefreshed, | |
| 107 weak_factory_.GetWeakPtr(), | |
| 108 ready_callback)); | |
| 109 break; | |
| 110 case ALBUM_LIST_FRESH_STATE: | |
| 111 if (!albums_indexer_) { | |
| 112 albums_indexer_ = new SafePicasaAlbumsIndexer( | |
| 113 album_map_, | |
| 114 folder_map_, | |
| 115 base::Bind(&PicasaDataProvider::OnAlbumsIndexerDone, | |
| 116 weak_factory_.GetWeakPtr())); | |
| 117 albums_indexer_->Start(); | |
| 118 } | |
| 119 albums_indexer_ready_callbacks_.push(ready_callback); | |
| 120 break; | |
| 121 case ALBUMS_IMAGES_FRESH_STATE: | |
| 122 ready_callback.Run(); | |
| 123 break; | |
| 124 default: | |
| 125 NOTREACHED(); | |
| 126 } | |
| 127 } | |
| 128 | |
| 129 void PicasaDataProvider::OnAlbumListRefreshed( | |
| 130 scoped_refptr<SafePicasaAlbumTableReader> reader, | |
| 60 bool parse_success, | 131 bool parse_success, |
| 61 const std::vector<AlbumInfo>& albums, | 132 const std::vector<AlbumInfo>& albums, |
| 62 const std::vector<AlbumInfo>& folders) { | 133 const std::vector<AlbumInfo>& folders) { |
| 63 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | 134 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 135 // If the reader has already been deemed stale, ignore the result. | |
| 136 if (reader != album_table_reader_) | |
| 137 return; | |
| 138 | |
| 64 if (parse_success) { | 139 if (parse_success) { |
|
vandebo (ex-Chrome)
2013/07/11 16:56:27
What to do on parse_failure ? That's why the Itun
tommycli
2013/07/11 21:19:17
Yeah - I'm not sure what the right thing to do in
vandebo (ex-Chrome)
2013/07/11 23:22:37
I think we shouldn't trigger the invalidate until
tommycli
2013/07/12 01:37:48
Done.
| |
| 140 album_map_.clear(); | |
| 141 folder_map_.clear(); | |
| 65 UniquifyNames(albums, &album_map_); | 142 UniquifyNames(albums, &album_map_); |
| 66 UniquifyNames(folders, &folder_map_); | 143 UniquifyNames(folders, &folder_map_); |
| 67 } | 144 } |
| 68 ready_callback.Run(); | 145 |
| 146 data_state_ = ALBUM_LIST_FRESH_STATE; | |
| 147 | |
| 148 while (!album_list_ready_callbacks_.empty()) { | |
| 149 album_list_ready_callbacks_.front().Run(); | |
| 150 album_list_ready_callbacks_.pop(); | |
| 151 } | |
|
vandebo (ex-Chrome)
2013/07/11 16:56:27
Then, if |albums_indexer_ready_callbacks_| isn't e
tommycli
2013/07/11 21:19:17
The chaining itself is one of the callbacks in the
vandebo (ex-Chrome)
2013/07/11 23:22:37
I think this makes things more complicated and har
tommycli
2013/07/12 01:37:48
Done.
| |
| 152 } | |
| 153 | |
| 154 void PicasaDataProvider::OnAlbumsIndexerDone( | |
| 155 scoped_refptr<SafePicasaAlbumsIndexer> indexer, | |
|
vandebo (ex-Chrome)
2013/07/11 16:56:27
Should this also take a parse_success argument?
tommycli
2013/07/11 21:19:17
That parser doesn't have a success argument. It ju
| |
| 156 const picasa::AlbumImagesMap& albums_images) { | |
| 157 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | |
| 158 // If the indexer has already been deemed stale, ignore the result. | |
| 159 if (indexer != albums_indexer_) | |
| 160 return; | |
| 161 | |
| 162 albums_images_ = albums_images; | |
| 163 | |
| 164 data_state_ = ALBUMS_IMAGES_FRESH_STATE; | |
| 165 | |
| 166 while (!albums_indexer_ready_callbacks_.empty()) { | |
| 167 albums_indexer_ready_callbacks_.front().Run(); | |
| 168 albums_indexer_ready_callbacks_.pop(); | |
| 169 } | |
| 69 } | 170 } |
| 70 | 171 |
| 71 // static | 172 // static |
| 72 std::string PicasaDataProvider::DateToPathString(const base::Time& time) { | 173 std::string PicasaDataProvider::DateToPathString(const base::Time& time) { |
| 73 base::Time::Exploded exploded_time; | 174 base::Time::Exploded exploded_time; |
| 74 time.LocalExplode(&exploded_time); | 175 time.LocalExplode(&exploded_time); |
| 75 | 176 |
| 76 // TODO(tommycli): Investigate better localization and persisting which locale | 177 // TODO(tommycli): Investigate better localization and persisting which locale |
| 77 // we use to generate these unique names. | 178 // we use to generate these unique names. |
| 78 return base::StringPrintf("%04d-%02d-%02d", exploded_time.year, | 179 return base::StringPrintf("%04d-%02d-%02d", exploded_time.year, |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 102 if (total_counts[name] != 1) { | 203 if (total_counts[name] != 1) { |
| 103 name = base::StringPrintf("%s (%d)", name.c_str(), | 204 name = base::StringPrintf("%s (%d)", name.c_str(), |
| 104 ++current_counts[name]); | 205 ++current_counts[name]); |
| 105 } | 206 } |
| 106 | 207 |
| 107 result_map->insert(std::pair<std::string, AlbumInfo>(name, info_list[i])); | 208 result_map->insert(std::pair<std::string, AlbumInfo>(name, info_list[i])); |
| 108 } | 209 } |
| 109 } | 210 } |
| 110 | 211 |
| 111 } // namespace picasa | 212 } // namespace picasa |
| OLD | NEW |