| 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_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/stl_util.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
| 11 #include "chrome/browser/media_galleries/media_file_system_registry.h" | 11 #include "chrome/browser/media_galleries/media_file_system_registry.h" |
| 12 #include "chrome/browser/media_galleries/media_galleries_histograms.h" | 12 #include "chrome/browser/media_galleries/media_galleries_histograms.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/storage_monitor/storage_info.h" | 14 #include "chrome/browser/storage_monitor/storage_info.h" |
| 15 #include "chrome/browser/storage_monitor/storage_monitor.h" | 15 #include "chrome/browser/storage_monitor/storage_monitor.h" |
| 16 #include "chrome/browser/ui/chrome_select_file_policy.h" | 16 #include "chrome/browser/ui/chrome_select_file_policy.h" |
| 17 #include "chrome/common/chrome_paths.h" | 17 #include "chrome/common/chrome_paths.h" |
| 18 #include "chrome/common/extensions/extension.h" | 18 #include "chrome/common/extensions/extension.h" |
| 19 #include "chrome/common/extensions/permissions/media_galleries_permission.h" | 19 #include "chrome/common/extensions/permissions/media_galleries_permission.h" |
| 20 #include "chrome/common/extensions/permissions/permissions_data.h" | 20 #include "chrome/common/extensions/permissions/permissions_data.h" |
| 21 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
| 22 #include "content/public/browser/web_contents_view.h" | 22 #include "content/public/browser/web_contents_view.h" |
| 23 #include "grit/generated_resources.h" | 23 #include "grit/generated_resources.h" |
| 24 #include "ui/base/l10n/l10n_util.h" | 24 #include "ui/base/l10n/l10n_util.h" |
| 25 #include "ui/base/models/simple_menu_model.h" |
| 25 #include "ui/base/text/bytes_formatting.h" | 26 #include "ui/base/text/bytes_formatting.h" |
| 26 | 27 |
| 27 using extensions::APIPermission; | 28 using extensions::APIPermission; |
| 28 using extensions::Extension; | 29 using extensions::Extension; |
| 29 | 30 |
| 30 namespace { | 31 namespace { |
| 31 | 32 |
| 32 // Comparator for sorting GalleryPermissionsVector -- sorts | 33 // Comparator for sorting GalleryPermissionsVector -- sorts |
| 33 // allowed galleries low, and then sorts by absolute path. | 34 // allowed galleries low, and then sorts by absolute path. |
| 34 bool GalleriesVectorComparator( | 35 bool GalleriesVectorComparator( |
| 35 const MediaGalleriesDialogController::GalleryPermission& a, | 36 const MediaGalleriesDialogController::GalleryPermission& a, |
| 36 const MediaGalleriesDialogController::GalleryPermission& b) { | 37 const MediaGalleriesDialogController::GalleryPermission& b) { |
| 37 if (a.allowed && !b.allowed) | 38 if (a.allowed && !b.allowed) |
| 38 return true; | 39 return true; |
| 39 if (!a.allowed && b.allowed) | 40 if (!a.allowed && b.allowed) |
| 40 return false; | 41 return false; |
| 41 | 42 |
| 42 return a.pref_info.AbsolutePath() < b.pref_info.AbsolutePath(); | 43 return a.pref_info.AbsolutePath() < b.pref_info.AbsolutePath(); |
| 43 } | 44 } |
| 44 | 45 |
| 45 } // namespace | 46 } // namespace |
| 46 | 47 |
| 48 class GalleryContextMenuModel : public ui::SimpleMenuModel::Delegate { |
| 49 public: |
| 50 explicit GalleryContextMenuModel(MediaGalleriesDialogController* controller) |
| 51 : controller_(controller), id_(kInvalidMediaGalleryPrefId) {} |
| 52 virtual ~GalleryContextMenuModel() {} |
| 53 |
| 54 void set_media_gallery_pref_id(MediaGalleryPrefId id) { |
| 55 id_ = id; |
| 56 } |
| 57 |
| 58 virtual bool IsCommandIdChecked(int command_id) const OVERRIDE { |
| 59 return false; |
| 60 } |
| 61 virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE { |
| 62 return true; |
| 63 } |
| 64 virtual bool IsCommandIdVisible(int command_id) const OVERRIDE { |
| 65 return true; |
| 66 } |
| 67 |
| 68 virtual bool GetAcceleratorForCommandId( |
| 69 int command_id, ui::Accelerator* accelerator) OVERRIDE { |
| 70 return false; |
| 71 } |
| 72 |
| 73 virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE { |
| 74 controller_->DidForgetGallery(id_); |
| 75 } |
| 76 |
| 77 private: |
| 78 MediaGalleriesDialogController* controller_; |
| 79 MediaGalleryPrefId id_; |
| 80 }; |
| 81 |
| 47 MediaGalleriesDialogController::MediaGalleriesDialogController( | 82 MediaGalleriesDialogController::MediaGalleriesDialogController( |
| 48 content::WebContents* web_contents, | 83 content::WebContents* web_contents, |
| 49 const Extension& extension, | 84 const Extension& extension, |
| 50 const base::Closure& on_finish) | 85 const base::Closure& on_finish) |
| 51 : web_contents_(web_contents), | 86 : web_contents_(web_contents), |
| 52 extension_(&extension), | 87 extension_(&extension), |
| 53 on_finish_(on_finish) { | 88 on_finish_(on_finish) { |
| 54 preferences_ = | 89 preferences_ = |
| 55 g_browser_process->media_file_system_registry()->GetPreferences( | 90 g_browser_process->media_file_system_registry()->GetPreferences( |
| 56 Profile::FromBrowserContext(web_contents_->GetBrowserContext())); | 91 Profile::FromBrowserContext(web_contents_->GetBrowserContext())); |
| 57 // Passing unretained pointer is safe, since the dialog controller | 92 // Passing unretained pointer is safe, since the dialog controller |
| 58 // is self-deleting, and so won't be deleted until it can be shown | 93 // is self-deleting, and so won't be deleted until it can be shown |
| 59 // and then closed. | 94 // and then closed. |
| 60 preferences_->EnsureInitialized( | 95 preferences_->EnsureInitialized( |
| 61 base::Bind(&MediaGalleriesDialogController::OnPreferencesInitialized, | 96 base::Bind(&MediaGalleriesDialogController::OnPreferencesInitialized, |
| 62 base::Unretained(this))); | 97 base::Unretained(this))); |
| 98 |
| 99 gallery_menu_model_.reset(new GalleryContextMenuModel(this)); |
| 100 ui::SimpleMenuModel* menu_model = |
| 101 new ui::SimpleMenuModel(gallery_menu_model_.get()); |
| 102 menu_model->AddItem( |
| 103 1, l10n_util::GetStringUTF16(IDS_MEDIA_GALLERIES_DIALOG_DELETE)); |
| 104 context_menu_model_.reset(menu_model); |
| 63 } | 105 } |
| 64 | 106 |
| 65 void MediaGalleriesDialogController::OnPreferencesInitialized() { | 107 void MediaGalleriesDialogController::OnPreferencesInitialized() { |
| 66 InitializePermissions(); | 108 InitializePermissions(); |
| 67 | 109 |
| 68 dialog_.reset(MediaGalleriesDialog::Create(this)); | 110 dialog_.reset(MediaGalleriesDialog::Create(this)); |
| 69 | 111 |
| 70 StorageMonitor::GetInstance()->AddObserver(this); | 112 StorageMonitor::GetInstance()->AddObserver(this); |
| 71 | 113 |
| 72 preferences_->AddGalleryChangeObserver(this); | 114 preferences_->AddGalleryChangeObserver(this); |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 for (GalleryPermissionsVector::iterator iter = new_galleries_.begin(); | 253 for (GalleryPermissionsVector::iterator iter = new_galleries_.begin(); |
| 212 iter != new_galleries_.end(); ++iter) { | 254 iter != new_galleries_.end(); ++iter) { |
| 213 if (iter->pref_info.path == gallery.path && | 255 if (iter->pref_info.path == gallery.path && |
| 214 iter->pref_info.device_id == gallery.device_id) { | 256 iter->pref_info.device_id == gallery.device_id) { |
| 215 iter->allowed = enabled; | 257 iter->allowed = enabled; |
| 216 return; | 258 return; |
| 217 } | 259 } |
| 218 } | 260 } |
| 219 } | 261 } |
| 220 | 262 |
| 263 void MediaGalleriesDialogController::DidForgetGallery( |
| 264 MediaGalleryPrefId pref_id) { |
| 265 DCHECK(preferences_); |
| 266 preferences_->ForgetGalleryById(pref_id); |
| 267 } |
| 268 |
| 221 void MediaGalleriesDialogController::DialogFinished(bool accepted) { | 269 void MediaGalleriesDialogController::DialogFinished(bool accepted) { |
| 222 // The dialog has finished, so there is no need to watch for more updates | 270 // The dialog has finished, so there is no need to watch for more updates |
| 223 // from |preferences_|. Do this here and not in the dtor since this is the | 271 // from |preferences_|. Do this here and not in the dtor since this is the |
| 224 // only non-test code path that deletes |this|. The test ctor never adds | 272 // only non-test code path that deletes |this|. The test ctor never adds |
| 225 // this observer in the first place. | 273 // this observer in the first place. |
| 226 preferences_->RemoveGalleryChangeObserver(this); | 274 preferences_->RemoveGalleryChangeObserver(this); |
| 227 | 275 |
| 228 if (accepted) | 276 if (accepted) |
| 229 SavePermissions(); | 277 SavePermissions(); |
| 230 | 278 |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 405 } | 453 } |
| 406 | 454 |
| 407 dialog_->UpdateGalleries(); | 455 dialog_->UpdateGalleries(); |
| 408 } | 456 } |
| 409 | 457 |
| 410 void MediaGalleriesDialogController::UpdateGalleriesOnDeviceEvent( | 458 void MediaGalleriesDialogController::UpdateGalleriesOnDeviceEvent( |
| 411 const std::string& device_id) { | 459 const std::string& device_id) { |
| 412 dialog_->UpdateGalleries(); | 460 dialog_->UpdateGalleries(); |
| 413 } | 461 } |
| 414 | 462 |
| 463 ui::MenuModel* MediaGalleriesDialogController::GetContextMenuModel( |
| 464 MediaGalleryPrefId id) { |
| 465 gallery_menu_model_->set_media_gallery_pref_id(id); |
| 466 return context_menu_model_.get(); |
| 467 } |
| 468 |
| 415 // MediaGalleries dialog ------------------------------------------------------- | 469 // MediaGalleries dialog ------------------------------------------------------- |
| 416 | 470 |
| 417 MediaGalleriesDialog::~MediaGalleriesDialog() {} | 471 MediaGalleriesDialog::~MediaGalleriesDialog() {} |
| OLD | NEW |