Extract code handling PrinterProviderAPI from PrintPreviewHandler
[chromium-blink-merge.git] / chrome / browser / media_galleries / media_galleries_permission_controller.h
blobf23205e3ff22eb5e95042a892df77fcfc2a85944
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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_GALLERIES_PERMISSION_CONTROLLER_H_
6 #define CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_GALLERIES_PERMISSION_CONTROLLER_H_
8 #include <map>
9 #include <string>
10 #include <vector>
12 #include "base/callback.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/strings/string16.h"
15 #include "chrome/browser/media_galleries/media_galleries_dialog_controller.h"
16 #include "chrome/browser/media_galleries/media_galleries_preferences.h"
17 #include "components/storage_monitor/removable_storage_observer.h"
18 #include "ui/gfx/native_widget_types.h"
19 #include "ui/shell_dialogs/select_file_dialog.h"
21 namespace content {
22 class WebContents;
25 namespace extensions {
26 class Extension;
29 namespace ui {
30 class MenuModel;
33 class MediaGalleriesDialogController;
34 class MediaGalleryContextMenu;
35 class Profile;
37 // Newly added galleries are not added to preferences until the dialog commits,
38 // so they do not have a pref id while the dialog is open; leading to
39 // complicated code in the dialogs. To solve this complication, the controller
40 // maps pref ids into a new space where it can also assign ids to new galleries.
41 // The new number space is only valid for the lifetime of the controller. To
42 // make it more clear where real pref ids are used and where the fake ids are
43 // used, the GalleryDialogId type is used where fake ids are needed.
44 typedef MediaGalleryPrefId GalleryDialogId;
46 class MediaGalleriesPermissionController
47 : public MediaGalleriesDialogController,
48 public ui::SelectFileDialog::Listener,
49 public storage_monitor::RemovableStorageObserver,
50 public MediaGalleriesPreferences::GalleryChangeObserver {
51 public:
52 // The constructor creates a dialog controller which owns itself.
53 MediaGalleriesPermissionController(content::WebContents* web_contents,
54 const extensions::Extension& extension,
55 const base::Closure& on_finish);
57 // MediaGalleriesDialogController implementation.
58 base::string16 GetHeader() const override;
59 base::string16 GetSubtext() const override;
60 bool IsAcceptAllowed() const override;
61 bool ShouldShowFolderViewer(const Entry& entry) const override;
62 std::vector<base::string16> GetSectionHeaders() const override;
63 Entries GetSectionEntries(size_t index) const override;
64 // Auxiliary button for this dialog is the 'Add Folder' button.
65 base::string16 GetAuxiliaryButtonText() const override;
66 void DidClickAuxiliaryButton() override;
67 void DidToggleEntry(GalleryDialogId gallery_id, bool selected) override;
68 void DidClickOpenFolderViewer(GalleryDialogId gallery_id) override;
69 void DidForgetEntry(GalleryDialogId gallery_id) override;
70 base::string16 GetAcceptButtonText() const override;
71 void DialogFinished(bool accepted) override;
72 ui::MenuModel* GetContextMenu(GalleryDialogId gallery_id) override;
73 content::WebContents* WebContents() override;
75 protected:
76 friend class MediaGalleriesPermissionControllerTest;
78 typedef base::Callback<MediaGalleriesDialog* (
79 MediaGalleriesDialogController*)> CreateDialogCallback;
81 // For use with tests.
82 MediaGalleriesPermissionController(
83 const extensions::Extension& extension,
84 MediaGalleriesPreferences* preferences,
85 const CreateDialogCallback& create_dialog_callback,
86 const base::Closure& on_finish);
88 ~MediaGalleriesPermissionController() override;
90 private:
91 // This type keeps track of media galleries already known to the prefs system.
92 typedef std::map<GalleryDialogId, Entry> GalleryPermissionsMap;
93 typedef std::map<GalleryDialogId, bool /*permitted*/> ToggledGalleryMap;
95 class DialogIdMap {
96 public:
97 DialogIdMap();
98 ~DialogIdMap();
99 GalleryDialogId GetDialogId(MediaGalleryPrefId pref_id);
100 MediaGalleryPrefId GetPrefId(GalleryDialogId id) const;
102 private:
103 GalleryDialogId next_dialog_id_;
104 std::map<MediaGalleryPrefId, GalleryDialogId> back_map_;
105 std::vector<MediaGalleryPrefId> forward_mapping_;
106 DISALLOW_COPY_AND_ASSIGN(DialogIdMap);
110 // Bottom half of constructor -- called when |preferences_| is initialized.
111 void OnPreferencesInitialized();
113 // SelectFileDialog::Listener implementation:
114 void FileSelected(const base::FilePath& path,
115 int index,
116 void* params) override;
118 // RemovableStorageObserver implementation.
119 // Used to keep dialog in sync with removable device status.
120 void OnRemovableStorageAttached(
121 const storage_monitor::StorageInfo& info) override;
122 void OnRemovableStorageDetached(
123 const storage_monitor::StorageInfo& info) override;
125 // MediaGalleriesPreferences::GalleryChangeObserver implementations.
126 // Used to keep the dialog in sync when the preferences change.
127 void OnPermissionAdded(MediaGalleriesPreferences* pref,
128 const std::string& extension_id,
129 MediaGalleryPrefId pref_id) override;
130 void OnPermissionRemoved(MediaGalleriesPreferences* pref,
131 const std::string& extension_id,
132 MediaGalleryPrefId pref_id) override;
133 void OnGalleryAdded(MediaGalleriesPreferences* pref,
134 MediaGalleryPrefId pref_id) override;
135 void OnGalleryRemoved(MediaGalleriesPreferences* pref,
136 MediaGalleryPrefId pref_id) override;
137 void OnGalleryInfoUpdated(MediaGalleriesPreferences* pref,
138 MediaGalleryPrefId pref_id) override;
140 // Populates |known_galleries_| from |preferences_|. Subsequent calls merge
141 // into |known_galleries_| and do not change permissions for user toggled
142 // galleries.
143 void InitializePermissions();
145 // Saves state of |known_galleries_|, |new_galleries_| and
146 // |forgotten_galleries_| to model.
148 // NOTE: possible states for a gallery:
149 // K N F (K = Known, N = New, F = Forgotten)
150 // +---+---+---+
151 // | Y | N | N |
152 // +---+---+---+
153 // | N | Y | N |
154 // +---+---+---+
155 // | Y | N | Y |
156 // +---+---+---+
157 void SavePermissions();
159 // Updates the model and view when |preferences_| changes. Some of the
160 // possible changes includes a gallery getting blacklisted, or a new
161 // auto detected gallery becoming available.
162 void UpdateGalleriesOnPreferencesEvent();
164 // Updates the model and view when a device is attached or detached.
165 void UpdateGalleriesOnDeviceEvent(const std::string& device_id);
167 GalleryDialogId GetDialogId(MediaGalleryPrefId pref_id);
168 MediaGalleryPrefId GetPrefId(GalleryDialogId id) const;
170 Profile* GetProfile();
172 // The web contents from which the request originated.
173 content::WebContents* web_contents_;
175 // This is just a reference, but it's assumed that it won't become invalid
176 // while the dialog is showing.
177 const extensions::Extension* extension_;
179 // Mapping between pref ids and dialog ids.
180 DialogIdMap id_map_;
182 // This map excludes those galleries which have been blacklisted; it only
183 // counts active known galleries.
184 GalleryPermissionsMap known_galleries_;
186 // Galleries in |known_galleries_| that the user have toggled.
187 ToggledGalleryMap toggled_galleries_;
189 // The current set of permitted galleries (according to prefs).
190 MediaGalleryPrefIdSet pref_permitted_galleries_;
192 // Map of new galleries the user added, but have not saved. This list should
193 // never overlap with |known_galleries_|.
194 GalleryPermissionsMap new_galleries_;
196 // Galleries in |known_galleries_| that the user has forgotten.
197 std::set<GalleryDialogId> forgotten_galleries_;
199 // Callback to run when the dialog closes.
200 base::Closure on_finish_;
202 // The model that tracks galleries and extensions' permissions.
203 // This is the authoritative source for gallery information.
204 MediaGalleriesPreferences* preferences_;
206 // The view that's showing.
207 scoped_ptr<MediaGalleriesDialog> dialog_;
209 scoped_refptr<ui::SelectFileDialog> select_folder_dialog_;
211 scoped_ptr<MediaGalleryContextMenu> context_menu_;
213 // Creates the dialog. Only changed for unit tests.
214 CreateDialogCallback create_dialog_callback_;
216 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesPermissionController);
219 #endif // CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_GALLERIES_PERMISSION_CONTROLLER_H_