Home | History | Annotate | Download | only in media_galleries
      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 "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"
     18 
     19 namespace content {
     20 class WebContents;
     21 }
     22 
     23 namespace extensions {
     24 class Extension;
     25 }
     26 
     27 namespace chrome {
     28 
     29 class MediaGalleriesDialogController;
     30 
     31 // The view.
     32 class MediaGalleriesDialog {
     33  public:
     34   virtual ~MediaGalleriesDialog();
     35 
     36   // Updates the entry for |gallery| with the checkbox set to the value in
     37   // |permitted|. |gallery| is owned by the controller and is guaranteed to
     38   // live longer than the dialog. If the entry does not already exist, it
     39   // should be created.
     40   virtual void UpdateGallery(const MediaGalleryPrefInfo& gallery,
     41                              bool permitted) = 0;
     42 
     43   // If there exists an entry for |gallery|, it should be removed.
     44   virtual void ForgetGallery(MediaGalleryPrefId gallery) = 0;
     45 
     46   // Constructs a platform-specific dialog owned and controlled by |controller|.
     47   static MediaGalleriesDialog* Create(
     48       MediaGalleriesDialogController* controller);
     49 };
     50 
     51 // The controller is responsible for handling the logic of the dialog and
     52 // interfacing with the model (i.e., MediaGalleriesPreferences). It shows
     53 // the dialog and owns itself.
     54 class MediaGalleriesDialogController
     55     : public ui::SelectFileDialog::Listener,
     56       public RemovableStorageObserver,
     57       public MediaGalleriesPreferences::GalleryChangeObserver {
     58  public:
     59   struct GalleryPermission {
     60     GalleryPermission(const MediaGalleryPrefInfo& pref_info, bool allowed)
     61         : pref_info(pref_info), allowed(allowed) {}
     62     GalleryPermission() {}
     63 
     64     MediaGalleryPrefInfo pref_info;
     65     bool allowed;
     66   };
     67 
     68   typedef std::vector<GalleryPermission> GalleryPermissionsVector;
     69 
     70   // The constructor creates a dialog controller which owns itself.
     71   MediaGalleriesDialogController(content::WebContents* web_contents,
     72                                  const extensions::Extension& extension,
     73                                  const base::Closure& on_finish);
     74 
     75   // The title of the dialog view.
     76   string16 GetHeader() const;
     77 
     78   // Explanatory text directly below the title.
     79   string16 GetSubtext() const;
     80 
     81   // Header for unattached devices part of the dialog.
     82   string16 GetUnattachedLocationsHeader() const;
     83 
     84   // Initial state of whether the dialog's confirmation button will be enabled.
     85   bool HasPermittedGalleries() const;
     86 
     87   // Get the set of permissions to attached galleries.
     88   virtual GalleryPermissionsVector AttachedPermissions() const;
     89 
     90   // Get the set of permissions to unattached galleries.
     91   virtual GalleryPermissionsVector UnattachedPermissions() const;
     92 
     93   // Called when the add-folder button in the dialog is clicked.
     94   virtual void OnAddFolderClicked();
     95 
     96   // A checkbox beside a gallery permission was checked. The full set
     97   // of gallery permissions checkbox settings is sent on every checkbox toggle.
     98   virtual void DidToggleGalleryId(MediaGalleryPrefId pref_id,
     99                                   bool enabled);
    100 
    101   // The dialog is being deleted.
    102   virtual void DialogFinished(bool accepted);
    103 
    104   virtual content::WebContents* web_contents();
    105 
    106  protected:
    107   // For use with tests.
    108   explicit MediaGalleriesDialogController(
    109       const extensions::Extension& extension);
    110 
    111   virtual ~MediaGalleriesDialogController();
    112 
    113  private:
    114   // This type keeps track of media galleries already known to the prefs system.
    115   typedef std::map<MediaGalleryPrefId, GalleryPermission>
    116       KnownGalleryPermissions;
    117 
    118   // Bottom half of constructor -- called when the storage monitor
    119   // is initialized.
    120   void OnStorageMonitorInitialized();
    121 
    122   // SelectFileDialog::Listener implementation:
    123   virtual void FileSelected(const base::FilePath& path,
    124                             int index,
    125                             void* params) OVERRIDE;
    126 
    127   // RemovableStorageObserver implementation.
    128   // Used to keep dialog in sync with removable device status.
    129   virtual void OnRemovableStorageAttached(const StorageInfo& info) OVERRIDE;
    130   virtual void OnRemovableStorageDetached(const StorageInfo& info) OVERRIDE;
    131 
    132   // MediaGalleriesPreferences::GalleryChangeObserver implementation.
    133   // Used to keep the dialog in sync when the preferences change.
    134   virtual void OnGalleryChanged(MediaGalleriesPreferences* pref,
    135                                 const std::string& extension_id,
    136                                 MediaGalleryPrefId pref_id,
    137                                 bool has_permission) OVERRIDE;
    138 
    139   // Populates |known_galleries_| from |preferences_|. Subsequent calls merge
    140   // into |known_galleries_| and do not change permissions for user toggled
    141   // galleries.
    142   void InitializePermissions();
    143 
    144   // Saves state of |known_galleries_| and |new_galleries_| to model.
    145   void SavePermissions();
    146 
    147   // Updates the model and view when |preferences_| changes. Some of the
    148   // possible changes includes a gallery getting blacklisted, or a new
    149   // auto detected gallery becoming available.
    150   void UpdateGalleriesOnPreferencesEvent();
    151 
    152   // Updates the model and view when a device is attached or detached.
    153   void UpdateGalleriesOnDeviceEvent(const std::string& device_id);
    154 
    155   // Fill |permissions| with a sorted list of either attached or unattached
    156   // gallery permissions.
    157   void FillPermissions(bool attached,
    158                        GalleryPermissionsVector* permissions) const;
    159 
    160   // The web contents from which the request originated.
    161   content::WebContents* web_contents_;
    162 
    163   // This is just a reference, but it's assumed that it won't become invalid
    164   // while the dialog is showing.
    165   const extensions::Extension* extension_;
    166 
    167   // This map excludes those galleries which have been blacklisted; it only
    168   // counts active known galleries.
    169   KnownGalleryPermissions known_galleries_;
    170 
    171   // Galleries in |known_galleries_| that the user have toggled.
    172   MediaGalleryPrefIdSet toggled_galleries_;
    173 
    174   // Map of new galleries the user added, but have not saved. This list should
    175   // never overlap with |known_galleries_|.
    176   GalleryPermissionsVector new_galleries_;
    177 
    178   // Callback to run when the dialog closes.
    179   base::Closure on_finish_;
    180 
    181   // The model that tracks galleries and extensions' permissions.
    182   // This is the authoritative source for gallery information.
    183   MediaGalleriesPreferences* preferences_;
    184 
    185   // The view that's showing.
    186   scoped_ptr<MediaGalleriesDialog> dialog_;
    187 
    188   scoped_refptr<ui::SelectFileDialog> select_folder_dialog_;
    189 
    190   DISALLOW_COPY_AND_ASSIGN(MediaGalleriesDialogController);
    191 };
    192 
    193 }  // namespace chrome
    194 
    195 #endif  // CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_GALLERIES_DIALOG_CONTROLLER_H_
    196