| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_permission_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" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 #include "ui/base/models/simple_menu_model.h" | 27 #include "ui/base/models/simple_menu_model.h" |
| 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 gallery entries. Sort Removable entries above |
| 38 // selected galleries low, and then sorts by absolute path. | 38 // non-removable ones. Within those two groups, sort on media counts |
| 39 // if populated, otherwise on paths. |
| 39 bool GalleriesVectorComparator( | 40 bool GalleriesVectorComparator( |
| 40 const MediaGalleriesDialogController::Entry& a, | 41 const MediaGalleriesDialogController::Entry& a, |
| 41 const MediaGalleriesDialogController::Entry& b) { | 42 const MediaGalleriesDialogController::Entry& b) { |
| 42 if (a.selected && !b.selected) | 43 if (StorageInfo::IsRemovableDevice(a.pref_info.device_id) != |
| 43 return true; | 44 StorageInfo::IsRemovableDevice(b.pref_info.device_id)) { |
| 44 if (!a.selected && b.selected) | 45 return StorageInfo::IsRemovableDevice(a.pref_info.device_id); |
| 45 return false; | 46 } |
| 46 | 47 int a_media_count = a.pref_info.audio_count + a.pref_info.image_count + |
| 48 a.pref_info.video_count; |
| 49 int b_media_count = b.pref_info.audio_count + b.pref_info.image_count + |
| 50 b.pref_info.video_count; |
| 51 if (a_media_count != b_media_count) |
| 52 return a_media_count > b_media_count; |
| 47 return a.pref_info.AbsolutePath() < b.pref_info.AbsolutePath(); | 53 return a.pref_info.AbsolutePath() < b.pref_info.AbsolutePath(); |
| 48 } | 54 } |
| 49 | 55 |
| 50 } // namespace | 56 } // namespace |
| 51 | 57 |
| 52 MediaGalleriesPermissionController::MediaGalleriesPermissionController( | 58 MediaGalleriesPermissionController::MediaGalleriesPermissionController( |
| 53 content::WebContents* web_contents, | 59 content::WebContents* web_contents, |
| 54 const Extension& extension, | 60 const Extension& extension, |
| 55 const base::Closure& on_finish) | 61 const base::Closure& on_finish) |
| 56 : web_contents_(web_contents), | 62 : web_contents_(web_contents), |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 bool MediaGalleriesPermissionController::ShouldShowFolderViewer( | 163 bool MediaGalleriesPermissionController::ShouldShowFolderViewer( |
| 158 const Entry& entry) const { | 164 const Entry& entry) const { |
| 159 return false; | 165 return false; |
| 160 } | 166 } |
| 161 | 167 |
| 162 std::vector<base::string16> | 168 std::vector<base::string16> |
| 163 MediaGalleriesPermissionController::GetSectionHeaders() const { | 169 MediaGalleriesPermissionController::GetSectionHeaders() const { |
| 164 std::vector<base::string16> result; | 170 std::vector<base::string16> result; |
| 165 result.push_back(base::string16()); // First section has no header. | 171 result.push_back(base::string16()); // First section has no header. |
| 166 result.push_back( | 172 result.push_back( |
| 167 l10n_util::GetStringUTF16(IDS_MEDIA_GALLERIES_UNATTACHED_LOCATIONS)); | 173 l10n_util::GetStringUTF16(IDS_MEDIA_GALLERIES_PERMISSION_SUGGESTIONS)); |
| 168 return result; | 174 return result; |
| 169 } | 175 } |
| 170 | 176 |
| 171 // Note: sorts by display criterion: GalleriesVectorComparator. | 177 // Note: sorts by display criterion: GalleriesVectorComparator. |
| 172 MediaGalleriesDialogController::Entries | 178 MediaGalleriesDialogController::Entries |
| 173 MediaGalleriesPermissionController::GetSectionEntries(size_t index) const { | 179 MediaGalleriesPermissionController::GetSectionEntries(size_t index) const { |
| 174 DCHECK_GT(2U, index); // This dialog only has two sections. | 180 DCHECK_GT(2U, index); // This dialog only has two sections. |
| 175 | 181 |
| 176 // TODO(vandebo): Change this to be permitted/non-permitted instead of | 182 bool existing = !index; |
| 177 // attached/non-attached. | |
| 178 bool attached = !index; | |
| 179 MediaGalleriesDialogController::Entries result; | 183 MediaGalleriesDialogController::Entries result; |
| 180 for (GalleryPermissionsMap::const_iterator iter = known_galleries_.begin(); | 184 for (GalleryPermissionsMap::const_iterator iter = known_galleries_.begin(); |
| 181 iter != known_galleries_.end(); ++iter) { | 185 iter != known_galleries_.end(); ++iter) { |
| 186 MediaGalleryPrefId pref_id = GetPrefId(iter->first); |
| 182 if (!ContainsKey(forgotten_galleries_, iter->first) && | 187 if (!ContainsKey(forgotten_galleries_, iter->first) && |
| 183 attached == iter->second.pref_info.IsGalleryAvailable()) { | 188 existing == ContainsKey(pref_permitted_galleries_, pref_id)) { |
| 184 result.push_back(iter->second); | 189 result.push_back(iter->second); |
| 185 } | 190 } |
| 186 } | 191 } |
| 187 for (GalleryPermissionsMap::const_iterator iter = new_galleries_.begin(); | 192 if (existing) { |
| 188 iter != new_galleries_.end(); ++iter) { | 193 for (GalleryPermissionsMap::const_iterator iter = new_galleries_.begin(); |
| 189 if (attached == iter->second.pref_info.IsGalleryAvailable()) { | 194 iter != new_galleries_.end(); ++iter) { |
| 190 result.push_back(iter->second); | 195 result.push_back(iter->second); |
| 191 } | 196 } |
| 192 } | 197 } |
| 193 | 198 |
| 194 std::sort(result.begin(), result.end(), GalleriesVectorComparator); | 199 std::sort(result.begin(), result.end(), GalleriesVectorComparator); |
| 195 return result; | 200 return result; |
| 196 } | 201 } |
| 197 | 202 |
| 198 base::string16 | 203 base::string16 |
| 199 MediaGalleriesPermissionController::GetAuxiliaryButtonText() const { | 204 MediaGalleriesPermissionController::GetAuxiliaryButtonText() const { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 222 | 227 |
| 223 void MediaGalleriesPermissionController::DidToggleEntry( | 228 void MediaGalleriesPermissionController::DidToggleEntry( |
| 224 GalleryDialogId gallery_id, bool selected) { | 229 GalleryDialogId gallery_id, bool selected) { |
| 225 // Check known galleries. | 230 // Check known galleries. |
| 226 GalleryPermissionsMap::iterator iter = known_galleries_.find(gallery_id); | 231 GalleryPermissionsMap::iterator iter = known_galleries_.find(gallery_id); |
| 227 if (iter != known_galleries_.end()) { | 232 if (iter != known_galleries_.end()) { |
| 228 if (iter->second.selected == selected) | 233 if (iter->second.selected == selected) |
| 229 return; | 234 return; |
| 230 | 235 |
| 231 iter->second.selected = selected; | 236 iter->second.selected = selected; |
| 232 if (ContainsKey(toggled_galleries_, gallery_id)) | 237 toggled_galleries_[gallery_id] = selected; |
| 233 toggled_galleries_.erase(gallery_id); | |
| 234 else | |
| 235 toggled_galleries_.insert(gallery_id); | |
| 236 return; | 238 return; |
| 237 } | 239 } |
| 238 | 240 |
| 239 iter = new_galleries_.find(gallery_id); | 241 iter = new_galleries_.find(gallery_id); |
| 240 if (iter != new_galleries_.end()) | 242 if (iter != new_galleries_.end()) |
| 241 iter->second.selected = selected; | 243 iter->second.selected = selected; |
| 242 | 244 |
| 243 // Don't sort -- the dialog is open, and we don't want to adjust any | 245 // Don't sort -- the dialog is open, and we don't want to adjust any |
| 244 // positions for future updates to the dialog contents until they are | 246 // positions for future updates to the dialog contents until they are |
| 245 // redrawn. | 247 // redrawn. |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 390 ++iter) { | 392 ++iter) { |
| 391 const MediaGalleryPrefInfo& gallery = iter->second; | 393 const MediaGalleryPrefInfo& gallery = iter->second; |
| 392 if (gallery.IsBlackListedType()) | 394 if (gallery.IsBlackListedType()) |
| 393 continue; | 395 continue; |
| 394 | 396 |
| 395 GalleryDialogId gallery_id = GetDialogId(gallery.pref_id); | 397 GalleryDialogId gallery_id = GetDialogId(gallery.pref_id); |
| 396 known_galleries_[gallery_id] = Entry(gallery, false); | 398 known_galleries_[gallery_id] = Entry(gallery, false); |
| 397 known_galleries_[gallery_id].pref_info.pref_id = gallery_id; | 399 known_galleries_[gallery_id].pref_info.pref_id = gallery_id; |
| 398 } | 400 } |
| 399 | 401 |
| 400 MediaGalleryPrefIdSet permitted = | 402 pref_permitted_galleries_ = preferences_->GalleriesForExtension(*extension_); |
| 401 preferences_->GalleriesForExtension(*extension_); | |
| 402 | 403 |
| 403 for (MediaGalleryPrefIdSet::iterator iter = permitted.begin(); | 404 for (MediaGalleryPrefIdSet::iterator iter = pref_permitted_galleries_.begin(); |
| 404 iter != permitted.end(); ++iter) { | 405 iter != pref_permitted_galleries_.end(); |
| 406 ++iter) { |
| 405 GalleryDialogId gallery_id = GetDialogId(*iter); | 407 GalleryDialogId gallery_id = GetDialogId(*iter); |
| 406 if (ContainsKey(toggled_galleries_, gallery_id)) | |
| 407 continue; | |
| 408 DCHECK(ContainsKey(known_galleries_, gallery_id)); | 408 DCHECK(ContainsKey(known_galleries_, gallery_id)); |
| 409 known_galleries_[gallery_id].selected = true; | 409 known_galleries_[gallery_id].selected = true; |
| 410 } | 410 } |
| 411 |
| 412 // Preserve state of toggled galleries. |
| 413 for (ToggledGalleryMap::const_iterator iter = toggled_galleries_.begin(); |
| 414 iter != toggled_galleries_.end(); |
| 415 ++iter) { |
| 416 known_galleries_[iter->first].selected = iter->second; |
| 417 } |
| 411 } | 418 } |
| 412 | 419 |
| 413 void MediaGalleriesPermissionController::SavePermissions() { | 420 void MediaGalleriesPermissionController::SavePermissions() { |
| 414 DCHECK(preferences_); | 421 DCHECK(preferences_); |
| 415 media_galleries::UsageCount(media_galleries::SAVE_DIALOG); | 422 media_galleries::UsageCount(media_galleries::SAVE_DIALOG); |
| 416 for (GalleryPermissionsMap::const_iterator iter = known_galleries_.begin(); | 423 for (GalleryPermissionsMap::const_iterator iter = known_galleries_.begin(); |
| 417 iter != known_galleries_.end(); ++iter) { | 424 iter != known_galleries_.end(); ++iter) { |
| 418 MediaGalleryPrefId pref_id = GetPrefId(iter->first); | 425 MediaGalleryPrefId pref_id = GetPrefId(iter->first); |
| 419 if (ContainsKey(forgotten_galleries_, iter->first)) { | 426 if (ContainsKey(forgotten_galleries_, iter->first)) { |
| 420 preferences_->ForgetGalleryById(pref_id); | 427 preferences_->ForgetGalleryById(pref_id); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 495 context_menu_->set_pref_id(gallery_id); | 502 context_menu_->set_pref_id(gallery_id); |
| 496 return context_menu_.get(); | 503 return context_menu_.get(); |
| 497 } | 504 } |
| 498 | 505 |
| 499 GalleryDialogId MediaGalleriesPermissionController::GetDialogId( | 506 GalleryDialogId MediaGalleriesPermissionController::GetDialogId( |
| 500 MediaGalleryPrefId pref_id) { | 507 MediaGalleryPrefId pref_id) { |
| 501 return id_map_.GetDialogId(pref_id); | 508 return id_map_.GetDialogId(pref_id); |
| 502 } | 509 } |
| 503 | 510 |
| 504 MediaGalleryPrefId MediaGalleriesPermissionController::GetPrefId( | 511 MediaGalleryPrefId MediaGalleriesPermissionController::GetPrefId( |
| 505 GalleryDialogId id) { | 512 GalleryDialogId id) const { |
| 506 return id_map_.GetPrefId(id); | 513 return id_map_.GetPrefId(id); |
| 507 } | 514 } |
| 508 | 515 |
| 509 Profile* MediaGalleriesPermissionController::GetProfile() { | 516 Profile* MediaGalleriesPermissionController::GetProfile() { |
| 510 return Profile::FromBrowserContext(web_contents_->GetBrowserContext()); | 517 return Profile::FromBrowserContext(web_contents_->GetBrowserContext()); |
| 511 } | 518 } |
| 512 | 519 |
| 513 MediaGalleriesPermissionController::DialogIdMap::DialogIdMap() | 520 MediaGalleriesPermissionController::DialogIdMap::DialogIdMap() |
| 514 : next_dialog_id_(1) { | 521 : next_dialog_id_(1) { |
| 515 // Dialog id of 0 is invalid, so fill the slot. | 522 // Dialog id of 0 is invalid, so fill the slot. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 530 GalleryDialogId result = next_dialog_id_++; | 537 GalleryDialogId result = next_dialog_id_++; |
| 531 DCHECK_EQ(result, forward_mapping_.size()); | 538 DCHECK_EQ(result, forward_mapping_.size()); |
| 532 forward_mapping_.push_back(pref_id); | 539 forward_mapping_.push_back(pref_id); |
| 533 if (pref_id != kInvalidMediaGalleryPrefId) | 540 if (pref_id != kInvalidMediaGalleryPrefId) |
| 534 back_map_[pref_id] = result; | 541 back_map_[pref_id] = result; |
| 535 return result; | 542 return result; |
| 536 } | 543 } |
| 537 | 544 |
| 538 MediaGalleryPrefId | 545 MediaGalleryPrefId |
| 539 MediaGalleriesPermissionController::DialogIdMap::GetPrefId( | 546 MediaGalleriesPermissionController::DialogIdMap::GetPrefId( |
| 540 GalleryDialogId id) { | 547 GalleryDialogId id) const { |
| 541 DCHECK_LT(id, next_dialog_id_); | 548 DCHECK_LT(id, next_dialog_id_); |
| 542 return forward_mapping_[id]; | 549 return forward_mapping_[id]; |
| 543 } | 550 } |
| 544 | 551 |
| 545 // MediaGalleries dialog ------------------------------------------------------- | 552 // MediaGalleries dialog ------------------------------------------------------- |
| 546 | 553 |
| 547 MediaGalleriesDialog::~MediaGalleriesDialog() {} | 554 MediaGalleriesDialog::~MediaGalleriesDialog() {} |
| OLD | NEW |