Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / media_galleries / media_galleries_dialog_controller.h
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 "components/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 MediaGalleriesDialogController;
32 class MediaGalleryContextMenu;
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 {
57     GalleryPermission(const MediaGalleryPrefInfo& pref_info, bool allowed)
58         : pref_info(pref_info), allowed(allowed) {}
59     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* GetContextMenu(MediaGalleryPrefId id);
109
110  protected:
111   friend class MediaGalleriesDialogControllerTest;
112
113   typedef base::Callback<MediaGalleriesDialog* (
114       MediaGalleriesDialogController*)> CreateDialogCallback;
115
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);
122
123   virtual ~MediaGalleriesDialogController();
124
125  private:
126   // This type keeps track of media galleries already known to the prefs system.
127   typedef std::map<MediaGalleryPrefId, GalleryPermission>
128       KnownGalleryPermissions;
129
130   // Bottom half of constructor -- called when |preferences_| is initialized.
131   void OnPreferencesInitialized();
132
133   // SelectFileDialog::Listener implementation:
134   virtual void FileSelected(const base::FilePath& path,
135                             int index,
136                             void* params) OVERRIDE;
137
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;
142
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;
157
158   // Populates |known_galleries_| from |preferences_|. Subsequent calls merge
159   // into |known_galleries_| and do not change permissions for user toggled
160   // galleries.
161   void InitializePermissions();
162
163   // Saves state of |known_galleries_|, |new_galleries_| and
164   // |forgotten_gallery_ids_| to model.
165   //
166   // NOTE: possible states for a gallery:
167   //   K   N   F   (K = Known, N = New, F = Forgotten)
168   // +---+---+---+
169   // | Y | N | N |
170   // +---+---+---+
171   // | N | Y | N |
172   // +---+---+---+
173   // | Y | N | Y |
174   // +---+---+---+
175   void SavePermissions();
176
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();
181
182   // Updates the model and view when a device is attached or detached.
183   void UpdateGalleriesOnDeviceEvent(const std::string& device_id);
184
185   // Fill |permissions| with a sorted list of either attached or unattached
186   // gallery permissions.
187   void FillPermissions(bool attached,
188                        GalleryPermissionsVector* permissions) const;
189
190   Profile* GetProfile();
191
192   // The web contents from which the request originated.
193   content::WebContents* web_contents_;
194
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_;
198
199   // This map excludes those galleries which have been blacklisted; it only
200   // counts active known galleries.
201   KnownGalleryPermissions known_galleries_;
202
203   // Galleries in |known_galleries_| that the user have toggled.
204   MediaGalleryPrefIdSet toggled_galleries_;
205
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_;
209
210   // Galleries in |known_galleries_| that the user has forgotten.
211   MediaGalleryPrefIdSet forgotten_gallery_ids_;
212
213   // Callback to run when the dialog closes.
214   base::Closure on_finish_;
215
216   // The model that tracks galleries and extensions' permissions.
217   // This is the authoritative source for gallery information.
218   MediaGalleriesPreferences* preferences_;
219
220   // The view that's showing.
221   scoped_ptr<MediaGalleriesDialog> dialog_;
222
223   scoped_refptr<ui::SelectFileDialog> select_folder_dialog_;
224
225   scoped_ptr<MediaGalleryContextMenu> context_menu_;
226
227   // Creates the dialog. Only changed for unit tests.
228   CreateDialogCallback create_dialog_callback_;
229
230   DISALLOW_COPY_AND_ASSIGN(MediaGalleriesDialogController);
231 };
232
233 #endif  // CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_GALLERIES_DIALOG_CONTROLLER_H_