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()) { |
| 142 DCHECK_NE(iter->second.allowed, enabled); | |
| 139 iter->second.allowed = enabled; | 143 iter->second.allowed = enabled; |
| 144 if (ContainsKey(toggled_galleries_, gallery->pref_id)) | |
| 145 toggled_galleries_.erase(gallery->pref_id); | |
| 146 else | |
| 147 toggled_galleries_.insert(gallery->pref_id); | |
| 140 return; | 148 return; |
| 141 } | 149 } |
| 142 | 150 |
| 143 // Check new galleries. | 151 // Check new galleries. |
| 144 for (NewGalleryPermissions::iterator iter = new_galleries_.begin(); | 152 for (NewGalleryPermissions::iterator iter = new_galleries_.begin(); |
| 145 iter != new_galleries_.end(); ++iter) { | 153 iter != new_galleries_.end(); ++iter) { |
| 146 if (&iter->pref_info == gallery) { | 154 if (&iter->pref_info == gallery) { |
| 147 iter->allowed = enabled; | 155 iter->allowed = enabled; |
| 148 return; | 156 return; |
| 149 } | 157 } |
| 150 } | 158 } |
| 151 | 159 |
| 152 NOTREACHED(); | 160 NOTREACHED(); |
| 153 } | 161 } |
| 154 | 162 |
| 155 void MediaGalleriesDialogController::DialogFinished(bool accepted) { | 163 void MediaGalleriesDialogController::DialogFinished(bool accepted) { |
| 164 // The dialog has finished, so there is no need to watch for more updates | |
| 165 // from |preferences_|. Do this here and not in the dtor since this is the | |
| 166 // only non-test code path that deletes |this|. The test ctor never adds | |
| 167 // this observer in the first place. | |
| 168 preferences_->RemoveGalleryChangeObserver(this); | |
| 169 | |
| 156 if (accepted) | 170 if (accepted) |
| 157 SavePermissions(); | 171 SavePermissions(); |
| 158 | 172 |
| 159 on_finish_.Run(); | 173 on_finish_.Run(); |
| 160 delete this; | 174 delete this; |
| 161 } | 175 } |
| 162 | 176 |
| 163 const MediaGalleriesDialogController::KnownGalleryPermissions& | 177 const MediaGalleriesDialogController::KnownGalleryPermissions& |
| 164 MediaGalleriesDialogController::permissions() const { | 178 MediaGalleriesDialogController::permissions() const { |
| 165 return known_galleries_; | 179 return known_galleries_; |
| 166 } | 180 } |
| 167 | 181 |
| 168 content::WebContents* MediaGalleriesDialogController::web_contents() { | 182 content::WebContents* MediaGalleriesDialogController::web_contents() { |
| 169 return web_contents_; | 183 return web_contents_; |
| 170 } | 184 } |
| 171 | 185 |
| 172 void MediaGalleriesDialogController::FileSelected(const base::FilePath& path, | 186 void MediaGalleriesDialogController::FileSelected(const base::FilePath& path, |
| 173 int /*index*/, | 187 int /*index*/, |
| 174 void* /*params*/) { | 188 void* /*params*/) { |
| 175 // Try to find it in |known_galleries_|. | 189 // Try to find it in the prefs. |
| 176 MediaGalleryPrefInfo gallery; | 190 MediaGalleryPrefInfo gallery; |
| 177 bool gallery_exists = preferences_->LookUpGalleryByPath(path, &gallery); | 191 bool gallery_exists = preferences_->LookUpGalleryByPath(path, &gallery); |
| 178 if (gallery_exists && gallery.type != MediaGalleryPrefInfo::kBlackListed) { | 192 if (gallery_exists && gallery.type != MediaGalleryPrefInfo::kBlackListed) { |
| 193 // The prefs are in sync with |known_galleries_|, so it should exist in | |
| 194 // |known_galleries_| as well. User selecting a known gallery effectively | |
| 195 // just sets the gallery to permitted. | |
| 179 KnownGalleryPermissions::const_iterator iter = | 196 KnownGalleryPermissions::const_iterator iter = |
| 180 known_galleries_.find(gallery.pref_id); | 197 known_galleries_.find(gallery.pref_id); |
| 181 | 198 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); | 199 dialog_->UpdateGallery(&iter->second.pref_info, true); |
| 190 return; | 200 return; |
| 191 } | 201 } |
| 192 | 202 |
| 193 // Try to find it in |new_galleries_| (user added same folder twice). | 203 // Try to find it in |new_galleries_| (user added same folder twice). |
| 194 for (NewGalleryPermissions::iterator iter = new_galleries_.begin(); | 204 for (NewGalleryPermissions::iterator iter = new_galleries_.begin(); |
| 195 iter != new_galleries_.end(); ++iter) { | 205 iter != new_galleries_.end(); ++iter) { |
| 196 if (iter->pref_info.path == gallery.path && | 206 if (iter->pref_info.path == gallery.path && |
| 197 iter->pref_info.device_id == gallery.device_id) { | 207 iter->pref_info.device_id == gallery.device_id) { |
| 198 iter->allowed = true; | 208 iter->allowed = true; |
| 199 dialog_->UpdateGallery(&iter->pref_info, true); | 209 dialog_->UpdateGallery(&iter->pref_info, true); |
| 200 return; | 210 return; |
| 201 } | 211 } |
| 202 } | 212 } |
| 203 | 213 |
| 204 // Lastly, add it to |new_galleries_|. | 214 // Lastly, add a new gallery to |new_galleries_|. |
| 205 new_galleries_.push_back(GalleryPermission(gallery, true)); | 215 new_galleries_.push_back(GalleryPermission(gallery, true)); |
| 206 dialog_->UpdateGallery(&new_galleries_.back().pref_info, true); | 216 dialog_->UpdateGallery(&new_galleries_.back().pref_info, true); |
| 207 } | 217 } |
| 208 | 218 |
| 209 void MediaGalleriesDialogController::OnRemovableStorageAttached( | 219 void MediaGalleriesDialogController::OnRemovableStorageAttached( |
| 210 const RemovableStorageNotifications::StorageInfo& info) { | 220 const RemovableStorageNotifications::StorageInfo& info) { |
| 211 UpdateGalleriesOnDeviceEvent(info.device_id); | 221 UpdateGalleriesOnDeviceEvent(info.device_id); |
| 212 } | 222 } |
| 213 | 223 |
| 214 void MediaGalleriesDialogController::OnRemovableStorageDetached( | 224 void MediaGalleriesDialogController::OnRemovableStorageDetached( |
| 215 const RemovableStorageNotifications::StorageInfo& info) { | 225 const RemovableStorageNotifications::StorageInfo& info) { |
| 216 UpdateGalleriesOnDeviceEvent(info.device_id); | 226 UpdateGalleriesOnDeviceEvent(info.device_id); |
| 217 } | 227 } |
| 218 | 228 |
| 229 void MediaGalleriesDialogController::OnGalleryChanged( | |
| 230 MediaGalleriesPreferences* pref, const std::string& extension_id) { | |
| 231 DCHECK_EQ(preferences_, pref); | |
| 232 if (extension_id.empty() || extension_id == extension_->id()) | |
|
Greg Billock
2013/02/08 20:09:25
Will this check end up being useful? That is, it s
| |
| 233 UpdateGalleriesOnPreferencesEvent(); | |
| 234 } | |
| 235 | |
| 219 void MediaGalleriesDialogController::InitializePermissions() { | 236 void MediaGalleriesDialogController::InitializePermissions() { |
| 220 const MediaGalleriesPrefInfoMap& galleries = preferences_->known_galleries(); | 237 const MediaGalleriesPrefInfoMap& galleries = preferences_->known_galleries(); |
| 221 for (MediaGalleriesPrefInfoMap::const_iterator iter = galleries.begin(); | 238 for (MediaGalleriesPrefInfoMap::const_iterator iter = galleries.begin(); |
| 222 iter != galleries.end(); | 239 iter != galleries.end(); |
| 223 ++iter) { | 240 ++iter) { |
| 224 const MediaGalleryPrefInfo& gallery = iter->second; | 241 const MediaGalleryPrefInfo& gallery = iter->second; |
| 225 if (gallery.type == MediaGalleryPrefInfo::kBlackListed) | 242 if (gallery.type == MediaGalleryPrefInfo::kBlackListed) |
| 226 continue; | 243 continue; |
| 227 | 244 |
| 228 known_galleries_[iter->first] = GalleryPermission(gallery, false); | 245 known_galleries_[iter->first] = GalleryPermission(gallery, false); |
| 229 } | 246 } |
| 230 | 247 |
| 231 MediaGalleryPrefIdSet permitted = | 248 MediaGalleryPrefIdSet permitted = |
| 232 preferences_->GalleriesForExtension(*extension_); | 249 preferences_->GalleriesForExtension(*extension_); |
| 233 | 250 |
| 234 for (MediaGalleryPrefIdSet::iterator iter = permitted.begin(); | 251 for (MediaGalleryPrefIdSet::iterator iter = permitted.begin(); |
| 235 iter != permitted.end(); ++iter) { | 252 iter != permitted.end(); ++iter) { |
| 253 if (ContainsKey(toggled_galleries_, *iter)) | |
| 254 continue; | |
| 255 DCHECK(ContainsKey(known_galleries_, *iter)); | |
| 236 known_galleries_[*iter].allowed = true; | 256 known_galleries_[*iter].allowed = true; |
| 237 } | 257 } |
| 238 } | 258 } |
| 239 | 259 |
| 240 void MediaGalleriesDialogController::SavePermissions() { | 260 void MediaGalleriesDialogController::SavePermissions() { |
| 241 for (KnownGalleryPermissions::const_iterator iter = known_galleries_.begin(); | 261 for (KnownGalleryPermissions::const_iterator iter = known_galleries_.begin(); |
| 242 iter != known_galleries_.end(); ++iter) { | 262 iter != known_galleries_.end(); ++iter) { |
| 243 preferences_->SetGalleryPermissionForExtension( | 263 preferences_->SetGalleryPermissionForExtension( |
| 244 *extension_, iter->first, iter->second.allowed); | 264 *extension_, iter->first, iter->second.allowed); |
| 245 } | 265 } |
| 246 | 266 |
| 247 for (NewGalleryPermissions::const_iterator iter = new_galleries_.begin(); | 267 for (NewGalleryPermissions::const_iterator iter = new_galleries_.begin(); |
| 248 iter != new_galleries_.end(); ++iter) { | 268 iter != new_galleries_.end(); ++iter) { |
| 249 // If the user added a gallery then unchecked it, forget about it. | 269 // If the user added a gallery then unchecked it, forget about it. |
| 250 if (!iter->allowed) | 270 if (!iter->allowed) |
| 251 continue; | 271 continue; |
| 252 | 272 |
| 253 const MediaGalleryPrefInfo& gallery = iter->pref_info; | 273 const MediaGalleryPrefInfo& gallery = iter->pref_info; |
| 254 MediaGalleryPrefId id = preferences_->AddGallery( | 274 MediaGalleryPrefId id = preferences_->AddGallery( |
| 255 gallery.device_id, gallery.display_name, gallery.path, true); | 275 gallery.device_id, gallery.display_name, gallery.path, true); |
| 256 preferences_->SetGalleryPermissionForExtension( | 276 preferences_->SetGalleryPermissionForExtension(*extension_, id, true); |
| 257 *extension_, id, true); | 277 } |
| 278 } | |
| 279 | |
| 280 void MediaGalleriesDialogController::UpdateGalleriesOnPreferencesEvent() { | |
| 281 // Merge in the permissions from |preferences_|. Afterwards, | |
| 282 // |known_galleries_| may contain galleries that no longer belong there, | |
| 283 // but the code below will put |known_galleries_| back in a consistent state. | |
| 284 InitializePermissions(); | |
|
Greg Billock
2013/02/08 20:09:25
Could we basically just re-initialize here, cleari
Lei Zhang
2013/02/08 21:02:12
The dialog UI code has raw pointers to the Gallery
| |
| 285 | |
| 286 // If a gallery no longer belongs in |known_galleries_|, forget it in the | |
| 287 // model/view. | |
| 288 // If a gallery still belong in |known_galleries_|, check for a duplicate | |
| 289 // entry in |new_galleries_|, merge its permission and remove it. Then update | |
| 290 // the view. | |
| 291 const MediaGalleriesPrefInfoMap& pref_galleries = | |
| 292 preferences_->known_galleries(); | |
| 293 MediaGalleryPrefIdSet galleries_to_forget; | |
| 294 for (KnownGalleryPermissions::iterator it = known_galleries_.begin(); | |
| 295 it != known_galleries_.end(); | |
| 296 ++it) { | |
| 297 const MediaGalleryPrefId& gallery_id = it->first; | |
| 298 GalleryPermission& gallery = it->second; | |
| 299 MediaGalleriesPrefInfoMap::const_iterator pref_it = | |
| 300 pref_galleries.find(gallery_id); | |
| 301 // Check for lingering entry that should be removed. | |
| 302 if (pref_it == pref_galleries.end() || | |
| 303 pref_it->second.type == MediaGalleryPrefInfo::kBlackListed) { | |
| 304 galleries_to_forget.insert(gallery_id); | |
| 305 dialog_->ForgetGallery(&gallery.pref_info); | |
| 306 continue; | |
| 307 } | |
| 308 | |
| 309 // Look for duplicate entries in |new_galleries_|. | |
| 310 for (NewGalleryPermissions::iterator new_it = new_galleries_.begin(); | |
| 311 new_it != new_galleries_.end(); | |
| 312 ++new_it) { | |
| 313 if (new_it->pref_info.path == gallery.pref_info.path && | |
| 314 new_it->pref_info.device_id == gallery.pref_info.device_id) { | |
| 315 // Found duplicate entry. Get the existing permission from it and then | |
| 316 // remove it. | |
| 317 gallery.allowed = new_it->allowed; | |
| 318 dialog_->ForgetGallery(&new_it->pref_info); | |
| 319 new_galleries_.erase(new_it); | |
| 320 break; | |
| 321 } | |
| 322 } | |
| 323 dialog_->UpdateGallery(&gallery.pref_info, gallery.allowed); | |
| 324 } | |
| 325 | |
| 326 // Remove the galleries to forget from |known_galleries_|. Doing it in the | |
| 327 // above loop would invalidate the iterator there. | |
| 328 for (MediaGalleryPrefIdSet::const_iterator it = galleries_to_forget.begin(); | |
| 329 it != galleries_to_forget.end(); | |
| 330 ++it) { | |
| 331 known_galleries_.erase(*it); | |
| 258 } | 332 } |
| 259 } | 333 } |
| 260 | 334 |
| 261 void MediaGalleriesDialogController::UpdateGalleriesOnDeviceEvent( | 335 void MediaGalleriesDialogController::UpdateGalleriesOnDeviceEvent( |
| 262 const std::string& device_id) { | 336 const std::string& device_id) { |
| 263 for (KnownGalleryPermissions::iterator iter = known_galleries_.begin(); | 337 for (KnownGalleryPermissions::iterator iter = known_galleries_.begin(); |
| 264 iter != known_galleries_.end(); ++iter) { | 338 iter != known_galleries_.end(); ++iter) { |
| 265 if (iter->second.pref_info.device_id == device_id) | 339 if (iter->second.pref_info.device_id == device_id) |
| 266 dialog_->UpdateGallery(&iter->second.pref_info, iter->second.allowed); | 340 dialog_->UpdateGallery(&iter->second.pref_info, iter->second.allowed); |
| 267 } | 341 } |
| 268 | 342 |
| 269 for (NewGalleryPermissions::iterator iter = new_galleries_.begin(); | 343 for (NewGalleryPermissions::iterator iter = new_galleries_.begin(); |
| 270 iter != new_galleries_.end(); ++iter) { | 344 iter != new_galleries_.end(); ++iter) { |
| 271 if (iter->pref_info.device_id == device_id) | 345 if (iter->pref_info.device_id == device_id) |
| 272 dialog_->UpdateGallery(&iter->pref_info, iter->allowed); | 346 dialog_->UpdateGallery(&iter->pref_info, iter->allowed); |
| 273 } | 347 } |
| 274 } | 348 } |
| 275 | 349 |
| 276 // MediaGalleries dialog ------------------------------------------------------- | 350 // MediaGalleries dialog ------------------------------------------------------- |
| 277 | 351 |
| 278 MediaGalleriesDialog::~MediaGalleriesDialog() {} | 352 MediaGalleriesDialog::~MediaGalleriesDialog() {} |
| 279 | 353 |
| 280 } // namespace chrome | 354 } // namespace chrome |
| OLD | NEW |