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_SCAN_RESULT_DIALOG_CONTROLLER_H_
6 #define CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_GALLERIES_SCAN_RESULT_DIALOG_CONTROLLER_H_
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 "chrome/browser/storage_monitor/removable_storage_observer.h"
22 namespace extensions
{
30 class MediaGalleriesScanResultDialogController
;
31 class MediaGalleryContextMenu
;
35 class MediaGalleriesScanResultDialog
{
37 virtual ~MediaGalleriesScanResultDialog();
39 // Tell the dialog to update its display list of scan results.
40 virtual void UpdateResults() = 0;
42 // Constructs a platform-specific dialog owned and controlled by |controller|.
43 static MediaGalleriesScanResultDialog
* Create(
44 MediaGalleriesScanResultDialogController
* controller
);
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
{
55 ScanResult(const MediaGalleryPrefInfo
& pref_info
, bool selected
)
56 : pref_info(pref_info
),
59 ScanResult() : selected(false) {}
61 MediaGalleryPrefInfo pref_info
;
64 typedef std::vector
<ScanResult
> OrderedScanResults
;
66 // |preferences| must be already initialized.
67 static size_t ScanResultCountForExtension(
68 MediaGalleriesPreferences
* preferences
,
69 const extensions::Extension
* extension
);
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
);
77 // The title of the dialog view.
78 base::string16
GetHeader() const;
80 // Explanatory text directly below the title.
81 base::string16
GetSubtext() const;
83 // Get the scan results and their current selection state.
84 virtual OrderedScanResults
GetGalleryList() const;
86 // A checkbox beside a scan result was toggled.
87 virtual void DidToggleGalleryId(MediaGalleryPrefId pref_id
, bool selected
);
89 // A folder viewer icon was clicked.
90 virtual void DidClickOpenFolderViewer(MediaGalleryPrefId pref_id
) const;
92 // The forget command in the context menu was selected.
93 virtual void DidForgetGallery(MediaGalleryPrefId pref_id
);
95 // The dialog is being deleted.
96 virtual void DialogFinished(bool accepted
);
98 virtual content::WebContents
* web_contents();
100 ui::MenuModel
* GetContextMenu(MediaGalleryPrefId id
);
103 typedef base::Callback
<MediaGalleriesScanResultDialog
* (
104 MediaGalleriesScanResultDialogController
*)> CreateDialogCallback
;
105 typedef std::map
<MediaGalleryPrefId
, ScanResult
> ScanResults
;
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
);
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
);
122 virtual ~MediaGalleriesScanResultDialogController();
125 friend class MediaGalleriesScanResultDialogControllerTest
;
126 friend class MediaGalleriesScanResultDialogCocoaTest
;
128 // Bottom half of constructor -- called when |preferences_| is initialized.
129 void OnPreferencesInitialized();
131 // Used to keep the dialog in sync with the preferences.
132 void OnPreferenceUpdate(const std::string
& extension_id
);
134 // Used to keep the dialog in sync with attached and detached devices.
135 void OnRemovableDeviceUpdate(const std::string device_id
);
137 Profile
* GetProfile() const;
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
;
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
;
159 // The web contents from which the request originated.
160 content::WebContents
* web_contents_
;
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_
;
166 // The scan results that aren't blacklisted and this extension doesn't
167 // already have access to.
168 ScanResults scan_results_
;
170 // The set of scan results which should be removed (blacklisted) - unless
171 // the user clicks Cancel.
172 MediaGalleryPrefIdSet results_to_remove_
;
174 // Callback to run when the dialog closes.
175 base::Closure on_finish_
;
177 // The model that tracks galleries and extensions' permissions.
178 // This is the authoritative source for gallery information.
179 MediaGalleriesPreferences
* preferences_
;
181 // Creates the dialog. Only changed for unit tests.
182 CreateDialogCallback create_dialog_callback_
;
184 // The view that's showing.
185 scoped_ptr
<MediaGalleriesScanResultDialog
> dialog_
;
187 scoped_ptr
<MediaGalleryContextMenu
> context_menu_
;
189 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesScanResultDialogController
);
192 #endif // CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_GALLERIES_SCAN_RESULT_DIALOG_CONTROLLER_H_