Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(47)

Side by Side Diff: google_apis/gcm/gcm_client_impl.cc

Issue 184273011: Merge 253787 "[GCM] Make sure GCM checkout logic is invoked when..." (Closed) Base URL: svn://svn.chromium.org/chrome/branches/1847/src/
Patch Set: Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « google_apis/gcm/gcm_client_impl.h ('k') | google_apis/gcm/gcm_client_impl_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "google_apis/gcm/gcm_client_impl.h" 5 #include "google_apis/gcm/gcm_client_impl.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/command_line.h"
8 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
9 #include "base/logging.h" 10 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop/message_loop.h" 12 #include "base/message_loop/message_loop.h"
13 #include "base/metrics/histogram.h"
12 #include "base/sequenced_task_runner.h" 14 #include "base/sequenced_task_runner.h"
13 #include "base/time/default_clock.h" 15 #include "base/time/default_clock.h"
14 #include "google_apis/gcm/base/mcs_message.h" 16 #include "google_apis/gcm/base/mcs_message.h"
15 #include "google_apis/gcm/base/mcs_util.h" 17 #include "google_apis/gcm/base/mcs_util.h"
16 #include "google_apis/gcm/engine/checkin_request.h" 18 #include "google_apis/gcm/engine/checkin_request.h"
17 #include "google_apis/gcm/engine/connection_factory_impl.h" 19 #include "google_apis/gcm/engine/connection_factory_impl.h"
18 #include "google_apis/gcm/engine/gcm_store_impl.h" 20 #include "google_apis/gcm/engine/gcm_store_impl.h"
19 #include "google_apis/gcm/engine/mcs_client.h" 21 #include "google_apis/gcm/engine/mcs_client.h"
20 #include "google_apis/gcm/engine/registration_request.h" 22 #include "google_apis/gcm/engine/registration_request.h"
21 #include "google_apis/gcm/engine/unregistration_request.h" 23 #include "google_apis/gcm/engine/unregistration_request.h"
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 const scoped_refptr<net::URLRequestContextGetter>& 132 const scoped_refptr<net::URLRequestContextGetter>&
131 url_request_context_getter, 133 url_request_context_getter,
132 Delegate* delegate) { 134 Delegate* delegate) {
133 DCHECK_EQ(UNINITIALIZED, state_); 135 DCHECK_EQ(UNINITIALIZED, state_);
134 DCHECK(url_request_context_getter); 136 DCHECK(url_request_context_getter);
135 DCHECK(delegate); 137 DCHECK(delegate);
136 138
137 chrome_build_proto_.CopyFrom(chrome_build_proto); 139 chrome_build_proto_.CopyFrom(chrome_build_proto);
138 url_request_context_getter_ = url_request_context_getter; 140 url_request_context_getter_ = url_request_context_getter;
139 141
140 gcm_store_.reset(new GCMStoreImpl(false, path, blocking_task_runner)); 142 bool use_mock_keychain = false;
141 gcm_store_->Load(base::Bind(&GCMClientImpl::OnLoadCompleted, 143 #if defined(OS_MACOSX)
142 weak_ptr_factory_.GetWeakPtr())); 144 if (CommandLine::ForCurrentProcess()->HasSwitch("use-mock-keychain"))
145 use_mock_keychain = true;
146 #endif
147
148 gcm_store_.reset(
149 new GCMStoreImpl(use_mock_keychain, path, blocking_task_runner));
143 150
144 delegate_ = delegate; 151 delegate_ = delegate;
145 152
146 // |mcs_client_| might already be set for testing at this point. No need to 153 state_ = INITIALIZED;
147 // create a |connection_factory_|. 154 }
148 if (!mcs_client_.get()) {
149 const net::HttpNetworkSession::Params* network_session_params =
150 url_request_context_getter->GetURLRequestContext()->
151 GetNetworkSessionParams();
152 DCHECK(network_session_params);
153 network_session_ = new net::HttpNetworkSession(*network_session_params);
154 connection_factory_.reset(new ConnectionFactoryImpl(
155 GURL(kMCSEndpoint),
156 kDefaultBackoffPolicy,
157 network_session_,
158 net_log_.net_log()));
159 mcs_client_.reset(new MCSClient(chrome_build_proto.chrome_version(),
160 clock_.get(),
161 connection_factory_.get(),
162 gcm_store_.get()));
163 }
164 155
156 void GCMClientImpl::Load() {
157 DCHECK_EQ(INITIALIZED, state_);
158
159 // Once the loading is completed, the check-in will be initiated.
160 gcm_store_->Load(base::Bind(&GCMClientImpl::OnLoadCompleted,
161 weak_ptr_factory_.GetWeakPtr()));
165 state_ = LOADING; 162 state_ = LOADING;
166 } 163 }
167 164
168 void GCMClientImpl::OnLoadCompleted(scoped_ptr<GCMStore::LoadResult> result) { 165 void GCMClientImpl::OnLoadCompleted(scoped_ptr<GCMStore::LoadResult> result) {
169 DCHECK_EQ(LOADING, state_); 166 DCHECK_EQ(LOADING, state_);
170 167
171 if (!result->success) { 168 if (!result->success) {
172 ResetState(); 169 ResetState();
173 return; 170 return;
174 } 171 }
175 172
176 device_checkin_info_.android_id = result->device_android_id; 173 device_checkin_info_.android_id = result->device_android_id;
177 device_checkin_info_.secret = result->device_security_token; 174 device_checkin_info_.secret = result->device_security_token;
178 InitializeMCSClient(result.Pass()); 175 InitializeMCSClient(result.Pass());
179 if (!device_checkin_info_.IsValid()) { 176 if (!device_checkin_info_.IsValid()) {
180 device_checkin_info_.Reset(); 177 device_checkin_info_.Reset();
181 state_ = INITIAL_DEVICE_CHECKIN; 178 state_ = INITIAL_DEVICE_CHECKIN;
182 StartCheckin(device_checkin_info_); 179 StartCheckin(device_checkin_info_);
183 return; 180 return;
184 } 181 }
185 182
186 OnReady(); 183 OnReady();
187 } 184 }
188 185
189 void GCMClientImpl::InitializeMCSClient( 186 void GCMClientImpl::InitializeMCSClient(
190 scoped_ptr<GCMStore::LoadResult> result) { 187 scoped_ptr<GCMStore::LoadResult> result) {
188 // |mcs_client_| might already be set for testing at this point. No need to
189 // create a |connection_factory_|.
190 if (!mcs_client_.get()) {
191 const net::HttpNetworkSession::Params* network_session_params =
192 url_request_context_getter_->GetURLRequestContext()->
193 GetNetworkSessionParams();
194 DCHECK(network_session_params);
195 network_session_ = new net::HttpNetworkSession(*network_session_params);
196 connection_factory_.reset(new ConnectionFactoryImpl(
197 GURL(kMCSEndpoint),
198 kDefaultBackoffPolicy,
199 network_session_,
200 net_log_.net_log()));
201 mcs_client_.reset(new MCSClient(chrome_build_proto_.chrome_version(),
202 clock_.get(),
203 connection_factory_.get(),
204 gcm_store_.get()));
205 }
206
191 mcs_client_->Initialize( 207 mcs_client_->Initialize(
192 base::Bind(&GCMClientImpl::OnMCSError, weak_ptr_factory_.GetWeakPtr()), 208 base::Bind(&GCMClientImpl::OnMCSError, weak_ptr_factory_.GetWeakPtr()),
193 base::Bind(&GCMClientImpl::OnMessageReceivedFromMCS, 209 base::Bind(&GCMClientImpl::OnMessageReceivedFromMCS,
194 weak_ptr_factory_.GetWeakPtr()), 210 weak_ptr_factory_.GetWeakPtr()),
195 base::Bind(&GCMClientImpl::OnMessageSentToMCS, 211 base::Bind(&GCMClientImpl::OnMessageSentToMCS,
196 weak_ptr_factory_.GetWeakPtr()), 212 weak_ptr_factory_.GetWeakPtr()),
197 result.Pass()); 213 result.Pass());
198 } 214 }
199 215
200 void GCMClientImpl::OnFirstTimeDeviceCheckinCompleted( 216 void GCMClientImpl::OnFirstTimeDeviceCheckinCompleted(
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
269 } 285 }
270 } 286 }
271 } 287 }
272 288
273 void GCMClientImpl::SetDeviceCredentialsCallback(bool success) { 289 void GCMClientImpl::SetDeviceCredentialsCallback(bool success) {
274 // TODO(fgorski): This is one of the signals that store needs a rebuild. 290 // TODO(fgorski): This is one of the signals that store needs a rebuild.
275 DCHECK(success); 291 DCHECK(success);
276 } 292 }
277 293
278 void GCMClientImpl::CheckOut() { 294 void GCMClientImpl::CheckOut() {
279 delegate_ = NULL;
280 device_checkin_info_.Reset(); 295 device_checkin_info_.Reset();
281 mcs_client_->Destroy(); // This will also destroy GCM store.
282 mcs_client_.reset(); 296 mcs_client_.reset();
297 gcm_store_->Destroy(base::Bind(&GCMClientImpl::OnGCMStoreDestroyed,
298 weak_ptr_factory_.GetWeakPtr()));
283 checkin_request_.reset(); 299 checkin_request_.reset();
284 pending_registrations_.clear(); 300 pending_registrations_.clear();
301 state_ = INITIALIZED;
285 } 302 }
286 303
287 void GCMClientImpl::Register(const std::string& app_id, 304 void GCMClientImpl::Register(const std::string& app_id,
288 const std::string& cert, 305 const std::string& cert,
289 const std::vector<std::string>& sender_ids) { 306 const std::vector<std::string>& sender_ids) {
290 DCHECK_EQ(state_, READY); 307 DCHECK_EQ(state_, READY);
291 RegistrationRequest::RequestInfo request_info( 308 RegistrationRequest::RequestInfo request_info(
292 device_checkin_info_.android_id, 309 device_checkin_info_.android_id,
293 device_checkin_info_.secret, 310 device_checkin_info_.secret,
294 app_id, 311 app_id,
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
361 delegate_->OnUnregisterFinished(app_id, status); 378 delegate_->OnUnregisterFinished(app_id, status);
362 379
363 PendingUnregistrations::iterator iter = pending_unregistrations_.find(app_id); 380 PendingUnregistrations::iterator iter = pending_unregistrations_.find(app_id);
364 if (iter == pending_unregistrations_.end()) 381 if (iter == pending_unregistrations_.end())
365 return; 382 return;
366 383
367 delete iter->second; 384 delete iter->second;
368 pending_unregistrations_.erase(iter); 385 pending_unregistrations_.erase(iter);
369 } 386 }
370 387
388 void GCMClientImpl::OnGCMStoreDestroyed(bool success) {
389 DLOG_IF(ERROR, !success) << "GCM store failed to be destroyed!";
390 UMA_HISTOGRAM_BOOLEAN("GCM.StoreDestroySucceeded", success);
391 }
392
371 void GCMClientImpl::Send(const std::string& app_id, 393 void GCMClientImpl::Send(const std::string& app_id,
372 const std::string& receiver_id, 394 const std::string& receiver_id,
373 const OutgoingMessage& message) { 395 const OutgoingMessage& message) {
374 DCHECK_EQ(state_, READY); 396 DCHECK_EQ(state_, READY);
375 397
376 mcs_proto::DataMessageStanza stanza; 398 mcs_proto::DataMessageStanza stanza;
377 stanza.set_ttl(message.time_to_live); 399 stanza.set_ttl(message.time_to_live);
378 stanza.set_sent(clock_->Now().ToInternalValue() / 400 stanza.set_sent(clock_->Now().ToInternalValue() /
379 base::Time::kMicrosecondsPerSecond); 401 base::Time::kMicrosecondsPerSecond);
380 stanza.set_id(message.id); 402 stanza.set_id(message.id);
381 stanza.set_from(kSendMessageFromValue); 403 stanza.set_from(kSendMessageFromValue);
382 stanza.set_to(receiver_id); 404 stanza.set_to(receiver_id);
383 stanza.set_category(app_id); 405 stanza.set_category(app_id);
384 406
385 for (MessageData::const_iterator iter = message.data.begin(); 407 for (MessageData::const_iterator iter = message.data.begin();
386 iter != message.data.end(); 408 iter != message.data.end();
387 ++iter) { 409 ++iter) {
388 mcs_proto::AppData* app_data = stanza.add_app_data(); 410 mcs_proto::AppData* app_data = stanza.add_app_data();
389 app_data->set_key(iter->first); 411 app_data->set_key(iter->first);
390 app_data->set_value(iter->second); 412 app_data->set_value(iter->second);
391 } 413 }
392 414
393 MCSMessage mcs_message(stanza); 415 MCSMessage mcs_message(stanza);
394 DVLOG(1) << "MCS message size: " << mcs_message.size(); 416 DVLOG(1) << "MCS message size: " << mcs_message.size();
395 mcs_client_->SendMessage(mcs_message); 417 mcs_client_->SendMessage(mcs_message);
396 } 418 }
397 419
398 bool GCMClientImpl::IsReady() const {
399 return state_ == READY;
400 }
401
402 void GCMClientImpl::OnMessageReceivedFromMCS(const gcm::MCSMessage& message) { 420 void GCMClientImpl::OnMessageReceivedFromMCS(const gcm::MCSMessage& message) {
403 switch (message.tag()) { 421 switch (message.tag()) {
404 case kLoginResponseTag: 422 case kLoginResponseTag:
405 DVLOG(1) << "Login response received by GCM Client. Ignoring."; 423 DVLOG(1) << "Login response received by GCM Client. Ignoring.";
406 return; 424 return;
407 case kDataMessageStanzaTag: 425 case kDataMessageStanzaTag:
408 DVLOG(1) << "A downstream message received. Processing..."; 426 DVLOG(1) << "A downstream message received. Processing...";
409 HandleIncomingMessage(message); 427 HandleIncomingMessage(message);
410 return; 428 return;
411 default: 429 default:
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
488 if (iter != incoming_message.data.end()) 506 if (iter != incoming_message.data.end())
489 message_id = iter->second; 507 message_id = iter->second;
490 delegate->OnMessageSendError(app_id, message_id, SERVER_ERROR); 508 delegate->OnMessageSendError(app_id, message_id, SERVER_ERROR);
491 } 509 }
492 510
493 void GCMClientImpl::SetMCSClientForTesting(scoped_ptr<MCSClient> mcs_client) { 511 void GCMClientImpl::SetMCSClientForTesting(scoped_ptr<MCSClient> mcs_client) {
494 mcs_client_ = mcs_client.Pass(); 512 mcs_client_ = mcs_client.Pass();
495 } 513 }
496 514
497 } // namespace gcm 515 } // namespace gcm
OLDNEW
« no previous file with comments | « google_apis/gcm/gcm_client_impl.h ('k') | google_apis/gcm/gcm_client_impl_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698