Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 129 NULL); | 132 NULL); |
| 130 } | 133 } |
| 131 | 134 |
| 132 void MediaGalleriesDialogController::DidToggleGallery( | 135 void MediaGalleriesDialogController::DidToggleGallery( |
| 133 const MediaGalleryPrefInfo* gallery, | 136 const MediaGalleryPrefInfo* gallery, |
| 134 bool enabled) { | 137 bool enabled) { |
| 135 // Check known galleries. | 138 // Check known galleries. |
| 136 KnownGalleryPermissions::iterator iter = | 139 KnownGalleryPermissions::iterator iter = |
| 137 known_galleries_.find(gallery->pref_id); | 140 known_galleries_.find(gallery->pref_id); |
| 138 if (iter != known_galleries_.end()) { | 141 if (iter != known_galleries_.end()) { |
| 139 iter->second.allowed = enabled; | 142 iter->second.allowed = enabled; |
|
vandebo (ex-Chrome)
2013/02/06 00:42:23
DCHECK_NE(iter->second.allowed, enabled) ?
Lei Zhang
2013/02/07 01:44:56
Done.
| |
| 143 if (ContainsKey(toggled_galleries_, gallery->pref_id)) | |
| 144 toggled_galleries_.erase(gallery->pref_id); | |
| 145 else | |
| 146 toggled_galleries_.insert(gallery->pref_id); | |
| 140 return; | 147 return; |
| 141 } | 148 } |
| 142 | 149 |
| 143 // Check new galleries. | 150 // Check new galleries. |
| 144 for (NewGalleryPermissions::iterator iter = new_galleries_.begin(); | 151 for (NewGalleryPermissions::iterator iter = new_galleries_.begin(); |
| 145 iter != new_galleries_.end(); ++iter) { | 152 iter != new_galleries_.end(); ++iter) { |
| 146 if (&iter->pref_info == gallery) { | 153 if (&iter->pref_info == gallery) { |
| 147 iter->allowed = enabled; | 154 iter->allowed = enabled; |
| 148 return; | 155 return; |
| 149 } | 156 } |
| 150 } | 157 } |
| 151 | 158 |
| 152 NOTREACHED(); | 159 NOTREACHED(); |
| 153 } | 160 } |
| 154 | 161 |
| 155 void MediaGalleriesDialogController::DialogFinished(bool accepted) { | 162 void MediaGalleriesDialogController::DialogFinished(bool accepted) { |
| 163 // The dialog has finished, so there is no need to watch for more updates | |
| 164 // from |preferences_|. Do this here and not in the dtor since this is the | |
| 165 // only non-test code path that deletes |this|. The test ctor never adds | |
|
vandebo (ex-Chrome)
2013/02/06 00:42:23
How can the test ctor never add the observer? The
Lei Zhang
2013/02/07 01:44:56
The tests go through MediaGalleriesDialogControlle
| |
| 166 // this observer in the first place. | |
| 167 preferences_->RemoveGalleryChangeObserver(this); | |
| 168 | |
| 156 if (accepted) | 169 if (accepted) |
| 157 SavePermissions(); | 170 SavePermissions(); |
| 158 | 171 |
| 159 on_finish_.Run(); | 172 on_finish_.Run(); |
| 160 delete this; | 173 delete this; |
| 161 } | 174 } |
| 162 | 175 |
| 163 const MediaGalleriesDialogController::KnownGalleryPermissions& | 176 const MediaGalleriesDialogController::KnownGalleryPermissions& |
| 164 MediaGalleriesDialogController::permissions() const { | 177 MediaGalleriesDialogController::permissions() const { |
| 165 return known_galleries_; | 178 return known_galleries_; |
| 166 } | 179 } |
| 167 | 180 |
| 168 content::WebContents* MediaGalleriesDialogController::web_contents() { | 181 content::WebContents* MediaGalleriesDialogController::web_contents() { |
| 169 return web_contents_; | 182 return web_contents_; |
| 170 } | 183 } |
| 171 | 184 |
| 172 void MediaGalleriesDialogController::FileSelected(const FilePath& path, | 185 void MediaGalleriesDialogController::FileSelected(const FilePath& path, |
| 173 int /*index*/, | 186 int /*index*/, |
| 174 void* /*params*/) { | 187 void* /*params*/) { |
| 175 // Try to find it in |known_galleries_|. | 188 // Try to find it in the prefs. |
| 176 MediaGalleryPrefInfo gallery; | 189 MediaGalleryPrefInfo gallery; |
| 177 bool gallery_exists = preferences_->LookUpGalleryByPath(path, &gallery); | 190 bool gallery_exists = preferences_->LookUpGalleryByPath(path, &gallery); |
| 178 if (gallery_exists && gallery.type != MediaGalleryPrefInfo::kBlackListed) { | 191 if (gallery_exists && gallery.type != MediaGalleryPrefInfo::kBlackListed) { |
| 192 // The prefs are in sync with |known_galleries_|, so it should exist in | |
| 193 // |known_galleries_| as well. User selecting a known gallery effectively | |
| 194 // just sets the gallery to permitted. | |
| 179 KnownGalleryPermissions::const_iterator iter = | 195 KnownGalleryPermissions::const_iterator iter = |
| 180 known_galleries_.find(gallery.pref_id); | 196 known_galleries_.find(gallery.pref_id); |
| 181 | 197 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); | 198 dialog_->UpdateGallery(&iter->second.pref_info, true); |
| 190 return; | 199 return; |
| 191 } | 200 } |
| 192 | 201 |
| 193 // Try to find it in |new_galleries_| (user added same folder twice). | 202 // Try to find it in |new_galleries_| (user added same folder twice). |
| 194 for (NewGalleryPermissions::iterator iter = new_galleries_.begin(); | 203 for (NewGalleryPermissions::iterator iter = new_galleries_.begin(); |
| 195 iter != new_galleries_.end(); ++iter) { | 204 iter != new_galleries_.end(); ++iter) { |
| 196 if (iter->pref_info.path == gallery.path && | 205 if (iter->pref_info.path == gallery.path && |
| 197 iter->pref_info.device_id == gallery.device_id) { | 206 iter->pref_info.device_id == gallery.device_id) { |
| 198 iter->allowed = true; | 207 iter->allowed = true; |
| 199 dialog_->UpdateGallery(&iter->pref_info, true); | 208 dialog_->UpdateGallery(&iter->pref_info, true); |
| 200 return; | 209 return; |
| 201 } | 210 } |
| 202 } | 211 } |
| 203 | 212 |
| 204 // Lastly, add it to |new_galleries_|. | 213 // Lastly, add a new gallery to |new_galleries_|. |
| 205 new_galleries_.push_back(GalleryPermission(gallery, true)); | 214 new_galleries_.push_back(GalleryPermission(gallery, true)); |
| 206 dialog_->UpdateGallery(&new_galleries_.back().pref_info, true); | 215 dialog_->UpdateGallery(&new_galleries_.back().pref_info, true); |
| 207 } | 216 } |
| 208 | 217 |
| 209 void MediaGalleriesDialogController::OnRemovableStorageAttached( | 218 void MediaGalleriesDialogController::OnRemovableStorageAttached( |
| 210 const RemovableStorageNotifications::StorageInfo& info) { | 219 const RemovableStorageNotifications::StorageInfo& info) { |
| 211 UpdateGalleriesOnDeviceEvent(info.device_id); | 220 UpdateGalleriesOnDeviceEvent(info.device_id); |
| 212 } | 221 } |
| 213 | 222 |
| 214 void MediaGalleriesDialogController::OnRemovableStorageDetached( | 223 void MediaGalleriesDialogController::OnRemovableStorageDetached( |
| 215 const RemovableStorageNotifications::StorageInfo& info) { | 224 const RemovableStorageNotifications::StorageInfo& info) { |
| 216 UpdateGalleriesOnDeviceEvent(info.device_id); | 225 UpdateGalleriesOnDeviceEvent(info.device_id); |
| 217 } | 226 } |
| 218 | 227 |
| 228 void MediaGalleriesDialogController::OnGalleryChanged( | |
| 229 MediaGalleriesPreferences* pref) { | |
| 230 DCHECK_EQ(preferences_, pref); | |
| 231 UpdateGalleriesOnPreferencesEvent(); | |
| 232 } | |
| 233 | |
| 219 void MediaGalleriesDialogController::InitializePermissions() { | 234 void MediaGalleriesDialogController::InitializePermissions() { |
| 220 const MediaGalleriesPrefInfoMap& galleries = preferences_->known_galleries(); | 235 const MediaGalleriesPrefInfoMap& galleries = preferences_->known_galleries(); |
| 221 for (MediaGalleriesPrefInfoMap::const_iterator iter = galleries.begin(); | 236 for (MediaGalleriesPrefInfoMap::const_iterator iter = galleries.begin(); |
| 222 iter != galleries.end(); | 237 iter != galleries.end(); |
| 223 ++iter) { | 238 ++iter) { |
| 224 const MediaGalleryPrefInfo& gallery = iter->second; | 239 const MediaGalleryPrefInfo& gallery = iter->second; |
| 225 if (gallery.type == MediaGalleryPrefInfo::kBlackListed) | 240 if (gallery.type == MediaGalleryPrefInfo::kBlackListed) |
| 226 continue; | 241 continue; |
| 227 | 242 |
| 228 known_galleries_[iter->first] = GalleryPermission(gallery, false); | 243 known_galleries_[iter->first] = GalleryPermission(gallery, false); |
| 229 } | 244 } |
| 230 | 245 |
| 231 MediaGalleryPrefIdSet permitted = | 246 MediaGalleryPrefIdSet permitted = |
| 232 preferences_->GalleriesForExtension(*extension_); | 247 preferences_->GalleriesForExtension(*extension_); |
| 233 | 248 |
| 234 for (MediaGalleryPrefIdSet::iterator iter = permitted.begin(); | 249 for (MediaGalleryPrefIdSet::iterator iter = permitted.begin(); |
| 235 iter != permitted.end(); ++iter) { | 250 iter != permitted.end(); ++iter) { |
| 251 if (ContainsKey(toggled_galleries_, *iter)) | |
| 252 continue; | |
| 253 DCHECK(ContainsKey(known_galleries_, *iter)); | |
| 236 known_galleries_[*iter].allowed = true; | 254 known_galleries_[*iter].allowed = true; |
| 237 } | 255 } |
| 238 } | 256 } |
| 239 | 257 |
| 240 void MediaGalleriesDialogController::SavePermissions() { | 258 void MediaGalleriesDialogController::SavePermissions() { |
| 241 for (KnownGalleryPermissions::const_iterator iter = known_galleries_.begin(); | 259 for (KnownGalleryPermissions::const_iterator iter = known_galleries_.begin(); |
| 242 iter != known_galleries_.end(); ++iter) { | 260 iter != known_galleries_.end(); ++iter) { |
| 243 preferences_->SetGalleryPermissionForExtension( | 261 preferences_->SetGalleryPermissionForExtension( |
| 244 *extension_, iter->first, iter->second.allowed); | 262 *extension_, iter->first, iter->second.allowed); |
| 245 } | 263 } |
| 246 | 264 |
| 247 for (NewGalleryPermissions::const_iterator iter = new_galleries_.begin(); | 265 for (NewGalleryPermissions::const_iterator iter = new_galleries_.begin(); |
| 248 iter != new_galleries_.end(); ++iter) { | 266 iter != new_galleries_.end(); ++iter) { |
| 249 // If the user added a gallery then unchecked it, forget about it. | 267 // If the user added a gallery then unchecked it, forget about it. |
| 250 if (!iter->allowed) | 268 if (!iter->allowed) |
| 251 continue; | 269 continue; |
| 252 | 270 |
| 253 const MediaGalleryPrefInfo& gallery = iter->pref_info; | 271 const MediaGalleryPrefInfo& gallery = iter->pref_info; |
| 254 MediaGalleryPrefId id = preferences_->AddGallery( | 272 MediaGalleryPrefId id = preferences_->AddGallery( |
| 255 gallery.device_id, gallery.display_name, gallery.path, true); | 273 gallery.device_id, gallery.display_name, gallery.path, true); |
| 256 preferences_->SetGalleryPermissionForExtension( | 274 preferences_->SetGalleryPermissionForExtension(*extension_, id, true); |
| 257 *extension_, id, true); | 275 } |
| 276 } | |
| 277 | |
| 278 void MediaGalleriesDialogController::UpdateGalleriesOnPreferencesEvent() { | |
|
vandebo (ex-Chrome)
2013/02/06 00:42:23
I've previously suggested walking both lists concu
| |
| 279 // Merge in the permissions from |preferences_|. Afterwards, | |
| 280 // |known_galleries_| may contain galleries that no longer belong there. | |
| 281 InitializePermissions(); | |
| 282 | |
| 283 // If a gallery still belong in |known_galleries_|, update its view. | |
| 284 // If it no longer belongs in |known_galleries_|, forget it in the model/view. | |
| 285 const MediaGalleriesPrefInfoMap& pref_galleries = | |
| 286 preferences_->known_galleries(); | |
| 287 MediaGalleryPrefIdSet galleries_to_forget; | |
| 288 for (KnownGalleryPermissions::const_iterator it = known_galleries_.begin(); | |
| 289 it != known_galleries_.end(); | |
| 290 ++it) { | |
| 291 const MediaGalleryPrefId& gallery_id = it->first; | |
| 292 const GalleryPermission& gallery = it->second; | |
| 293 MediaGalleriesPrefInfoMap::const_iterator pref_it = | |
| 294 pref_galleries.find(gallery_id); | |
| 295 if (pref_it == pref_galleries.end() || | |
| 296 pref_it->second.type == MediaGalleryPrefInfo::kBlackListed) { | |
| 297 galleries_to_forget.insert(gallery_id); | |
| 298 dialog_->ForgetGallery(&gallery.pref_info); | |
| 299 } else { | |
| 300 dialog_->UpdateGallery(&gallery.pref_info, gallery.allowed); | |
| 301 } | |
| 302 } | |
| 303 | |
| 304 // Remove the galleries to forget from |known_galleries_|. Doing it in the | |
| 305 // above loop would invalidate the iterator there. | |
| 306 for (MediaGalleryPrefIdSet::const_iterator it = galleries_to_forget.begin(); | |
| 307 it != galleries_to_forget.end(); | |
| 308 ++it) { | |
| 309 known_galleries_.erase(*it); | |
| 310 } | |
| 311 | |
| 312 // Resolve |new_galleries_| against the updated |known_galleries_|. | |
|
vandebo (ex-Chrome)
2013/02/06 00:42:23
Does this need to go first? We will have already
Lei Zhang
2013/02/07 01:44:56
I think it's fine to do it here. There's no way to
vandebo (ex-Chrome)
2013/02/07 02:08:05
I just don't know how the dialog actually works so
| |
| 313 NewGalleryPermissions::iterator new_it = new_galleries_.begin(); | |
| 314 while (new_it != new_galleries_.end()) { | |
| 315 bool new_gallery_in_known_galleries = false; | |
| 316 for (KnownGalleryPermissions::const_iterator it = known_galleries_.begin(); | |
| 317 it != known_galleries_.end(); ++it) { | |
| 318 const MediaGalleryPrefInfo& gallery = it->second.pref_info; | |
| 319 if (new_it->pref_info.path == gallery.path && | |
| 320 new_it->pref_info.device_id == gallery.device_id) { | |
| 321 new_gallery_in_known_galleries = true; | |
| 322 break; | |
| 323 } | |
| 324 } | |
| 325 if (new_gallery_in_known_galleries) { | |
| 326 dialog_->ForgetGallery(&new_it->pref_info); | |
| 327 new_it = new_galleries_.erase(new_it); | |
| 328 } else { | |
| 329 ++new_it; | |
| 330 } | |
| 258 } | 331 } |
| 259 } | 332 } |
| 260 | 333 |
| 261 void MediaGalleriesDialogController::UpdateGalleriesOnDeviceEvent( | 334 void MediaGalleriesDialogController::UpdateGalleriesOnDeviceEvent( |
| 262 const std::string& device_id) { | 335 const std::string& device_id) { |
| 263 for (KnownGalleryPermissions::iterator iter = known_galleries_.begin(); | 336 for (KnownGalleryPermissions::iterator iter = known_galleries_.begin(); |
| 264 iter != known_galleries_.end(); ++iter) { | 337 iter != known_galleries_.end(); ++iter) { |
| 265 if (iter->second.pref_info.device_id == device_id) | 338 if (iter->second.pref_info.device_id == device_id) |
| 266 dialog_->UpdateGallery(&iter->second.pref_info, iter->second.allowed); | 339 dialog_->UpdateGallery(&iter->second.pref_info, iter->second.allowed); |
| 267 } | 340 } |
| 268 | 341 |
| 269 for (NewGalleryPermissions::iterator iter = new_galleries_.begin(); | 342 for (NewGalleryPermissions::iterator iter = new_galleries_.begin(); |
| 270 iter != new_galleries_.end(); ++iter) { | 343 iter != new_galleries_.end(); ++iter) { |
| 271 if (iter->pref_info.device_id == device_id) | 344 if (iter->pref_info.device_id == device_id) |
| 272 dialog_->UpdateGallery(&iter->pref_info, iter->allowed); | 345 dialog_->UpdateGallery(&iter->pref_info, iter->allowed); |
| 273 } | 346 } |
| 274 } | 347 } |
| 275 | 348 |
| 276 // MediaGalleries dialog ------------------------------------------------------- | 349 // MediaGalleries dialog ------------------------------------------------------- |
| 277 | 350 |
| 278 MediaGalleriesDialog::~MediaGalleriesDialog() {} | 351 MediaGalleriesDialog::~MediaGalleriesDialog() {} |
| 279 | 352 |
| 280 } // namespace chrome | 353 } // namespace chrome |
| OLD | NEW |