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 #ifndef CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_PICASA_PICASA_DATA_PROVIDER_H_ | 5 #ifndef CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_PICASA_PICASA_DATA_PROVIDER_H_ |
6 #define CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_PICASA_PICASA_DATA_PROVIDER_H_ | 6 #define CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_PICASA_PICASA_DATA_PROVIDER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
| 9 #include <queue> |
9 #include <string> | 10 #include <string> |
10 #include <vector> | 11 #include <vector> |
11 | 12 |
12 #include "base/callback_forward.h" | 13 #include "base/callback.h" |
13 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
14 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
15 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
16 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
17 #include "base/time/time.h" | 18 #include "base/time/time.h" |
18 #include "chrome/common/media_galleries/picasa_types.h" | 19 #include "chrome/common/media_galleries/picasa_types.h" |
19 | 20 |
20 namespace picasa { | 21 namespace picasa { |
21 | 22 |
22 class SafePicasaAlbumTableReader; | 23 class SafePicasaAlbumTableReader; |
| 24 class SafePicasaAlbumsIndexer; |
23 | 25 |
24 // Created and owned by ImportedMediaGalleryRegistryTaskRunnerValues | 26 // Created and owned by ImportedMediaGalleryRegistryTaskRunnerValues |
25 class PicasaDataProvider { | 27 class PicasaDataProvider { |
26 public: | 28 public: |
| 29 typedef base::Callback<void(bool)> ReadyCallback; |
| 30 |
| 31 enum DataType { |
| 32 LIST_OF_ALBUMS_AND_FOLDERS_DATA, |
| 33 ALBUMS_IMAGES_DATA |
| 34 }; |
| 35 |
27 explicit PicasaDataProvider(const base::FilePath& database_path); | 36 explicit PicasaDataProvider(const base::FilePath& database_path); |
28 virtual ~PicasaDataProvider(); | 37 virtual ~PicasaDataProvider(); |
29 | 38 |
30 // Ask the data provider to refresh the data if necessary. |ready_callback| | 39 // Ask the data provider to refresh the data if necessary. |ready_callback| |
31 // will be called when the data is up to date | 40 // will be called when the data is up to date. |
32 // TODO(tommycli): Investigate having the callback return a bool indicating | 41 // TODO(tommycli): Investigate having the callback return a bool indicating |
33 // success or failure - and handling it intelligently in PicasaFileUtil. | 42 // success or failure - and handling it intelligently in PicasaFileUtil. |
34 virtual void RefreshData(const base::Closure& ready_callback); | 43 void RefreshData(DataType needed_data, const ReadyCallback& ready_callback); |
35 | 44 |
| 45 // These methods return scoped_ptrs because we want to return a copy that |
| 46 // will not change to the caller. |
36 scoped_ptr<AlbumMap> GetAlbums(); | 47 scoped_ptr<AlbumMap> GetAlbums(); |
37 scoped_ptr<AlbumMap> GetFolders(); | 48 scoped_ptr<AlbumMap> GetFolders(); |
38 // TODO(tommycli): Implement album contents. GetAlbumContents(...) | 49 // |error| must be non-NULL. |
39 | 50 scoped_ptr<AlbumImages> FindAlbumImages(const std::string& key, |
40 protected: | 51 base::PlatformFileError* error); |
41 // Protected for test class usage. | |
42 void OnDataRefreshed(const base::Closure& ready_callback, | |
43 bool parse_success, const std::vector<AlbumInfo>& albums, | |
44 const std::vector<AlbumInfo>& folder); | |
45 | 52 |
46 private: | 53 private: |
| 54 enum State { |
| 55 STALE_DATA_STATE, |
| 56 INVALID_DATA_STATE, |
| 57 LIST_OF_ALBUMS_AND_FOLDERS_FRESH_STATE, |
| 58 ALBUMS_IMAGES_FRESH_STATE |
| 59 }; |
| 60 |
47 friend class PicasaFileUtilTest; | 61 friend class PicasaFileUtilTest; |
48 friend class TestPicasaDataProvider; | 62 friend class PicasaDataProviderInvalidateSimpleTest; |
| 63 friend class PicasaDataProviderInvalidateInflightTableReaderTest; |
| 64 friend class PicasaDataProviderInvalidateInflightAlbumsIndexerTest; |
| 65 |
| 66 // Notifies data provider that any currently cached data is stale. |
| 67 void InvalidateData(); |
| 68 |
| 69 // Kicks off utility processes needed to fulfill any pending callbacks. |
| 70 void DoRefreshIfNecessary(); |
| 71 |
| 72 void OnAlbumTableReaderDone(scoped_refptr<SafePicasaAlbumTableReader> reader, |
| 73 bool parse_success, |
| 74 const std::vector<AlbumInfo>& albums, |
| 75 const std::vector<AlbumInfo>& folder); |
| 76 |
| 77 void OnAlbumsIndexerDone(scoped_refptr<SafePicasaAlbumsIndexer> indexer, |
| 78 bool success, |
| 79 const picasa::AlbumImagesMap& albums_images); |
49 | 80 |
50 static std::string DateToPathString(const base::Time& time); | 81 static std::string DateToPathString(const base::Time& time); |
51 static void UniquifyNames(const std::vector<AlbumInfo>& info_list, | 82 static void UniquifyNames(const std::vector<AlbumInfo>& info_list, |
52 AlbumMap* result_map); | 83 AlbumMap* result_map); |
53 | 84 |
| 85 // Methods are used in the browser test to tweak internal data for testing. |
| 86 void SetDatabasePathForTesting(const base::FilePath& database_path); |
| 87 void SetAlbumMapsForTesting(const AlbumMap& album_map, |
| 88 const AlbumMap& folder_map); |
| 89 |
54 AlbumMap album_map_; | 90 AlbumMap album_map_; |
55 AlbumMap folder_map_; | 91 AlbumMap folder_map_; |
| 92 AlbumImagesMap albums_images_; |
56 | 93 |
57 base::FilePath database_path_; | 94 base::FilePath database_path_; |
58 bool needs_refresh_; | |
59 | 95 |
| 96 State state_; |
| 97 |
| 98 // Callbacks that are waiting for their requested data to be ready. |
| 99 std::queue<ReadyCallback> album_list_ready_callbacks_; |
| 100 std::queue<ReadyCallback> albums_index_ready_callbacks_; |
| 101 |
| 102 // Stores the "live" in-flight utility processes. Callbacks from other |
| 103 // (older) utility processes are stale and ignored. Only one of these at a |
| 104 // time should be non-NULL. |
60 scoped_refptr<SafePicasaAlbumTableReader> album_table_reader_; | 105 scoped_refptr<SafePicasaAlbumTableReader> album_table_reader_; |
| 106 scoped_refptr<SafePicasaAlbumsIndexer> albums_indexer_; |
61 | 107 |
62 base::WeakPtrFactory<PicasaDataProvider> weak_factory_; | 108 base::WeakPtrFactory<PicasaDataProvider> weak_factory_; |
63 | 109 |
64 DISALLOW_COPY_AND_ASSIGN(PicasaDataProvider); | 110 DISALLOW_COPY_AND_ASSIGN(PicasaDataProvider); |
65 }; | 111 }; |
66 | 112 |
67 } // namespace picasa | 113 } // namespace picasa |
68 | 114 |
69 #endif // CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_PICASA_PICASA_DATA_PROVIDER_H_ | 115 #endif // CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_PICASA_PICASA_DATA_PROVIDER_H_ |
OLD | NEW |