Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / media_galleries / media_galleries_dialog_controller.h
bloba56731589ae071578e77b36c278d92ce1c5fa24c
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_
8 #include <list>
9 #include <map>
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"
19 namespace content {
20 class WebContents;
23 namespace extensions {
24 class Extension;
27 namespace ui {
28 class MenuModel;
31 class GalleryContextMenuModel;
32 class MediaGalleriesDialogController;
33 class Profile;
35 // The view.
36 class MediaGalleriesDialog {
37 public:
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 {
55 public:
56 struct GalleryPermission {
57 GalleryPermission(const MediaGalleryPrefInfo& pref_info, bool allowed)
58 : pref_info(pref_info), allowed(allowed) {}
59 GalleryPermission() {}
61 MediaGalleryPrefInfo pref_info;
62 bool allowed;
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,
96 bool enabled);
97 virtual void DidToggleNewGallery(const MediaGalleryPrefInfo& gallery,
98 bool enabled);
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* GetContextMenuModel(MediaGalleryPrefId id);
110 protected:
111 // For use with tests.
112 explicit MediaGalleriesDialogController(
113 const extensions::Extension& extension);
115 virtual ~MediaGalleriesDialogController();
117 private:
118 // This type keeps track of media galleries already known to the prefs system.
119 typedef std::map<MediaGalleryPrefId, GalleryPermission>
120 KnownGalleryPermissions;
122 // Bottom half of constructor -- called when |preferences_| is initialized.
123 void OnPreferencesInitialized();
125 // SelectFileDialog::Listener implementation:
126 virtual void FileSelected(const base::FilePath& path,
127 int index,
128 void* params) OVERRIDE;
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;
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;
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();
155 // Saves state of |known_galleries_| and |new_galleries_| to model.
156 void SavePermissions();
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();
163 // Updates the model and view when a device is attached or detached.
164 void UpdateGalleriesOnDeviceEvent(const std::string& device_id);
166 // Fill |permissions| with a sorted list of either attached or unattached
167 // gallery permissions.
168 void FillPermissions(bool attached,
169 GalleryPermissionsVector* permissions) const;
171 Profile* GetProfile();
173 // The web contents from which the request originated.
174 content::WebContents* web_contents_;
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_;
180 // This map excludes those galleries which have been blacklisted; it only
181 // counts active known galleries.
182 KnownGalleryPermissions known_galleries_;
184 // Galleries in |known_galleries_| that the user have toggled.
185 MediaGalleryPrefIdSet toggled_galleries_;
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_;
191 // Callback to run when the dialog closes.
192 base::Closure on_finish_;
194 // The model that tracks galleries and extensions' permissions.
195 // This is the authoritative source for gallery information.
196 MediaGalleriesPreferences* preferences_;
198 // The view that's showing.
199 scoped_ptr<MediaGalleriesDialog> dialog_;
201 scoped_refptr<ui::SelectFileDialog> select_folder_dialog_;
203 scoped_ptr<ui::MenuModel> context_menu_model_;
204 scoped_ptr<GalleryContextMenuModel> gallery_menu_model_;
206 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesDialogController);
209 #endif // CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_GALLERIES_DIALOG_CONTROLLER_H_