File: fcm_network_handler.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (308 lines) | stat: -rw-r--r-- 10,862 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/invalidation/impl/fcm_network_handler.h"

#include <memory>
#include <string>
#include <string_view>

#include "base/base64url.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/i18n/time_formatting.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "build/build_config.h"
#include "components/gcm_driver/gcm_driver.h"
#include "components/gcm_driver/gcm_profile_service.h"
#include "components/gcm_driver/instance_id/instance_id.h"
#include "components/gcm_driver/instance_id/instance_id_driver.h"
#include "components/invalidation/impl/invalidation_switches.h"
#include "components/invalidation/impl/status.h"
#include "components/invalidation/public/invalidator_state.h"

using instance_id::InstanceID;

namespace invalidation {

namespace {

const char kPayloadKey[] = "payload";
const char kPublicTopic[] = "external_name";
const char kVersionKey[] = "version";

// OAuth2 Scope passed to getToken to obtain GCM registration tokens.
// Must match Java GoogleCloudMessaging.INSTANCE_ID_SCOPE.
const char kGCMScope[] = "GCM";

// Lower bound time between two token validations when listening.
const int kTokenValidationPeriodMinutesDefault = 60 * 24;

// Returns the TTL (time-to-live) for the Instance ID token, or 0 if no TTL
// should be specified.
base::TimeDelta GetTimeToLive(const std::string& sender_id) {
  // This magic value is identical to kPolicyFCMInvalidationSenderID, i.e. the
  // value that ChromeOS policy uses for its invalidations.
  if (sender_id == "1013309121859") {
    if (!base::FeatureList::IsEnabled(switches::kPolicyInstanceIDTokenTTL)) {
      return base::TimeDelta();
    }

    return base::Seconds(switches::kPolicyInstanceIDTokenTTLSeconds.Get());
  }

  // The default for all other FCM clients is no TTL.
  return base::TimeDelta();
}

std::string GetValueFromMessage(const gcm::IncomingMessage& message,
                                const std::string& key) {
  std::string value;
  auto it = message.data.find(key);
  if (it != message.data.end())
    value = it->second;
  return value;
}

// Unpacks the private topic included in messages to the form returned for
// subscription requests.
//
// Subscriptions for private topics generate a private topic from the public
// topic of the form "/private/${public_topic}-${something}. Messages include
// this as the sender in the form
// "/topics/private/${public_topic}-${something}". For such messages, strip the
// "/topics" prefix.
//
// Subscriptions for public topics pass-through the public topic unchanged:
// "${public_topic}". Messages include the sender in the form
// "/topics/${public_topic}". For these messages, strip the "/topics/" prefix.
//
// If the provided sender does not match either pattern, return it unchanged.
std::string UnpackPrivateTopic(std::string_view private_topic) {
  if (base::StartsWith(private_topic, "/topics/private/")) {
    return std::string(private_topic.substr(strlen("/topics")));
  } else if (base::StartsWith(private_topic, "/topics/")) {
    return std::string(private_topic.substr(strlen("/topics/")));
  } else {
    return std::string(private_topic);
  }
}

InvalidationParsingStatus ParseIncomingMessage(
    const gcm::IncomingMessage& message,
    std::string* payload,
    std::string* private_topic,
    std::string* public_topic,
    int64_t* version) {
  *payload = GetValueFromMessage(message, kPayloadKey);
  std::string version_str = GetValueFromMessage(message, kVersionKey);

  // Version must always be there, and be an integer.
  if (version_str.empty())
    return InvalidationParsingStatus::kVersionEmpty;
  if (!base::StringToInt64(version_str, version))
    return InvalidationParsingStatus::kVersionInvalid;

  *public_topic = GetValueFromMessage(message, kPublicTopic);

  *private_topic = UnpackPrivateTopic(message.sender_id);
  if (private_topic->empty())
    return InvalidationParsingStatus::kPrivateTopicEmpty;

  return InvalidationParsingStatus::kSuccess;
}

void RecordFCMMessageStatus(InvalidationParsingStatus status,
                            const std::string& sender_id) {
  // These histograms are recorded quite frequently, so use the macros rather
  // than the functions.
  UMA_HISTOGRAM_ENUMERATION("FCMInvalidations.FCMMessageStatus", status);
  // Also split the histogram by a few well-known senders. The actual constants
  // aren't accessible here (they're defined in higher layers), so we simply
  // duplicate them here, strictly only for the purpose of metrics.
  constexpr char kDriveFcmSenderId[] = "947318989803";
  constexpr char kPolicyFCMInvalidationSenderID[] = "1013309121859";
  if (sender_id == kDriveFcmSenderId) {
    UMA_HISTOGRAM_ENUMERATION("FCMInvalidations.FCMMessageStatus.Drive",
                              status);
  } else if (sender_id == kPolicyFCMInvalidationSenderID) {
    UMA_HISTOGRAM_ENUMERATION("FCMInvalidations.FCMMessageStatus.Policy",
                              status);
  }
}

}  // namespace

FCMNetworkHandler::FCMNetworkHandler(
    gcm::GCMDriver* gcm_driver,
    instance_id::InstanceIDDriver* instance_id_driver,
    const std::string& sender_id,
    const std::string& app_id)
    : gcm_driver_(gcm_driver),
      instance_id_driver_(instance_id_driver),
      token_validation_timer_(std::make_unique<base::OneShotTimer>()),
      sender_id_(sender_id),
      app_id_(app_id) {}

FCMNetworkHandler::~FCMNetworkHandler() {
  StopListening();
}

// static
std::unique_ptr<FCMNetworkHandler> FCMNetworkHandler::Create(
    gcm::GCMDriver* gcm_driver,
    instance_id::InstanceIDDriver* instance_id_driver,
    const std::string& sender_id,
    const std::string& app_id) {
  return std::make_unique<FCMNetworkHandler>(gcm_driver, instance_id_driver,
                                             sender_id, app_id);
}

void FCMNetworkHandler::StartListening() {
  if (IsListening()) {
    StopListening();
  }
  // Adding ourselves as Handler means start listening.
  // Being the listener is pre-requirement for token operations.
  gcm_driver_->AddAppHandler(app_id_, this);

  instance_id_driver_->GetInstanceID(app_id_)->GetToken(
      sender_id_, kGCMScope, GetTimeToLive(sender_id_),
      /*flags=*/{InstanceID::Flags::kIsLazy},
      base::BindRepeating(&FCMNetworkHandler::DidRetrieveToken,
                          weak_ptr_factory_.GetWeakPtr()));
}

void FCMNetworkHandler::StopListening() {
  if (IsListening())
    gcm_driver_->RemoveAppHandler(app_id_);
}

bool FCMNetworkHandler::IsListening() const {
  return gcm_driver_->GetAppHandler(app_id_);
}

void FCMNetworkHandler::DidRetrieveToken(const std::string& subscription_token,
                                         InstanceID::Result result) {
  base::UmaHistogramEnumeration("FCMInvalidations.InitialTokenRetrievalStatus",
                                result);
  switch (result) {
    case InstanceID::SUCCESS:
      // The received token is assumed to be valid, therefore, we reschedule
      // validation.
      DeliverToken(subscription_token);
      token_ = subscription_token;
      UpdateChannelState(FcmChannelState::ENABLED);
      break;
    case InstanceID::INVALID_PARAMETER:
    case InstanceID::DISABLED:
    case InstanceID::ASYNC_OPERATION_PENDING:
    case InstanceID::SERVER_ERROR:
    case InstanceID::UNKNOWN_ERROR:
    case InstanceID::NETWORK_ERROR:
      DLOG(WARNING) << "Messaging subscription failed; InstanceID::Result = "
                    << result;
      UpdateChannelState(FcmChannelState::NO_INSTANCE_ID_TOKEN);
      break;
  }
  ScheduleNextTokenValidation();
}

void FCMNetworkHandler::ScheduleNextTokenValidation() {
  DCHECK(IsListening());

  token_validation_timer_->Start(
      FROM_HERE, base::Minutes(kTokenValidationPeriodMinutesDefault),
      base::BindOnce(&FCMNetworkHandler::StartTokenValidation,
                     weak_ptr_factory_.GetWeakPtr()));
}

void FCMNetworkHandler::StartTokenValidation() {
  DCHECK(IsListening());

  instance_id_driver_->GetInstanceID(app_id_)->GetToken(
      sender_id_, kGCMScope, GetTimeToLive(sender_id_),
      /*flags=*/{InstanceID::Flags::kIsLazy},
      base::BindOnce(&FCMNetworkHandler::DidReceiveTokenForValidation,
                     weak_ptr_factory_.GetWeakPtr()));
}

void FCMNetworkHandler::DidReceiveTokenForValidation(
    const std::string& new_token,
    InstanceID::Result result) {
  if (!IsListening()) {
    // After we requested the token, |StopListening| has been called. Thus,
    // ignore the token.
    return;
  }

  if (result == InstanceID::SUCCESS) {
    UpdateChannelState(FcmChannelState::ENABLED);
    if (token_ != new_token) {
      token_ = new_token;
      DeliverToken(new_token);
    }
  }

  ScheduleNextTokenValidation();
}

void FCMNetworkHandler::UpdateChannelState(FcmChannelState state) {
  if (channel_state_ == state)
    return;
  channel_state_ = state;
  NotifyChannelStateChange(channel_state_);
}

void FCMNetworkHandler::ShutdownHandler() {}

void FCMNetworkHandler::OnStoreReset() {}

void FCMNetworkHandler::OnMessage(const std::string& app_id,
                                  const gcm::IncomingMessage& message) {
  DCHECK_EQ(app_id, app_id_);

  std::string payload;
  std::string private_topic;
  std::string public_topic;
  int64_t version = 0;
  InvalidationParsingStatus status = ParseIncomingMessage(
      message, &payload, &private_topic, &public_topic, &version);

  RecordFCMMessageStatus(status, sender_id_);

  if (status == InvalidationParsingStatus::kSuccess)
    DeliverIncomingMessage(payload, private_topic, public_topic, version);
}

void FCMNetworkHandler::OnMessagesDeleted(const std::string& app_id) {
  DCHECK_EQ(app_id, app_id_);
  // Note: As of 2020-02, this doesn't actually happen in practice.
}

void FCMNetworkHandler::OnSendError(
    const std::string& app_id,
    const gcm::GCMClient::SendErrorDetails& details) {
  // Should never be called because we don't send GCM messages to
  // the server.
  NOTREACHED() << "FCMNetworkHandler doesn't send GCM messages.";
}

void FCMNetworkHandler::OnSendAcknowledged(const std::string& app_id,
                                           const std::string& message_id) {
  // Should never be called because we don't send GCM messages to
  // the server.
  NOTREACHED() << "FCMNetworkHandler doesn't send GCM messages.";
}

void FCMNetworkHandler::SetTokenValidationTimerForTesting(
    std::unique_ptr<base::OneShotTimer> token_validation_timer) {
  token_validation_timer_ = std::move(token_validation_timer);
}

}  // namespace invalidation