| 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_galleries/media_galleries_dialog_controller.h" | 5 #include "chrome/browser/media_galleries/media_galleries_permission_controller.h
" |
| 6 | 6 |
| 7 #include "base/base_paths.h" | 7 #include "base/base_paths.h" |
| 8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "chrome/browser/browser_process.h" | 11 #include "chrome/browser/browser_process.h" |
| 12 #include "chrome/browser/extensions/api/file_system/file_system_api.h" | 12 #include "chrome/browser/extensions/api/file_system/file_system_api.h" |
| 13 #include "chrome/browser/media_galleries/media_file_system_registry.h" | 13 #include "chrome/browser/media_galleries/media_file_system_registry.h" |
| 14 #include "chrome/browser/media_galleries/media_galleries_histograms.h" | 14 #include "chrome/browser/media_galleries/media_galleries_histograms.h" |
| 15 #include "chrome/browser/media_galleries/media_gallery_context_menu.h" | 15 #include "chrome/browser/media_galleries/media_gallery_context_menu.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 28 #include "ui/base/text/bytes_formatting.h" | 28 #include "ui/base/text/bytes_formatting.h" |
| 29 | 29 |
| 30 using extensions::APIPermission; | 30 using extensions::APIPermission; |
| 31 using extensions::Extension; | 31 using extensions::Extension; |
| 32 using storage_monitor::StorageInfo; | 32 using storage_monitor::StorageInfo; |
| 33 using storage_monitor::StorageMonitor; | 33 using storage_monitor::StorageMonitor; |
| 34 | 34 |
| 35 namespace { | 35 namespace { |
| 36 | 36 |
| 37 // Comparator for sorting GalleryPermissionsVector -- sorts | 37 // Comparator for sorting GalleryPermissionsVector -- sorts |
| 38 // allowed galleries low, and then sorts by absolute path. | 38 // selected galleries low, and then sorts by absolute path. |
| 39 bool GalleriesVectorComparator( | 39 bool GalleriesVectorComparator( |
| 40 const MediaGalleriesDialogController::GalleryPermission& a, | 40 const MediaGalleriesDialogController::Entry& a, |
| 41 const MediaGalleriesDialogController::GalleryPermission& b) { | 41 const MediaGalleriesDialogController::Entry& b) { |
| 42 if (a.allowed && !b.allowed) | 42 if (a.selected && !b.selected) |
| 43 return true; | 43 return true; |
| 44 if (!a.allowed && b.allowed) | 44 if (!a.selected && b.selected) |
| 45 return false; | 45 return false; |
| 46 | 46 |
| 47 return a.pref_info.AbsolutePath() < b.pref_info.AbsolutePath(); | 47 return a.pref_info.AbsolutePath() < b.pref_info.AbsolutePath(); |
| 48 } | 48 } |
| 49 | 49 |
| 50 } // namespace | 50 } // namespace |
| 51 | 51 |
| 52 MediaGalleriesDialogController::MediaGalleriesDialogController( | 52 MediaGalleriesPermissionController::MediaGalleriesPermissionController( |
| 53 content::WebContents* web_contents, | 53 content::WebContents* web_contents, |
| 54 const Extension& extension, | 54 const Extension& extension, |
| 55 const base::Closure& on_finish) | 55 const base::Closure& on_finish) |
| 56 : web_contents_(web_contents), | 56 : web_contents_(web_contents), |
| 57 extension_(&extension), | 57 extension_(&extension), |
| 58 on_finish_(on_finish), | 58 on_finish_(on_finish), |
| 59 preferences_( | 59 preferences_( |
| 60 g_browser_process->media_file_system_registry()->GetPreferences( | 60 g_browser_process->media_file_system_registry()->GetPreferences( |
| 61 GetProfile())), | 61 GetProfile())), |
| 62 create_dialog_callback_(base::Bind(&MediaGalleriesDialog::Create)) { | 62 create_dialog_callback_(base::Bind(&MediaGalleriesDialog::Create)) { |
| 63 // Passing unretained pointer is safe, since the dialog controller | 63 // Passing unretained pointer is safe, since the dialog controller |
| 64 // is self-deleting, and so won't be deleted until it can be shown | 64 // is self-deleting, and so won't be deleted until it can be shown |
| 65 // and then closed. | 65 // and then closed. |
| 66 preferences_->EnsureInitialized( | 66 preferences_->EnsureInitialized( |
| 67 base::Bind(&MediaGalleriesDialogController::OnPreferencesInitialized, | 67 base::Bind(&MediaGalleriesPermissionController::OnPreferencesInitialized, |
| 68 base::Unretained(this))); | 68 base::Unretained(this))); |
| 69 | 69 |
| 70 // Unretained is safe because |this| owns |context_menu_|. | 70 // Unretained is safe because |this| owns |context_menu_|. |
| 71 context_menu_.reset( | 71 context_menu_.reset( |
| 72 new MediaGalleryContextMenu( | 72 new MediaGalleryContextMenu( |
| 73 base::Bind(&MediaGalleriesDialogController::DidForgetGallery, | 73 base::Bind(&MediaGalleriesPermissionController::DidForgetEntry, |
| 74 base::Unretained(this)))); | 74 base::Unretained(this)))); |
| 75 } | 75 } |
| 76 | 76 |
| 77 void MediaGalleriesDialogController::OnPreferencesInitialized() { | 77 void MediaGalleriesPermissionController::OnPreferencesInitialized() { |
| 78 if (StorageMonitor::GetInstance()) | 78 if (StorageMonitor::GetInstance()) |
| 79 StorageMonitor::GetInstance()->AddObserver(this); | 79 StorageMonitor::GetInstance()->AddObserver(this); |
| 80 | 80 |
| 81 // |preferences_| may be NULL in tests. | 81 // |preferences_| may be NULL in tests. |
| 82 if (preferences_) { | 82 if (preferences_) { |
| 83 preferences_->AddGalleryChangeObserver(this); | 83 preferences_->AddGalleryChangeObserver(this); |
| 84 InitializePermissions(); | 84 InitializePermissions(); |
| 85 } | 85 } |
| 86 | 86 |
| 87 dialog_.reset(create_dialog_callback_.Run(this)); | 87 dialog_.reset(create_dialog_callback_.Run(this)); |
| 88 } | 88 } |
| 89 | 89 |
| 90 MediaGalleriesDialogController::MediaGalleriesDialogController( | 90 MediaGalleriesPermissionController::MediaGalleriesPermissionController( |
| 91 const extensions::Extension& extension, | 91 const extensions::Extension& extension, |
| 92 MediaGalleriesPreferences* preferences, | 92 MediaGalleriesPreferences* preferences, |
| 93 const CreateDialogCallback& create_dialog_callback, | 93 const CreateDialogCallback& create_dialog_callback, |
| 94 const base::Closure& on_finish) | 94 const base::Closure& on_finish) |
| 95 : web_contents_(NULL), | 95 : web_contents_(NULL), |
| 96 extension_(&extension), | 96 extension_(&extension), |
| 97 on_finish_(on_finish), | 97 on_finish_(on_finish), |
| 98 preferences_(preferences), | 98 preferences_(preferences), |
| 99 create_dialog_callback_(create_dialog_callback) { | 99 create_dialog_callback_(create_dialog_callback) { |
| 100 OnPreferencesInitialized(); | 100 OnPreferencesInitialized(); |
| 101 } | 101 } |
| 102 | 102 |
| 103 MediaGalleriesDialogController::~MediaGalleriesDialogController() { | 103 MediaGalleriesPermissionController::~MediaGalleriesPermissionController() { |
| 104 if (StorageMonitor::GetInstance()) | 104 if (StorageMonitor::GetInstance()) |
| 105 StorageMonitor::GetInstance()->RemoveObserver(this); | 105 StorageMonitor::GetInstance()->RemoveObserver(this); |
| 106 | 106 |
| 107 // |preferences_| may be NULL in tests. | 107 // |preferences_| may be NULL in tests. |
| 108 if (preferences_) | 108 if (preferences_) |
| 109 preferences_->RemoveGalleryChangeObserver(this); | 109 preferences_->RemoveGalleryChangeObserver(this); |
| 110 | 110 |
| 111 if (select_folder_dialog_.get()) | 111 if (select_folder_dialog_.get()) |
| 112 select_folder_dialog_->ListenerDestroyed(); | 112 select_folder_dialog_->ListenerDestroyed(); |
| 113 } | 113 } |
| 114 | 114 |
| 115 base::string16 MediaGalleriesDialogController::GetHeader() const { | 115 base::string16 MediaGalleriesPermissionController::GetHeader() const { |
| 116 return l10n_util::GetStringFUTF16(IDS_MEDIA_GALLERIES_DIALOG_HEADER, | 116 return l10n_util::GetStringFUTF16(IDS_MEDIA_GALLERIES_DIALOG_HEADER, |
| 117 base::UTF8ToUTF16(extension_->name())); | 117 base::UTF8ToUTF16(extension_->name())); |
| 118 } | 118 } |
| 119 | 119 |
| 120 base::string16 MediaGalleriesDialogController::GetSubtext() const { | 120 base::string16 MediaGalleriesPermissionController::GetSubtext() const { |
| 121 extensions::MediaGalleriesPermission::CheckParam copy_to_param( | 121 extensions::MediaGalleriesPermission::CheckParam copy_to_param( |
| 122 extensions::MediaGalleriesPermission::kCopyToPermission); | 122 extensions::MediaGalleriesPermission::kCopyToPermission); |
| 123 extensions::MediaGalleriesPermission::CheckParam delete_param( | 123 extensions::MediaGalleriesPermission::CheckParam delete_param( |
| 124 extensions::MediaGalleriesPermission::kDeletePermission); | 124 extensions::MediaGalleriesPermission::kDeletePermission); |
| 125 bool has_copy_to_permission = | 125 bool has_copy_to_permission = |
| 126 extensions::PermissionsData::CheckAPIPermissionWithParam( | 126 extensions::PermissionsData::CheckAPIPermissionWithParam( |
| 127 extension_, APIPermission::kMediaGalleries, ©_to_param); | 127 extension_, APIPermission::kMediaGalleries, ©_to_param); |
| 128 bool has_delete_permission = | 128 bool has_delete_permission = |
| 129 extensions::PermissionsData::CheckAPIPermissionWithParam( | 129 extensions::PermissionsData::CheckAPIPermissionWithParam( |
| 130 extension_, APIPermission::kMediaGalleries, &delete_param); | 130 extension_, APIPermission::kMediaGalleries, &delete_param); |
| 131 | 131 |
| 132 int id; | 132 int id; |
| 133 if (has_copy_to_permission) | 133 if (has_copy_to_permission) |
| 134 id = IDS_MEDIA_GALLERIES_DIALOG_SUBTEXT_READ_WRITE; | 134 id = IDS_MEDIA_GALLERIES_DIALOG_SUBTEXT_READ_WRITE; |
| 135 else if (has_delete_permission) | 135 else if (has_delete_permission) |
| 136 id = IDS_MEDIA_GALLERIES_DIALOG_SUBTEXT_READ_DELETE; | 136 id = IDS_MEDIA_GALLERIES_DIALOG_SUBTEXT_READ_DELETE; |
| 137 else | 137 else |
| 138 id = IDS_MEDIA_GALLERIES_DIALOG_SUBTEXT_READ_ONLY; | 138 id = IDS_MEDIA_GALLERIES_DIALOG_SUBTEXT_READ_ONLY; |
| 139 | 139 |
| 140 return l10n_util::GetStringFUTF16(id, base::UTF8ToUTF16(extension_->name())); | 140 return l10n_util::GetStringFUTF16(id, base::UTF8ToUTF16(extension_->name())); |
| 141 } | 141 } |
| 142 | 142 |
| 143 base::string16 MediaGalleriesDialogController::GetUnattachedLocationsHeader() | 143 bool MediaGalleriesPermissionController::IsAcceptAllowed() const { |
| 144 const { | |
| 145 return l10n_util::GetStringUTF16(IDS_MEDIA_GALLERIES_UNATTACHED_LOCATIONS); | |
| 146 } | |
| 147 | |
| 148 bool MediaGalleriesDialogController::IsAcceptAllowed() const { | |
| 149 if (!toggled_galleries_.empty() || !forgotten_galleries_.empty()) | 144 if (!toggled_galleries_.empty() || !forgotten_galleries_.empty()) |
| 150 return true; | 145 return true; |
| 151 | 146 |
| 152 for (GalleryPermissionsMap::const_iterator iter = new_galleries_.begin(); | 147 for (GalleryPermissionsMap::const_iterator iter = new_galleries_.begin(); |
| 153 iter != new_galleries_.end(); | 148 iter != new_galleries_.end(); |
| 154 ++iter) { | 149 ++iter) { |
| 155 if (iter->second.allowed) | 150 if (iter->second.selected) |
| 156 return true; | 151 return true; |
| 157 } | 152 } |
| 158 | 153 |
| 159 return false; | 154 return false; |
| 160 } | 155 } |
| 161 | 156 |
| 157 bool MediaGalleriesPermissionController::ShouldShowFolderViewer( |
| 158 const Entry& entry) const { |
| 159 return false; |
| 160 } |
| 161 |
| 162 std::vector<base::string16> |
| 163 MediaGalleriesPermissionController::GetSectionHeaders() const { |
| 164 std::vector<base::string16> result; |
| 165 result.push_back(base::string16()); // First section has no header. |
| 166 result.push_back( |
| 167 l10n_util::GetStringUTF16(IDS_MEDIA_GALLERIES_UNATTACHED_LOCATIONS)); |
| 168 return result; |
| 169 } |
| 170 |
| 162 // Note: sorts by display criterion: GalleriesVectorComparator. | 171 // Note: sorts by display criterion: GalleriesVectorComparator. |
| 163 MediaGalleriesDialogController::GalleryPermissionsVector | 172 MediaGalleriesDialogController::Entries |
| 164 MediaGalleriesDialogController::FillPermissions(bool attached) const { | 173 MediaGalleriesPermissionController::GetSectionEntries(size_t index) const { |
| 165 GalleryPermissionsVector result; | 174 DCHECK_GT(2U, index); // This dialog only has two sections. |
| 175 |
| 176 // TODO(vandebo): Change this to be permitted/non-permitted instead of |
| 177 // attached/non-attached. |
| 178 bool attached = !index; |
| 179 MediaGalleriesDialogController::Entries result; |
| 166 for (GalleryPermissionsMap::const_iterator iter = known_galleries_.begin(); | 180 for (GalleryPermissionsMap::const_iterator iter = known_galleries_.begin(); |
| 167 iter != known_galleries_.end(); ++iter) { | 181 iter != known_galleries_.end(); ++iter) { |
| 168 if (!ContainsKey(forgotten_galleries_, iter->first) && | 182 if (!ContainsKey(forgotten_galleries_, iter->first) && |
| 169 attached == iter->second.pref_info.IsGalleryAvailable()) { | 183 attached == iter->second.pref_info.IsGalleryAvailable()) { |
| 170 result.push_back(iter->second); | 184 result.push_back(iter->second); |
| 171 } | 185 } |
| 172 } | 186 } |
| 173 for (GalleryPermissionsMap::const_iterator iter = new_galleries_.begin(); | 187 for (GalleryPermissionsMap::const_iterator iter = new_galleries_.begin(); |
| 174 iter != new_galleries_.end(); ++iter) { | 188 iter != new_galleries_.end(); ++iter) { |
| 175 if (attached == iter->second.pref_info.IsGalleryAvailable()) { | 189 if (attached == iter->second.pref_info.IsGalleryAvailable()) { |
| 176 result.push_back(iter->second); | 190 result.push_back(iter->second); |
| 177 } | 191 } |
| 178 } | 192 } |
| 179 | 193 |
| 180 std::sort(result.begin(), result.end(), GalleriesVectorComparator); | 194 std::sort(result.begin(), result.end(), GalleriesVectorComparator); |
| 181 return result; | 195 return result; |
| 182 } | 196 } |
| 183 | 197 |
| 184 MediaGalleriesDialogController::GalleryPermissionsVector | 198 base::string16 |
| 185 MediaGalleriesDialogController::AttachedPermissions() const { | 199 MediaGalleriesPermissionController::GetAuxiliaryButtonText() const { |
| 186 return FillPermissions(true); | 200 return l10n_util::GetStringUTF16(IDS_MEDIA_GALLERIES_DIALOG_ADD_GALLERY); |
| 187 } | 201 } |
| 188 | 202 |
| 189 MediaGalleriesDialogController::GalleryPermissionsVector | 203 // This is the 'Add Folder' button. |
| 190 MediaGalleriesDialogController::UnattachedPermissions() const { | 204 void MediaGalleriesPermissionController::DidClickAuxiliaryButton() { |
| 191 return FillPermissions(false); | |
| 192 } | |
| 193 | |
| 194 void MediaGalleriesDialogController::OnAddFolderClicked() { | |
| 195 base::FilePath default_path = | 205 base::FilePath default_path = |
| 196 extensions::file_system_api::GetLastChooseEntryDirectory( | 206 extensions::file_system_api::GetLastChooseEntryDirectory( |
| 197 extensions::ExtensionPrefs::Get(GetProfile()), extension_->id()); | 207 extensions::ExtensionPrefs::Get(GetProfile()), extension_->id()); |
| 198 if (default_path.empty()) | 208 if (default_path.empty()) |
| 199 PathService::Get(base::DIR_USER_DESKTOP, &default_path); | 209 PathService::Get(base::DIR_USER_DESKTOP, &default_path); |
| 200 select_folder_dialog_ = | 210 select_folder_dialog_ = |
| 201 ui::SelectFileDialog::Create(this, new ChromeSelectFilePolicy(NULL)); | 211 ui::SelectFileDialog::Create(this, new ChromeSelectFilePolicy(NULL)); |
| 202 select_folder_dialog_->SelectFile( | 212 select_folder_dialog_->SelectFile( |
| 203 ui::SelectFileDialog::SELECT_FOLDER, | 213 ui::SelectFileDialog::SELECT_FOLDER, |
| 204 l10n_util::GetStringUTF16(IDS_MEDIA_GALLERIES_DIALOG_ADD_GALLERY_TITLE), | 214 l10n_util::GetStringUTF16(IDS_MEDIA_GALLERIES_DIALOG_ADD_GALLERY_TITLE), |
| 205 default_path, | 215 default_path, |
| 206 NULL, | 216 NULL, |
| 207 0, | 217 0, |
| 208 base::FilePath::StringType(), | 218 base::FilePath::StringType(), |
| 209 web_contents_->GetTopLevelNativeWindow(), | 219 web_contents_->GetTopLevelNativeWindow(), |
| 210 NULL); | 220 NULL); |
| 211 } | 221 } |
| 212 | 222 |
| 213 void MediaGalleriesDialogController::DidToggleGallery( | 223 void MediaGalleriesPermissionController::DidToggleEntry( |
| 214 GalleryDialogId gallery_id, bool enabled) { | 224 GalleryDialogId gallery_id, bool selected) { |
| 215 // Check known galleries. | 225 // Check known galleries. |
| 216 GalleryPermissionsMap::iterator iter = known_galleries_.find(gallery_id); | 226 GalleryPermissionsMap::iterator iter = known_galleries_.find(gallery_id); |
| 217 if (iter != known_galleries_.end()) { | 227 if (iter != known_galleries_.end()) { |
| 218 if (iter->second.allowed == enabled) | 228 if (iter->second.selected == selected) |
| 219 return; | 229 return; |
| 220 | 230 |
| 221 iter->second.allowed = enabled; | 231 iter->second.selected = selected; |
| 222 if (ContainsKey(toggled_galleries_, gallery_id)) | 232 if (ContainsKey(toggled_galleries_, gallery_id)) |
| 223 toggled_galleries_.erase(gallery_id); | 233 toggled_galleries_.erase(gallery_id); |
| 224 else | 234 else |
| 225 toggled_galleries_.insert(gallery_id); | 235 toggled_galleries_.insert(gallery_id); |
| 226 return; | 236 return; |
| 227 } | 237 } |
| 228 | 238 |
| 229 iter = new_galleries_.find(gallery_id); | 239 iter = new_galleries_.find(gallery_id); |
| 230 if (iter != new_galleries_.end()) | 240 if (iter != new_galleries_.end()) |
| 231 iter->second.allowed = enabled; | 241 iter->second.selected = selected; |
| 232 | 242 |
| 233 // Don't sort -- the dialog is open, and we don't want to adjust any | 243 // Don't sort -- the dialog is open, and we don't want to adjust any |
| 234 // positions for future updates to the dialog contents until they are | 244 // positions for future updates to the dialog contents until they are |
| 235 // redrawn. | 245 // redrawn. |
| 236 } | 246 } |
| 237 | 247 |
| 238 void MediaGalleriesDialogController::DidForgetGallery( | 248 void MediaGalleriesPermissionController::DidClickOpenFolderViewer( |
| 249 GalleryDialogId gallery_id) { |
| 250 NOTREACHED(); |
| 251 } |
| 252 |
| 253 void MediaGalleriesPermissionController::DidForgetEntry( |
| 239 GalleryDialogId gallery_id) { | 254 GalleryDialogId gallery_id) { |
| 240 media_galleries::UsageCount(media_galleries::DIALOG_FORGET_GALLERY); | 255 media_galleries::UsageCount(media_galleries::DIALOG_FORGET_GALLERY); |
| 241 if (!new_galleries_.erase(gallery_id)) { | 256 if (!new_galleries_.erase(gallery_id)) { |
| 242 DCHECK(ContainsKey(known_galleries_, gallery_id)); | 257 DCHECK(ContainsKey(known_galleries_, gallery_id)); |
| 243 forgotten_galleries_.insert(gallery_id); | 258 forgotten_galleries_.insert(gallery_id); |
| 244 } | 259 } |
| 245 dialog_->UpdateGalleries(); | 260 dialog_->UpdateGalleries(); |
| 246 } | 261 } |
| 247 | 262 |
| 248 void MediaGalleriesDialogController::DialogFinished(bool accepted) { | 263 base::string16 MediaGalleriesPermissionController::GetAcceptButtonText() const { |
| 264 return l10n_util::GetStringUTF16(IDS_MEDIA_GALLERIES_DIALOG_CONFIRM); |
| 265 } |
| 266 |
| 267 void MediaGalleriesPermissionController::DialogFinished(bool accepted) { |
| 249 // The dialog has finished, so there is no need to watch for more updates | 268 // The dialog has finished, so there is no need to watch for more updates |
| 250 // from |preferences_|. | 269 // from |preferences_|. |
| 251 // |preferences_| may be NULL in tests. | 270 // |preferences_| may be NULL in tests. |
| 252 if (preferences_) | 271 if (preferences_) |
| 253 preferences_->RemoveGalleryChangeObserver(this); | 272 preferences_->RemoveGalleryChangeObserver(this); |
| 254 | 273 |
| 255 if (accepted) | 274 if (accepted) |
| 256 SavePermissions(); | 275 SavePermissions(); |
| 257 | 276 |
| 258 on_finish_.Run(); | 277 on_finish_.Run(); |
| 259 | 278 |
| 260 delete this; | 279 delete this; |
| 261 } | 280 } |
| 262 | 281 |
| 263 content::WebContents* MediaGalleriesDialogController::web_contents() { | 282 content::WebContents* MediaGalleriesPermissionController::WebContents() { |
| 264 return web_contents_; | 283 return web_contents_; |
| 265 } | 284 } |
| 266 | 285 |
| 267 void MediaGalleriesDialogController::FileSelected(const base::FilePath& path, | 286 void MediaGalleriesPermissionController::FileSelected( |
| 268 int /*index*/, | 287 const base::FilePath& path, |
| 269 void* /*params*/) { | 288 int /*index*/, |
| 289 void* /*params*/) { |
| 270 extensions::file_system_api::SetLastChooseEntryDirectory( | 290 extensions::file_system_api::SetLastChooseEntryDirectory( |
| 271 extensions::ExtensionPrefs::Get(GetProfile()), | 291 extensions::ExtensionPrefs::Get(GetProfile()), |
| 272 extension_->id(), | 292 extension_->id(), |
| 273 path); | 293 path); |
| 274 | 294 |
| 275 // Try to find it in the prefs. | 295 // Try to find it in the prefs. |
| 276 MediaGalleryPrefInfo gallery; | 296 MediaGalleryPrefInfo gallery; |
| 277 DCHECK(preferences_); | 297 DCHECK(preferences_); |
| 278 bool gallery_exists = preferences_->LookUpGalleryByPath(path, &gallery); | 298 bool gallery_exists = preferences_->LookUpGalleryByPath(path, &gallery); |
| 279 if (gallery_exists && !gallery.IsBlackListedType()) { | 299 if (gallery_exists && !gallery.IsBlackListedType()) { |
| 280 // The prefs are in sync with |known_galleries_|, so it should exist in | 300 // The prefs are in sync with |known_galleries_|, so it should exist in |
| 281 // |known_galleries_| as well. User selecting a known gallery effectively | 301 // |known_galleries_| as well. User selecting a known gallery effectively |
| 282 // just sets the gallery to permitted. | 302 // just sets the gallery to permitted. |
| 283 GalleryDialogId gallery_id = GetDialogId(gallery.pref_id); | 303 GalleryDialogId gallery_id = GetDialogId(gallery.pref_id); |
| 284 GalleryPermissionsMap::iterator iter = known_galleries_.find(gallery_id); | 304 GalleryPermissionsMap::iterator iter = known_galleries_.find(gallery_id); |
| 285 DCHECK(iter != known_galleries_.end()); | 305 DCHECK(iter != known_galleries_.end()); |
| 286 iter->second.allowed = true; | 306 iter->second.selected = true; |
| 287 forgotten_galleries_.erase(gallery_id); | 307 forgotten_galleries_.erase(gallery_id); |
| 288 dialog_->UpdateGalleries(); | 308 dialog_->UpdateGalleries(); |
| 289 return; | 309 return; |
| 290 } | 310 } |
| 291 | 311 |
| 292 // Try to find it in |new_galleries_| (user added same folder twice). | 312 // Try to find it in |new_galleries_| (user added same folder twice). |
| 293 for (GalleryPermissionsMap::iterator iter = new_galleries_.begin(); | 313 for (GalleryPermissionsMap::iterator iter = new_galleries_.begin(); |
| 294 iter != new_galleries_.end(); ++iter) { | 314 iter != new_galleries_.end(); ++iter) { |
| 295 if (iter->second.pref_info.path == gallery.path && | 315 if (iter->second.pref_info.path == gallery.path && |
| 296 iter->second.pref_info.device_id == gallery.device_id) { | 316 iter->second.pref_info.device_id == gallery.device_id) { |
| 297 iter->second.allowed = true; | 317 iter->second.selected = true; |
| 298 dialog_->UpdateGalleries(); | 318 dialog_->UpdateGalleries(); |
| 299 return; | 319 return; |
| 300 } | 320 } |
| 301 } | 321 } |
| 302 | 322 |
| 303 // Lastly, if not found, add a new gallery to |new_galleries_|. | 323 // Lastly, if not found, add a new gallery to |new_galleries_|. |
| 304 // Note that it will have prefId = kInvalidMediaGalleryPrefId. | 324 DCHECK_EQ(kInvalidMediaGalleryPrefId, gallery.pref_id); |
| 305 GalleryDialogId gallery_id = GetDialogId(gallery.pref_id); | 325 gallery.pref_id = GetDialogId(kInvalidMediaGalleryPrefId); |
| 306 new_galleries_[gallery_id] = GalleryPermission(gallery_id, gallery, true); | 326 new_galleries_[gallery.pref_id] = Entry(gallery, true); |
| 307 dialog_->UpdateGalleries(); | 327 dialog_->UpdateGalleries(); |
| 308 } | 328 } |
| 309 | 329 |
| 310 void MediaGalleriesDialogController::OnRemovableStorageAttached( | 330 void MediaGalleriesPermissionController::OnRemovableStorageAttached( |
| 311 const StorageInfo& info) { | 331 const StorageInfo& info) { |
| 312 UpdateGalleriesOnDeviceEvent(info.device_id()); | 332 UpdateGalleriesOnDeviceEvent(info.device_id()); |
| 313 } | 333 } |
| 314 | 334 |
| 315 void MediaGalleriesDialogController::OnRemovableStorageDetached( | 335 void MediaGalleriesPermissionController::OnRemovableStorageDetached( |
| 316 const StorageInfo& info) { | 336 const StorageInfo& info) { |
| 317 UpdateGalleriesOnDeviceEvent(info.device_id()); | 337 UpdateGalleriesOnDeviceEvent(info.device_id()); |
| 318 } | 338 } |
| 319 | 339 |
| 320 void MediaGalleriesDialogController::OnPermissionAdded( | 340 void MediaGalleriesPermissionController::OnPermissionAdded( |
| 321 MediaGalleriesPreferences* /* prefs */, | 341 MediaGalleriesPreferences* /* prefs */, |
| 322 const std::string& extension_id, | 342 const std::string& extension_id, |
| 323 MediaGalleryPrefId /* pref_id */) { | 343 MediaGalleryPrefId /* pref_id */) { |
| 324 if (extension_id != extension_->id()) | 344 if (extension_id != extension_->id()) |
| 325 return; | 345 return; |
| 326 UpdateGalleriesOnPreferencesEvent(); | 346 UpdateGalleriesOnPreferencesEvent(); |
| 327 } | 347 } |
| 328 | 348 |
| 329 void MediaGalleriesDialogController::OnPermissionRemoved( | 349 void MediaGalleriesPermissionController::OnPermissionRemoved( |
| 330 MediaGalleriesPreferences* /* prefs */, | 350 MediaGalleriesPreferences* /* prefs */, |
| 331 const std::string& extension_id, | 351 const std::string& extension_id, |
| 332 MediaGalleryPrefId /* pref_id */) { | 352 MediaGalleryPrefId /* pref_id */) { |
| 333 if (extension_id != extension_->id()) | 353 if (extension_id != extension_->id()) |
| 334 return; | 354 return; |
| 335 UpdateGalleriesOnPreferencesEvent(); | 355 UpdateGalleriesOnPreferencesEvent(); |
| 336 } | 356 } |
| 337 | 357 |
| 338 void MediaGalleriesDialogController::OnGalleryAdded( | 358 void MediaGalleriesPermissionController::OnGalleryAdded( |
| 339 MediaGalleriesPreferences* /* prefs */, | 359 MediaGalleriesPreferences* /* prefs */, |
| 340 MediaGalleryPrefId /* pref_id */) { | 360 MediaGalleryPrefId /* pref_id */) { |
| 341 UpdateGalleriesOnPreferencesEvent(); | 361 UpdateGalleriesOnPreferencesEvent(); |
| 342 } | 362 } |
| 343 | 363 |
| 344 void MediaGalleriesDialogController::OnGalleryRemoved( | 364 void MediaGalleriesPermissionController::OnGalleryRemoved( |
| 345 MediaGalleriesPreferences* /* prefs */, | 365 MediaGalleriesPreferences* /* prefs */, |
| 346 MediaGalleryPrefId /* pref_id */) { | 366 MediaGalleryPrefId /* pref_id */) { |
| 347 UpdateGalleriesOnPreferencesEvent(); | 367 UpdateGalleriesOnPreferencesEvent(); |
| 348 } | 368 } |
| 349 | 369 |
| 350 void MediaGalleriesDialogController::OnGalleryInfoUpdated( | 370 void MediaGalleriesPermissionController::OnGalleryInfoUpdated( |
| 351 MediaGalleriesPreferences* prefs, | 371 MediaGalleriesPreferences* prefs, |
| 352 MediaGalleryPrefId pref_id) { | 372 MediaGalleryPrefId pref_id) { |
| 353 DCHECK(preferences_); | 373 DCHECK(preferences_); |
| 354 const MediaGalleriesPrefInfoMap& pref_galleries = | 374 const MediaGalleriesPrefInfoMap& pref_galleries = |
| 355 preferences_->known_galleries(); | 375 preferences_->known_galleries(); |
| 356 MediaGalleriesPrefInfoMap::const_iterator pref_it = | 376 MediaGalleriesPrefInfoMap::const_iterator pref_it = |
| 357 pref_galleries.find(pref_id); | 377 pref_galleries.find(pref_id); |
| 358 if (pref_it == pref_galleries.end()) | 378 if (pref_it == pref_galleries.end()) |
| 359 return; | 379 return; |
| 360 const MediaGalleryPrefInfo& gallery_info = pref_it->second; | 380 const MediaGalleryPrefInfo& gallery_info = pref_it->second; |
| 361 UpdateGalleriesOnDeviceEvent(gallery_info.device_id); | 381 UpdateGalleriesOnDeviceEvent(gallery_info.device_id); |
| 362 } | 382 } |
| 363 | 383 |
| 364 void MediaGalleriesDialogController::InitializePermissions() { | 384 void MediaGalleriesPermissionController::InitializePermissions() { |
| 365 known_galleries_.clear(); | 385 known_galleries_.clear(); |
| 366 DCHECK(preferences_); | 386 DCHECK(preferences_); |
| 367 const MediaGalleriesPrefInfoMap& galleries = preferences_->known_galleries(); | 387 const MediaGalleriesPrefInfoMap& galleries = preferences_->known_galleries(); |
| 368 for (MediaGalleriesPrefInfoMap::const_iterator iter = galleries.begin(); | 388 for (MediaGalleriesPrefInfoMap::const_iterator iter = galleries.begin(); |
| 369 iter != galleries.end(); | 389 iter != galleries.end(); |
| 370 ++iter) { | 390 ++iter) { |
| 371 const MediaGalleryPrefInfo& gallery = iter->second; | 391 const MediaGalleryPrefInfo& gallery = iter->second; |
| 372 if (gallery.IsBlackListedType()) | 392 if (gallery.IsBlackListedType()) |
| 373 continue; | 393 continue; |
| 374 | 394 |
| 375 GalleryDialogId gallery_id = GetDialogId(gallery.pref_id); | 395 GalleryDialogId gallery_id = GetDialogId(gallery.pref_id); |
| 376 known_galleries_[gallery_id] = | 396 known_galleries_[gallery_id] = Entry(gallery, false); |
| 377 GalleryPermission(gallery_id, gallery, false); | 397 known_galleries_[gallery_id].pref_info.pref_id = gallery_id; |
| 378 } | 398 } |
| 379 | 399 |
| 380 MediaGalleryPrefIdSet permitted = | 400 MediaGalleryPrefIdSet permitted = |
| 381 preferences_->GalleriesForExtension(*extension_); | 401 preferences_->GalleriesForExtension(*extension_); |
| 382 | 402 |
| 383 for (MediaGalleryPrefIdSet::iterator iter = permitted.begin(); | 403 for (MediaGalleryPrefIdSet::iterator iter = permitted.begin(); |
| 384 iter != permitted.end(); ++iter) { | 404 iter != permitted.end(); ++iter) { |
| 385 GalleryDialogId gallery_id = GetDialogId(*iter); | 405 GalleryDialogId gallery_id = GetDialogId(*iter); |
| 386 if (ContainsKey(toggled_galleries_, gallery_id)) | 406 if (ContainsKey(toggled_galleries_, gallery_id)) |
| 387 continue; | 407 continue; |
| 388 DCHECK(ContainsKey(known_galleries_, gallery_id)); | 408 DCHECK(ContainsKey(known_galleries_, gallery_id)); |
| 389 known_galleries_[gallery_id].allowed = true; | 409 known_galleries_[gallery_id].selected = true; |
| 390 } | 410 } |
| 391 } | 411 } |
| 392 | 412 |
| 393 void MediaGalleriesDialogController::SavePermissions() { | 413 void MediaGalleriesPermissionController::SavePermissions() { |
| 394 DCHECK(preferences_); | 414 DCHECK(preferences_); |
| 395 media_galleries::UsageCount(media_galleries::SAVE_DIALOG); | 415 media_galleries::UsageCount(media_galleries::SAVE_DIALOG); |
| 396 for (GalleryPermissionsMap::const_iterator iter = known_galleries_.begin(); | 416 for (GalleryPermissionsMap::const_iterator iter = known_galleries_.begin(); |
| 397 iter != known_galleries_.end(); ++iter) { | 417 iter != known_galleries_.end(); ++iter) { |
| 398 MediaGalleryPrefId pref_id = iter->second.pref_info.pref_id; | 418 MediaGalleryPrefId pref_id = GetPrefId(iter->first); |
| 399 if (ContainsKey(forgotten_galleries_, iter->first)) { | 419 if (ContainsKey(forgotten_galleries_, iter->first)) { |
| 400 preferences_->ForgetGalleryById(pref_id); | 420 preferences_->ForgetGalleryById(pref_id); |
| 401 } else { | 421 } else { |
| 402 bool changed = preferences_->SetGalleryPermissionForExtension( | 422 bool changed = preferences_->SetGalleryPermissionForExtension( |
| 403 *extension_, pref_id, iter->second.allowed); | 423 *extension_, pref_id, iter->second.selected); |
| 404 if (changed) { | 424 if (changed) { |
| 405 if (iter->second.allowed) { | 425 if (iter->second.selected) { |
| 406 media_galleries::UsageCount( | 426 media_galleries::UsageCount( |
| 407 media_galleries::DIALOG_PERMISSION_ADDED); | 427 media_galleries::DIALOG_PERMISSION_ADDED); |
| 408 } else { | 428 } else { |
| 409 media_galleries::UsageCount( | 429 media_galleries::UsageCount( |
| 410 media_galleries::DIALOG_PERMISSION_REMOVED); | 430 media_galleries::DIALOG_PERMISSION_REMOVED); |
| 411 } | 431 } |
| 412 } | 432 } |
| 413 } | 433 } |
| 414 } | 434 } |
| 415 | 435 |
| 416 for (GalleryPermissionsMap::const_iterator iter = new_galleries_.begin(); | 436 for (GalleryPermissionsMap::const_iterator iter = new_galleries_.begin(); |
| 417 iter != new_galleries_.end(); ++iter) { | 437 iter != new_galleries_.end(); ++iter) { |
| 418 media_galleries::UsageCount(media_galleries::DIALOG_GALLERY_ADDED); | 438 media_galleries::UsageCount(media_galleries::DIALOG_GALLERY_ADDED); |
| 419 // If the user added a gallery then unchecked it, forget about it. | 439 // If the user added a gallery then unchecked it, forget about it. |
| 420 if (!iter->second.allowed) | 440 if (!iter->second.selected) |
| 421 continue; | 441 continue; |
| 422 | 442 |
| 423 const MediaGalleryPrefInfo& gallery = iter->second.pref_info; | 443 const MediaGalleryPrefInfo& gallery = iter->second.pref_info; |
| 424 MediaGalleryPrefId id = preferences_->AddGallery( | 444 MediaGalleryPrefId id = preferences_->AddGallery( |
| 425 gallery.device_id, gallery.path, MediaGalleryPrefInfo::kUserAdded, | 445 gallery.device_id, gallery.path, MediaGalleryPrefInfo::kUserAdded, |
| 426 gallery.volume_label, gallery.vendor_name, gallery.model_name, | 446 gallery.volume_label, gallery.vendor_name, gallery.model_name, |
| 427 gallery.total_size_in_bytes, gallery.last_attach_time, 0, 0, 0); | 447 gallery.total_size_in_bytes, gallery.last_attach_time, 0, 0, 0); |
| 428 preferences_->SetGalleryPermissionForExtension(*extension_, id, true); | 448 preferences_->SetGalleryPermissionForExtension(*extension_, id, true); |
| 429 } | 449 } |
| 430 } | 450 } |
| 431 | 451 |
| 432 void MediaGalleriesDialogController::UpdateGalleriesOnPreferencesEvent() { | 452 void MediaGalleriesPermissionController::UpdateGalleriesOnPreferencesEvent() { |
| 433 // Merge in the permissions from |preferences_|. Afterwards, | 453 // Merge in the permissions from |preferences_|. Afterwards, |
| 434 // |known_galleries_| may contain galleries that no longer belong there, | 454 // |known_galleries_| may contain galleries that no longer belong there, |
| 435 // but the code below will put |known_galleries_| back in a consistent state. | 455 // but the code below will put |known_galleries_| back in a consistent state. |
| 436 InitializePermissions(); | 456 InitializePermissions(); |
| 437 | 457 |
| 438 std::set<GalleryDialogId> new_galleries_to_remove; | 458 std::set<GalleryDialogId> new_galleries_to_remove; |
| 439 // Look for duplicate entries in |new_galleries_| in case one was added | 459 // Look for duplicate entries in |new_galleries_| in case one was added |
| 440 // in another dialog. | 460 // in another dialog. |
| 441 for (GalleryPermissionsMap::iterator it = known_galleries_.begin(); | 461 for (GalleryPermissionsMap::iterator it = known_galleries_.begin(); |
| 442 it != known_galleries_.end(); | 462 it != known_galleries_.end(); |
| 443 ++it) { | 463 ++it) { |
| 444 GalleryPermission& gallery = it->second; | 464 Entry& gallery = it->second; |
| 445 for (GalleryPermissionsMap::iterator new_it = new_galleries_.begin(); | 465 for (GalleryPermissionsMap::iterator new_it = new_galleries_.begin(); |
| 446 new_it != new_galleries_.end(); | 466 new_it != new_galleries_.end(); |
| 447 ++new_it) { | 467 ++new_it) { |
| 448 if (new_it->second.pref_info.path == gallery.pref_info.path && | 468 if (new_it->second.pref_info.path == gallery.pref_info.path && |
| 449 new_it->second.pref_info.device_id == gallery.pref_info.device_id) { | 469 new_it->second.pref_info.device_id == gallery.pref_info.device_id) { |
| 450 // Found duplicate entry. Get the existing permission from it and then | 470 // Found duplicate entry. Get the existing permission from it and then |
| 451 // remove it. | 471 // remove it. |
| 452 gallery.allowed = new_it->second.allowed; | 472 gallery.selected = new_it->second.selected; |
| 453 new_galleries_to_remove.insert(new_it->first); | 473 new_galleries_to_remove.insert(new_it->first); |
| 454 break; | 474 break; |
| 455 } | 475 } |
| 456 } | 476 } |
| 457 } | 477 } |
| 458 for (std::set<GalleryDialogId>::const_iterator it = | 478 for (std::set<GalleryDialogId>::const_iterator it = |
| 459 new_galleries_to_remove.begin(); | 479 new_galleries_to_remove.begin(); |
| 460 it != new_galleries_to_remove.end(); | 480 it != new_galleries_to_remove.end(); |
| 461 ++it) { | 481 ++it) { |
| 462 new_galleries_.erase(*it); | 482 new_galleries_.erase(*it); |
| 463 } | 483 } |
| 464 | 484 |
| 465 dialog_->UpdateGalleries(); | 485 dialog_->UpdateGalleries(); |
| 466 } | 486 } |
| 467 | 487 |
| 468 void MediaGalleriesDialogController::UpdateGalleriesOnDeviceEvent( | 488 void MediaGalleriesPermissionController::UpdateGalleriesOnDeviceEvent( |
| 469 const std::string& device_id) { | 489 const std::string& device_id) { |
| 470 dialog_->UpdateGalleries(); | 490 dialog_->UpdateGalleries(); |
| 471 } | 491 } |
| 472 | 492 |
| 473 ui::MenuModel* MediaGalleriesDialogController::GetContextMenu( | 493 ui::MenuModel* MediaGalleriesPermissionController::GetContextMenu( |
| 474 GalleryDialogId gallery_id) { | 494 GalleryDialogId gallery_id) { |
| 475 context_menu_->set_pref_id(gallery_id); | 495 context_menu_->set_pref_id(gallery_id); |
| 476 return context_menu_.get(); | 496 return context_menu_.get(); |
| 477 } | 497 } |
| 478 | 498 |
| 479 GalleryDialogId MediaGalleriesDialogController::GetDialogId( | 499 GalleryDialogId MediaGalleriesPermissionController::GetDialogId( |
| 480 MediaGalleryPrefId pref_id) { | 500 MediaGalleryPrefId pref_id) { |
| 481 return id_map_.GetDialogId(pref_id); | 501 return id_map_.GetDialogId(pref_id); |
| 482 } | 502 } |
| 483 | 503 |
| 484 Profile* MediaGalleriesDialogController::GetProfile() { | 504 MediaGalleryPrefId MediaGalleriesPermissionController::GetPrefId( |
| 505 GalleryDialogId id) { |
| 506 return id_map_.GetPrefId(id); |
| 507 } |
| 508 |
| 509 Profile* MediaGalleriesPermissionController::GetProfile() { |
| 485 return Profile::FromBrowserContext(web_contents_->GetBrowserContext()); | 510 return Profile::FromBrowserContext(web_contents_->GetBrowserContext()); |
| 486 } | 511 } |
| 487 | 512 |
| 488 MediaGalleriesDialogController::DialogIdMap::DialogIdMap() | 513 MediaGalleriesPermissionController::DialogIdMap::DialogIdMap() |
| 489 : next_dialog_id_(1) { | 514 : next_dialog_id_(1) { |
| 515 // Dialog id of 0 is invalid, so fill the slot. |
| 516 forward_mapping_.push_back(kInvalidMediaGalleryPrefId); |
| 490 } | 517 } |
| 491 | 518 |
| 492 MediaGalleriesDialogController::DialogIdMap::~DialogIdMap() { | 519 MediaGalleriesPermissionController::DialogIdMap::~DialogIdMap() { |
| 493 } | 520 } |
| 494 | 521 |
| 495 GalleryDialogId | 522 GalleryDialogId |
| 496 MediaGalleriesDialogController::DialogIdMap::GetDialogId( | 523 MediaGalleriesPermissionController::DialogIdMap::GetDialogId( |
| 497 MediaGalleryPrefId pref_id) { | 524 MediaGalleryPrefId pref_id) { |
| 498 std::map<GalleryDialogId, MediaGalleryPrefId>::const_iterator it = | 525 std::map<GalleryDialogId, MediaGalleryPrefId>::const_iterator it = |
| 499 mapping_.find(pref_id); | 526 back_map_.find(pref_id); |
| 500 if (it != mapping_.end()) | 527 if (it != back_map_.end()) |
| 501 return it->second; | 528 return it->second; |
| 502 | 529 |
| 503 GalleryDialogId result = next_dialog_id_++; | 530 GalleryDialogId result = next_dialog_id_++; |
| 531 DCHECK_EQ(result, forward_mapping_.size()); |
| 532 forward_mapping_.push_back(pref_id); |
| 504 if (pref_id != kInvalidMediaGalleryPrefId) | 533 if (pref_id != kInvalidMediaGalleryPrefId) |
| 505 mapping_[pref_id] = result; | 534 back_map_[pref_id] = result; |
| 506 return result; | 535 return result; |
| 507 } | 536 } |
| 508 | 537 |
| 538 MediaGalleryPrefId |
| 539 MediaGalleriesPermissionController::DialogIdMap::GetPrefId( |
| 540 GalleryDialogId id) { |
| 541 DCHECK_LT(id, next_dialog_id_); |
| 542 return forward_mapping_[id]; |
| 543 } |
| 544 |
| 509 // MediaGalleries dialog ------------------------------------------------------- | 545 // MediaGalleries dialog ------------------------------------------------------- |
| 510 | 546 |
| 511 MediaGalleriesDialog::~MediaGalleriesDialog() {} | 547 MediaGalleriesDialog::~MediaGalleriesDialog() {} |
| OLD | NEW |