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
;
21 class SingleThreadTaskRunner
;
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
35 class GCMInvalidationBridge
: public gcm::GCMAppHandler
,
36 public gcm::GCMConnectionObserver
,
37 public OAuth2TokenService::Consumer
,
38 public base::NonThreadSafe
{
42 GCMInvalidationBridge(gcm::GCMDriver
* gcm_driver
,
43 IdentityProvider
* identity_provider
);
44 virtual ~GCMInvalidationBridge();
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
;
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
;
65 // gcm::GCMConnectionObserver implementation.
66 virtual void OnConnected(const net::IPEndPoint
& ip_endpoint
) OVERRIDE
;
67 virtual 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
);
91 gcm::GCMDriver
* const gcm_driver_
;
92 IdentityProvider
* const identity_provider_
;
94 base::WeakPtr
<Core
> core_
;
95 scoped_refptr
<base::SingleThreadTaskRunner
> core_thread_task_runner_
;
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_
;
103 base::WeakPtrFactory
<GCMInvalidationBridge
> weak_factory_
;
105 DISALLOW_COPY_AND_ASSIGN(GCMInvalidationBridge
);
108 } // namespace invalidation
110 #endif // COMPONENTS_INVALIDATION_GCM_INVALIDATION_BRIDGE_H_