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