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.
5 #ifndef CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_GALLERIES_DIALOG_CONTROLLER_H_
6 #define CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_GALLERIES_DIALOG_CONTROLLER_H_
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"
23 namespace extensions
{
31 class MediaGalleriesDialogController
;
32 class MediaGalleryContextMenu
;
36 class MediaGalleriesDialog
{
38 virtual ~MediaGalleriesDialog();
40 // Tell the dialog to update its display list of galleries.
41 virtual void UpdateGalleries() = 0;
43 // Constructs a platform-specific dialog owned and controlled by |controller|.
44 static MediaGalleriesDialog
* Create(
45 MediaGalleriesDialogController
* controller
);
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
{
56 struct GalleryPermission
{
57 GalleryPermission(const MediaGalleryPrefInfo
& pref_info
, bool allowed
)
58 : pref_info(pref_info
), allowed(allowed
) {}
59 GalleryPermission() {}
61 MediaGalleryPrefInfo pref_info
;
65 typedef std::vector
<GalleryPermission
> GalleryPermissionsVector
;
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
);
72 // The title of the dialog view.
73 base::string16
GetHeader() const;
75 // Explanatory text directly below the title.
76 base::string16
GetSubtext() const;
78 // Header for unattached devices part of the dialog.
79 base::string16
GetUnattachedLocationsHeader() const;
81 // Initial state of whether the dialog's confirmation button will be enabled.
82 bool HasPermittedGalleries() const;
84 // Get the set of permissions to attached galleries.
85 virtual GalleryPermissionsVector
AttachedPermissions() const;
87 // Get the set of permissions to unattached galleries.
88 virtual GalleryPermissionsVector
UnattachedPermissions() const;
90 // Called when the add-folder button in the dialog is clicked.
91 virtual void OnAddFolderClicked();
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
,
97 virtual void DidToggleNewGallery(const MediaGalleryPrefInfo
& gallery
,
100 // The forget command in the context menu was selected.
101 virtual void DidForgetGallery(MediaGalleryPrefId pref_id
);
103 // The dialog is being deleted.
104 virtual void DialogFinished(bool accepted
);
106 virtual content::WebContents
* web_contents();
108 ui::MenuModel
* GetContextMenu(MediaGalleryPrefId id
);
111 friend class MediaGalleriesDialogControllerTest
;
113 typedef base::Callback
<MediaGalleriesDialog
* (
114 MediaGalleriesDialogController
*)> CreateDialogCallback
;
116 // For use with tests.
117 MediaGalleriesDialogController(
118 const extensions::Extension
& extension
,
119 MediaGalleriesPreferences
* preferences
,
120 const CreateDialogCallback
& create_dialog_callback
,
121 const base::Closure
& on_finish
);
123 virtual ~MediaGalleriesDialogController();
126 // This type keeps track of media galleries already known to the prefs system.
127 typedef std::map
<MediaGalleryPrefId
, GalleryPermission
>
128 KnownGalleryPermissions
;
130 // Bottom half of constructor -- called when |preferences_| is initialized.
131 void OnPreferencesInitialized();
133 // SelectFileDialog::Listener implementation:
134 virtual void FileSelected(const base::FilePath
& path
,
136 void* params
) OVERRIDE
;
138 // RemovableStorageObserver implementation.
139 // Used to keep dialog in sync with removable device status.
140 virtual void OnRemovableStorageAttached(const StorageInfo
& info
) OVERRIDE
;
141 virtual void OnRemovableStorageDetached(const StorageInfo
& info
) OVERRIDE
;
143 // MediaGalleriesPreferences::GalleryChangeObserver implementations.
144 // Used to keep the dialog in sync when the preferences change.
145 virtual void OnPermissionAdded(MediaGalleriesPreferences
* pref
,
146 const std::string
& extension_id
,
147 MediaGalleryPrefId pref_id
) OVERRIDE
;
148 virtual void OnPermissionRemoved(MediaGalleriesPreferences
* pref
,
149 const std::string
& extension_id
,
150 MediaGalleryPrefId pref_id
) OVERRIDE
;
151 virtual void OnGalleryAdded(MediaGalleriesPreferences
* pref
,
152 MediaGalleryPrefId pref_id
) OVERRIDE
;
153 virtual void OnGalleryRemoved(MediaGalleriesPreferences
* pref
,
154 MediaGalleryPrefId pref_id
) OVERRIDE
;
155 virtual void OnGalleryInfoUpdated(MediaGalleriesPreferences
* pref
,
156 MediaGalleryPrefId pref_id
) OVERRIDE
;
158 // Populates |known_galleries_| from |preferences_|. Subsequent calls merge
159 // into |known_galleries_| and do not change permissions for user toggled
161 void InitializePermissions();
163 // Saves state of |known_galleries_|, |new_galleries_| and
164 // |forgotten_gallery_ids_| to model.
166 // NOTE: possible states for a gallery:
167 // K N F (K = Known, N = New, F = Forgotten)
175 void SavePermissions();
177 // Updates the model and view when |preferences_| changes. Some of the
178 // possible changes includes a gallery getting blacklisted, or a new
179 // auto detected gallery becoming available.
180 void UpdateGalleriesOnPreferencesEvent();
182 // Updates the model and view when a device is attached or detached.
183 void UpdateGalleriesOnDeviceEvent(const std::string
& device_id
);
185 // Fill |permissions| with a sorted list of either attached or unattached
186 // gallery permissions.
187 void FillPermissions(bool attached
,
188 GalleryPermissionsVector
* permissions
) const;
190 Profile
* GetProfile();
192 // The web contents from which the request originated.
193 content::WebContents
* web_contents_
;
195 // This is just a reference, but it's assumed that it won't become invalid
196 // while the dialog is showing.
197 const extensions::Extension
* extension_
;
199 // This map excludes those galleries which have been blacklisted; it only
200 // counts active known galleries.
201 KnownGalleryPermissions known_galleries_
;
203 // Galleries in |known_galleries_| that the user have toggled.
204 MediaGalleryPrefIdSet toggled_galleries_
;
206 // Map of new galleries the user added, but have not saved. This list should
207 // never overlap with |known_galleries_|.
208 GalleryPermissionsVector new_galleries_
;
210 // Galleries in |known_galleries_| that the user has forgotten.
211 MediaGalleryPrefIdSet forgotten_gallery_ids_
;
213 // Callback to run when the dialog closes.
214 base::Closure on_finish_
;
216 // The model that tracks galleries and extensions' permissions.
217 // This is the authoritative source for gallery information.
218 MediaGalleriesPreferences
* preferences_
;
220 // The view that's showing.
221 scoped_ptr
<MediaGalleriesDialog
> dialog_
;
223 scoped_refptr
<ui::SelectFileDialog
> select_folder_dialog_
;
225 scoped_ptr
<MediaGalleryContextMenu
> context_menu_
;
227 // Creates the dialog. Only changed for unit tests.
228 CreateDialogCallback create_dialog_callback_
;
230 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesDialogController
);
233 #endif // CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_GALLERIES_DIALOG_CONTROLLER_H_