Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / media_galleries / media_galleries_scan_result_dialog_controller.h
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.
4
5 #ifndef CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_GALLERIES_SCAN_RESULT_DIALOG_CONTROLLER_H_
6 #define CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_GALLERIES_SCAN_RESULT_DIALOG_CONTROLLER_H_
7
8 #include <map>
9 #include <set>
10 #include <string>
11
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_preferences.h"
16 #include "components/storage_monitor/removable_storage_observer.h"
17
18 namespace content {
19 class WebContents;
20 }
21
22 namespace extensions {
23 class Extension;
24 }
25
26 namespace ui {
27 class MenuModel;
28 }
29
30 class MediaGalleriesScanResultDialogController;
31 class MediaGalleryContextMenu;
32 class Profile;
33
34 // The view.
35 class MediaGalleriesScanResultDialog {
36  public:
37   virtual ~MediaGalleriesScanResultDialog();
38
39   // Tell the dialog to update its display list of scan results.
40   virtual void UpdateResults() = 0;
41
42   // Constructs a platform-specific dialog owned and controlled by |controller|.
43   static MediaGalleriesScanResultDialog* Create(
44       MediaGalleriesScanResultDialogController* controller);
45 };
46
47 // The controller is responsible for handling the logic of the dialog and
48 // interfacing with the model (i.e., MediaGalleriesPreferences). It shows
49 // the dialog and owns itself.
50 class MediaGalleriesScanResultDialogController
51     : public RemovableStorageObserver,
52       public MediaGalleriesPreferences::GalleryChangeObserver {
53  public:
54   struct ScanResult {
55     ScanResult(const MediaGalleryPrefInfo& pref_info, bool selected)
56         : pref_info(pref_info),
57           selected(selected) {
58     }
59     ScanResult() : selected(false) {}
60
61     MediaGalleryPrefInfo pref_info;
62     bool selected;
63   };
64   typedef std::vector<ScanResult> OrderedScanResults;
65
66   // |preferences| must be already initialized.
67   static size_t ScanResultCountForExtension(
68       MediaGalleriesPreferences* preferences,
69       const extensions::Extension* extension);
70
71   // The constructor creates a dialog controller which owns itself.
72   MediaGalleriesScanResultDialogController(
73       content::WebContents* web_contents,
74       const extensions::Extension& extension,
75       const base::Closure& on_finish);
76
77   // The title of the dialog view.
78   base::string16 GetHeader() const;
79
80   // Explanatory text directly below the title.
81   base::string16 GetSubtext() const;
82
83   // Get the scan results and their current selection state.
84   virtual OrderedScanResults GetGalleryList() const;
85
86   // A checkbox beside a scan result was toggled.
87   virtual void DidToggleGalleryId(MediaGalleryPrefId pref_id, bool selected);
88
89   // A folder viewer icon was clicked.
90   virtual void DidClickOpenFolderViewer(MediaGalleryPrefId pref_id) const;
91
92   // The forget command in the context menu was selected.
93   virtual void DidForgetGallery(MediaGalleryPrefId pref_id);
94
95   // The dialog is being deleted.
96   virtual void DialogFinished(bool accepted);
97
98   virtual content::WebContents* web_contents();
99
100   ui::MenuModel* GetContextMenu(MediaGalleryPrefId id);
101
102  protected:
103   typedef base::Callback<MediaGalleriesScanResultDialog* (
104       MediaGalleriesScanResultDialogController*)> CreateDialogCallback;
105   typedef std::map<MediaGalleryPrefId, ScanResult> ScanResults;
106
107   // Updates |scan_results| from |preferences|. Will not add galleries from
108   // |ignore_list| onto |scan_results|.
109   static void UpdateScanResultsFromPreferences(
110       MediaGalleriesPreferences* preferences,
111       const extensions::Extension* extension,
112       MediaGalleryPrefIdSet ignore_list,
113       ScanResults* scan_results);
114
115   // Used for unit tests.
116   MediaGalleriesScanResultDialogController(
117       const extensions::Extension& extension,
118       MediaGalleriesPreferences* preferences_,
119       const CreateDialogCallback& create_dialog_callback,
120       const base::Closure& on_finish);
121
122   virtual ~MediaGalleriesScanResultDialogController();
123
124  private:
125   friend class MediaGalleriesScanResultDialogControllerTest;
126   friend class MediaGalleriesScanResultDialogCocoaTest;
127
128   // Bottom half of constructor -- called when |preferences_| is initialized.
129   void OnPreferencesInitialized();
130
131   // Used to keep the dialog in sync with the preferences.
132   void OnPreferenceUpdate(const std::string& extension_id);
133
134   // Used to keep the dialog in sync with attached and detached devices.
135   void OnRemovableDeviceUpdate(const std::string device_id);
136
137   Profile* GetProfile() const;
138
139   // RemovableStorageObserver implementation.
140   // Used to keep dialog in sync with removable device status.
141   virtual void OnRemovableStorageAttached(const StorageInfo& info) OVERRIDE;
142   virtual void OnRemovableStorageDetached(const StorageInfo& info) OVERRIDE;
143
144   // MediaGalleriesPreferences::GalleryChangeObserver implementations.
145   // Used to keep the dialog in sync when the preferences change.
146   virtual void OnPermissionAdded(MediaGalleriesPreferences* pref,
147                                  const std::string& extension_id,
148                                  MediaGalleryPrefId pref_id) OVERRIDE;
149   virtual void OnPermissionRemoved(MediaGalleriesPreferences* pref,
150                                    const std::string& extension_id,
151                                    MediaGalleryPrefId pref_id) OVERRIDE;
152   virtual void OnGalleryAdded(MediaGalleriesPreferences* pref,
153                               MediaGalleryPrefId pref_id) OVERRIDE;
154   virtual void OnGalleryRemoved(MediaGalleriesPreferences* pref,
155                                 MediaGalleryPrefId pref_id) OVERRIDE;
156   virtual void OnGalleryInfoUpdated(MediaGalleriesPreferences* pref,
157                                     MediaGalleryPrefId pref_id) OVERRIDE;
158
159   // The web contents from which the request originated.
160   content::WebContents* web_contents_;
161
162   // This is just a reference, but it's assumed that it won't become invalid
163   // while the dialog is showing.
164   const extensions::Extension* extension_;
165
166   // The scan results that aren't blacklisted and this extension doesn't
167   // already have access to.
168   ScanResults scan_results_;
169
170   // The set of scan results which should be removed (blacklisted) - unless
171   // the user clicks Cancel.
172   MediaGalleryPrefIdSet results_to_remove_;
173
174   // Callback to run when the dialog closes.
175   base::Closure on_finish_;
176
177   // The model that tracks galleries and extensions' permissions.
178   // This is the authoritative source for gallery information.
179   MediaGalleriesPreferences* preferences_;
180
181   // Creates the dialog. Only changed for unit tests.
182   CreateDialogCallback create_dialog_callback_;
183
184   // The view that's showing.
185   scoped_ptr<MediaGalleriesScanResultDialog> dialog_;
186
187   scoped_ptr<MediaGalleryContextMenu> context_menu_;
188
189   DISALLOW_COPY_AND_ASSIGN(MediaGalleriesScanResultDialogController);
190 };
191
192 #endif  // CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_GALLERIES_SCAN_RESULT_DIALOG_CONTROLLER_H_