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 3 // found in the LICENSE file. 4 5 #ifndef CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_GALLERIES_DIALOG_CONTROLLER_H_ 6 #define CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_GALLERIES_DIALOG_CONTROLLER_H_ 7 8 #include <list> 9 #include <map> 10 11 #include "base/callback.h" 12 #include "base/memory/scoped_ptr.h" 13 #include "base/strings/string16.h" 14 #include "chrome/browser/media_galleries/media_galleries_preferences.h" 15 #include "chrome/browser/storage_monitor/removable_storage_observer.h" 16 #include "ui/gfx/native_widget_types.h" 17 #include "ui/shell_dialogs/select_file_dialog.h" 18 19 namespace content { 20 class WebContents; 21 } 22 23 namespace extensions { 24 class Extension; 25 } 26 27 namespace ui { 28 class MenuModel; 29 } 30 31 class GalleryContextMenuModel; 32 class MediaGalleriesDialogController; 33 class Profile; 34 35 // The view. 36 class MediaGalleriesDialog { 37 public: 38 virtual ~MediaGalleriesDialog(); 39 40 // Tell the dialog to update its display list of galleries. 41 virtual void UpdateGalleries() = 0; 42 43 // Constructs a platform-specific dialog owned and controlled by |controller|. 44 static MediaGalleriesDialog* Create( 45 MediaGalleriesDialogController* controller); 46 }; 47 48 // The controller is responsible for handling the logic of the dialog and 49 // interfacing with the model (i.e., MediaGalleriesPreferences). It shows 50 // the dialog and owns itself. 51 class MediaGalleriesDialogController 52 : public ui::SelectFileDialog::Listener, 53 public RemovableStorageObserver, 54 public MediaGalleriesPreferences::GalleryChangeObserver { 55 public: 56 struct GalleryPermission { GalleryPermissionGalleryPermission57 GalleryPermission(const MediaGalleryPrefInfo& pref_info, bool allowed) 58 : pref_info(pref_info), allowed(allowed) {} GalleryPermissionGalleryPermission59 GalleryPermission() {} 60 61 MediaGalleryPrefInfo pref_info; 62 bool allowed; 63 }; 64 65 typedef std::vector<GalleryPermission> GalleryPermissionsVector; 66 67 // The constructor creates a dialog controller which owns itself. 68 MediaGalleriesDialogController(content::WebContents* web_contents, 69 const extensions::Extension& extension, 70 const base::Closure& on_finish); 71 72 // The title of the dialog view. 73 base::string16 GetHeader() const; 74 75 // Explanatory text directly below the title. 76 base::string16 GetSubtext() const; 77 78 // Header for unattached devices part of the dialog. 79 base::string16 GetUnattachedLocationsHeader() const; 80 81 // Initial state of whether the dialog's confirmation button will be enabled. 82 bool HasPermittedGalleries() const; 83 84 // Get the set of permissions to attached galleries. 85 virtual GalleryPermissionsVector AttachedPermissions() const; 86 87 // Get the set of permissions to unattached galleries. 88 virtual GalleryPermissionsVector UnattachedPermissions() const; 89 90 // Called when the add-folder button in the dialog is clicked. 91 virtual void OnAddFolderClicked(); 92 93 // A checkbox beside a gallery permission was checked. The full set 94 // of gallery permissions checkbox settings is sent on every checkbox toggle. 95 virtual void DidToggleGalleryId(MediaGalleryPrefId pref_id, 96 bool enabled); 97 virtual void DidToggleNewGallery(const MediaGalleryPrefInfo& gallery, 98 bool enabled); 99 100 // The forget command in the context menu was selected. 101 virtual void DidForgetGallery(MediaGalleryPrefId pref_id); 102 103 // The dialog is being deleted. 104 virtual void DialogFinished(bool accepted); 105 106 virtual content::WebContents* web_contents(); 107 108 ui::MenuModel* GetContextMenuModel(MediaGalleryPrefId id); 109 110 protected: 111 // For use with tests. 112 explicit MediaGalleriesDialogController( 113 const extensions::Extension& extension); 114 115 virtual ~MediaGalleriesDialogController(); 116 117 private: 118 // This type keeps track of media galleries already known to the prefs system. 119 typedef std::map<MediaGalleryPrefId, GalleryPermission> 120 KnownGalleryPermissions; 121 122 // Bottom half of constructor -- called when |preferences_| is initialized. 123 void OnPreferencesInitialized(); 124 125 // SelectFileDialog::Listener implementation: 126 virtual void FileSelected(const base::FilePath& path, 127 int index, 128 void* params) OVERRIDE; 129 130 // RemovableStorageObserver implementation. 131 // Used to keep dialog in sync with removable device status. 132 virtual void OnRemovableStorageAttached(const StorageInfo& info) OVERRIDE; 133 virtual void OnRemovableStorageDetached(const StorageInfo& info) OVERRIDE; 134 135 // MediaGalleriesPreferences::GalleryChangeObserver implementations. 136 // Used to keep the dialog in sync when the preferences change. 137 virtual void OnPermissionAdded(MediaGalleriesPreferences* pref, 138 const std::string& extension_id, 139 MediaGalleryPrefId pref_id) OVERRIDE; 140 virtual void OnPermissionRemoved(MediaGalleriesPreferences* pref, 141 const std::string& extension_id, 142 MediaGalleryPrefId pref_id) OVERRIDE; 143 virtual void OnGalleryAdded(MediaGalleriesPreferences* pref, 144 MediaGalleryPrefId pref_id) OVERRIDE; 145 virtual void OnGalleryRemoved(MediaGalleriesPreferences* pref, 146 MediaGalleryPrefId pref_id) OVERRIDE; 147 virtual void OnGalleryInfoUpdated(MediaGalleriesPreferences* pref, 148 MediaGalleryPrefId pref_id) OVERRIDE; 149 150 // Populates |known_galleries_| from |preferences_|. Subsequent calls merge 151 // into |known_galleries_| and do not change permissions for user toggled 152 // galleries. 153 void InitializePermissions(); 154 155 // Saves state of |known_galleries_| and |new_galleries_| to model. 156 void SavePermissions(); 157 158 // Updates the model and view when |preferences_| changes. Some of the 159 // possible changes includes a gallery getting blacklisted, or a new 160 // auto detected gallery becoming available. 161 void UpdateGalleriesOnPreferencesEvent(); 162 163 // Updates the model and view when a device is attached or detached. 164 void UpdateGalleriesOnDeviceEvent(const std::string& device_id); 165 166 // Fill |permissions| with a sorted list of either attached or unattached 167 // gallery permissions. 168 void FillPermissions(bool attached, 169 GalleryPermissionsVector* permissions) const; 170 171 Profile* GetProfile(); 172 173 // The web contents from which the request originated. 174 content::WebContents* web_contents_; 175 176 // This is just a reference, but it's assumed that it won't become invalid 177 // while the dialog is showing. 178 const extensions::Extension* extension_; 179 180 // This map excludes those galleries which have been blacklisted; it only 181 // counts active known galleries. 182 KnownGalleryPermissions known_galleries_; 183 184 // Galleries in |known_galleries_| that the user have toggled. 185 MediaGalleryPrefIdSet toggled_galleries_; 186 187 // Map of new galleries the user added, but have not saved. This list should 188 // never overlap with |known_galleries_|. 189 GalleryPermissionsVector new_galleries_; 190 191 // Callback to run when the dialog closes. 192 base::Closure on_finish_; 193 194 // The model that tracks galleries and extensions' permissions. 195 // This is the authoritative source for gallery information. 196 MediaGalleriesPreferences* preferences_; 197 198 // The view that's showing. 199 scoped_ptr<MediaGalleriesDialog> dialog_; 200 201 scoped_refptr<ui::SelectFileDialog> select_folder_dialog_; 202 203 scoped_ptr<ui::MenuModel> context_menu_model_; 204 scoped_ptr<GalleryContextMenuModel> gallery_menu_model_; 205 206 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesDialogController); 207 }; 208 209 #endif // CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_GALLERIES_DIALOG_CONTROLLER_H_ 210