OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/location.h" | 6 #include "base/location.h" |
7 #include "base/single_thread_task_runner.h" | 7 #include "base/single_thread_task_runner.h" |
8 #include "base/thread_task_runner_handle.h" | 8 #include "base/thread_task_runner_handle.h" |
9 #include "components/gcm_driver/gcm_driver.h" | 9 #include "components/gcm_driver/gcm_driver.h" |
10 #include "components/invalidation/gcm_invalidation_bridge.h" | 10 #include "components/invalidation/gcm_invalidation_bridge.h" |
(...skipping 23 matching lines...) Expand all Loading... |
34 // and passes all calls to GCMInvalidationBridge. All calls should be serialized | 34 // and passes all calls to GCMInvalidationBridge. All calls should be serialized |
35 // through GCMInvalidationBridge to avoid race conditions. | 35 // through GCMInvalidationBridge to avoid race conditions. |
36 class GCMInvalidationBridge::Core : public syncer::GCMNetworkChannelDelegate, | 36 class GCMInvalidationBridge::Core : public syncer::GCMNetworkChannelDelegate, |
37 public base::NonThreadSafe { | 37 public base::NonThreadSafe { |
38 public: | 38 public: |
39 Core(base::WeakPtr<GCMInvalidationBridge> bridge, | 39 Core(base::WeakPtr<GCMInvalidationBridge> bridge, |
40 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner); | 40 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner); |
41 virtual ~Core(); | 41 virtual ~Core(); |
42 | 42 |
43 // syncer::GCMNetworkChannelDelegate implementation. | 43 // syncer::GCMNetworkChannelDelegate implementation. |
44 virtual void Initialize() OVERRIDE; | 44 virtual void Initialize(ConnectionStateCallback callback) OVERRIDE; |
45 virtual void RequestToken(RequestTokenCallback callback) OVERRIDE; | 45 virtual void RequestToken(RequestTokenCallback callback) OVERRIDE; |
46 virtual void InvalidateToken(const std::string& token) OVERRIDE; | 46 virtual void InvalidateToken(const std::string& token) OVERRIDE; |
47 virtual void Register(RegisterCallback callback) OVERRIDE; | 47 virtual void Register(RegisterCallback callback) OVERRIDE; |
48 virtual void SetMessageReceiver(MessageCallback callback) OVERRIDE; | 48 virtual void SetMessageReceiver(MessageCallback callback) OVERRIDE; |
49 | 49 |
50 void RequestTokenFinished(RequestTokenCallback callback, | 50 void RequestTokenFinished(RequestTokenCallback callback, |
51 const GoogleServiceAuthError& error, | 51 const GoogleServiceAuthError& error, |
52 const std::string& token); | 52 const std::string& token); |
53 | 53 |
54 void RegisterFinished(RegisterCallback callback, | 54 void RegisterFinished(RegisterCallback callback, |
55 const std::string& registration_id, | 55 const std::string& registration_id, |
56 gcm::GCMClient::Result result); | 56 gcm::GCMClient::Result result); |
57 | 57 |
58 void OnIncomingMessage(const std::string& message, | 58 void OnIncomingMessage(const std::string& message, |
59 const std::string& echo_token); | 59 const std::string& echo_token); |
60 | 60 |
| 61 void OnConnectionStateChanged(ConnectionState connection_state); |
| 62 |
61 private: | 63 private: |
62 base::WeakPtr<GCMInvalidationBridge> bridge_; | 64 base::WeakPtr<GCMInvalidationBridge> bridge_; |
63 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner_; | 65 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner_; |
64 | 66 |
65 MessageCallback message_callback_; | 67 MessageCallback message_callback_; |
| 68 ConnectionStateCallback connection_state_callback_; |
66 | 69 |
67 base::WeakPtrFactory<Core> weak_factory_; | 70 base::WeakPtrFactory<Core> weak_factory_; |
68 | 71 |
69 DISALLOW_COPY_AND_ASSIGN(Core); | 72 DISALLOW_COPY_AND_ASSIGN(Core); |
70 }; | 73 }; |
71 | 74 |
72 GCMInvalidationBridge::Core::Core( | 75 GCMInvalidationBridge::Core::Core( |
73 base::WeakPtr<GCMInvalidationBridge> bridge, | 76 base::WeakPtr<GCMInvalidationBridge> bridge, |
74 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner) | 77 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner) |
75 : bridge_(bridge), | 78 : bridge_(bridge), |
76 ui_thread_task_runner_(ui_thread_task_runner), | 79 ui_thread_task_runner_(ui_thread_task_runner), |
77 weak_factory_(this) { | 80 weak_factory_(this) { |
78 // Core is created on UI thread but all calls happen on IO thread. | 81 // Core is created on UI thread but all calls happen on IO thread. |
79 DetachFromThread(); | 82 DetachFromThread(); |
80 } | 83 } |
81 | 84 |
82 GCMInvalidationBridge::Core::~Core() {} | 85 GCMInvalidationBridge::Core::~Core() {} |
83 | 86 |
84 void GCMInvalidationBridge::Core::Initialize() { | 87 void GCMInvalidationBridge::Core::Initialize(ConnectionStateCallback callback) { |
85 DCHECK(CalledOnValidThread()); | 88 DCHECK(CalledOnValidThread()); |
| 89 connection_state_callback_ = callback; |
86 // Pass core WeapPtr and TaskRunner to GCMInvalidationBridge for it to be able | 90 // Pass core WeapPtr and TaskRunner to GCMInvalidationBridge for it to be able |
87 // to post back. | 91 // to post back. |
88 ui_thread_task_runner_->PostTask( | 92 ui_thread_task_runner_->PostTask( |
89 FROM_HERE, | 93 FROM_HERE, |
90 base::Bind(&GCMInvalidationBridge::CoreInitializationDone, | 94 base::Bind(&GCMInvalidationBridge::CoreInitializationDone, |
91 bridge_, | 95 bridge_, |
92 weak_factory_.GetWeakPtr(), | 96 weak_factory_.GetWeakPtr(), |
93 base::ThreadTaskRunnerHandle::Get())); | 97 base::ThreadTaskRunnerHandle::Get())); |
94 } | 98 } |
95 | 99 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 callback.Run(registration_id, result); | 142 callback.Run(registration_id, result); |
139 } | 143 } |
140 | 144 |
141 void GCMInvalidationBridge::Core::OnIncomingMessage( | 145 void GCMInvalidationBridge::Core::OnIncomingMessage( |
142 const std::string& message, | 146 const std::string& message, |
143 const std::string& echo_token) { | 147 const std::string& echo_token) { |
144 DCHECK(!message_callback_.is_null()); | 148 DCHECK(!message_callback_.is_null()); |
145 message_callback_.Run(message, echo_token); | 149 message_callback_.Run(message, echo_token); |
146 } | 150 } |
147 | 151 |
| 152 void GCMInvalidationBridge::Core::OnConnectionStateChanged( |
| 153 ConnectionState connection_state) { |
| 154 if (!connection_state_callback_.is_null()) { |
| 155 connection_state_callback_.Run(connection_state); |
| 156 } |
| 157 } |
| 158 |
148 GCMInvalidationBridge::GCMInvalidationBridge( | 159 GCMInvalidationBridge::GCMInvalidationBridge( |
149 gcm::GCMDriver* gcm_driver, | 160 gcm::GCMDriver* gcm_driver, |
150 IdentityProvider* identity_provider) | 161 IdentityProvider* identity_provider) |
151 : OAuth2TokenService::Consumer("gcm_network_channel"), | 162 : OAuth2TokenService::Consumer("gcm_network_channel"), |
152 gcm_driver_(gcm_driver), | 163 gcm_driver_(gcm_driver), |
153 identity_provider_(identity_provider), | 164 identity_provider_(identity_provider), |
154 subscribed_for_incoming_messages_(false), | 165 subscribed_for_incoming_messages_(false), |
155 weak_factory_(this) {} | 166 weak_factory_(this) {} |
156 | 167 |
157 GCMInvalidationBridge::~GCMInvalidationBridge() { | 168 GCMInvalidationBridge::~GCMInvalidationBridge() { |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 } | 321 } |
311 | 322 |
312 void GCMInvalidationBridge::OnSendError( | 323 void GCMInvalidationBridge::OnSendError( |
313 const std::string& app_id, | 324 const std::string& app_id, |
314 const gcm::GCMClient::SendErrorDetails& send_error_details) { | 325 const gcm::GCMClient::SendErrorDetails& send_error_details) { |
315 // cacheinvalidation doesn't send messages over GCM. | 326 // cacheinvalidation doesn't send messages over GCM. |
316 NOTREACHED(); | 327 NOTREACHED(); |
317 } | 328 } |
318 | 329 |
319 void GCMInvalidationBridge::OnConnected(const net::IPEndPoint& ip_endpoint) { | 330 void GCMInvalidationBridge::OnConnected(const net::IPEndPoint& ip_endpoint) { |
320 // TODO(pavely): update invalidator state. | 331 core_thread_task_runner_->PostTask( |
| 332 FROM_HERE, |
| 333 base::Bind(&GCMInvalidationBridge::Core::OnConnectionStateChanged, |
| 334 core_, |
| 335 syncer::GCMNetworkChannelDelegate::CONNECTION_STATE_ONLINE)); |
321 } | 336 } |
322 | 337 |
323 void GCMInvalidationBridge::OnDisconnected() { | 338 void GCMInvalidationBridge::OnDisconnected() { |
324 // TODO(pavely): update invalidator state. | 339 core_thread_task_runner_->PostTask( |
| 340 FROM_HERE, |
| 341 base::Bind(&GCMInvalidationBridge::Core::OnConnectionStateChanged, |
| 342 core_, |
| 343 syncer::GCMNetworkChannelDelegate::CONNECTION_STATE_OFFLINE)); |
325 } | 344 } |
326 | 345 |
327 | 346 |
328 } // namespace invalidation | 347 } // namespace invalidation |
OLD | NEW |