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 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 |
35 ready_callback.Run(); | 38 switch (needed_data) { |
36 return; | 39 case ALBUM_LIST_DATA: |
40 EnsureAlbumListRefreshed(ready_callback); | |
41 break; | |
42 case ALBUMS_IMAGES_DATA: | |
43 EnsureAlbumsImagesRefreshed(ready_callback); | |
44 break; | |
45 default: | |
46 NOTREACHED(); | |
37 } | 47 } |
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 } | 48 } |
47 | 49 |
48 scoped_ptr<AlbumMap> PicasaDataProvider::GetFolders() { | 50 scoped_ptr<AlbumMap> PicasaDataProvider::GetFolders() { |
49 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | 51 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
50 return make_scoped_ptr(new AlbumMap(folder_map_)); | 52 return make_scoped_ptr(new AlbumMap(folder_map_)); |
51 } | 53 } |
52 | 54 |
53 scoped_ptr<AlbumMap> PicasaDataProvider::GetAlbums() { | 55 scoped_ptr<AlbumMap> PicasaDataProvider::GetAlbums() { |
54 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | 56 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
55 return make_scoped_ptr(new AlbumMap(album_map_)); | 57 return make_scoped_ptr(new AlbumMap(album_map_)); |
56 } | 58 } |
57 | 59 |
58 void PicasaDataProvider::OnDataRefreshed( | 60 scoped_ptr<AlbumImagesMap> PicasaDataProvider::GetAlbumsImages() { |
59 const base::Closure& ready_callback, | 61 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
62 return make_scoped_ptr(new AlbumImagesMap(albums_images_)); | |
63 } | |
64 | |
65 void PicasaDataProvider::InvalidateData() { | |
66 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | |
67 DCHECK(!(album_table_reader_ && albums_indexer_)); | |
68 | |
69 // Set data state to stale and ignore responses from any in-flight processes. | |
70 // TODO(tommycli): Implement and call Cancel function for these | |
71 // UtilityProcessHostClients to actually kill the in-flight processes. | |
72 data_state_ = STALE_DATA_STATE; | |
73 album_table_reader_ = NULL; | |
74 albums_indexer_ = NULL; | |
75 | |
76 if (!albums_indexer_ready_callbacks_.empty()) { | |
77 StartAlbumListRefreshIfNecessary(); | |
78 album_list_ready_callbacks_.push( | |
79 base::Bind(&PicasaDataProvider::StartAlbumsImagesRefreshIfNecessary, | |
vandebo (ex-Chrome)
2013/07/11 23:22:37
You could end up with multiple copies of this call
tommycli
2013/07/12 01:37:48
Done.
| |
80 weak_factory_.GetWeakPtr())); | |
81 return; | |
82 } | |
83 | |
84 if (!album_list_ready_callbacks_.empty()) { | |
85 StartAlbumListRefreshIfNecessary(); | |
86 return; | |
87 } | |
88 } | |
89 | |
90 void PicasaDataProvider::EnsureAlbumListRefreshed( | |
91 const base::Closure& ready_callback) { | |
92 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | |
93 switch (data_state_) { | |
94 case STALE_DATA_STATE: | |
95 StartAlbumListRefreshIfNecessary(); | |
96 album_list_ready_callbacks_.push(ready_callback); | |
97 break; | |
98 case ALBUM_LIST_FRESH_STATE: | |
99 case ALBUMS_IMAGES_FRESH_STATE: | |
100 ready_callback.Run(); | |
101 break; | |
102 default: | |
103 NOTREACHED(); | |
104 } | |
105 } | |
106 | |
107 void PicasaDataProvider::EnsureAlbumsImagesRefreshed( | |
108 const base::Closure& ready_callback) { | |
109 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | |
110 switch (data_state_) { | |
111 case STALE_DATA_STATE: | |
112 StartAlbumListRefreshIfNecessary(); | |
113 album_list_ready_callbacks_.push( | |
114 base::Bind(&PicasaDataProvider::StartAlbumsImagesRefreshIfNecessary, | |
115 weak_factory_.GetWeakPtr())); | |
116 albums_indexer_ready_callbacks_.push(ready_callback); | |
117 break; | |
118 case ALBUM_LIST_FRESH_STATE: | |
119 StartAlbumsImagesRefreshIfNecessary(); | |
120 albums_indexer_ready_callbacks_.push(ready_callback); | |
121 break; | |
122 case ALBUMS_IMAGES_FRESH_STATE: | |
123 ready_callback.Run(); | |
124 break; | |
125 default: | |
126 NOTREACHED(); | |
127 } | |
128 } | |
129 | |
130 void PicasaDataProvider::StartAlbumListRefreshIfNecessary() { | |
131 if (!album_table_reader_) | |
132 return; | |
133 album_table_reader_ = new SafePicasaAlbumTableReader( | |
134 AlbumTableFiles(database_path_), | |
135 base::Bind(&PicasaDataProvider::OnAlbumListRefreshed, | |
136 weak_factory_.GetWeakPtr())); | |
137 album_table_reader_->Start(); | |
138 } | |
139 | |
140 void PicasaDataProvider::StartAlbumsImagesRefreshIfNecessary() { | |
141 if (!albums_indexer_) | |
142 return; | |
143 albums_indexer_ = new SafePicasaAlbumsIndexer( | |
144 album_map_, | |
145 folder_map_, | |
146 base::Bind(&PicasaDataProvider::OnAlbumsIndexerDone, | |
147 weak_factory_.GetWeakPtr())); | |
148 albums_indexer_->Start(); | |
149 } | |
150 | |
151 void PicasaDataProvider::OnAlbumListRefreshed( | |
152 scoped_refptr<SafePicasaAlbumTableReader> reader, | |
60 bool parse_success, | 153 bool parse_success, |
61 const std::vector<AlbumInfo>& albums, | 154 const std::vector<AlbumInfo>& albums, |
62 const std::vector<AlbumInfo>& folders) { | 155 const std::vector<AlbumInfo>& folders) { |
63 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | 156 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); |
157 // If the reader has already been deemed stale, ignore the result. | |
158 if (reader != album_table_reader_) | |
159 return; | |
160 | |
64 if (parse_success) { | 161 if (parse_success) { |
162 album_map_.clear(); | |
163 folder_map_.clear(); | |
65 UniquifyNames(albums, &album_map_); | 164 UniquifyNames(albums, &album_map_); |
66 UniquifyNames(folders, &folder_map_); | 165 UniquifyNames(folders, &folder_map_); |
67 } | 166 } |
68 ready_callback.Run(); | 167 |
168 data_state_ = ALBUM_LIST_FRESH_STATE; | |
169 | |
170 while (!album_list_ready_callbacks_.empty()) { | |
171 album_list_ready_callbacks_.front().Run(); | |
172 album_list_ready_callbacks_.pop(); | |
173 } | |
174 } | |
175 | |
176 void PicasaDataProvider::OnAlbumsIndexerDone( | |
177 scoped_refptr<SafePicasaAlbumsIndexer> indexer, | |
178 const picasa::AlbumImagesMap& albums_images) { | |
179 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | |
180 // If the indexer has already been deemed stale, ignore the result. | |
181 if (indexer != albums_indexer_) | |
182 return; | |
183 | |
184 albums_images_ = albums_images; | |
185 | |
186 data_state_ = ALBUMS_IMAGES_FRESH_STATE; | |
187 | |
188 while (!albums_indexer_ready_callbacks_.empty()) { | |
189 albums_indexer_ready_callbacks_.front().Run(); | |
190 albums_indexer_ready_callbacks_.pop(); | |
191 } | |
69 } | 192 } |
70 | 193 |
71 // static | 194 // static |
72 std::string PicasaDataProvider::DateToPathString(const base::Time& time) { | 195 std::string PicasaDataProvider::DateToPathString(const base::Time& time) { |
73 base::Time::Exploded exploded_time; | 196 base::Time::Exploded exploded_time; |
74 time.LocalExplode(&exploded_time); | 197 time.LocalExplode(&exploded_time); |
75 | 198 |
76 // TODO(tommycli): Investigate better localization and persisting which locale | 199 // TODO(tommycli): Investigate better localization and persisting which locale |
77 // we use to generate these unique names. | 200 // we use to generate these unique names. |
78 return base::StringPrintf("%04d-%02d-%02d", exploded_time.year, | 201 return base::StringPrintf("%04d-%02d-%02d", exploded_time.year, |
(...skipping 23 matching lines...) Expand all Loading... | |
102 if (total_counts[name] != 1) { | 225 if (total_counts[name] != 1) { |
103 name = base::StringPrintf("%s (%d)", name.c_str(), | 226 name = base::StringPrintf("%s (%d)", name.c_str(), |
104 ++current_counts[name]); | 227 ++current_counts[name]); |
105 } | 228 } |
106 | 229 |
107 result_map->insert(std::pair<std::string, AlbumInfo>(name, info_list[i])); | 230 result_map->insert(std::pair<std::string, AlbumInfo>(name, info_list[i])); |
108 } | 231 } |
109 } | 232 } |
110 | 233 |
111 } // namespace picasa | 234 } // namespace picasa |
OLD | NEW |