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