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

Side by Side Diff: chrome/browser/media_gallery/media_galleries_dialog_controller.cc

Issue 12095074: Media Galleries: Keep media gallery permission dialogs in sync with the gallery (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 7 years, 10 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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_gallery/media_galleries_dialog_controller.h" 5 #include "chrome/browser/media_gallery/media_galleries_dialog_controller.h"
6 6
7 #include "base/path_service.h" 7 #include "base/path_service.h"
8 #include "base/stl_util.h"
8 #include "base/utf_string_conversions.h" 9 #include "base/utf_string_conversions.h"
9 #include "chrome/browser/browser_process.h" 10 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/media_gallery/media_file_system_registry.h" 11 #include "chrome/browser/media_gallery/media_file_system_registry.h"
11 #include "chrome/browser/profiles/profile.h" 12 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/system_monitor/media_storage_util.h" 13 #include "chrome/browser/system_monitor/media_storage_util.h"
13 #include "chrome/browser/system_monitor/removable_storage_notifications.h" 14 #include "chrome/browser/system_monitor/removable_storage_notifications.h"
14 #include "chrome/browser/ui/chrome_select_file_policy.h" 15 #include "chrome/browser/ui/chrome_select_file_policy.h"
15 #include "chrome/common/chrome_paths.h" 16 #include "chrome/common/chrome_paths.h"
16 #include "chrome/common/extensions/extension.h" 17 #include "chrome/common/extensions/extension.h"
17 #include "content/public/browser/web_contents.h" 18 #include "content/public/browser/web_contents.h"
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 preferences_ = registry->GetPreferences( 53 preferences_ = registry->GetPreferences(
53 Profile::FromBrowserContext(web_contents->GetBrowserContext())); 54 Profile::FromBrowserContext(web_contents->GetBrowserContext()));
54 InitializePermissions(); 55 InitializePermissions();
55 56
56 dialog_.reset(MediaGalleriesDialog::Create(this)); 57 dialog_.reset(MediaGalleriesDialog::Create(this));
57 58
58 RemovableStorageNotifications* notifications = 59 RemovableStorageNotifications* notifications =
59 RemovableStorageNotifications::GetInstance(); 60 RemovableStorageNotifications::GetInstance();
60 if (notifications) 61 if (notifications)
61 notifications->AddObserver(this); 62 notifications->AddObserver(this);
63
64 preferences_->AddGalleryChangeObserver(this);
62 } 65 }
63 66
64 MediaGalleriesDialogController::MediaGalleriesDialogController() 67 MediaGalleriesDialogController::MediaGalleriesDialogController()
65 : web_contents_(NULL), 68 : web_contents_(NULL),
66 extension_(NULL), 69 extension_(NULL),
67 preferences_(NULL) {} 70 preferences_(NULL) {}
68 71
69 MediaGalleriesDialogController::~MediaGalleriesDialogController() { 72 MediaGalleriesDialogController::~MediaGalleriesDialogController() {
70 RemovableStorageNotifications* notifications = 73 RemovableStorageNotifications* notifications =
71 RemovableStorageNotifications::GetInstance(); 74 RemovableStorageNotifications::GetInstance();
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 if (&iter->pref_info == gallery) { 149 if (&iter->pref_info == gallery) {
147 iter->allowed = enabled; 150 iter->allowed = enabled;
148 return; 151 return;
149 } 152 }
150 } 153 }
151 154
152 NOTREACHED(); 155 NOTREACHED();
153 } 156 }
154 157
155 void MediaGalleriesDialogController::DialogFinished(bool accepted) { 158 void MediaGalleriesDialogController::DialogFinished(bool accepted) {
159 // The dialog has finished, so there is no need to watch for more updates
160 // from |preferences_|. Do this here and not in the dtor since this is the
161 // only non-test code path that deletes |this|. The test ctor never adds
162 // this observer in the first place.
163 preferences_->RemoveGalleryChangeObserver(this);
164
156 if (accepted) 165 if (accepted)
157 SavePermissions(); 166 SavePermissions();
158 167
159 on_finish_.Run(); 168 on_finish_.Run();
160 delete this; 169 delete this;
161 } 170 }
162 171
163 const MediaGalleriesDialogController::KnownGalleryPermissions& 172 const MediaGalleriesDialogController::KnownGalleryPermissions&
164 MediaGalleriesDialogController::permissions() const { 173 MediaGalleriesDialogController::permissions() const {
165 return known_galleries_; 174 return known_galleries_;
166 } 175 }
167 176
168 content::WebContents* MediaGalleriesDialogController::web_contents() { 177 content::WebContents* MediaGalleriesDialogController::web_contents() {
169 return web_contents_; 178 return web_contents_;
170 } 179 }
171 180
172 void MediaGalleriesDialogController::FileSelected(const FilePath& path, 181 void MediaGalleriesDialogController::FileSelected(const FilePath& path,
173 int /*index*/, 182 int /*index*/,
174 void* /*params*/) { 183 void* /*params*/) {
175 // Try to find it in |known_galleries_|. 184 // Try to find it in the prefs.
176 MediaGalleryPrefInfo gallery; 185 MediaGalleryPrefInfo gallery;
177 bool gallery_exists = preferences_->LookUpGalleryByPath(path, &gallery); 186 bool gallery_exists = preferences_->LookUpGalleryByPath(path, &gallery);
178 if (gallery_exists && gallery.type != MediaGalleryPrefInfo::kBlackListed) { 187 if (gallery_exists && gallery.type != MediaGalleryPrefInfo::kBlackListed) {
188 // The prefs are in sync with |known_galleries_|, so it should exist in
189 // |known_galleries_| as well. User selecting a known gallery effectively
190 // just sets the gallery to permitted.
179 KnownGalleryPermissions::const_iterator iter = 191 KnownGalleryPermissions::const_iterator iter =
180 known_galleries_.find(gallery.pref_id); 192 known_galleries_.find(gallery.pref_id);
181 193 DCHECK(iter != known_galleries_.end());
182 if (iter == known_galleries_.end()) {
183 // This is rare, but could happen if a gallery was not "known"
184 // when the controller first initialized, but has since been added.
185 known_galleries_[gallery.pref_id] = GalleryPermission(gallery, true);
186 iter = known_galleries_.find(gallery.pref_id);
187 }
188
189 dialog_->UpdateGallery(&iter->second.pref_info, true); 194 dialog_->UpdateGallery(&iter->second.pref_info, true);
190 return; 195 return;
191 } 196 }
192 197
193 // Try to find it in |new_galleries_| (user added same folder twice). 198 // Try to find it in |new_galleries_| (user added same folder twice).
194 for (NewGalleryPermissions::iterator iter = new_galleries_.begin(); 199 for (NewGalleryPermissions::iterator iter = new_galleries_.begin();
195 iter != new_galleries_.end(); ++iter) { 200 iter != new_galleries_.end(); ++iter) {
196 if (iter->pref_info.path == gallery.path && 201 if (iter->pref_info.path == gallery.path &&
197 iter->pref_info.device_id == gallery.device_id) { 202 iter->pref_info.device_id == gallery.device_id) {
198 iter->allowed = true; 203 iter->allowed = true;
199 dialog_->UpdateGallery(&iter->pref_info, true); 204 dialog_->UpdateGallery(&iter->pref_info, true);
200 return; 205 return;
201 } 206 }
202 } 207 }
203 208
204 // Lastly, add it to |new_galleries_|. 209 // Lastly, add a new gallery to |new_galleries_|.
205 new_galleries_.push_back(GalleryPermission(gallery, true)); 210 new_galleries_.push_back(GalleryPermission(gallery, true));
206 dialog_->UpdateGallery(&new_galleries_.back().pref_info, true); 211 dialog_->UpdateGallery(&new_galleries_.back().pref_info, true);
207 } 212 }
208 213
209 void MediaGalleriesDialogController::OnRemovableStorageAttached( 214 void MediaGalleriesDialogController::OnRemovableStorageAttached(
210 const RemovableStorageNotifications::StorageInfo& info) { 215 const RemovableStorageNotifications::StorageInfo& info) {
211 UpdateGalleriesOnDeviceEvent(info.device_id); 216 UpdateGalleriesOnDeviceEvent(info.device_id);
212 } 217 }
213 218
214 void MediaGalleriesDialogController::OnRemovableStorageDetached( 219 void MediaGalleriesDialogController::OnRemovableStorageDetached(
215 const RemovableStorageNotifications::StorageInfo& info) { 220 const RemovableStorageNotifications::StorageInfo& info) {
216 UpdateGalleriesOnDeviceEvent(info.device_id); 221 UpdateGalleriesOnDeviceEvent(info.device_id);
217 } 222 }
218 223
224 void MediaGalleriesDialogController::OnGalleryChanged(
225 MediaGalleriesPreferences* pref) {
226 DCHECK_EQ(preferences_, pref);
227 UpdateGalleriesOnPreferencesEvent();
228 }
229
219 void MediaGalleriesDialogController::InitializePermissions() { 230 void MediaGalleriesDialogController::InitializePermissions() {
220 const MediaGalleriesPrefInfoMap& galleries = preferences_->known_galleries(); 231 const MediaGalleriesPrefInfoMap& galleries = preferences_->known_galleries();
221 for (MediaGalleriesPrefInfoMap::const_iterator iter = galleries.begin(); 232 for (MediaGalleriesPrefInfoMap::const_iterator iter = galleries.begin();
222 iter != galleries.end(); 233 iter != galleries.end();
223 ++iter) { 234 ++iter) {
224 const MediaGalleryPrefInfo& gallery = iter->second; 235 const MediaGalleryPrefInfo& gallery = iter->second;
225 if (gallery.type == MediaGalleryPrefInfo::kBlackListed) 236 if (gallery.type == MediaGalleryPrefInfo::kBlackListed)
226 continue; 237 continue;
227 238
228 known_galleries_[iter->first] = GalleryPermission(gallery, false); 239 known_galleries_[iter->first] = GalleryPermission(gallery, false);
229 } 240 }
230 241
231 MediaGalleryPrefIdSet permitted = 242 MediaGalleryPrefIdSet permitted =
232 preferences_->GalleriesForExtension(*extension_); 243 preferences_->GalleriesForExtension(*extension_);
233 244
234 for (MediaGalleryPrefIdSet::iterator iter = permitted.begin(); 245 for (MediaGalleryPrefIdSet::iterator iter = permitted.begin();
235 iter != permitted.end(); ++iter) { 246 iter != permitted.end(); ++iter) {
247 DCHECK(ContainsKey(known_galleries_, *iter));
236 known_galleries_[*iter].allowed = true; 248 known_galleries_[*iter].allowed = true;
237 } 249 }
238 } 250 }
239 251
240 void MediaGalleriesDialogController::SavePermissions() { 252 void MediaGalleriesDialogController::SavePermissions() {
241 for (KnownGalleryPermissions::const_iterator iter = known_galleries_.begin(); 253 for (KnownGalleryPermissions::const_iterator iter = known_galleries_.begin();
242 iter != known_galleries_.end(); ++iter) { 254 iter != known_galleries_.end(); ++iter) {
243 preferences_->SetGalleryPermissionForExtension( 255 preferences_->SetGalleryPermissionForExtension(
244 *extension_, iter->first, iter->second.allowed); 256 *extension_, iter->first, iter->second.allowed);
245 } 257 }
246 258
247 for (NewGalleryPermissions::const_iterator iter = new_galleries_.begin(); 259 for (NewGalleryPermissions::const_iterator iter = new_galleries_.begin();
248 iter != new_galleries_.end(); ++iter) { 260 iter != new_galleries_.end(); ++iter) {
249 // If the user added a gallery then unchecked it, forget about it. 261 // If the user added a gallery then unchecked it, forget about it.
250 if (!iter->allowed) 262 if (!iter->allowed)
251 continue; 263 continue;
252 264
253 const MediaGalleryPrefInfo& gallery = iter->pref_info; 265 const MediaGalleryPrefInfo& gallery = iter->pref_info;
254 MediaGalleryPrefId id = preferences_->AddGallery( 266 MediaGalleryPrefId id = preferences_->AddGallery(
255 gallery.device_id, gallery.display_name, gallery.path, true); 267 gallery.device_id, gallery.display_name, gallery.path, true);
256 preferences_->SetGalleryPermissionForExtension( 268 preferences_->SetGalleryPermissionForExtension(*extension_, id, true);
257 *extension_, id, true); 269 }
270 }
271
272 void MediaGalleriesDialogController::UpdateGalleriesOnPreferencesEvent() {
273 // First, update |known_galleries_| from |pref_galleries|.
vandebo (ex-Chrome) 2013/01/31 01:54:07 Seems that you could just put this all in OnGaller
Lei Zhang 2013/01/31 06:59:21 Yes, this is not strictly necessary. I hope it's m
vandebo (ex-Chrome) 2013/01/31 19:38:50 Didn't notice that part. The storage attached/det
274 const MediaGalleriesPrefInfoMap& pref_galleries =
275 preferences_->known_galleries();
276 for (MediaGalleriesPrefInfoMap::const_iterator it = pref_galleries.begin();
277 it != pref_galleries.end();
278 ++it) {
279 const MediaGalleryPrefInfo& gallery = it->second;
280 if (gallery.type == MediaGalleryPrefInfo::kBlackListed)
vandebo (ex-Chrome) 2013/01/31 01:54:07 Seems that this shares some code with InitializePe
Lei Zhang 2013/02/01 22:04:11 Done.
281 continue;
282
283 if (ContainsKey(known_galleries_, it->first)) {
284 // Reset all existing galleries to unpermitted.
285 const MediaGalleryPrefInfo& existing_gallery =
286 known_galleries_[it->first].pref_info;
287 DCHECK_EQ(existing_gallery.display_name, gallery.display_name);
vandebo (ex-Chrome) 2013/01/31 01:54:07 We might get a gallery change event because the na
Lei Zhang 2013/01/31 06:59:21 I don't think that can happen. Do you have an exam
vandebo (ex-Chrome) 2013/01/31 19:38:50 See chrome/browser/media_gallery/media_galleries_p
Lei Zhang 2013/02/01 22:04:11 Ya, I'm doing that since I just call InitializePer
288 DCHECK_EQ(existing_gallery.device_id, gallery.device_id);
289 DCHECK_EQ(existing_gallery.path.value(), gallery.path.value());
290 known_galleries_[it->first].allowed = false;
291 } else {
292 // Add new galleries as unpermitted.
293 known_galleries_[it->first] = GalleryPermission(gallery, false);
294 continue;
295 }
296 }
297
298 // Next, find all the galleries in |known_galleries_| that no longer exist
vandebo (ex-Chrome) 2013/01/31 01:54:07 Would it be easier to sort the new and old collect
Lei Zhang 2013/01/31 06:59:21 This seems to go against the suggestion above to r
vandebo (ex-Chrome) 2013/01/31 19:38:50 It may not be practical to do both sure, but tryin
Lei Zhang 2013/02/01 22:04:11 Got it down to InitializePermissions + 3 loops.
299 // in |pref_galleries|.
300 MediaGalleryPrefIdSet galleries_to_forget;
301 for (KnownGalleryPermissions::const_iterator it = known_galleries_.begin();
302 it != known_galleries_.end();
303 ++it) {
304 MediaGalleriesPrefInfoMap::const_iterator gallery_it =
305 pref_galleries.find(it->first);
306 if (gallery_it == pref_galleries.end() ||
307 gallery_it->second.type == MediaGalleryPrefInfo::kBlackListed) {
308 galleries_to_forget.insert(it->first);
309 }
310 }
311
312 // And forget them both in |known_galleries_| and in the view.
313 for (MediaGalleryPrefIdSet::const_iterator it = galleries_to_forget.begin();
314 it != galleries_to_forget.end();
315 ++it) {
316 dialog_->ForgetGallery(&known_galleries_[*it].pref_info);
317 known_galleries_.erase(*it);
318 }
319
320 // Set the permitted bits for the remaining valid galleries.
321 MediaGalleryPrefIdSet permitted =
322 preferences_->GalleriesForExtension(*extension_);
323 for (MediaGalleryPrefIdSet::iterator it = permitted.begin();
324 it != permitted.end();
325 ++it) {
326 DCHECK(ContainsKey(known_galleries_, *it));
327 known_galleries_[*it].allowed = true;
328 }
329
330 // Update the view for |known_galleries_|.
331 for (KnownGalleryPermissions::const_iterator it = known_galleries_.begin();
332 it != known_galleries_.end();
333 ++it) {
334 dialog_->UpdateGallery(&it->second.pref_info, it->second.allowed);
335 }
336
337 // Finally, resolve |new_galleries_| again the updated |known_galleries_|.
338 NewGalleryPermissions::iterator new_it = new_galleries_.begin();
339 while (new_it != new_galleries_.end()) {
340 bool new_gallery_in_known_galleries = false;
341 for (KnownGalleryPermissions::const_iterator it = known_galleries_.begin();
342 it != known_galleries_.end(); ++it) {
343 const MediaGalleryPrefInfo& gallery = it->second.pref_info;
344 if (new_it->pref_info.path == gallery.path &&
345 new_it->pref_info.device_id == gallery.device_id) {
346 new_gallery_in_known_galleries = true;
347 break;
348 }
349 }
350 if (new_gallery_in_known_galleries) {
351 dialog_->ForgetGallery(&new_it->pref_info);
352 new_it = new_galleries_.erase(new_it);
353 } else {
354 ++new_it;
355 }
258 } 356 }
259 } 357 }
260 358
261 void MediaGalleriesDialogController::UpdateGalleriesOnDeviceEvent( 359 void MediaGalleriesDialogController::UpdateGalleriesOnDeviceEvent(
262 const std::string& device_id) { 360 const std::string& device_id) {
263 for (KnownGalleryPermissions::iterator iter = known_galleries_.begin(); 361 for (KnownGalleryPermissions::iterator iter = known_galleries_.begin();
264 iter != known_galleries_.end(); ++iter) { 362 iter != known_galleries_.end(); ++iter) {
265 if (iter->second.pref_info.device_id == device_id) 363 if (iter->second.pref_info.device_id == device_id)
266 dialog_->UpdateGallery(&iter->second.pref_info, iter->second.allowed); 364 dialog_->UpdateGallery(&iter->second.pref_info, iter->second.allowed);
267 } 365 }
268 366
269 for (NewGalleryPermissions::iterator iter = new_galleries_.begin(); 367 for (NewGalleryPermissions::iterator iter = new_galleries_.begin();
270 iter != new_galleries_.end(); ++iter) { 368 iter != new_galleries_.end(); ++iter) {
271 if (iter->pref_info.device_id == device_id) 369 if (iter->pref_info.device_id == device_id)
272 dialog_->UpdateGallery(&iter->pref_info, iter->allowed); 370 dialog_->UpdateGallery(&iter->pref_info, iter->allowed);
273 } 371 }
274 } 372 }
275 373
276 // MediaGalleries dialog ------------------------------------------------------- 374 // MediaGalleries dialog -------------------------------------------------------
277 375
278 MediaGalleriesDialog::~MediaGalleriesDialog() {} 376 MediaGalleriesDialog::~MediaGalleriesDialog() {}
279 377
280 } // namespace chrome 378 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698