Home | History | Annotate | Download | only in invalidation
      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 COMPONENTS_INVALIDATION_GCM_INVALIDATION_BRIDGE_H_
      6 #define COMPONENTS_INVALIDATION_GCM_INVALIDATION_BRIDGE_H_
      7 
      8 #include "base/callback.h"
      9 #include "base/memory/scoped_ptr.h"
     10 #include "base/memory/weak_ptr.h"
     11 #include "base/threading/non_thread_safe.h"
     12 #include "components/gcm_driver/gcm_app_handler.h"
     13 #include "components/gcm_driver/gcm_client.h"
     14 #include "components/invalidation/gcm_network_channel_delegate.h"
     15 #include "google_apis/gaia/oauth2_token_service.h"
     16 
     17 class IdentityProvider;
     18 
     19 namespace base {
     20 class SingleThreadTaskRunner;
     21 }  // namespace base
     22 
     23 namespace gcm {
     24 class GCMDriver;
     25 }  // namespace gcm
     26 
     27 namespace invalidation {
     28 
     29 // GCMInvalidationBridge and GCMInvalidationBridge::Core implement functions
     30 // needed for GCMNetworkChannel. GCMInvalidationBridge lives on UI thread while
     31 // Core lives on IO thread. Core implements GCMNetworkChannelDelegate and posts
     32 // all function calls to GCMInvalidationBridge which does actual work to perform
     33 // them.
     34 class GCMInvalidationBridge : public gcm::GCMAppHandler,
     35                               public OAuth2TokenService::Consumer,
     36                               public base::NonThreadSafe {
     37  public:
     38   class Core;
     39 
     40   GCMInvalidationBridge(gcm::GCMDriver* gcm_driver,
     41                         IdentityProvider* identity_provider);
     42   virtual ~GCMInvalidationBridge();
     43 
     44   // OAuth2TokenService::Consumer implementation.
     45   virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
     46                                  const std::string& access_token,
     47                                  const base::Time& expiration_time) OVERRIDE;
     48   virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request,
     49                                  const GoogleServiceAuthError& error) OVERRIDE;
     50 
     51   // gcm::GCMAppHandler implementation.
     52   virtual void ShutdownHandler() OVERRIDE;
     53   virtual void OnMessage(
     54       const std::string& app_id,
     55       const gcm::GCMClient::IncomingMessage& message) OVERRIDE;
     56   virtual void OnMessagesDeleted(const std::string& app_id) OVERRIDE;
     57   virtual void OnSendError(
     58       const std::string& app_id,
     59       const gcm::GCMClient::SendErrorDetails& send_error_details) OVERRIDE;
     60   virtual void OnConnected(const net::IPEndPoint& ip_endpoint) OVERRIDE;
     61   virtual void OnDisconnected() OVERRIDE;
     62 
     63   scoped_ptr<syncer::GCMNetworkChannelDelegate> CreateDelegate();
     64 
     65   void CoreInitializationDone(
     66       base::WeakPtr<Core> core,
     67       scoped_refptr<base::SingleThreadTaskRunner> core_thread_task_runner);
     68 
     69   // Functions reflecting GCMNetworkChannelDelegate interface. These are called
     70   // on UI thread to perform actual work.
     71   void RequestToken(
     72       syncer::GCMNetworkChannelDelegate::RequestTokenCallback callback);
     73   void InvalidateToken(const std::string& token);
     74 
     75   void Register(syncer::GCMNetworkChannelDelegate::RegisterCallback callback);
     76 
     77   void SubscribeForIncomingMessages();
     78 
     79   void RegisterFinished(
     80       syncer::GCMNetworkChannelDelegate::RegisterCallback callback,
     81       const std::string& registration_id,
     82       gcm::GCMClient::Result result);
     83 
     84  private:
     85   gcm::GCMDriver* const gcm_driver_;
     86   IdentityProvider* const identity_provider_;
     87 
     88   base::WeakPtr<Core> core_;
     89   scoped_refptr<base::SingleThreadTaskRunner> core_thread_task_runner_;
     90 
     91   // Fields related to RequestToken function.
     92   scoped_ptr<OAuth2TokenService::Request> access_token_request_;
     93   syncer::GCMNetworkChannelDelegate::RequestTokenCallback
     94       request_token_callback_;
     95   bool subscribed_for_incoming_messages_;
     96 
     97   base::WeakPtrFactory<GCMInvalidationBridge> weak_factory_;
     98 
     99   DISALLOW_COPY_AND_ASSIGN(GCMInvalidationBridge);
    100 };
    101 
    102 }  // namespace invalidation
    103 
    104 #endif  // COMPONENTS_INVALIDATION_GCM_INVALIDATION_BRIDGE_H_
    105