Home | History | Annotate | Download | only in extensions
      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_EXTENSIONS_EXTENSION_GCM_APP_HANDLER_H_
      6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_GCM_APP_HANDLER_H_
      7 
      8 #include <string>
      9 
     10 #include "base/compiler_specific.h"
     11 #include "base/macros.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #include "base/memory/weak_ptr.h"
     14 #include "base/scoped_observer.h"
     15 #include "components/gcm_driver/gcm_app_handler.h"
     16 #include "components/gcm_driver/gcm_client.h"
     17 #include "extensions/browser/browser_context_keyed_api_factory.h"
     18 #include "extensions/browser/extension_registry_observer.h"
     19 
     20 class Profile;
     21 
     22 namespace content {
     23 class BrowserContext;
     24 }
     25 
     26 namespace gcm {
     27 class GCMDriver;
     28 class GCMProfileService;
     29 }
     30 
     31 namespace extensions {
     32 
     33 class ExtensionRegistry;
     34 class GcmJsEventRouter;
     35 
     36 // Defines the interface to provide handling logic for a given app.
     37 class ExtensionGCMAppHandler : public gcm::GCMAppHandler,
     38                                public BrowserContextKeyedAPI,
     39                                public ExtensionRegistryObserver {
     40  public:
     41   explicit ExtensionGCMAppHandler(content::BrowserContext* context);
     42   virtual ~ExtensionGCMAppHandler();
     43 
     44   // BrowserContextKeyedAPI implementation.
     45   static BrowserContextKeyedAPIFactory<ExtensionGCMAppHandler>*
     46   GetFactoryInstance();
     47 
     48   // gcm::GCMAppHandler implementation.
     49   virtual void ShutdownHandler() OVERRIDE;
     50   virtual void OnMessage(
     51       const std::string& app_id,
     52       const gcm::GCMClient::IncomingMessage& message) OVERRIDE;
     53   virtual void OnMessagesDeleted(const std::string& app_id) OVERRIDE;
     54   virtual void OnSendError(
     55       const std::string& app_id,
     56       const gcm::GCMClient::SendErrorDetails& send_error_details) OVERRIDE;
     57 
     58  protected:
     59   // Could be overridden by testing purpose.
     60   virtual void OnUnregisterCompleted(const std::string& app_id,
     61                                      gcm::GCMClient::Result result);
     62   virtual void AddAppHandler(const std::string& app_id);
     63   virtual void RemoveAppHandler(const std::string& app_id);
     64 
     65   gcm::GCMDriver* GetGCMDriver() const;
     66 
     67 private:
     68   friend class BrowserContextKeyedAPIFactory<ExtensionGCMAppHandler>;
     69 
     70   // ExtensionRegistryObserver implementation.
     71   virtual void OnExtensionLoaded(content::BrowserContext* browser_context,
     72                                  const Extension* extension) OVERRIDE;
     73   virtual void OnExtensionUnloaded(
     74       content::BrowserContext* browser_context,
     75       const Extension* extension,
     76       UnloadedExtensionInfo::Reason reason) OVERRIDE;
     77   virtual void OnExtensionUninstalled(content::BrowserContext* browser_context,
     78                                       const Extension* extension) OVERRIDE;
     79 
     80   void AddDummyAppHandler();
     81   void RemoveDummyAppHandler();
     82 
     83   // BrowserContextKeyedAPI implementation.
     84   static const char* service_name() { return "ExtensionGCMAppHandler"; }
     85   static const bool kServiceIsNULLWhileTesting = true;
     86 
     87   Profile* profile_;
     88 
     89   // Listen to extension load, unloaded notifications.
     90   ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
     91       extension_registry_observer_;
     92 
     93 #if !defined(OS_ANDROID)
     94   scoped_ptr<extensions::GcmJsEventRouter> js_event_router_;
     95 #endif
     96 
     97   base::WeakPtrFactory<ExtensionGCMAppHandler> weak_factory_;
     98 
     99   DISALLOW_COPY_AND_ASSIGN(ExtensionGCMAppHandler);
    100 };
    101 
    102 }  // namespace extensions
    103 
    104 #endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_GCM_APP_HANDLER_H_
    105