Home | History | Annotate | Download | only in media_galleries
      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_CONTROLLER_H_
      6 #define CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_GALLERIES_SCAN_RESULT_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_dialog_controller.h"
     16 #include "chrome/browser/media_galleries/media_galleries_preferences.h"
     17 #include "components/storage_monitor/removable_storage_observer.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 MediaGalleriesScanResultController;
     32 class MediaGalleryContextMenu;
     33 class Profile;
     34 
     35 // The controller is responsible for handling the logic of the dialog and
     36 // interfacing with the model (i.e., MediaGalleriesPreferences). It shows
     37 // the dialog and owns itself.
     38 class MediaGalleriesScanResultController
     39     : public MediaGalleriesDialogController,
     40       public storage_monitor::RemovableStorageObserver,
     41       public MediaGalleriesPreferences::GalleryChangeObserver {
     42  public:
     43   // |preferences| must be already initialized.
     44   static size_t ScanResultCountForExtension(
     45       MediaGalleriesPreferences* preferences,
     46       const extensions::Extension* extension);
     47 
     48   // The constructor creates a dialog controller which owns itself.
     49   MediaGalleriesScanResultController(
     50       content::WebContents* web_contents,
     51       const extensions::Extension& extension,
     52       const base::Closure& on_finish);
     53 
     54   // MediaGalleriesDialogController implementation.
     55   virtual base::string16 GetHeader() const OVERRIDE;
     56   virtual base::string16 GetSubtext() const OVERRIDE;
     57   virtual bool IsAcceptAllowed() const OVERRIDE;
     58   virtual bool ShouldShowFolderViewer(const Entry& entry) const OVERRIDE;
     59   virtual std::vector<base::string16> GetSectionHeaders() const OVERRIDE;
     60   virtual Entries GetSectionEntries(size_t index) const OVERRIDE;
     61   virtual base::string16 GetAuxiliaryButtonText() const OVERRIDE;
     62   virtual void DidClickAuxiliaryButton() OVERRIDE;
     63   virtual void DidToggleEntry(MediaGalleryPrefId id, bool selected) OVERRIDE;
     64   virtual void DidClickOpenFolderViewer(MediaGalleryPrefId id) OVERRIDE;
     65   virtual void DidForgetEntry(MediaGalleryPrefId id) OVERRIDE;
     66   virtual base::string16 GetAcceptButtonText() const OVERRIDE;
     67   virtual void DialogFinished(bool accepted) OVERRIDE;
     68   virtual ui::MenuModel* GetContextMenu(MediaGalleryPrefId id) OVERRIDE;
     69   virtual content::WebContents* WebContents() OVERRIDE;
     70 
     71  protected:
     72   typedef base::Callback<MediaGalleriesDialog* (
     73       MediaGalleriesDialogController*)> CreateDialogCallback;
     74   typedef std::map<MediaGalleryPrefId, Entry> ScanResults;
     75 
     76   // Updates |scan_results| from |preferences|. Will not add galleries from
     77   // |ignore_list| onto |scan_results|.
     78   static void UpdateScanResultsFromPreferences(
     79       MediaGalleriesPreferences* preferences,
     80       const extensions::Extension* extension,
     81       MediaGalleryPrefIdSet ignore_list,
     82       ScanResults* scan_results);
     83 
     84   // Used for unit tests.
     85   MediaGalleriesScanResultController(
     86       const extensions::Extension& extension,
     87       MediaGalleriesPreferences* preferences_,
     88       const CreateDialogCallback& create_dialog_callback,
     89       const base::Closure& on_finish);
     90 
     91   virtual ~MediaGalleriesScanResultController();
     92 
     93  private:
     94   friend class MediaGalleriesScanResultControllerTest;
     95   friend class MediaGalleriesScanResultCocoaTest;
     96   friend class TestMediaGalleriesAddScanResultsFunction;
     97 
     98   // Bottom half of constructor -- called when |preferences_| is initialized.
     99   void OnPreferencesInitialized();
    100 
    101   // Used to keep the dialog in sync with the preferences.
    102   void OnPreferenceUpdate(const std::string& extension_id);
    103 
    104   // Used to keep the dialog in sync with attached and detached devices.
    105   void OnRemovableDeviceUpdate(const std::string device_id);
    106 
    107   Profile* GetProfile() const;
    108 
    109   // RemovableStorageObserver implementation.
    110   // Used to keep dialog in sync with removable device status.
    111   virtual void OnRemovableStorageAttached(
    112       const storage_monitor::StorageInfo& info) OVERRIDE;
    113   virtual void OnRemovableStorageDetached(
    114       const storage_monitor::StorageInfo& info) OVERRIDE;
    115 
    116   // MediaGalleriesPreferences::GalleryChangeObserver implementations.
    117   // Used to keep the dialog in sync when the preferences change.
    118   virtual void OnPermissionAdded(MediaGalleriesPreferences* pref,
    119                                  const std::string& extension_id,
    120                                  MediaGalleryPrefId pref_id) OVERRIDE;
    121   virtual void OnPermissionRemoved(MediaGalleriesPreferences* pref,
    122                                    const std::string& extension_id,
    123                                    MediaGalleryPrefId pref_id) OVERRIDE;
    124   virtual void OnGalleryAdded(MediaGalleriesPreferences* pref,
    125                               MediaGalleryPrefId pref_id) OVERRIDE;
    126   virtual void OnGalleryRemoved(MediaGalleriesPreferences* pref,
    127                                 MediaGalleryPrefId pref_id) OVERRIDE;
    128   virtual void OnGalleryInfoUpdated(MediaGalleriesPreferences* pref,
    129                                     MediaGalleryPrefId pref_id) OVERRIDE;
    130 
    131   // The web contents from which the request originated.
    132   content::WebContents* web_contents_;
    133 
    134   // This is just a reference, but it's assumed that it won't become invalid
    135   // while the dialog is showing.
    136   const extensions::Extension* extension_;
    137 
    138   // The scan results that aren't blacklisted and this extension doesn't
    139   // already have access to.
    140   ScanResults scan_results_;
    141 
    142   // The set of scan results which should be removed (blacklisted) - unless
    143   // the user clicks Cancel.
    144   MediaGalleryPrefIdSet results_to_remove_;
    145 
    146   // Callback to run when the dialog closes.
    147   base::Closure on_finish_;
    148 
    149   // The model that tracks galleries and extensions' permissions.
    150   // This is the authoritative source for gallery information.
    151   MediaGalleriesPreferences* preferences_;
    152 
    153   // Creates the dialog. Only changed for unit tests.
    154   CreateDialogCallback create_dialog_callback_;
    155 
    156   // The view that's showing.
    157   scoped_ptr<MediaGalleriesDialog> dialog_;
    158 
    159   scoped_ptr<MediaGalleryContextMenu> context_menu_;
    160 
    161   DISALLOW_COPY_AND_ASSIGN(MediaGalleriesScanResultController);
    162 };
    163 
    164 #endif  // CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_GALLERIES_SCAN_RESULT_CONTROLLER_H_
    165