Home | History | Annotate | Download | only in media_galleries_private
      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_EXTENSIONS_API_MEDIA_GALLERIES_PRIVATE_MEDIA_GALLERIES_PRIVATE_API_H_
      6 #define CHROME_BROWSER_EXTENSIONS_API_MEDIA_GALLERIES_PRIVATE_MEDIA_GALLERIES_PRIVATE_API_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/memory/scoped_ptr.h"
     12 #include "base/memory/weak_ptr.h"
     13 #include "chrome/browser/extensions/api/media_galleries_private/gallery_watch_state_tracker.h"
     14 #include "chrome/browser/extensions/chrome_extension_function.h"
     15 #include "chrome/browser/media_galleries/media_galleries_preferences.h"
     16 #include "chrome/common/extensions/api/media_galleries_private.h"
     17 #include "extensions/browser/browser_context_keyed_api_factory.h"
     18 #include "extensions/browser/event_router.h"
     19 
     20 class Profile;
     21 
     22 namespace content {
     23 class BrowserContext;
     24 }
     25 
     26 namespace extensions {
     27 
     28 class MediaGalleriesPrivateEventRouter;
     29 
     30 // The profile-keyed service that manages the media galleries private extension
     31 // API. Created at the same time as the Profile.
     32 class MediaGalleriesPrivateAPI : public BrowserContextKeyedAPI,
     33                                  public EventRouter::Observer {
     34  public:
     35   explicit MediaGalleriesPrivateAPI(content::BrowserContext* context);
     36   virtual ~MediaGalleriesPrivateAPI();
     37 
     38   // KeyedService implementation.
     39   virtual void Shutdown() OVERRIDE;
     40 
     41   // BrowserContextKeyedAPI implementation.
     42   static BrowserContextKeyedAPIFactory<MediaGalleriesPrivateAPI>*
     43       GetFactoryInstance();
     44 
     45   // Convenience method to get the MediaGalleriesPrivateAPI for a profile.
     46   static MediaGalleriesPrivateAPI* Get(content::BrowserContext* context);
     47 
     48   // EventRouter::Observer implementation.
     49   virtual void OnListenerAdded(const EventListenerInfo& details) OVERRIDE;
     50 
     51   MediaGalleriesPrivateEventRouter* GetEventRouter();
     52   GalleryWatchStateTracker* GetGalleryWatchStateTracker();
     53 
     54  private:
     55   friend class BrowserContextKeyedAPIFactory<MediaGalleriesPrivateAPI>;
     56 
     57   void MaybeInitializeEventRouterAndTracker();
     58 
     59   // BrowserContextKeyedAPI implementation.
     60   static const char* service_name() {
     61     return "MediaGalleriesPrivateAPI";
     62   }
     63   static const bool kServiceIsNULLWhileTesting = true;
     64 
     65   // Current profile.
     66   Profile* profile_;
     67 
     68   // Created lazily on first access.
     69   scoped_ptr<GalleryWatchStateTracker> tracker_;
     70 
     71   // Created lazily on first access.
     72   scoped_ptr<MediaGalleriesPrivateEventRouter>
     73       media_galleries_private_event_router_;
     74 
     75   base::WeakPtrFactory<MediaGalleriesPrivateAPI> weak_ptr_factory_;
     76 
     77   DISALLOW_COPY_AND_ASSIGN(MediaGalleriesPrivateAPI);
     78 };
     79 
     80 // Implements the chrome.mediaGalleriesPrivate.addGalleryWatch method.
     81 class MediaGalleriesPrivateAddGalleryWatchFunction
     82     : public ChromeAsyncExtensionFunction {
     83  public:
     84   DECLARE_EXTENSION_FUNCTION("mediaGalleriesPrivate.addGalleryWatch",
     85                              MEDIAGALLERIESPRIVATE_ADDGALLERYWATCH);
     86 
     87  protected:
     88   virtual ~MediaGalleriesPrivateAddGalleryWatchFunction();
     89 
     90   // AsyncExtensionFunction overrides.
     91   virtual bool RunAsync() OVERRIDE;
     92 
     93  private:
     94   void OnPreferencesInit(const std::string& pref_id);
     95 
     96   // Gallery watch request handler.
     97   void HandleResponse(MediaGalleryPrefId gallery_id, bool success);
     98 };
     99 
    100 // Implements the chrome.mediaGalleriesPrivate.removeGalleryWatch method.
    101 class MediaGalleriesPrivateRemoveGalleryWatchFunction
    102     : public ChromeAsyncExtensionFunction {
    103  public:
    104   DECLARE_EXTENSION_FUNCTION("mediaGalleriesPrivate.removeGalleryWatch",
    105                              MEDIAGALLERIESPRIVATE_REMOVEGALLERYWATCH);
    106 
    107  protected:
    108   virtual ~MediaGalleriesPrivateRemoveGalleryWatchFunction();
    109 
    110   // SyncExtensionFunction overrides.
    111   virtual bool RunAsync() OVERRIDE;
    112 
    113  private:
    114   void OnPreferencesInit(const std::string& pref_id);
    115 };
    116 
    117 // Implements the chrome.mediaGalleriesPrivate.getAllGalleryWatch method.
    118 class MediaGalleriesPrivateGetAllGalleryWatchFunction
    119     : public ChromeAsyncExtensionFunction {
    120  public:
    121   DECLARE_EXTENSION_FUNCTION("mediaGalleriesPrivate.getAllGalleryWatch",
    122                              MEDIAGALLERIESPRIVATE_GETALLGALLERYWATCH);
    123  protected:
    124   virtual ~MediaGalleriesPrivateGetAllGalleryWatchFunction();
    125 
    126   // SyncExtensionFunction overrides.
    127   virtual bool RunAsync() OVERRIDE;
    128 
    129  private:
    130   void OnPreferencesInit();
    131 };
    132 
    133 // Implements the chrome.mediaGalleriesPrivate.removeAllGalleryWatch method.
    134 class MediaGalleriesPrivateRemoveAllGalleryWatchFunction
    135     : public ChromeAsyncExtensionFunction {
    136  public:
    137   DECLARE_EXTENSION_FUNCTION("mediaGalleriesPrivate.removeAllGalleryWatch",
    138                              MEDIAGALLERIESPRIVATE_REMOVEALLGALLERYWATCH);
    139  protected:
    140   virtual ~MediaGalleriesPrivateRemoveAllGalleryWatchFunction();
    141 
    142   // SyncExtensionFunction overrides.
    143   virtual bool RunAsync() OVERRIDE;
    144 
    145  private:
    146   void OnPreferencesInit();
    147 };
    148 
    149 }  // namespace extensions
    150 
    151 #endif  // CHROME_BROWSER_EXTENSIONS_API_MEDIA_GALLERIES_PRIVATE_MEDIA_GALLERIES_PRIVATE_API_H_
    152