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

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

Issue 18986012: Media Galleries API Picasa: Make PicasaDataProvider handle async PMP and INI parsing robustly. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@0039-picasa-import-sandbox-ini-parsing
Patch Set: Nevermind. Can't be done. Created 7 years, 4 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
OLDNEW
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 RunAllCallbacks(
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) {
40 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
26 } 41 }
27 42
28 PicasaDataProvider::~PicasaDataProvider() {} 43 PicasaDataProvider::~PicasaDataProvider() {}
29 44
30 void PicasaDataProvider::RefreshData(const base::Closure& ready_callback) { 45 void PicasaDataProvider::RefreshData(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_ == LIST_OF_ALBUMS_AND_FOLDERS_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_ == LIST_OF_ALBUMS_AND_FOLDERS_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();
vandebo (ex-Chrome) 2013/08/14 23:48:11 This is already "IfNecessary", so why not check th
tommycli 2013/08/15 22:52:36 Done.
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_)
vandebo (ex-Chrome) 2013/08/14 23:48:11 DCHECK state_
tommycli 2013/08/15 22:52:36 Done.
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
145 DCHECK(state_ == STALE_DATA_STATE);
146
147 if (!parse_success) {
148 // If we didn't get the list successfully, fail all those waiting for
149 // the albums indexer also.
150 state_ = INVALID_DATA_STATE;
151 RunAllCallbacks(&album_list_ready_callbacks_, false /* success */);
152 RunAllCallbacks(&albums_indexer_ready_callbacks_, false /* success */);
153 return;
67 } 154 }
68 ready_callback.Run(); 155
156 album_map_.clear();
157 folder_map_.clear();
158 UniquifyNames(albums, &album_map_);
159 UniquifyNames(folders, &folder_map_);
160
161 state_ = LIST_OF_ALBUMS_AND_FOLDERS_FRESH_STATE;
162 RunAllCallbacks(&album_list_ready_callbacks_, parse_success);
163
164 // Chain from this process onto refreshing the albums images if necessary.
165 if (!albums_indexer_ready_callbacks_.empty())
166 DoRefreshIfNecessary();
167 }
168
169 void PicasaDataProvider::OnAlbumsIndexerDone(
170 scoped_refptr<SafePicasaAlbumsIndexer> indexer,
171 bool success,
172 const picasa::AlbumImagesMap& albums_images) {
173
vandebo (ex-Chrome) 2013/08/14 23:48:11 Remove extra line
tommycli 2013/08/15 22:52:36 Done.
174 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread());
175 // If the indexer has already been deemed stale, ignore the result.
176 if (indexer != albums_indexer_)
177 return;
178
179 DCHECK(state_ == LIST_OF_ALBUMS_AND_FOLDERS_FRESH_STATE);
180
181 if (success) {
182 state_ = ALBUMS_IMAGES_FRESH_STATE;
183
184 albums_images_ = albums_images;
185 }
186
187 RunAllCallbacks(&albums_indexer_ready_callbacks_, success);
69 } 188 }
70 189
71 // static 190 // static
72 std::string PicasaDataProvider::DateToPathString(const base::Time& time) { 191 std::string PicasaDataProvider::DateToPathString(const base::Time& time) {
73 base::Time::Exploded exploded_time; 192 base::Time::Exploded exploded_time;
74 time.LocalExplode(&exploded_time); 193 time.LocalExplode(&exploded_time);
75 194
76 // TODO(tommycli): Investigate better localization and persisting which locale 195 // TODO(tommycli): Investigate better localization and persisting which locale
77 // we use to generate these unique names. 196 // we use to generate these unique names.
78 return base::StringPrintf("%04d-%02d-%02d", exploded_time.year, 197 return base::StringPrintf("%04d-%02d-%02d", exploded_time.year,
(...skipping 23 matching lines...) Expand all
102 if (total_counts[name] != 1) { 221 if (total_counts[name] != 1) {
103 name = base::StringPrintf("%s (%d)", name.c_str(), 222 name = base::StringPrintf("%s (%d)", name.c_str(),
104 ++current_counts[name]); 223 ++current_counts[name]);
105 } 224 }
106 225
107 result_map->insert(std::pair<std::string, AlbumInfo>(name, info_list[i])); 226 result_map->insert(std::pair<std::string, AlbumInfo>(name, info_list[i]));
108 } 227 }
109 } 228 }
110 229
111 } // namespace picasa 230 } // namespace picasa
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698