Index: chrome/browser/media_galleries/fileapi/picasa/picasa_data_provider.cc |
diff --git a/chrome/browser/media_galleries/fileapi/picasa/picasa_data_provider.cc b/chrome/browser/media_galleries/fileapi/picasa/picasa_data_provider.cc |
index a2f503cc35634af7addaa668f2a9d6032bd1705f..dbf2d467256505ae0931e448afac7d8a2ae59844 100644 |
--- a/chrome/browser/media_galleries/fileapi/picasa/picasa_data_provider.cc |
+++ b/chrome/browser/media_galleries/fileapi/picasa/picasa_data_provider.cc |
@@ -7,10 +7,11 @@ |
#include <utility> |
#include "base/basictypes.h" |
-#include "base/callback.h" |
+#include "base/bind_helpers.h" |
#include "base/strings/stringprintf.h" |
#include "chrome/browser/media_galleries/fileapi/media_file_system_backend.h" |
#include "chrome/browser/media_galleries/fileapi/safe_picasa_album_table_reader.h" |
+#include "chrome/browser/media_galleries/fileapi/safe_picasa_albums_indexer.h" |
#include "chrome/browser/media_galleries/imported_media_gallery_registry.h" |
#include "webkit/browser/fileapi/file_system_operation_context.h" |
#include "webkit/browser/fileapi/file_system_url.h" |
@@ -21,28 +22,40 @@ namespace picasa { |
PicasaDataProvider::PicasaDataProvider(const base::FilePath& database_path) |
: database_path_(database_path), |
- needs_refresh_(true), |
+ data_state_(NO_FRESH_DATA_STATE), |
weak_factory_(this) { |
} |
PicasaDataProvider::~PicasaDataProvider() {} |
-void PicasaDataProvider::RefreshData(const base::Closure& ready_callback) { |
+void PicasaDataProvider::RefreshData( |
+ 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.
|
+ const base::Closure& ready_callback) { |
DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
// TODO(tommycli): Need to watch the database_path_ folder and handle |
// rereading the data when it changes. |
- if (!needs_refresh_) { |
- ready_callback.Run(); |
+ |
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.
|
+ if (need_albums_images) |
+ EnsureAlbumListRefreshed(ready_callback); |
+ else |
+ EnsureAlbumsImagesRefreshed(ready_callback); |
+} |
+ |
+void PicasaDataProvider::InvalidateData() { |
+ DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
+ DCHECK(!(album_table_reader_ && albums_indexer_)); |
+ |
+ 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.
|
+ album_table_reader_ = NULL; |
+ 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.
|
return; |
} |
- needs_refresh_ = false; |
- album_table_reader_ = new SafePicasaAlbumTableReader( |
- AlbumTableFiles(database_path_), |
- base::Bind(&PicasaDataProvider::OnDataRefreshed, |
- weak_factory_.GetWeakPtr(), |
- ready_callback)); |
- album_table_reader_->Start(); |
+ if (albums_indexer_) { |
+ albums_indexer_ = NULL; |
+ EnsureAlbumsImagesRefreshed(base::Bind(&base::DoNothing)); |
+ return; |
+ } |
} |
scoped_ptr<AlbumMap> PicasaDataProvider::GetFolders() { |
@@ -55,17 +68,105 @@ scoped_ptr<AlbumMap> PicasaDataProvider::GetAlbums() { |
return make_scoped_ptr(new AlbumMap(album_map_)); |
} |
-void PicasaDataProvider::OnDataRefreshed( |
- const base::Closure& ready_callback, |
+scoped_ptr<AlbumImagesMap> PicasaDataProvider::GetAlbumsImages() { |
+ DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
+ return make_scoped_ptr(new AlbumImagesMap(albums_images_)); |
+} |
+ |
+void PicasaDataProvider::EnsureAlbumListRefreshed( |
+ const base::Closure& ready_callback) { |
+ DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
+ switch (data_state_) { |
+ case NO_FRESH_DATA_STATE: |
+ if (!album_table_reader_) { |
+ album_table_reader_ = new SafePicasaAlbumTableReader( |
+ AlbumTableFiles(database_path_), |
+ base::Bind(&PicasaDataProvider::OnAlbumListRefreshed, |
+ weak_factory_.GetWeakPtr())); |
+ album_table_reader_->Start(); |
+ } |
+ album_list_ready_callbacks_.push(ready_callback); |
+ break; |
+ case ALBUM_LIST_FRESH_STATE: |
+ case ALBUMS_IMAGES_FRESH_STATE: |
+ ready_callback.Run(); |
+ break; |
+ default: |
+ NOTREACHED(); |
+ } |
+} |
+ |
+void PicasaDataProvider::EnsureAlbumsImagesRefreshed( |
+ const base::Closure& ready_callback) { |
+ DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
+ switch (data_state_) { |
+ case NO_FRESH_DATA_STATE: |
+ // Album list not ready. Refresh that first, then call this again. |
+ EnsureAlbumListRefreshed( |
+ base::Bind(&PicasaDataProvider::EnsureAlbumsImagesRefreshed, |
+ weak_factory_.GetWeakPtr(), |
+ ready_callback)); |
+ break; |
+ case ALBUM_LIST_FRESH_STATE: |
+ if (!albums_indexer_) { |
+ albums_indexer_ = new SafePicasaAlbumsIndexer( |
+ album_map_, |
+ folder_map_, |
+ base::Bind(&PicasaDataProvider::OnAlbumsIndexerDone, |
+ weak_factory_.GetWeakPtr())); |
+ albums_indexer_->Start(); |
+ } |
+ albums_indexer_ready_callbacks_.push(ready_callback); |
+ break; |
+ case ALBUMS_IMAGES_FRESH_STATE: |
+ ready_callback.Run(); |
+ break; |
+ default: |
+ NOTREACHED(); |
+ } |
+} |
+ |
+void PicasaDataProvider::OnAlbumListRefreshed( |
+ scoped_refptr<SafePicasaAlbumTableReader> reader, |
bool parse_success, |
const std::vector<AlbumInfo>& albums, |
const std::vector<AlbumInfo>& folders) { |
DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
+ // If the reader has already been deemed stale, ignore the result. |
+ if (reader != album_table_reader_) |
+ return; |
+ |
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.
|
+ album_map_.clear(); |
+ folder_map_.clear(); |
UniquifyNames(albums, &album_map_); |
UniquifyNames(folders, &folder_map_); |
} |
- ready_callback.Run(); |
+ |
+ data_state_ = ALBUM_LIST_FRESH_STATE; |
+ |
+ while (!album_list_ready_callbacks_.empty()) { |
+ album_list_ready_callbacks_.front().Run(); |
+ album_list_ready_callbacks_.pop(); |
+ } |
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.
|
+} |
+ |
+void PicasaDataProvider::OnAlbumsIndexerDone( |
+ 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
|
+ const picasa::AlbumImagesMap& albums_images) { |
+ DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
+ // If the indexer has already been deemed stale, ignore the result. |
+ if (indexer != albums_indexer_) |
+ return; |
+ |
+ albums_images_ = albums_images; |
+ |
+ data_state_ = ALBUMS_IMAGES_FRESH_STATE; |
+ |
+ while (!albums_indexer_ready_callbacks_.empty()) { |
+ albums_indexer_ready_callbacks_.front().Run(); |
+ albums_indexer_ready_callbacks_.pop(); |
+ } |
} |
// static |