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(1, GetDeleteMenuCommand()); | |
vandebo (ex-Chrome)
2013/10/29 14:55:17
Now that this is in the controller, you don't need
Greg Billock
2013/10/29 17:04:06
Done.
| |
103 context_menu_model_.reset(menu_model); | |
63 } | 104 } |
64 | 105 |
65 void MediaGalleriesDialogController::OnPreferencesInitialized() { | 106 void MediaGalleriesDialogController::OnPreferencesInitialized() { |
66 InitializePermissions(); | 107 InitializePermissions(); |
67 | 108 |
68 dialog_.reset(MediaGalleriesDialog::Create(this)); | 109 dialog_.reset(MediaGalleriesDialog::Create(this)); |
69 | 110 |
70 StorageMonitor::GetInstance()->AddObserver(this); | 111 StorageMonitor::GetInstance()->AddObserver(this); |
71 | 112 |
72 preferences_->AddGalleryChangeObserver(this); | 113 preferences_->AddGalleryChangeObserver(this); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
109 else | 150 else |
110 id = IDS_MEDIA_GALLERIES_DIALOG_SUBTEXT_READ_ONLY; | 151 id = IDS_MEDIA_GALLERIES_DIALOG_SUBTEXT_READ_ONLY; |
111 | 152 |
112 return l10n_util::GetStringFUTF16(id, UTF8ToUTF16(extension_->name())); | 153 return l10n_util::GetStringFUTF16(id, UTF8ToUTF16(extension_->name())); |
113 } | 154 } |
114 | 155 |
115 string16 MediaGalleriesDialogController::GetUnattachedLocationsHeader() const { | 156 string16 MediaGalleriesDialogController::GetUnattachedLocationsHeader() const { |
116 return l10n_util::GetStringUTF16(IDS_MEDIA_GALLERIES_UNATTACHED_LOCATIONS); | 157 return l10n_util::GetStringUTF16(IDS_MEDIA_GALLERIES_UNATTACHED_LOCATIONS); |
117 } | 158 } |
118 | 159 |
160 string16 MediaGalleriesDialogController::GetDeleteMenuCommand() const { | |
161 return l10n_util::GetStringUTF16(IDS_MEDIA_GALLERIES_DIALOG_DELETE); | |
162 } | |
163 | |
119 // TODO(gbillock): Call this something a bit more connected to the | 164 // TODO(gbillock): Call this something a bit more connected to the |
120 // messaging in the dialog. | 165 // messaging in the dialog. |
121 bool MediaGalleriesDialogController::HasPermittedGalleries() const { | 166 bool MediaGalleriesDialogController::HasPermittedGalleries() const { |
122 for (KnownGalleryPermissions::const_iterator iter = known_galleries_.begin(); | 167 for (KnownGalleryPermissions::const_iterator iter = known_galleries_.begin(); |
123 iter != known_galleries_.end(); ++iter) { | 168 iter != known_galleries_.end(); ++iter) { |
124 if (iter->second.allowed) | 169 if (iter->second.allowed) |
125 return true; | 170 return true; |
126 } | 171 } |
127 | 172 |
128 // Do this? Views did. | 173 // Do this? Views did. |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
211 for (GalleryPermissionsVector::iterator iter = new_galleries_.begin(); | 256 for (GalleryPermissionsVector::iterator iter = new_galleries_.begin(); |
212 iter != new_galleries_.end(); ++iter) { | 257 iter != new_galleries_.end(); ++iter) { |
213 if (iter->pref_info.path == gallery.path && | 258 if (iter->pref_info.path == gallery.path && |
214 iter->pref_info.device_id == gallery.device_id) { | 259 iter->pref_info.device_id == gallery.device_id) { |
215 iter->allowed = enabled; | 260 iter->allowed = enabled; |
216 return; | 261 return; |
217 } | 262 } |
218 } | 263 } |
219 } | 264 } |
220 | 265 |
266 void MediaGalleriesDialogController::DidForgetGallery( | |
267 MediaGalleryPrefId pref_id) { | |
268 DCHECK(preferences_); | |
269 preferences_->ForgetGalleryById(pref_id); | |
270 } | |
271 | |
221 void MediaGalleriesDialogController::DialogFinished(bool accepted) { | 272 void MediaGalleriesDialogController::DialogFinished(bool accepted) { |
222 // The dialog has finished, so there is no need to watch for more updates | 273 // 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 | 274 // 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 | 275 // only non-test code path that deletes |this|. The test ctor never adds |
225 // this observer in the first place. | 276 // this observer in the first place. |
226 preferences_->RemoveGalleryChangeObserver(this); | 277 preferences_->RemoveGalleryChangeObserver(this); |
227 | 278 |
228 if (accepted) | 279 if (accepted) |
229 SavePermissions(); | 280 SavePermissions(); |
230 | 281 |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
405 } | 456 } |
406 | 457 |
407 dialog_->UpdateGalleries(); | 458 dialog_->UpdateGalleries(); |
408 } | 459 } |
409 | 460 |
410 void MediaGalleriesDialogController::UpdateGalleriesOnDeviceEvent( | 461 void MediaGalleriesDialogController::UpdateGalleriesOnDeviceEvent( |
411 const std::string& device_id) { | 462 const std::string& device_id) { |
412 dialog_->UpdateGalleries(); | 463 dialog_->UpdateGalleries(); |
413 } | 464 } |
414 | 465 |
466 ui::MenuModel* MediaGalleriesDialogController::GetContextMenuModel( | |
467 MediaGalleryPrefId id) { | |
468 gallery_menu_model_->set_media_gallery_pref_id(id); | |
469 return context_menu_model_.get(); | |
470 } | |
471 | |
415 // MediaGalleries dialog ------------------------------------------------------- | 472 // MediaGalleries dialog ------------------------------------------------------- |
416 | 473 |
417 MediaGalleriesDialog::~MediaGalleriesDialog() {} | 474 MediaGalleriesDialog::~MediaGalleriesDialog() {} |
OLD | NEW |