4f3e735078343f0ba590736592918b0b5c093daf
[platform/framework/web/crosswalk.git] / src / components / invalidation / gcm_invalidation_bridge.h
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   ~GCMInvalidationBridge() override;
45
46   // OAuth2TokenService::Consumer implementation.
47   void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
48                          const std::string& access_token,
49                          const base::Time& expiration_time) override;
50   void OnGetTokenFailure(const OAuth2TokenService::Request* request,
51                          const GoogleServiceAuthError& error) override;
52
53   // gcm::GCMAppHandler implementation.
54   void ShutdownHandler() override;
55   void OnMessage(const std::string& app_id,
56                  const gcm::GCMClient::IncomingMessage& message) override;
57   void OnMessagesDeleted(const std::string& app_id) override;
58   void OnSendError(
59       const std::string& app_id,
60       const gcm::GCMClient::SendErrorDetails& send_error_details) override;
61   void OnSendAcknowledged(const std::string& app_id,
62                           const std::string& message_id) override;
63
64   // gcm::GCMConnectionObserver implementation.
65   void OnConnected(const net::IPEndPoint& ip_endpoint) override;
66   void OnDisconnected() override;
67
68   scoped_ptr<syncer::GCMNetworkChannelDelegate> CreateDelegate();
69
70   void CoreInitializationDone(
71       base::WeakPtr<Core> core,
72       scoped_refptr<base::SingleThreadTaskRunner> core_thread_task_runner);
73
74   // Functions reflecting GCMNetworkChannelDelegate interface. These are called
75   // on UI thread to perform actual work.
76   void RequestToken(
77       syncer::GCMNetworkChannelDelegate::RequestTokenCallback callback);
78   void InvalidateToken(const std::string& token);
79
80   void Register(syncer::GCMNetworkChannelDelegate::RegisterCallback callback);
81
82   void SubscribeForIncomingMessages();
83
84   void RegisterFinished(
85       syncer::GCMNetworkChannelDelegate::RegisterCallback callback,
86       const std::string& registration_id,
87       gcm::GCMClient::Result result);
88
89  private:
90   gcm::GCMDriver* const gcm_driver_;
91   IdentityProvider* const identity_provider_;
92
93   base::WeakPtr<Core> core_;
94   scoped_refptr<base::SingleThreadTaskRunner> core_thread_task_runner_;
95
96   // Fields related to RequestToken function.
97   scoped_ptr<OAuth2TokenService::Request> access_token_request_;
98   syncer::GCMNetworkChannelDelegate::RequestTokenCallback
99       request_token_callback_;
100   bool subscribed_for_incoming_messages_;
101
102   base::WeakPtrFactory<GCMInvalidationBridge> weak_factory_;
103
104   DISALLOW_COPY_AND_ASSIGN(GCMInvalidationBridge);
105 };
106
107 }  // namespace invalidation
108
109 #endif  // COMPONENTS_INVALIDATION_GCM_INVALIDATION_BRIDGE_H_