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_IMPL_GCM_INVALIDATION_BRIDGE_H_
6 #define COMPONENTS_INVALIDATION_IMPL_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/common/gcm_messages.h"
13 #include "components/gcm_driver/gcm_app_handler.h"
14 #include "components/gcm_driver/gcm_client.h"
15 #include "components/gcm_driver/gcm_connection_observer.h"
16 #include "components/invalidation/impl/gcm_network_channel_delegate.h"
17 #include "google_apis/gaia/oauth2_token_service.h"
19 class IdentityProvider
;
22 class SingleThreadTaskRunner
;
29 namespace invalidation
{
31 // GCMInvalidationBridge and GCMInvalidationBridge::Core implement functions
32 // needed for GCMNetworkChannel. GCMInvalidationBridge lives on UI thread while
33 // Core lives on IO thread. Core implements GCMNetworkChannelDelegate and posts
34 // all function calls to GCMInvalidationBridge which does actual work to perform
36 class GCMInvalidationBridge
: public gcm::GCMAppHandler
,
37 public gcm::GCMConnectionObserver
,
38 public OAuth2TokenService::Consumer
,
39 public base::NonThreadSafe
{
43 GCMInvalidationBridge(gcm::GCMDriver
* gcm_driver
,
44 IdentityProvider
* identity_provider
);
45 ~GCMInvalidationBridge() override
;
47 // OAuth2TokenService::Consumer implementation.
48 void OnGetTokenSuccess(const OAuth2TokenService::Request
* request
,
49 const std::string
& access_token
,
50 const base::Time
& expiration_time
) override
;
51 void OnGetTokenFailure(const OAuth2TokenService::Request
* request
,
52 const GoogleServiceAuthError
& error
) override
;
54 // gcm::GCMAppHandler implementation.
55 void ShutdownHandler() override
;
56 void OnMessage(const std::string
& app_id
,
57 const gcm::IncomingMessage
& message
) override
;
58 void OnMessagesDeleted(const std::string
& app_id
) override
;
60 const std::string
& app_id
,
61 const gcm::GCMClient::SendErrorDetails
& send_error_details
) override
;
62 void OnSendAcknowledged(const std::string
& app_id
,
63 const std::string
& message_id
) override
;
65 // gcm::GCMConnectionObserver implementation.
66 void OnConnected(const net::IPEndPoint
& ip_endpoint
) override
;
67 void OnDisconnected() override
;
69 scoped_ptr
<syncer::GCMNetworkChannelDelegate
> CreateDelegate();
71 void CoreInitializationDone(
72 base::WeakPtr
<Core
> core
,
73 scoped_refptr
<base::SingleThreadTaskRunner
> core_thread_task_runner
);
75 // Functions reflecting GCMNetworkChannelDelegate interface. These are called
76 // on UI thread to perform actual work.
78 syncer::GCMNetworkChannelDelegate::RequestTokenCallback callback
);
79 void InvalidateToken(const std::string
& token
);
81 void Register(syncer::GCMNetworkChannelDelegate::RegisterCallback callback
);
83 void SubscribeForIncomingMessages();
85 void RegisterFinished(
86 syncer::GCMNetworkChannelDelegate::RegisterCallback callback
,
87 const std::string
& registration_id
,
88 gcm::GCMClient::Result result
);
92 static void UnregisterFinishedNoOp(gcm::GCMClient::Result result
);
95 gcm::GCMDriver
* const gcm_driver_
;
96 IdentityProvider
* const identity_provider_
;
98 base::WeakPtr
<Core
> core_
;
99 scoped_refptr
<base::SingleThreadTaskRunner
> core_thread_task_runner_
;
101 // Fields related to RequestToken function.
102 scoped_ptr
<OAuth2TokenService::Request
> access_token_request_
;
103 syncer::GCMNetworkChannelDelegate::RequestTokenCallback
104 request_token_callback_
;
105 bool subscribed_for_incoming_messages_
;
107 base::WeakPtrFactory
<GCMInvalidationBridge
> weak_factory_
;
109 DISALLOW_COPY_AND_ASSIGN(GCMInvalidationBridge
);
112 } // namespace invalidation
114 #endif // COMPONENTS_INVALIDATION_IMPL_GCM_INVALIDATION_BRIDGE_H_