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 #ifndef CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_GALLERIES_DIALOG_CONTROLLER_H_ | 5 #ifndef CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_GALLERIES_PERMISSION_CONTROLLER_H_ |
6 #define CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_GALLERIES_DIALOG_CONTROLLER_H_ | 6 #define CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_GALLERIES_PERMISSION_CONTROLLER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/callback.h" | 12 #include "base/callback.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
| 15 #include "chrome/browser/media_galleries/media_galleries_dialog_controller.h" |
15 #include "chrome/browser/media_galleries/media_galleries_preferences.h" | 16 #include "chrome/browser/media_galleries/media_galleries_preferences.h" |
16 #include "components/storage_monitor/removable_storage_observer.h" | 17 #include "components/storage_monitor/removable_storage_observer.h" |
17 #include "ui/gfx/native_widget_types.h" | 18 #include "ui/gfx/native_widget_types.h" |
18 #include "ui/shell_dialogs/select_file_dialog.h" | 19 #include "ui/shell_dialogs/select_file_dialog.h" |
19 | 20 |
20 namespace content { | 21 namespace content { |
21 class WebContents; | 22 class WebContents; |
22 } | 23 } |
23 | 24 |
24 namespace extensions { | 25 namespace extensions { |
(...skipping 10 matching lines...) Expand all Loading... |
35 | 36 |
36 // Newly added galleries are not added to preferences until the dialog commits, | 37 // Newly added galleries are not added to preferences until the dialog commits, |
37 // so they do not have a pref id while the dialog is open; leading to | 38 // so they do not have a pref id while the dialog is open; leading to |
38 // complicated code in the dialogs. To solve this complication, the controller | 39 // complicated code in the dialogs. To solve this complication, the controller |
39 // maps pref ids into a new space where it can also assign ids to new galleries. | 40 // maps pref ids into a new space where it can also assign ids to new galleries. |
40 // The new number space is only valid for the lifetime of the controller. To | 41 // The new number space is only valid for the lifetime of the controller. To |
41 // make it more clear where real pref ids are used and where the fake ids are | 42 // make it more clear where real pref ids are used and where the fake ids are |
42 // used, the GalleryDialogId type is used where fake ids are needed. | 43 // used, the GalleryDialogId type is used where fake ids are needed. |
43 typedef MediaGalleryPrefId GalleryDialogId; | 44 typedef MediaGalleryPrefId GalleryDialogId; |
44 | 45 |
45 // The view. | 46 class MediaGalleriesPermissionController |
46 class MediaGalleriesDialog { | 47 : public MediaGalleriesDialogController, |
47 public: | 48 public ui::SelectFileDialog::Listener, |
48 virtual ~MediaGalleriesDialog(); | |
49 | |
50 // Tell the dialog to update its display list of galleries. | |
51 virtual void UpdateGalleries() = 0; | |
52 | |
53 // Constructs a platform-specific dialog owned and controlled by |controller|. | |
54 static MediaGalleriesDialog* Create( | |
55 MediaGalleriesDialogController* controller); | |
56 }; | |
57 | |
58 // The controller is responsible for handling the logic of the dialog and | |
59 // interfacing with the model (i.e., MediaGalleriesPreferences). It shows | |
60 // the dialog and owns itself. | |
61 class MediaGalleriesDialogController | |
62 : public ui::SelectFileDialog::Listener, | |
63 public storage_monitor::RemovableStorageObserver, | 49 public storage_monitor::RemovableStorageObserver, |
64 public MediaGalleriesPreferences::GalleryChangeObserver { | 50 public MediaGalleriesPreferences::GalleryChangeObserver { |
65 public: | 51 public: |
66 struct GalleryPermission { | 52 // The constructor creates a dialog controller which owns itself. |
67 GalleryPermission(GalleryDialogId gallery_id, | 53 MediaGalleriesPermissionController(content::WebContents* web_contents, |
68 const MediaGalleryPrefInfo& pref_info, | 54 const extensions::Extension& extension, |
69 bool allowed) | 55 const base::Closure& on_finish); |
70 : gallery_id(gallery_id), | |
71 pref_info(pref_info), | |
72 allowed(allowed) { | |
73 } | |
74 GalleryPermission() {} | |
75 | 56 |
76 GalleryDialogId gallery_id; | 57 // MediaGalleriesDialogController implementation. |
77 MediaGalleryPrefInfo pref_info; | 58 virtual base::string16 GetHeader() const OVERRIDE; |
78 bool allowed; | 59 virtual base::string16 GetSubtext() const OVERRIDE; |
79 }; | 60 virtual bool IsAcceptAllowed() const OVERRIDE; |
80 | 61 virtual bool ShouldShowFolderViewer(const Entry& entry) const OVERRIDE; |
81 typedef std::vector<GalleryPermission> GalleryPermissionsVector; | 62 virtual std::vector<base::string16> GetSectionHeaders() const OVERRIDE; |
82 | 63 virtual Entries GetSectionEntries(size_t index) const OVERRIDE; |
83 // The constructor creates a dialog controller which owns itself. | 64 // Auxiliary button for this dialog is the 'Add Folder' button. |
84 MediaGalleriesDialogController(content::WebContents* web_contents, | 65 virtual base::string16 GetAuxiliaryButtonText() const OVERRIDE; |
85 const extensions::Extension& extension, | 66 virtual void DidClickAuxiliaryButton() OVERRIDE; |
86 const base::Closure& on_finish); | 67 virtual void DidToggleEntry(GalleryDialogId gallery_id, |
87 | 68 bool selected) OVERRIDE; |
88 // The title of the dialog view. | 69 virtual void DidClickOpenFolderViewer(GalleryDialogId gallery_id) OVERRIDE; |
89 base::string16 GetHeader() const; | 70 virtual void DidForgetEntry(GalleryDialogId gallery_id) OVERRIDE; |
90 | 71 virtual base::string16 GetAcceptButtonText() const OVERRIDE; |
91 // Explanatory text directly below the title. | 72 virtual void DialogFinished(bool accepted) OVERRIDE; |
92 base::string16 GetSubtext() const; | 73 virtual ui::MenuModel* GetContextMenu(GalleryDialogId gallery_id) OVERRIDE; |
93 | 74 virtual content::WebContents* WebContents() OVERRIDE; |
94 // Header for unattached devices part of the dialog. | |
95 base::string16 GetUnattachedLocationsHeader() const; | |
96 | |
97 // Initial state of whether the dialog's confirmation button will be enabled. | |
98 bool IsAcceptAllowed() const; | |
99 | |
100 // Get the set of permissions to attached galleries. | |
101 virtual GalleryPermissionsVector AttachedPermissions() const; | |
102 | |
103 // Get the set of permissions to unattached galleries. | |
104 virtual GalleryPermissionsVector UnattachedPermissions() const; | |
105 | |
106 // Called when the add-folder button in the dialog is clicked. | |
107 virtual void OnAddFolderClicked(); | |
108 | |
109 // A checkbox beside a gallery permission was checked. The full set | |
110 // of gallery permissions checkbox settings is sent on every checkbox toggle. | |
111 virtual void DidToggleGallery(GalleryDialogId gallery_id, bool enabled); | |
112 | |
113 // The forget command in the context menu was selected. | |
114 virtual void DidForgetGallery(GalleryDialogId gallery_id); | |
115 | |
116 // The dialog is being deleted. | |
117 virtual void DialogFinished(bool accepted); | |
118 | |
119 virtual content::WebContents* web_contents(); | |
120 | |
121 ui::MenuModel* GetContextMenu(GalleryDialogId gallery_id); | |
122 | 75 |
123 protected: | 76 protected: |
124 friend class MediaGalleriesDialogControllerTest; | 77 friend class MediaGalleriesPermissionControllerTest; |
125 | 78 |
126 typedef base::Callback<MediaGalleriesDialog* ( | 79 typedef base::Callback<MediaGalleriesDialog* ( |
127 MediaGalleriesDialogController*)> CreateDialogCallback; | 80 MediaGalleriesDialogController*)> CreateDialogCallback; |
128 | 81 |
129 // For use with tests. | 82 // For use with tests. |
130 MediaGalleriesDialogController( | 83 MediaGalleriesPermissionController( |
131 const extensions::Extension& extension, | 84 const extensions::Extension& extension, |
132 MediaGalleriesPreferences* preferences, | 85 MediaGalleriesPreferences* preferences, |
133 const CreateDialogCallback& create_dialog_callback, | 86 const CreateDialogCallback& create_dialog_callback, |
134 const base::Closure& on_finish); | 87 const base::Closure& on_finish); |
135 | 88 |
136 virtual ~MediaGalleriesDialogController(); | 89 virtual ~MediaGalleriesPermissionController(); |
137 | 90 |
138 private: | 91 private: |
139 // This type keeps track of media galleries already known to the prefs system. | 92 // This type keeps track of media galleries already known to the prefs system. |
140 typedef std::map<GalleryDialogId, GalleryPermission> | 93 typedef std::map<GalleryDialogId, Entry> GalleryPermissionsMap; |
141 GalleryPermissionsMap; | |
142 | 94 |
143 class DialogIdMap { | 95 class DialogIdMap { |
144 public: | 96 public: |
145 DialogIdMap(); | 97 DialogIdMap(); |
146 ~DialogIdMap(); | 98 ~DialogIdMap(); |
147 GalleryDialogId GetDialogId(MediaGalleryPrefId pref_id); | 99 GalleryDialogId GetDialogId(MediaGalleryPrefId pref_id); |
| 100 MediaGalleryPrefId GetPrefId(GalleryDialogId id); |
148 | 101 |
149 private: | 102 private: |
150 GalleryDialogId next_dialog_id_; | 103 GalleryDialogId next_dialog_id_; |
151 std::map<GalleryDialogId, MediaGalleryPrefId> mapping_; | 104 std::map<MediaGalleryPrefId, GalleryDialogId> back_map_; |
| 105 std::vector<MediaGalleryPrefId> forward_mapping_; |
152 DISALLOW_COPY_AND_ASSIGN(DialogIdMap); | 106 DISALLOW_COPY_AND_ASSIGN(DialogIdMap); |
153 }; | 107 }; |
154 | 108 |
155 | 109 |
156 // Bottom half of constructor -- called when |preferences_| is initialized. | 110 // Bottom half of constructor -- called when |preferences_| is initialized. |
157 void OnPreferencesInitialized(); | 111 void OnPreferencesInitialized(); |
158 | 112 |
159 // SelectFileDialog::Listener implementation: | 113 // SelectFileDialog::Listener implementation: |
160 virtual void FileSelected(const base::FilePath& path, | 114 virtual void FileSelected(const base::FilePath& path, |
161 int index, | 115 int index, |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 void SavePermissions(); | 157 void SavePermissions(); |
204 | 158 |
205 // Updates the model and view when |preferences_| changes. Some of the | 159 // Updates the model and view when |preferences_| changes. Some of the |
206 // possible changes includes a gallery getting blacklisted, or a new | 160 // possible changes includes a gallery getting blacklisted, or a new |
207 // auto detected gallery becoming available. | 161 // auto detected gallery becoming available. |
208 void UpdateGalleriesOnPreferencesEvent(); | 162 void UpdateGalleriesOnPreferencesEvent(); |
209 | 163 |
210 // Updates the model and view when a device is attached or detached. | 164 // Updates the model and view when a device is attached or detached. |
211 void UpdateGalleriesOnDeviceEvent(const std::string& device_id); | 165 void UpdateGalleriesOnDeviceEvent(const std::string& device_id); |
212 | 166 |
213 // Return a sorted vector of either attached or unattached gallery | |
214 // permissions. | |
215 GalleryPermissionsVector FillPermissions(bool attached) const; | |
216 | |
217 GalleryDialogId GetDialogId(MediaGalleryPrefId pref_id); | 167 GalleryDialogId GetDialogId(MediaGalleryPrefId pref_id); |
| 168 MediaGalleryPrefId GetPrefId(GalleryDialogId id); |
218 | 169 |
219 Profile* GetProfile(); | 170 Profile* GetProfile(); |
220 | 171 |
221 // The web contents from which the request originated. | 172 // The web contents from which the request originated. |
222 content::WebContents* web_contents_; | 173 content::WebContents* web_contents_; |
223 | 174 |
224 // This is just a reference, but it's assumed that it won't become invalid | 175 // This is just a reference, but it's assumed that it won't become invalid |
225 // while the dialog is showing. | 176 // while the dialog is showing. |
226 const extensions::Extension* extension_; | 177 const extensions::Extension* extension_; |
227 | 178 |
(...skipping 24 matching lines...) Expand all Loading... |
252 // The view that's showing. | 203 // The view that's showing. |
253 scoped_ptr<MediaGalleriesDialog> dialog_; | 204 scoped_ptr<MediaGalleriesDialog> dialog_; |
254 | 205 |
255 scoped_refptr<ui::SelectFileDialog> select_folder_dialog_; | 206 scoped_refptr<ui::SelectFileDialog> select_folder_dialog_; |
256 | 207 |
257 scoped_ptr<MediaGalleryContextMenu> context_menu_; | 208 scoped_ptr<MediaGalleryContextMenu> context_menu_; |
258 | 209 |
259 // Creates the dialog. Only changed for unit tests. | 210 // Creates the dialog. Only changed for unit tests. |
260 CreateDialogCallback create_dialog_callback_; | 211 CreateDialogCallback create_dialog_callback_; |
261 | 212 |
262 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesDialogController); | 213 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesPermissionController); |
263 }; | 214 }; |
264 | 215 |
265 #endif // CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_GALLERIES_DIALOG_CONTROLLER_H_ | 216 #endif // CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_GALLERIES_PERMISSION_CONTROLLER_
H_ |
OLD | NEW |