| 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 ShowFolderViewer(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 virtual base::string16 GetAuxilliaryButtonText() const OVERRIDE; |
| 84 MediaGalleriesDialogController(content::WebContents* web_contents, | 65 virtual void OnAuxilliaryButtonClicked() OVERRIDE; |
| 85 const extensions::Extension& extension, | 66 virtual void DidToggleEntry(GalleryDialogId gallery_id, |
| 86 const base::Closure& on_finish); | 67 bool selected) OVERRIDE; |
| 87 | 68 virtual void DidClickOpenFolderViewer(GalleryDialogId gallery_id) OVERRIDE; |
| 88 // The title of the dialog view. | 69 virtual void DidForgetEntry(GalleryDialogId gallery_id) OVERRIDE; |
| 89 base::string16 GetHeader() const; | 70 virtual base::string16 GetAcceptButtonText() const OVERRIDE; |
| 90 | 71 virtual void DialogFinished(bool accepted) OVERRIDE; |
| 91 // Explanatory text directly below the title. | 72 virtual ui::MenuModel* GetContextMenu(GalleryDialogId gallery_id) OVERRIDE; |
| 92 base::string16 GetSubtext() const; | 73 virtual content::WebContents* web_contents() OVERRIDE; |
| 93 | |
| 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 | 74 |
| 123 protected: | 75 protected: |
| 124 friend class MediaGalleriesDialogControllerTest; | 76 friend class MediaGalleriesPermissionControllerTest; |
| 125 | 77 |
| 126 typedef base::Callback<MediaGalleriesDialog* ( | 78 typedef base::Callback<MediaGalleriesDialog* ( |
| 127 MediaGalleriesDialogController*)> CreateDialogCallback; | 79 MediaGalleriesDialogController*)> CreateDialogCallback; |
| 128 | 80 |
| 129 // For use with tests. | 81 // For use with tests. |
| 130 MediaGalleriesDialogController( | 82 MediaGalleriesPermissionController( |
| 131 const extensions::Extension& extension, | 83 const extensions::Extension& extension, |
| 132 MediaGalleriesPreferences* preferences, | 84 MediaGalleriesPreferences* preferences, |
| 133 const CreateDialogCallback& create_dialog_callback, | 85 const CreateDialogCallback& create_dialog_callback, |
| 134 const base::Closure& on_finish); | 86 const base::Closure& on_finish); |
| 135 | 87 |
| 136 virtual ~MediaGalleriesDialogController(); | 88 virtual ~MediaGalleriesPermissionController(); |
| 137 | 89 |
| 138 private: | 90 private: |
| 139 // This type keeps track of media galleries already known to the prefs system. | 91 // This type keeps track of media galleries already known to the prefs system. |
| 140 typedef std::map<GalleryDialogId, GalleryPermission> | 92 typedef std::map<GalleryDialogId, Entry> GalleryPermissionsMap; |
| 141 GalleryPermissionsMap; | |
| 142 | 93 |
| 143 class DialogIdMap { | 94 class DialogIdMap { |
| 144 public: | 95 public: |
| 145 DialogIdMap(); | 96 DialogIdMap(); |
| 146 ~DialogIdMap(); | 97 ~DialogIdMap(); |
| 147 GalleryDialogId GetDialogId(MediaGalleryPrefId pref_id); | 98 GalleryDialogId GetDialogId(MediaGalleryPrefId pref_id); |
| 99 MediaGalleryPrefId GetPrefId(GalleryDialogId id); |
| 148 | 100 |
| 149 private: | 101 private: |
| 150 GalleryDialogId next_dialog_id_; | 102 GalleryDialogId next_dialog_id_; |
| 151 std::map<GalleryDialogId, MediaGalleryPrefId> mapping_; | 103 std::map<MediaGalleryPrefId, GalleryDialogId> back_map_; |
| 104 std::vector<MediaGalleryPrefId> forward_mapping_; |
| 152 DISALLOW_COPY_AND_ASSIGN(DialogIdMap); | 105 DISALLOW_COPY_AND_ASSIGN(DialogIdMap); |
| 153 }; | 106 }; |
| 154 | 107 |
| 155 | 108 |
| 156 // Bottom half of constructor -- called when |preferences_| is initialized. | 109 // Bottom half of constructor -- called when |preferences_| is initialized. |
| 157 void OnPreferencesInitialized(); | 110 void OnPreferencesInitialized(); |
| 158 | 111 |
| 159 // SelectFileDialog::Listener implementation: | 112 // SelectFileDialog::Listener implementation: |
| 160 virtual void FileSelected(const base::FilePath& path, | 113 virtual void FileSelected(const base::FilePath& path, |
| 161 int index, | 114 int index, |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 void SavePermissions(); | 156 void SavePermissions(); |
| 204 | 157 |
| 205 // Updates the model and view when |preferences_| changes. Some of the | 158 // Updates the model and view when |preferences_| changes. Some of the |
| 206 // possible changes includes a gallery getting blacklisted, or a new | 159 // possible changes includes a gallery getting blacklisted, or a new |
| 207 // auto detected gallery becoming available. | 160 // auto detected gallery becoming available. |
| 208 void UpdateGalleriesOnPreferencesEvent(); | 161 void UpdateGalleriesOnPreferencesEvent(); |
| 209 | 162 |
| 210 // Updates the model and view when a device is attached or detached. | 163 // Updates the model and view when a device is attached or detached. |
| 211 void UpdateGalleriesOnDeviceEvent(const std::string& device_id); | 164 void UpdateGalleriesOnDeviceEvent(const std::string& device_id); |
| 212 | 165 |
| 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); | 166 GalleryDialogId GetDialogId(MediaGalleryPrefId pref_id); |
| 167 MediaGalleryPrefId GetPrefId(GalleryDialogId id); |
| 218 | 168 |
| 219 Profile* GetProfile(); | 169 Profile* GetProfile(); |
| 220 | 170 |
| 221 // The web contents from which the request originated. | 171 // The web contents from which the request originated. |
| 222 content::WebContents* web_contents_; | 172 content::WebContents* web_contents_; |
| 223 | 173 |
| 224 // This is just a reference, but it's assumed that it won't become invalid | 174 // This is just a reference, but it's assumed that it won't become invalid |
| 225 // while the dialog is showing. | 175 // while the dialog is showing. |
| 226 const extensions::Extension* extension_; | 176 const extensions::Extension* extension_; |
| 227 | 177 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 252 // The view that's showing. | 202 // The view that's showing. |
| 253 scoped_ptr<MediaGalleriesDialog> dialog_; | 203 scoped_ptr<MediaGalleriesDialog> dialog_; |
| 254 | 204 |
| 255 scoped_refptr<ui::SelectFileDialog> select_folder_dialog_; | 205 scoped_refptr<ui::SelectFileDialog> select_folder_dialog_; |
| 256 | 206 |
| 257 scoped_ptr<MediaGalleryContextMenu> context_menu_; | 207 scoped_ptr<MediaGalleryContextMenu> context_menu_; |
| 258 | 208 |
| 259 // Creates the dialog. Only changed for unit tests. | 209 // Creates the dialog. Only changed for unit tests. |
| 260 CreateDialogCallback create_dialog_callback_; | 210 CreateDialogCallback create_dialog_callback_; |
| 261 | 211 |
| 262 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesDialogController); | 212 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesPermissionController); |
| 263 }; | 213 }; |
| 264 | 214 |
| 265 #endif // CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_GALLERIES_DIALOG_CONTROLLER_H_ | 215 #endif // CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_GALLERIES_PERMISSION_CONTROLLER_
H_ |
| OLD | NEW |