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 |
| 23 namespace { | |
| 24 | |
| 25 void CallAllReadyCallbacks( | |
|
vandebo (ex-Chrome)
2013/07/12 22:17:28
nit: It sounds like this checks if a callback is r
tommycli
2013/08/14 18:23:17
Done.
| |
| 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) { |
| 26 } | 40 } |
| 27 | 41 |
| 28 PicasaDataProvider::~PicasaDataProvider() {} | 42 PicasaDataProvider::~PicasaDataProvider() {} |
| 29 | 43 |
| 30 void PicasaDataProvider::RefreshData(const base::Closure& ready_callback) { | 44 void PicasaDataProvider::RefreshData( |
| 45 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_ != STALE_DATA_STATE) { |
| 41 AlbumTableFiles(database_path_), | 58 ready_callback.Run(true /* success */); |
| 42 base::Bind(&PicasaDataProvider::OnDataRefreshed, | 59 return; |
| 43 weak_factory_.GetWeakPtr(), | 60 } |
| 44 ready_callback)); | 61 album_list_ready_callbacks_.push(ready_callback); |
| 45 album_table_reader_->Start(); | 62 } else { |
| 63 if (state_ == ALBUMS_IMAGES_FRESH_STATE) { | |
| 64 ready_callback.Run(true /* success */); | |
| 65 return; | |
| 66 } | |
| 67 albums_indexer_ready_callbacks_.push(ready_callback); | |
| 68 } | |
| 69 DoRefreshIfNecessary(); | |
| 46 } | 70 } |
| 47 | 71 |
| 48 scoped_ptr<AlbumMap> PicasaDataProvider::GetFolders() { | 72 scoped_ptr<AlbumMap> PicasaDataProvider::GetFolders() { |
| 49 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | 73 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 74 DCHECK(state_ == ALBUM_LIST_FRESH_STATE || | |
| 75 state_ == ALBUMS_IMAGES_FRESH_STATE); | |
| 50 return make_scoped_ptr(new AlbumMap(folder_map_)); | 76 return make_scoped_ptr(new AlbumMap(folder_map_)); |
| 51 } | 77 } |
| 52 | 78 |
| 53 scoped_ptr<AlbumMap> PicasaDataProvider::GetAlbums() { | 79 scoped_ptr<AlbumMap> PicasaDataProvider::GetAlbums() { |
| 54 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | 80 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 81 DCHECK(state_ == ALBUM_LIST_FRESH_STATE || | |
| 82 state_ == ALBUMS_IMAGES_FRESH_STATE); | |
| 55 return make_scoped_ptr(new AlbumMap(album_map_)); | 83 return make_scoped_ptr(new AlbumMap(album_map_)); |
| 56 } | 84 } |
| 57 | 85 |
| 58 void PicasaDataProvider::OnDataRefreshed( | 86 scoped_ptr<AlbumImagesMap> PicasaDataProvider::GetAlbumsImages() { |
| 59 const base::Closure& ready_callback, | 87 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 88 DCHECK(state_ == ALBUMS_IMAGES_FRESH_STATE); | |
| 89 return make_scoped_ptr(new AlbumImagesMap(albums_images_)); | |
| 90 } | |
| 91 | |
| 92 void PicasaDataProvider::InvalidateData() { | |
| 93 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | |
| 94 DCHECK(!(album_table_reader_ && albums_indexer_)); | |
| 95 | |
| 96 // Set data state to stale and ignore responses from any in-flight processes. | |
| 97 // TODO(tommycli): Implement and call Cancel function for these | |
| 98 // UtilityProcessHostClients to actually kill the in-flight processes. | |
| 99 state_ = STALE_DATA_STATE; | |
| 100 album_table_reader_ = NULL; | |
| 101 albums_indexer_ = NULL; | |
| 102 | |
| 103 if (!album_list_ready_callbacks_.empty() || | |
| 104 !albums_indexer_ready_callbacks_.empty()) { | |
| 105 DoRefreshIfNecessary(); | |
| 106 } | |
| 107 } | |
| 108 | |
| 109 void PicasaDataProvider::DoRefreshIfNecessary() { | |
| 110 DCHECK(state_ != INVALID_DATA_STATE); | |
| 111 DCHECK(state_ != ALBUMS_IMAGES_FRESH_STATE); | |
| 112 DCHECK(!album_list_ready_callbacks_.empty() || | |
| 113 !albums_indexer_ready_callbacks_.empty()); | |
| 114 | |
| 115 if (state_ == STALE_DATA_STATE) { | |
| 116 if (album_table_reader_) | |
| 117 return; | |
| 118 album_table_reader_ = new SafePicasaAlbumTableReader( | |
| 119 AlbumTableFiles(database_path_), | |
| 120 base::Bind(&PicasaDataProvider::OnAlbumListRefreshed, | |
| 121 weak_factory_.GetWeakPtr())); | |
| 122 album_table_reader_->Start(); | |
| 123 } else { | |
| 124 if (albums_indexer_) | |
| 125 return; | |
| 126 albums_indexer_ = new SafePicasaAlbumsIndexer( | |
| 127 album_map_, | |
| 128 folder_map_, | |
| 129 base::Bind(&PicasaDataProvider::OnAlbumsIndexerDone, | |
| 130 weak_factory_.GetWeakPtr())); | |
| 131 albums_indexer_->Start(); | |
| 132 } | |
| 133 } | |
| 134 | |
| 135 void PicasaDataProvider::OnAlbumListRefreshed( | |
| 136 scoped_refptr<SafePicasaAlbumTableReader> reader, | |
| 60 bool parse_success, | 137 bool parse_success, |
| 61 const std::vector<AlbumInfo>& albums, | 138 const std::vector<AlbumInfo>& albums, |
| 62 const std::vector<AlbumInfo>& folders) { | 139 const std::vector<AlbumInfo>& folders) { |
| 63 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | 140 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
| 64 if (parse_success) { | 141 // If the reader has already been deemed stale, ignore the result. |
| 65 UniquifyNames(albums, &album_map_); | 142 if (reader != album_table_reader_) |
| 66 UniquifyNames(folders, &folder_map_); | 143 return; |
| 144 | |
|
vandebo (ex-Chrome)
2013/07/12 22:17:28
DCHECK state here.
tommycli
2013/08/14 18:23:17
Done.
| |
| 145 if (!parse_success) { | |
| 146 // If we didn't get the list successfully, fail all those waiting for | |
| 147 // the albums indexer also. | |
| 148 state_ = INVALID_DATA_STATE; | |
| 149 CallAllReadyCallbacks(&albums_indexer_ready_callbacks_, | |
| 150 false /* success */); | |
| 67 } | 151 } |
|
vandebo (ex-Chrome)
2013/07/12 22:17:28
You need a return in the if loop since we set stat
tommycli
2013/08/14 18:23:17
Done.
| |
| 68 ready_callback.Run(); | 152 |
| 153 album_map_.clear(); | |
| 154 folder_map_.clear(); | |
| 155 UniquifyNames(albums, &album_map_); | |
| 156 UniquifyNames(folders, &folder_map_); | |
| 157 | |
| 158 state_ = ALBUM_LIST_FRESH_STATE; | |
| 159 CallAllReadyCallbacks(&album_list_ready_callbacks_, parse_success); | |
| 160 | |
| 161 // Chain from this process onto refreshing the albums images if necessary. | |
| 162 if (!albums_indexer_ready_callbacks_.empty()) | |
| 163 DoRefreshIfNecessary(); | |
| 164 } | |
| 165 | |
| 166 void PicasaDataProvider::OnAlbumsIndexerDone( | |
| 167 scoped_refptr<SafePicasaAlbumsIndexer> indexer, | |
| 168 bool success, | |
| 169 const picasa::AlbumImagesMap& albums_images) { | |
| 170 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | |
| 171 // If the indexer has already been deemed stale, ignore the result. | |
| 172 if (indexer != albums_indexer_) | |
| 173 return; | |
| 174 | |
|
vandebo (ex-Chrome)
2013/07/12 22:17:28
DCHECK state here.
tommycli
2013/08/14 18:23:17
Done.
| |
| 175 if (success) { | |
| 176 state_ = ALBUMS_IMAGES_FRESH_STATE; | |
| 177 | |
| 178 albums_images_ = albums_images; | |
| 179 } | |
| 180 | |
| 181 CallAllReadyCallbacks(&albums_indexer_ready_callbacks_, success); | |
| 69 } | 182 } |
| 70 | 183 |
| 71 // static | 184 // static |
| 72 std::string PicasaDataProvider::DateToPathString(const base::Time& time) { | 185 std::string PicasaDataProvider::DateToPathString(const base::Time& time) { |
| 73 base::Time::Exploded exploded_time; | 186 base::Time::Exploded exploded_time; |
| 74 time.LocalExplode(&exploded_time); | 187 time.LocalExplode(&exploded_time); |
| 75 | 188 |
| 76 // TODO(tommycli): Investigate better localization and persisting which locale | 189 // TODO(tommycli): Investigate better localization and persisting which locale |
| 77 // we use to generate these unique names. | 190 // we use to generate these unique names. |
| 78 return base::StringPrintf("%04d-%02d-%02d", exploded_time.year, | 191 return base::StringPrintf("%04d-%02d-%02d", exploded_time.year, |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 102 if (total_counts[name] != 1) { | 215 if (total_counts[name] != 1) { |
| 103 name = base::StringPrintf("%s (%d)", name.c_str(), | 216 name = base::StringPrintf("%s (%d)", name.c_str(), |
| 104 ++current_counts[name]); | 217 ++current_counts[name]); |
| 105 } | 218 } |
| 106 | 219 |
| 107 result_map->insert(std::pair<std::string, AlbumInfo>(name, info_list[i])); | 220 result_map->insert(std::pair<std::string, AlbumInfo>(name, info_list[i])); |
| 108 } | 221 } |
| 109 } | 222 } |
| 110 | 223 |
| 111 } // namespace picasa | 224 } // namespace picasa |
| OLD | NEW |