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

Side by Side Diff: components/gcm_driver/gcm_client_impl.cc

Issue 2578583002: Provide a mechanism for the GCM driver to send message receipts to GCM.
Patch Set: Adding new file I missed previously. Created 3 years, 11 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
OLDNEW
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 "components/gcm_driver/gcm_client_impl.h" 5 #include "components/gcm_driver/gcm_client_impl.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 #include <utility> 10 #include <utility>
11 11
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/files/file_path.h" 13 #include "base/files/file_path.h"
14 #include "base/location.h" 14 #include "base/location.h"
15 #include "base/logging.h" 15 #include "base/logging.h"
16 #include "base/memory/ptr_util.h" 16 #include "base/memory/ptr_util.h"
17 #include "base/metrics/histogram_macros.h" 17 #include "base/metrics/histogram_macros.h"
18 #include "base/sequenced_task_runner.h" 18 #include "base/sequenced_task_runner.h"
19 #include "base/single_thread_task_runner.h" 19 #include "base/single_thread_task_runner.h"
20 #include "base/stl_util.h" 20 #include "base/stl_util.h"
21 #include "base/strings/string_number_conversions.h" 21 #include "base/strings/string_number_conversions.h"
22 #include "base/strings/stringprintf.h" 22 #include "base/strings/stringprintf.h"
23 #include "base/threading/thread_task_runner_handle.h" 23 #include "base/threading/thread_task_runner_handle.h"
24 #include "base/time/default_clock.h" 24 #include "base/time/default_clock.h"
25 #include "base/time/time.h"
25 #include "base/timer/timer.h" 26 #include "base/timer/timer.h"
26 #include "components/crx_file/id_util.h" 27 #include "components/crx_file/id_util.h"
27 #include "components/gcm_driver/gcm_account_mapper.h" 28 #include "components/gcm_driver/gcm_account_mapper.h"
28 #include "components/gcm_driver/gcm_backoff_policy.h" 29 #include "components/gcm_driver/gcm_backoff_policy.h"
30 #include "components/gcm_driver/gcm_message_status.h"
Peter Beverloo 2017/01/13 01:46:12 This file is now included trice - once in gcm_clie
harkness 2017/01/19 13:20:41 Once I changed it to enum class, it's only include
31 #include "components/sync/base/bind_to_task_runner.h"
29 #include "google_apis/gcm/base/encryptor.h" 32 #include "google_apis/gcm/base/encryptor.h"
30 #include "google_apis/gcm/base/mcs_message.h" 33 #include "google_apis/gcm/base/mcs_message.h"
31 #include "google_apis/gcm/base/mcs_util.h" 34 #include "google_apis/gcm/base/mcs_util.h"
32 #include "google_apis/gcm/engine/checkin_request.h" 35 #include "google_apis/gcm/engine/checkin_request.h"
33 #include "google_apis/gcm/engine/connection_factory_impl.h" 36 #include "google_apis/gcm/engine/connection_factory_impl.h"
34 #include "google_apis/gcm/engine/gcm_registration_request_handler.h" 37 #include "google_apis/gcm/engine/gcm_registration_request_handler.h"
35 #include "google_apis/gcm/engine/gcm_store_impl.h" 38 #include "google_apis/gcm/engine/gcm_store_impl.h"
36 #include "google_apis/gcm/engine/gcm_unregistration_request_handler.h" 39 #include "google_apis/gcm/engine/gcm_unregistration_request_handler.h"
37 #include "google_apis/gcm/engine/instance_id_delete_token_request_handler.h" 40 #include "google_apis/gcm/engine/instance_id_delete_token_request_handler.h"
38 #include "google_apis/gcm/engine/instance_id_get_token_request_handler.h" 41 #include "google_apis/gcm/engine/instance_id_get_token_request_handler.h"
39 #include "google_apis/gcm/monitoring/gcm_stats_recorder.h" 42 #include "google_apis/gcm/monitoring/gcm_stats_recorder.h"
40 #include "google_apis/gcm/protocol/checkin.pb.h" 43 #include "google_apis/gcm/protocol/checkin.pb.h"
41 #include "google_apis/gcm/protocol/mcs.pb.h" 44 #include "google_apis/gcm/protocol/mcs.pb.h"
42 #include "net/http/http_network_session.h" 45 #include "net/http/http_network_session.h"
43 #include "net/http/http_transaction_factory.h" 46 #include "net/http/http_transaction_factory.h"
44 #include "net/url_request/url_request_context.h" 47 #include "net/url_request/url_request_context.h"
45 #include "url/gurl.h" 48 #include "url/gurl.h"
46 49
47 namespace gcm { 50 namespace gcm {
48 51
49 namespace { 52 namespace {
50 53
54 const char kGcmMessageTypeKey[] = "type";
55 const char kMessageReceiptType[] = "message_receipt";
56 const char kReceiptMessageIdKey[] = "message_id";
57 const char kReceiptStatusKey[] = "status";
58 // kReceiptGCMDestinationID is the destination ID which GCM listens to for
59 // upstream receipt messages.
Peter Beverloo 2017/01/13 01:46:12 Who owns it? Where does the value come from? Maybe
harkness 2017/01/19 13:20:41 I just asked Pinar and she generated the receiver
60 const char kReceiptGCMDestinationID[] = "1029510549786@google.com";
61 const int kReceiptTTLInSeconds = 10;
62
51 // Indicates a message type of the received message. 63 // Indicates a message type of the received message.
52 enum MessageType { 64 enum MessageType {
53 UNKNOWN, // Undetermined type. 65 UNKNOWN, // Undetermined type.
54 DATA_MESSAGE, // Regular data message. 66 DATA_MESSAGE, // Regular data message.
55 DELETED_MESSAGES, // Messages were deleted on the server. 67 DELETED_MESSAGES, // Messages were deleted on the server.
56 SEND_ERROR, // Error sending a message. 68 SEND_ERROR, // Error sending a message.
57 }; 69 };
58 70
59 enum OutgoingMessageTTLCategory { 71 enum OutgoingMessageTTLCategory {
60 TTL_ZERO, 72 TTL_ZERO,
(...skipping 1274 matching lines...) Expand 10 before | Expand all | Expand 10 after
1335 HandleIncomingDataMessage(app_id, use_subtype, data_message_stanza, 1347 HandleIncomingDataMessage(app_id, use_subtype, data_message_stanza,
1336 message_data); 1348 message_data);
1337 break; 1349 break;
1338 case DELETED_MESSAGES: 1350 case DELETED_MESSAGES:
1339 HandleIncomingDeletedMessages(app_id, data_message_stanza, message_data); 1351 HandleIncomingDeletedMessages(app_id, data_message_stanza, message_data);
1340 break; 1352 break;
1341 case SEND_ERROR: 1353 case SEND_ERROR:
1342 HandleIncomingSendError(app_id, data_message_stanza, message_data); 1354 HandleIncomingSendError(app_id, data_message_stanza, message_data);
1343 break; 1355 break;
1344 case UNKNOWN: 1356 case UNKNOWN:
1357 SendMessageReceipt(data_message_stanza.id(), app_id,
1358 GCM_UNKNOWN_MESSAGE_TYPE);
1345 DVLOG(1) << "Unknown message_type received. Message ignored. " 1359 DVLOG(1) << "Unknown message_type received. Message ignored. "
1346 << "App ID: " << app_id << "."; 1360 << "App ID: " << app_id << ".";
1347 break; 1361 break;
1348 } 1362 }
1349 } 1363 }
1350 1364
1351 void GCMClientImpl::HandleIncomingDataMessage( 1365 void GCMClientImpl::HandleIncomingDataMessage(
1352 const std::string& app_id, 1366 const std::string& app_id,
1353 bool was_subtype, 1367 bool was_subtype,
1354 const mcs_proto::DataMessageStanza& data_message_stanza, 1368 const mcs_proto::DataMessageStanza& data_message_stanza,
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
1403 if (registered) { 1417 if (registered) {
1404 UMA_HISTOGRAM_BOOLEAN("GCM.DataMessageReceived", true); 1418 UMA_HISTOGRAM_BOOLEAN("GCM.DataMessageReceived", true);
1405 bool has_collapse_key = 1419 bool has_collapse_key =
1406 data_message_stanza.has_token() && !data_message_stanza.token().empty(); 1420 data_message_stanza.has_token() && !data_message_stanza.token().empty();
1407 UMA_HISTOGRAM_BOOLEAN("GCM.DataMessageReceivedHasCollapseKey", 1421 UMA_HISTOGRAM_BOOLEAN("GCM.DataMessageReceivedHasCollapseKey",
1408 has_collapse_key); 1422 has_collapse_key);
1409 } 1423 }
1410 recorder_.RecordDataMessageReceived(app_id, sender, 1424 recorder_.RecordDataMessageReceived(app_id, sender,
1411 data_message_stanza.ByteSize(), registered, 1425 data_message_stanza.ByteSize(), registered,
1412 GCMStatsRecorder::DATA_MESSAGE); 1426 GCMStatsRecorder::DATA_MESSAGE);
1413 if (!registered) 1427 if (!registered) {
1428 SendMessageReceipt(data_message_stanza.id(), app_id, GCM_INVALID_SUBTYPE);
1414 return; 1429 return;
1430 }
1415 1431
1416 IncomingMessage incoming_message; 1432 IncomingMessage incoming_message;
1417 incoming_message.sender_id = data_message_stanza.from(); 1433 incoming_message.sender_id = data_message_stanza.from();
1418 if (data_message_stanza.has_token()) 1434 if (data_message_stanza.has_token())
1419 incoming_message.collapse_key = data_message_stanza.token(); 1435 incoming_message.collapse_key = data_message_stanza.token();
1420 incoming_message.data = message_data; 1436 incoming_message.data = message_data;
1421 incoming_message.raw_data = data_message_stanza.raw_data(); 1437 incoming_message.raw_data = data_message_stanza.raw_data();
1422 1438
1423 delegate_->OnMessageReceived(app_id, incoming_message); 1439 // Pass the message up to the GCMDriver. Also pass a callback which will send
1440 // a receipt for this message if invoked with a message status.
1441 delegate_->OnMessageReceived(
1442 app_id, incoming_message,
1443 syncer::BindToCurrentThread(base::Bind(
1444 &GCMClient::SendMessageReceipt, weak_ptr_factory_.GetWeakPtr(),
1445 data_message_stanza.id(), app_id)));
1424 } 1446 }
1425 1447
1426 void GCMClientImpl::HandleIncomingDeletedMessages( 1448 void GCMClientImpl::HandleIncomingDeletedMessages(
1427 const std::string& app_id, 1449 const std::string& app_id,
1428 const mcs_proto::DataMessageStanza& data_message_stanza, 1450 const mcs_proto::DataMessageStanza& data_message_stanza,
1429 MessageData& message_data) { 1451 MessageData& message_data) {
1430 int deleted_count = 0; 1452 int deleted_count = 0;
1431 MessageData::iterator count_iter = message_data.find(kDeletedCountKey); 1453 MessageData::iterator count_iter = message_data.find(kDeletedCountKey);
1432 if (count_iter != message_data.end()) { 1454 if (count_iter != message_data.end()) {
1433 if (!base::StringToInt(count_iter->second, &deleted_count)) 1455 if (!base::StringToInt(count_iter->second, &deleted_count))
(...skipping 30 matching lines...) Expand all
1464 1486
1465 bool GCMClientImpl::HasStandaloneRegisteredApp() const { 1487 bool GCMClientImpl::HasStandaloneRegisteredApp() const {
1466 if (registrations_.empty()) 1488 if (registrations_.empty())
1467 return false; 1489 return false;
1468 // Note that account mapper is not counted as a standalone app since it is 1490 // Note that account mapper is not counted as a standalone app since it is
1469 // automatically started when other app uses GCM. 1491 // automatically started when other app uses GCM.
1470 return registrations_.size() > 1 || 1492 return registrations_.size() > 1 ||
1471 !ExistsGCMRegistrationInMap(registrations_, kGCMAccountMapperAppId); 1493 !ExistsGCMRegistrationInMap(registrations_, kGCMAccountMapperAppId);
1472 } 1494 }
1473 1495
1496 void GCMClientImpl::SendMessageReceipt(const std::string& message_id,
1497 const std::string& app_id,
1498 GCMMessageStatus status) {
1499 DCHECK(!app_id.empty());
1500
1501 // Prepare a message to send to GCM which will log the status of the received
1502 // message. This is aggregated by GCM to provide better error alerting.
1503 OutgoingMessage message;
1504 message.time_to_live = kReceiptTTLInSeconds;
1505 message.id =
1506 base::Int64ToString(base::Time::NowFromSystemTime().ToInternalValue());
1507 message.data[kGcmMessageTypeKey] = kMessageReceiptType;
1508 message.data[kReceiptMessageIdKey] = message_id;
1509 message.data[kReceiptStatusKey] = base::IntToString(status);
1510
1511 Send(app_id, kReceiptGCMDestinationID, message);
1512 }
1513
1474 } // namespace gcm 1514 } // namespace gcm
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698