Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / components / invalidation / gcm_invalidation_bridge.h
blob9f9fe42696996f062922e086176c2cb9bf1b55b6
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.
5 #ifndef COMPONENTS_INVALIDATION_GCM_INVALIDATION_BRIDGE_H_
6 #define COMPONENTS_INVALIDATION_GCM_INVALIDATION_BRIDGE_H_
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"
18 class IdentityProvider;
20 namespace base {
21 class SingleThreadTaskRunner;
22 } // namespace base
24 namespace gcm {
25 class GCMDriver;
26 } // namespace gcm
28 namespace invalidation {
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;
42 GCMInvalidationBridge(gcm::GCMDriver* gcm_driver,
43 IdentityProvider* identity_provider);
44 ~GCMInvalidationBridge() override;
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;
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;
64 // gcm::GCMConnectionObserver implementation.
65 void OnConnected(const net::IPEndPoint& ip_endpoint) override;
66 void OnDisconnected() override;
68 scoped_ptr<syncer::GCMNetworkChannelDelegate> CreateDelegate();
70 void CoreInitializationDone(
71 base::WeakPtr<Core> core,
72 scoped_refptr<base::SingleThreadTaskRunner> core_thread_task_runner);
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);
80 void Register(syncer::GCMNetworkChannelDelegate::RegisterCallback callback);
82 void SubscribeForIncomingMessages();
84 void RegisterFinished(
85 syncer::GCMNetworkChannelDelegate::RegisterCallback callback,
86 const std::string& registration_id,
87 gcm::GCMClient::Result result);
89 void Unregister();
91 static void UnregisterFinishedNoOp(gcm::GCMClient::Result result);
93 private:
94 gcm::GCMDriver* const gcm_driver_;
95 IdentityProvider* const identity_provider_;
97 base::WeakPtr<Core> core_;
98 scoped_refptr<base::SingleThreadTaskRunner> core_thread_task_runner_;
100 // Fields related to RequestToken function.
101 scoped_ptr<OAuth2TokenService::Request> access_token_request_;
102 syncer::GCMNetworkChannelDelegate::RequestTokenCallback
103 request_token_callback_;
104 bool subscribed_for_incoming_messages_;
106 base::WeakPtrFactory<GCMInvalidationBridge> weak_factory_;
108 DISALLOW_COPY_AND_ASSIGN(GCMInvalidationBridge);
111 } // namespace invalidation
113 #endif // COMPONENTS_INVALIDATION_GCM_INVALIDATION_BRIDGE_H_