Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 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 #ifndef CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_GALLERIES_DIALOG_CONTROLLER_H_ | 5 #ifndef CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_GALLERIES_DIALOG_CONTROLLER_H_ |
| 6 #define CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_GALLERIES_DIALOG_CONTROLLER_H_ | 6 #define CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_GALLERIES_DIALOG_CONTROLLER_H_ |
| 7 | 7 |
| 8 #include <map> | |
| 9 #include <string> | |
| 10 #include <vector> | 8 #include <vector> |
| 11 | 9 |
| 12 #include "base/callback.h" | |
| 13 #include "base/memory/scoped_ptr.h" | |
| 14 #include "base/strings/string16.h" | 10 #include "base/strings/string16.h" |
| 15 #include "chrome/browser/media_galleries/media_galleries_preferences.h" | 11 #include "chrome/browser/media_galleries/media_galleries_preferences.h" |
| 16 #include "components/storage_monitor/removable_storage_observer.h" | |
| 17 #include "ui/gfx/native_widget_types.h" | |
| 18 #include "ui/shell_dialogs/select_file_dialog.h" | |
| 19 | 12 |
| 20 namespace content { | 13 namespace content { |
| 21 class WebContents; | 14 class WebContents; |
| 22 } | 15 } |
| 23 | 16 |
| 24 namespace extensions { | |
| 25 class Extension; | |
| 26 } | |
| 27 | |
| 28 namespace ui { | 17 namespace ui { |
| 29 class MenuModel; | 18 class MenuModel; |
| 30 } | 19 } |
| 31 | 20 |
| 32 class MediaGalleriesDialogController; | 21 class MediaGalleriesDialogController; |
| 33 class MediaGalleryContextMenu; | |
| 34 class Profile; | |
| 35 | |
| 36 // 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 // 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 // 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 // used, the GalleryDialogId type is used where fake ids are needed. | |
| 43 typedef MediaGalleryPrefId GalleryDialogId; | |
| 44 | 22 |
| 45 // The view. | 23 // The view. |
| 46 class MediaGalleriesDialog { | 24 class MediaGalleriesDialog { |
| 47 public: | 25 public: |
| 48 virtual ~MediaGalleriesDialog(); | 26 virtual ~MediaGalleriesDialog(); |
| 49 | 27 |
| 50 // Tell the dialog to update its display list of galleries. | 28 // Tell the dialog to update its display list of galleries. |
| 51 virtual void UpdateGalleries() = 0; | 29 virtual void UpdateGalleries() = 0; |
| 52 | 30 |
| 53 // Constructs a platform-specific dialog owned and controlled by |controller|. | 31 // Constructs a platform-specific dialog owned and controlled by |controller|. |
| 54 static MediaGalleriesDialog* Create( | 32 static MediaGalleriesDialog* Create( |
| 55 MediaGalleriesDialogController* controller); | 33 MediaGalleriesDialogController* controller); |
| 56 }; | 34 }; |
| 57 | 35 |
| 58 // The controller is responsible for handling the logic of the dialog and | 36 // Multiple dialog controllers are based on this interface. |
| 59 // interfacing with the model (i.e., MediaGalleriesPreferences). It shows | 37 // Implementations of this controller interface are responsible for handling |
| 60 // the dialog and owns itself. | 38 // the logic of the dialog and interfacing with the model (i.e., |
| 61 class MediaGalleriesDialogController | 39 // MediaGalleriesPreferences). It shows the dialog and owns itself. |
| 62 : public ui::SelectFileDialog::Listener, | 40 class MediaGalleriesDialogController { |
| 63 public storage_monitor::RemovableStorageObserver, | |
| 64 public MediaGalleriesPreferences::GalleryChangeObserver { | |
| 65 public: | 41 public: |
| 66 struct GalleryPermission { | 42 struct Entry { |
| 67 GalleryPermission(GalleryDialogId gallery_id, | 43 Entry(const MediaGalleryPrefInfo& pref_info, bool selected) |
| 68 const MediaGalleryPrefInfo& pref_info, | 44 : pref_info(pref_info), |
| 69 bool allowed) | 45 selected(selected) { |
| 70 : gallery_id(gallery_id), | |
| 71 pref_info(pref_info), | |
| 72 allowed(allowed) { | |
| 73 } | 46 } |
| 74 GalleryPermission() {} | 47 Entry() {} |
| 75 | 48 |
| 76 GalleryDialogId gallery_id; | |
| 77 MediaGalleryPrefInfo pref_info; | 49 MediaGalleryPrefInfo pref_info; |
| 78 bool allowed; | 50 bool selected; |
| 79 }; | 51 }; |
| 80 | 52 |
| 81 typedef std::vector<GalleryPermission> GalleryPermissionsVector; | 53 typedef std::vector<Entry> Entries; |
| 82 | |
| 83 // The constructor creates a dialog controller which owns itself. | |
| 84 MediaGalleriesDialogController(content::WebContents* web_contents, | |
| 85 const extensions::Extension& extension, | |
| 86 const base::Closure& on_finish); | |
| 87 | 54 |
| 88 // The title of the dialog view. | 55 // The title of the dialog view. |
| 89 base::string16 GetHeader() const; | 56 virtual base::string16 GetHeader() const = 0; |
| 90 | 57 |
| 91 // Explanatory text directly below the title. | 58 // Explanatory text directly below the title. |
| 92 base::string16 GetSubtext() const; | 59 virtual base::string16 GetSubtext() const = 0; |
| 93 | |
| 94 // Header for unattached devices part of the dialog. | |
| 95 base::string16 GetUnattachedLocationsHeader() const; | |
| 96 | 60 |
| 97 // Initial state of whether the dialog's confirmation button will be enabled. | 61 // Initial state of whether the dialog's confirmation button will be enabled. |
| 98 bool IsAcceptAllowed() const; | 62 virtual bool IsAcceptAllowed() const = 0; |
| 63 | |
| 64 // Should the dialog present UI for the user to view the entry contents. | |
| 65 virtual bool ShowFolderViewer(const Entry& entry) const = 0; | |
|
Lei Zhang
2014/06/05 07:56:25
ShouldShowFolderViewer to make it clear this is no
vandebo (ex-Chrome)
2014/06/05 19:25:28
Done.
| |
| 66 | |
| 67 // The titles for different sections of entries. Empty hides the header. | |
| 68 virtual std::vector<base::string16> GetSectionHeaders() const = 0; | |
| 99 | 69 |
| 100 // Get the set of permissions to attached galleries. | 70 // Get the set of permissions to attached galleries. |
| 101 virtual GalleryPermissionsVector AttachedPermissions() const; | 71 virtual Entries GetSectionEntries(size_t index) const = 0; |
|
Lei Zhang
2014/06/05 07:56:25
What does |index| mean here?
vandebo (ex-Chrome)
2014/06/05 19:25:29
Done.
| |
| 102 | 72 |
| 103 // Get the set of permissions to unattached galleries. | 73 // The text for auxiliary an button. Empty hides the button. |
|
Lei Zhang
2014/06/05 07:56:25
Did you mean "text for an auxiliary button" ?
vandebo (ex-Chrome)
2014/06/05 19:25:28
Done.
| |
| 104 virtual GalleryPermissionsVector UnattachedPermissions() const; | 74 virtual base::string16 GetAuxilliaryButtonText() const = 0; |
| 105 | 75 |
| 106 // Called when the add-folder button in the dialog is clicked. | 76 // Called when an auxiliary button is clicked. |
| 107 virtual void OnAddFolderClicked(); | 77 virtual void OnAuxilliaryButtonClicked() = 0; |
| 108 | 78 |
| 109 // A checkbox beside a gallery permission was checked. The full set | 79 // An entry checkbox was toggled. |
| 110 // of gallery permissions checkbox settings is sent on every checkbox toggle. | 80 virtual void DidToggleEntry(MediaGalleryPrefId id, bool selected) = 0; |
|
Lei Zhang
2014/06/05 07:56:25
Can you name there all OnFoo or DidFoo?
vandebo (ex-Chrome)
2014/06/05 19:25:29
Done.
| |
| 111 virtual void DidToggleGallery(GalleryDialogId gallery_id, bool enabled); | 81 |
| 82 // A folder viewer button was clicked for one of the entries. | |
| 83 virtual void DidClickOpenFolderViewer(MediaGalleryPrefId id) = 0; | |
| 112 | 84 |
| 113 // The forget command in the context menu was selected. | 85 // The forget command in the context menu was selected. |
| 114 virtual void DidForgetGallery(GalleryDialogId gallery_id); | 86 virtual void DidForgetEntry(MediaGalleryPrefId id) = 0; |
| 87 | |
| 88 // The text for the accept button. | |
| 89 virtual base::string16 GetAcceptButtonText() const = 0; | |
| 115 | 90 |
| 116 // The dialog is being deleted. | 91 // The dialog is being deleted. |
| 117 virtual void DialogFinished(bool accepted); | 92 virtual void DialogFinished(bool accepted) = 0; |
| 118 | 93 |
| 119 virtual content::WebContents* web_contents(); | 94 virtual ui::MenuModel* GetContextMenu(MediaGalleryPrefId id) = 0; |
| 120 | 95 |
| 121 ui::MenuModel* GetContextMenu(GalleryDialogId gallery_id); | 96 virtual content::WebContents* web_contents() = 0; |
|
Lei Zhang
2014/06/05 07:56:25
Virtual functions should never be named unix_hacke
vandebo (ex-Chrome)
2014/06/05 19:25:28
Done.
| |
| 122 | 97 |
| 123 protected: | 98 protected: |
| 124 friend class MediaGalleriesDialogControllerTest; | 99 MediaGalleriesDialogController(); |
| 125 | |
| 126 typedef base::Callback<MediaGalleriesDialog* ( | |
| 127 MediaGalleriesDialogController*)> CreateDialogCallback; | |
| 128 | |
| 129 // For use with tests. | |
| 130 MediaGalleriesDialogController( | |
| 131 const extensions::Extension& extension, | |
| 132 MediaGalleriesPreferences* preferences, | |
| 133 const CreateDialogCallback& create_dialog_callback, | |
| 134 const base::Closure& on_finish); | |
| 135 | |
| 136 virtual ~MediaGalleriesDialogController(); | 100 virtual ~MediaGalleriesDialogController(); |
| 137 | 101 |
| 138 private: | |
| 139 // This type keeps track of media galleries already known to the prefs system. | |
| 140 typedef std::map<GalleryDialogId, GalleryPermission> | |
| 141 GalleryPermissionsMap; | |
| 142 | |
| 143 class DialogIdMap { | |
| 144 public: | |
| 145 DialogIdMap(); | |
| 146 ~DialogIdMap(); | |
| 147 GalleryDialogId GetDialogId(MediaGalleryPrefId pref_id); | |
| 148 | |
| 149 private: | |
| 150 GalleryDialogId next_dialog_id_; | |
| 151 std::map<GalleryDialogId, MediaGalleryPrefId> mapping_; | |
| 152 DISALLOW_COPY_AND_ASSIGN(DialogIdMap); | |
| 153 }; | |
| 154 | |
| 155 | |
| 156 // Bottom half of constructor -- called when |preferences_| is initialized. | |
| 157 void OnPreferencesInitialized(); | |
| 158 | |
| 159 // SelectFileDialog::Listener implementation: | |
| 160 virtual void FileSelected(const base::FilePath& path, | |
| 161 int index, | |
| 162 void* params) OVERRIDE; | |
| 163 | |
| 164 // RemovableStorageObserver implementation. | |
| 165 // Used to keep dialog in sync with removable device status. | |
| 166 virtual void OnRemovableStorageAttached( | |
| 167 const storage_monitor::StorageInfo& info) OVERRIDE; | |
| 168 virtual void OnRemovableStorageDetached( | |
| 169 const storage_monitor::StorageInfo& info) OVERRIDE; | |
| 170 | |
| 171 // MediaGalleriesPreferences::GalleryChangeObserver implementations. | |
| 172 // Used to keep the dialog in sync when the preferences change. | |
| 173 virtual void OnPermissionAdded(MediaGalleriesPreferences* pref, | |
| 174 const std::string& extension_id, | |
| 175 MediaGalleryPrefId pref_id) OVERRIDE; | |
| 176 virtual void OnPermissionRemoved(MediaGalleriesPreferences* pref, | |
| 177 const std::string& extension_id, | |
| 178 MediaGalleryPrefId pref_id) OVERRIDE; | |
| 179 virtual void OnGalleryAdded(MediaGalleriesPreferences* pref, | |
| 180 MediaGalleryPrefId pref_id) OVERRIDE; | |
| 181 virtual void OnGalleryRemoved(MediaGalleriesPreferences* pref, | |
| 182 MediaGalleryPrefId pref_id) OVERRIDE; | |
| 183 virtual void OnGalleryInfoUpdated(MediaGalleriesPreferences* pref, | |
| 184 MediaGalleryPrefId pref_id) OVERRIDE; | |
| 185 | |
| 186 // Populates |known_galleries_| from |preferences_|. Subsequent calls merge | |
| 187 // into |known_galleries_| and do not change permissions for user toggled | |
| 188 // galleries. | |
| 189 void InitializePermissions(); | |
| 190 | |
| 191 // Saves state of |known_galleries_|, |new_galleries_| and | |
| 192 // |forgotten_galleries_| to model. | |
| 193 // | |
| 194 // NOTE: possible states for a gallery: | |
| 195 // K N F (K = Known, N = New, F = Forgotten) | |
| 196 // +---+---+---+ | |
| 197 // | Y | N | N | | |
| 198 // +---+---+---+ | |
| 199 // | N | Y | N | | |
| 200 // +---+---+---+ | |
| 201 // | Y | N | Y | | |
| 202 // +---+---+---+ | |
| 203 void SavePermissions(); | |
| 204 | |
| 205 // Updates the model and view when |preferences_| changes. Some of the | |
| 206 // possible changes includes a gallery getting blacklisted, or a new | |
| 207 // auto detected gallery becoming available. | |
| 208 void UpdateGalleriesOnPreferencesEvent(); | |
| 209 | |
| 210 // Updates the model and view when a device is attached or detached. | |
| 211 void UpdateGalleriesOnDeviceEvent(const std::string& device_id); | |
| 212 | |
| 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); | |
| 218 | |
| 219 Profile* GetProfile(); | |
| 220 | |
| 221 // The web contents from which the request originated. | |
| 222 content::WebContents* web_contents_; | |
| 223 | |
| 224 // This is just a reference, but it's assumed that it won't become invalid | |
| 225 // while the dialog is showing. | |
| 226 const extensions::Extension* extension_; | |
| 227 | |
| 228 // Mapping between pref ids and dialog ids. | |
| 229 DialogIdMap id_map_; | |
| 230 | |
| 231 // This map excludes those galleries which have been blacklisted; it only | |
| 232 // counts active known galleries. | |
| 233 GalleryPermissionsMap known_galleries_; | |
| 234 | |
| 235 // Galleries in |known_galleries_| that the user have toggled. | |
| 236 std::set<GalleryDialogId> toggled_galleries_; | |
| 237 | |
| 238 // Map of new galleries the user added, but have not saved. This list should | |
| 239 // never overlap with |known_galleries_|. | |
| 240 GalleryPermissionsMap new_galleries_; | |
| 241 | |
| 242 // Galleries in |known_galleries_| that the user has forgotten. | |
| 243 std::set<GalleryDialogId> forgotten_galleries_; | |
| 244 | |
| 245 // Callback to run when the dialog closes. | |
| 246 base::Closure on_finish_; | |
| 247 | |
| 248 // The model that tracks galleries and extensions' permissions. | |
| 249 // This is the authoritative source for gallery information. | |
| 250 MediaGalleriesPreferences* preferences_; | |
| 251 | |
| 252 // The view that's showing. | |
| 253 scoped_ptr<MediaGalleriesDialog> dialog_; | |
| 254 | |
| 255 scoped_refptr<ui::SelectFileDialog> select_folder_dialog_; | |
| 256 | |
| 257 scoped_ptr<MediaGalleryContextMenu> context_menu_; | |
| 258 | |
| 259 // Creates the dialog. Only changed for unit tests. | |
| 260 CreateDialogCallback create_dialog_callback_; | |
| 261 | |
| 262 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesDialogController); | 102 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesDialogController); |
| 263 }; | 103 }; |
| 264 | 104 |
| 265 #endif // CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_GALLERIES_DIALOG_CONTROLLER_H_ | 105 #endif // CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_GALLERIES_DIALOG_CONTROLLER_H_ |
| OLD | NEW |