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/macros.h" | 7 #include "base/macros.h" |
8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
9 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
10 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
(...skipping 25 matching lines...) Expand all Loading... |
36 // and passes all calls to GCMInvalidationBridge. All calls should be serialized | 36 // and passes all calls to GCMInvalidationBridge. All calls should be serialized |
37 // through GCMInvalidationBridge to avoid race conditions. | 37 // through GCMInvalidationBridge to avoid race conditions. |
38 class GCMInvalidationBridge::Core : public syncer::GCMNetworkChannelDelegate, | 38 class GCMInvalidationBridge::Core : public syncer::GCMNetworkChannelDelegate, |
39 public base::NonThreadSafe { | 39 public base::NonThreadSafe { |
40 public: | 40 public: |
41 Core(base::WeakPtr<GCMInvalidationBridge> bridge, | 41 Core(base::WeakPtr<GCMInvalidationBridge> bridge, |
42 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner); | 42 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner); |
43 ~Core() override; | 43 ~Core() override; |
44 | 44 |
45 // syncer::GCMNetworkChannelDelegate implementation. | 45 // syncer::GCMNetworkChannelDelegate implementation. |
46 void Initialize(ConnectionStateCallback callback) override; | 46 void Initialize(ConnectionStateCallback connection_state_callback, |
| 47 base::Closure store_reset_callback) override; |
47 void RequestToken(RequestTokenCallback callback) override; | 48 void RequestToken(RequestTokenCallback callback) override; |
48 void InvalidateToken(const std::string& token) override; | 49 void InvalidateToken(const std::string& token) override; |
49 void Register(RegisterCallback callback) override; | 50 void Register(RegisterCallback callback) override; |
50 void SetMessageReceiver(MessageCallback callback) override; | 51 void SetMessageReceiver(MessageCallback callback) override; |
51 | 52 |
52 void RequestTokenFinished(RequestTokenCallback callback, | 53 void RequestTokenFinished(RequestTokenCallback callback, |
53 const GoogleServiceAuthError& error, | 54 const GoogleServiceAuthError& error, |
54 const std::string& token); | 55 const std::string& token); |
55 | 56 |
56 void RegisterFinished(RegisterCallback callback, | 57 void RegisterFinished(RegisterCallback callback, |
57 const std::string& registration_id, | 58 const std::string& registration_id, |
58 gcm::GCMClient::Result result); | 59 gcm::GCMClient::Result result); |
59 | 60 |
60 void OnIncomingMessage(const std::string& message, | 61 void OnIncomingMessage(const std::string& message, |
61 const std::string& echo_token); | 62 const std::string& echo_token); |
62 | 63 |
63 void OnConnectionStateChanged(bool online); | 64 void OnConnectionStateChanged(bool online); |
| 65 void OnStoreReset(); |
64 | 66 |
65 private: | 67 private: |
66 base::WeakPtr<GCMInvalidationBridge> bridge_; | 68 base::WeakPtr<GCMInvalidationBridge> bridge_; |
67 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner_; | 69 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner_; |
68 | 70 |
69 MessageCallback message_callback_; | 71 MessageCallback message_callback_; |
70 ConnectionStateCallback connection_state_callback_; | 72 ConnectionStateCallback connection_state_callback_; |
| 73 base::Closure store_reset_callback_; |
71 | 74 |
72 base::WeakPtrFactory<Core> weak_factory_; | 75 base::WeakPtrFactory<Core> weak_factory_; |
73 | 76 |
74 DISALLOW_COPY_AND_ASSIGN(Core); | 77 DISALLOW_COPY_AND_ASSIGN(Core); |
75 }; | 78 }; |
76 | 79 |
77 GCMInvalidationBridge::Core::Core( | 80 GCMInvalidationBridge::Core::Core( |
78 base::WeakPtr<GCMInvalidationBridge> bridge, | 81 base::WeakPtr<GCMInvalidationBridge> bridge, |
79 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner) | 82 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner) |
80 : bridge_(bridge), | 83 : bridge_(bridge), |
81 ui_thread_task_runner_(ui_thread_task_runner), | 84 ui_thread_task_runner_(ui_thread_task_runner), |
82 weak_factory_(this) { | 85 weak_factory_(this) { |
83 // Core is created on UI thread but all calls happen on IO thread. | 86 // Core is created on UI thread but all calls happen on IO thread. |
84 DetachFromThread(); | 87 DetachFromThread(); |
85 } | 88 } |
86 | 89 |
87 GCMInvalidationBridge::Core::~Core() {} | 90 GCMInvalidationBridge::Core::~Core() {} |
88 | 91 |
89 void GCMInvalidationBridge::Core::Initialize(ConnectionStateCallback callback) { | 92 void GCMInvalidationBridge::Core::Initialize( |
| 93 ConnectionStateCallback connection_state_callback, |
| 94 base::Closure store_reset_callback) { |
90 DCHECK(CalledOnValidThread()); | 95 DCHECK(CalledOnValidThread()); |
91 connection_state_callback_ = callback; | 96 connection_state_callback_ = connection_state_callback; |
| 97 store_reset_callback_ = store_reset_callback; |
92 // Pass core WeapPtr and TaskRunner to GCMInvalidationBridge for it to be able | 98 // Pass core WeapPtr and TaskRunner to GCMInvalidationBridge for it to be able |
93 // to post back. | 99 // to post back. |
94 ui_thread_task_runner_->PostTask( | 100 ui_thread_task_runner_->PostTask( |
95 FROM_HERE, | 101 FROM_HERE, |
96 base::Bind(&GCMInvalidationBridge::CoreInitializationDone, | 102 base::Bind(&GCMInvalidationBridge::CoreInitializationDone, |
97 bridge_, | 103 bridge_, |
98 weak_factory_.GetWeakPtr(), | 104 weak_factory_.GetWeakPtr(), |
99 base::ThreadTaskRunnerHandle::Get())); | 105 base::ThreadTaskRunnerHandle::Get())); |
100 } | 106 } |
101 | 107 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 DCHECK(!message_callback_.is_null()); | 156 DCHECK(!message_callback_.is_null()); |
151 message_callback_.Run(message, echo_token); | 157 message_callback_.Run(message, echo_token); |
152 } | 158 } |
153 | 159 |
154 void GCMInvalidationBridge::Core::OnConnectionStateChanged(bool online) { | 160 void GCMInvalidationBridge::Core::OnConnectionStateChanged(bool online) { |
155 if (!connection_state_callback_.is_null()) { | 161 if (!connection_state_callback_.is_null()) { |
156 connection_state_callback_.Run(online); | 162 connection_state_callback_.Run(online); |
157 } | 163 } |
158 } | 164 } |
159 | 165 |
| 166 void GCMInvalidationBridge::Core::OnStoreReset() { |
| 167 if (!store_reset_callback_.is_null()) { |
| 168 store_reset_callback_.Run(); |
| 169 } |
| 170 } |
| 171 |
160 GCMInvalidationBridge::GCMInvalidationBridge( | 172 GCMInvalidationBridge::GCMInvalidationBridge( |
161 gcm::GCMDriver* gcm_driver, | 173 gcm::GCMDriver* gcm_driver, |
162 IdentityProvider* identity_provider) | 174 IdentityProvider* identity_provider) |
163 : OAuth2TokenService::Consumer("gcm_network_channel"), | 175 : OAuth2TokenService::Consumer("gcm_network_channel"), |
164 gcm_driver_(gcm_driver), | 176 gcm_driver_(gcm_driver), |
165 identity_provider_(identity_provider), | 177 identity_provider_(identity_provider), |
166 subscribed_for_incoming_messages_(false), | 178 subscribed_for_incoming_messages_(false), |
167 weak_factory_(this) {} | 179 weak_factory_(this) {} |
168 | 180 |
169 GCMInvalidationBridge::~GCMInvalidationBridge() { | 181 GCMInvalidationBridge::~GCMInvalidationBridge() { |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
312 core_, | 324 core_, |
313 gcm_driver_->IsConnected())); | 325 gcm_driver_->IsConnected())); |
314 | 326 |
315 subscribed_for_incoming_messages_ = true; | 327 subscribed_for_incoming_messages_ = true; |
316 } | 328 } |
317 | 329 |
318 void GCMInvalidationBridge::ShutdownHandler() { | 330 void GCMInvalidationBridge::ShutdownHandler() { |
319 // Nothing to do. | 331 // Nothing to do. |
320 } | 332 } |
321 | 333 |
| 334 void GCMInvalidationBridge::OnStoreReset() { |
| 335 core_thread_task_runner_->PostTask( |
| 336 FROM_HERE, base::Bind(&GCMInvalidationBridge::Core::OnStoreReset, core_)); |
| 337 } |
| 338 |
322 void GCMInvalidationBridge::OnMessage(const std::string& app_id, | 339 void GCMInvalidationBridge::OnMessage(const std::string& app_id, |
323 const gcm::IncomingMessage& message) { | 340 const gcm::IncomingMessage& message) { |
324 gcm::MessageData::const_iterator it; | 341 gcm::MessageData::const_iterator it; |
325 std::string content; | 342 std::string content; |
326 std::string echo_token; | 343 std::string echo_token; |
327 it = message.data.find(kContentKey); | 344 it = message.data.find(kContentKey); |
328 if (it != message.data.end()) | 345 if (it != message.data.end()) |
329 content = it->second; | 346 content = it->second; |
330 it = message.data.find(kEchoTokenKey); | 347 it = message.data.find(kEchoTokenKey); |
331 if (it != message.data.end()) | 348 if (it != message.data.end()) |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
368 | 385 |
369 void GCMInvalidationBridge::OnDisconnected() { | 386 void GCMInvalidationBridge::OnDisconnected() { |
370 core_thread_task_runner_->PostTask( | 387 core_thread_task_runner_->PostTask( |
371 FROM_HERE, | 388 FROM_HERE, |
372 base::Bind(&GCMInvalidationBridge::Core::OnConnectionStateChanged, | 389 base::Bind(&GCMInvalidationBridge::Core::OnConnectionStateChanged, |
373 core_, | 390 core_, |
374 false)); | 391 false)); |
375 } | 392 } |
376 | 393 |
377 } // namespace invalidation | 394 } // namespace invalidation |
OLD | NEW |