Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(77)

Side by Side Diff: chrome/browser/media_galleries/fileapi/picasa/picasa_data_provider.cc

Issue 23717027: Media Galleries API Picasa: Move Picasa source files to correct location. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/media_galleries/fileapi/picasa/picasa_data_provider.h"
6
7 #include <utility>
8
9 #include "base/basictypes.h"
10 #include "base/bind_helpers.h"
11 #include "base/callback.h"
12 #include "base/file_util.h"
13 #include "base/strings/stringprintf.h"
14 #include "chrome/browser/media_galleries/fileapi/file_path_watcher_util.h"
15 #include "chrome/browser/media_galleries/fileapi/media_file_system_backend.h"
16 #include "chrome/browser/media_galleries/fileapi/safe_picasa_album_table_reader. h"
17 #include "chrome/browser/media_galleries/fileapi/safe_picasa_albums_indexer.h"
18 #include "chrome/browser/media_galleries/imported_media_gallery_registry.h"
19 #include "webkit/browser/fileapi/file_system_operation_context.h"
20 #include "webkit/browser/fileapi/file_system_url.h"
21
22 using chrome::MediaFileSystemBackend;
23
24 namespace picasa {
25
26 namespace {
27
28 void RunAllCallbacks(
29 std::queue<PicasaDataProvider::ReadyCallback>* ready_callbacks_queue,
30 bool success) {
31 while (!ready_callbacks_queue->empty()) {
32 ready_callbacks_queue->front().Run(success);
33 ready_callbacks_queue->pop();
34 }
35 }
36
37 } // namespace
38
39 PicasaDataProvider::PicasaDataProvider(const base::FilePath& database_path)
40 : database_path_(database_path),
41 state_(STALE_DATA_STATE),
42 weak_factory_(this) {
43 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
44
45 chrome::StartFilePathWatchOnMediaTaskRunner(
46 database_path_.DirName().AppendASCII(kPicasaTempDirName),
47 base::Bind(&PicasaDataProvider::OnTempDirWatchStarted,
48 weak_factory_.GetWeakPtr()),
49 base::Bind(&PicasaDataProvider::OnTempDirChanged,
50 weak_factory_.GetWeakPtr()));
51 }
52
53 PicasaDataProvider::~PicasaDataProvider() {}
54
55 void PicasaDataProvider::RefreshData(DataType needed_data,
56 const ReadyCallback& ready_callback) {
57 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
58 // TODO(tommycli): Need to watch the database_path_ folder and handle
59 // rereading the data when it changes.
60
61 if (state_ == INVALID_DATA_STATE) {
62 ready_callback.Run(false /* success */);
63 return;
64 }
65
66 if (needed_data == LIST_OF_ALBUMS_AND_FOLDERS_DATA) {
67 if (state_ == LIST_OF_ALBUMS_AND_FOLDERS_FRESH_STATE ||
68 state_ == ALBUMS_IMAGES_FRESH_STATE) {
69 ready_callback.Run(true /* success */);
70 return;
71 }
72 album_list_ready_callbacks_.push(ready_callback);
73 } else {
74 if (state_ == ALBUMS_IMAGES_FRESH_STATE) {
75 ready_callback.Run(true /* success */);
76 return;
77 }
78 albums_index_ready_callbacks_.push(ready_callback);
79 }
80 DoRefreshIfNecessary();
81 }
82
83 scoped_ptr<AlbumMap> PicasaDataProvider::GetFolders() {
84 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
85 DCHECK(state_ == LIST_OF_ALBUMS_AND_FOLDERS_FRESH_STATE ||
86 state_ == ALBUMS_IMAGES_FRESH_STATE);
87 return make_scoped_ptr(new AlbumMap(folder_map_));
88 }
89
90 scoped_ptr<AlbumMap> PicasaDataProvider::GetAlbums() {
91 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
92 DCHECK(state_ == LIST_OF_ALBUMS_AND_FOLDERS_FRESH_STATE ||
93 state_ == ALBUMS_IMAGES_FRESH_STATE);
94 return make_scoped_ptr(new AlbumMap(album_map_));
95 }
96
97 scoped_ptr<AlbumImages> PicasaDataProvider::FindAlbumImages(
98 const std::string& key,
99 base::PlatformFileError* error) {
100 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
101 DCHECK(state_ == ALBUMS_IMAGES_FRESH_STATE);
102 DCHECK(error);
103
104 AlbumImagesMap::const_iterator it = albums_images_.find(key);
105
106 if (it == albums_images_.end()) {
107 *error = base::PLATFORM_FILE_ERROR_NOT_FOUND;
108 return scoped_ptr<AlbumImages>();
109 }
110
111 *error = base::PLATFORM_FILE_OK;
112 return make_scoped_ptr(new AlbumImages(it->second));
113 }
114
115 void PicasaDataProvider::InvalidateData() {
116 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
117
118 // Set data state to stale and ignore responses from any in-flight processes.
119 // TODO(tommycli): Implement and call Cancel function for these
120 // UtilityProcessHostClients to actually kill the in-flight processes.
121 state_ = STALE_DATA_STATE;
122 album_table_reader_ = NULL;
123 albums_indexer_ = NULL;
124
125 DoRefreshIfNecessary();
126 }
127
128 void PicasaDataProvider::OnTempDirWatchStarted(
129 scoped_ptr<base::FilePathWatcher> temp_dir_watcher) {
130 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
131 temp_dir_watcher_.reset(temp_dir_watcher.release());
132 }
133
134 void PicasaDataProvider::OnTempDirChanged(const base::FilePath& temp_dir_path,
135 bool error) {
136 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
137 if (file_util::IsDirectoryEmpty(temp_dir_path))
138 InvalidateData();
139 }
140
141 void PicasaDataProvider::DoRefreshIfNecessary() {
142 DCHECK(state_ != INVALID_DATA_STATE);
143 DCHECK(state_ != ALBUMS_IMAGES_FRESH_STATE);
144 DCHECK(!(album_table_reader_ && albums_indexer_));
145
146 if (album_list_ready_callbacks_.empty() &&
147 albums_index_ready_callbacks_.empty()) {
148 return;
149 }
150
151 if (state_ == STALE_DATA_STATE) {
152 if (album_table_reader_)
153 return;
154 album_table_reader_ =
155 new SafePicasaAlbumTableReader(AlbumTableFiles(database_path_));
156 album_table_reader_->Start(
157 base::Bind(&PicasaDataProvider::OnAlbumTableReaderDone,
158 weak_factory_.GetWeakPtr(),
159 album_table_reader_));
160 } else {
161 DCHECK(state_ == LIST_OF_ALBUMS_AND_FOLDERS_FRESH_STATE);
162 if (albums_indexer_)
163 return;
164 albums_indexer_ = new SafePicasaAlbumsIndexer(album_map_, folder_map_);
165 albums_indexer_->Start(base::Bind(&PicasaDataProvider::OnAlbumsIndexerDone,
166 weak_factory_.GetWeakPtr(),
167 albums_indexer_));
168 }
169 }
170
171 void PicasaDataProvider::OnAlbumTableReaderDone(
172 scoped_refptr<SafePicasaAlbumTableReader> reader,
173 bool parse_success,
174 const std::vector<AlbumInfo>& albums,
175 const std::vector<AlbumInfo>& folders) {
176 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
177 // If the reader has already been deemed stale, ignore the result.
178 if (reader != album_table_reader_)
179 return;
180 album_table_reader_ = NULL;
181
182 DCHECK(state_ == STALE_DATA_STATE);
183
184 if (!parse_success) {
185 // If we didn't get the list successfully, fail all those waiting for
186 // the albums indexer also.
187 state_ = INVALID_DATA_STATE;
188 RunAllCallbacks(&album_list_ready_callbacks_, false /* success */);
189 RunAllCallbacks(&albums_index_ready_callbacks_, false /* success */);
190 return;
191 }
192
193 album_map_.clear();
194 folder_map_.clear();
195 UniquifyNames(albums, &album_map_);
196 UniquifyNames(folders, &folder_map_);
197
198 state_ = LIST_OF_ALBUMS_AND_FOLDERS_FRESH_STATE;
199 RunAllCallbacks(&album_list_ready_callbacks_, parse_success);
200
201 // Chain from this process onto refreshing the albums images if necessary.
202 DoRefreshIfNecessary();
203 }
204
205 void PicasaDataProvider::OnAlbumsIndexerDone(
206 scoped_refptr<SafePicasaAlbumsIndexer> indexer,
207 bool success,
208 const picasa::AlbumImagesMap& albums_images) {
209 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
210 // If the indexer has already been deemed stale, ignore the result.
211 if (indexer != albums_indexer_)
212 return;
213 albums_indexer_ = NULL;
214
215 DCHECK(state_ == LIST_OF_ALBUMS_AND_FOLDERS_FRESH_STATE);
216
217 if (success) {
218 state_ = ALBUMS_IMAGES_FRESH_STATE;
219
220 albums_images_ = albums_images;
221 }
222
223 RunAllCallbacks(&albums_index_ready_callbacks_, success);
224 }
225
226 // static
227 std::string PicasaDataProvider::DateToPathString(const base::Time& time) {
228 base::Time::Exploded exploded_time;
229 time.LocalExplode(&exploded_time);
230
231 // TODO(tommycli): Investigate better localization and persisting which locale
232 // we use to generate these unique names.
233 return base::StringPrintf("%04d-%02d-%02d", exploded_time.year,
234 exploded_time.month, exploded_time.day_of_month);
235 }
236
237 // static
238 void PicasaDataProvider::UniquifyNames(const std::vector<AlbumInfo>& info_list,
239 AlbumMap* result_map) {
240 // TODO(tommycli): We should persist the uniquified names.
241 std::vector<std::string> desired_names;
242
243 std::map<std::string, int> total_counts;
244 std::map<std::string, int> current_counts;
245
246 for (std::vector<AlbumInfo>::const_iterator it = info_list.begin();
247 it != info_list.end(); ++it) {
248 std::string desired_name =
249 it->name + " " + DateToPathString(it->timestamp);
250 desired_names.push_back(desired_name);
251 ++total_counts[desired_name];
252 }
253
254 for (unsigned int i = 0; i < info_list.size(); i++) {
255 std::string name = desired_names[i];
256
257 if (total_counts[name] != 1) {
258 name = base::StringPrintf("%s (%d)", name.c_str(),
259 ++current_counts[name]);
260 }
261
262 result_map->insert(std::pair<std::string, AlbumInfo>(name, info_list[i]));
263 }
264 }
265
266 } // namespace picasa
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698