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 CHROME_BROWSER_INVALIDATION_GCM_INVALIDATION_BRIDGE_H_
6 #define CHROME_BROWSER_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 "chrome/browser/services/gcm/gcm_app_handler.h"
13 #include "google_apis/gaia/oauth2_token_service.h"
14 #include "google_apis/gcm/gcm_client.h"
15 #include "sync/notifier/gcm_network_channel_delegate.h"
17 class IdentityProvider
;
20 class SingleThreadTaskRunner
;
27 namespace invalidation
{
29 // GCMInvalidationBridge and GCMInvalidationBridge::Core implement functions
30 // needed for GCMNetworkChannel. GCMInvalidationBridge lives on UI thread while
31 // Core lives on IO thread. Core implements GCMNetworkChannelDelegate and posts
32 // all function calls to GCMInvalidationBridge which does actual work to perform
34 class GCMInvalidationBridge
: public gcm::GCMAppHandler
,
35 public OAuth2TokenService::Consumer
,
36 public base::NonThreadSafe
{
40 GCMInvalidationBridge(gcm::GCMService
* gcm_service
,
41 IdentityProvider
* identity_provider
);
42 virtual ~GCMInvalidationBridge();
44 // OAuth2TokenService::Consumer implementation.
45 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request
* request
,
46 const std::string
& access_token
,
47 const base::Time
& expiration_time
) OVERRIDE
;
48 virtual void OnGetTokenFailure(const OAuth2TokenService::Request
* request
,
49 const GoogleServiceAuthError
& error
) OVERRIDE
;
51 // gcm::GCMEventRouter implementation.
52 virtual void ShutdownHandler() OVERRIDE
;
53 virtual void OnMessage(const std::string
& app_id
,
54 const gcm::GCMClient::IncomingMessage
& message
)
56 virtual void OnMessagesDeleted(const std::string
& app_id
) OVERRIDE
;
57 virtual void OnSendError(
58 const std::string
& app_id
,
59 const gcm::GCMClient::SendErrorDetails
& send_error_details
) OVERRIDE
;
61 scoped_ptr
<syncer::GCMNetworkChannelDelegate
> CreateDelegate();
63 void CoreInitializationDone(
64 base::WeakPtr
<Core
> core
,
65 scoped_refptr
<base::SingleThreadTaskRunner
> core_thread_task_runner
);
67 // Functions reflecting GCMNetworkChannelDelegate interface. These are called
68 // on UI thread to perform actual work.
70 syncer::GCMNetworkChannelDelegate::RequestTokenCallback callback
);
71 void InvalidateToken(const std::string
& token
);
73 void Register(syncer::GCMNetworkChannelDelegate::RegisterCallback callback
);
75 void SubscribeForIncomingMessages();
77 void RegisterFinished(
78 syncer::GCMNetworkChannelDelegate::RegisterCallback callback
,
79 const std::string
& registration_id
,
80 gcm::GCMClient::Result result
);
83 gcm::GCMService
* const gcm_service_
;
84 IdentityProvider
* const identity_provider_
;
86 base::WeakPtr
<Core
> core_
;
87 scoped_refptr
<base::SingleThreadTaskRunner
> core_thread_task_runner_
;
89 // Fields related to RequestToken function.
90 scoped_ptr
<OAuth2TokenService::Request
> access_token_request_
;
91 syncer::GCMNetworkChannelDelegate::RequestTokenCallback
92 request_token_callback_
;
93 bool subscribed_for_incoming_messages_
;
95 base::WeakPtrFactory
<GCMInvalidationBridge
> weak_factory_
;
97 DISALLOW_COPY_AND_ASSIGN(GCMInvalidationBridge
);
100 } // namespace invalidation
102 #endif // CHROME_BROWSER_INVALIDATION_GCM_INVALIDATION_BRIDGE_H_