File: connection_event_tracker.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 (103 lines) | stat: -rw-r--r-- 3,649 bytes parent folder | download | duplicates (9)
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
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "google_apis/gcm/engine/connection_event_tracker.h"

#include "base/metrics/histogram_macros.h"
#include "base/time/time.h"
#include "net/base/network_change_notifier.h"

namespace {

// The maxiumum number of events which are stored before deleting old ones.
// This mirrors the behaviour of the GMS Core connection tracking.
constexpr size_t kMaxClientEvents = 30;

}  // namespace

namespace gcm {

ConnectionEventTracker::ConnectionEventTracker() = default;

ConnectionEventTracker::~ConnectionEventTracker() = default;

bool ConnectionEventTracker::IsEventInProgress() const {
  return current_event_.has_time_connection_started_ms();
}

void ConnectionEventTracker::StartConnectionAttempt() {
  // TODO(harkness): Can we dcheck here that there is not an in progress
  // connection?
  current_event_.set_time_connection_started_ms(
      base::Time::Now().InMillisecondsSinceUnixEpoch());
  // The connection type is passed to the server and stored there, so the
  // values should remain consistent.
  current_event_.set_network_type(
      static_cast<int>(net::NetworkChangeNotifier::GetConnectionType()));
}

void ConnectionEventTracker::EndConnectionAttempt() {
  DCHECK(IsEventInProgress());

  if (completed_events_.size() == kMaxClientEvents) {
    // Don't let the completed events grow beyond the max.
    completed_events_.pop_front();
    number_discarded_events_++;
  }

  // Current event is finished, so add it to our list of completed events.
  current_event_.set_time_connection_ended_ms(
      base::Time::Now().InMillisecondsSinceUnixEpoch());
  completed_events_.push_back(current_event_);
  current_event_.Clear();
}

void ConnectionEventTracker::ConnectionAttemptSucceeded() {
  // Record the successful connection so information about it can be sent in the
  // next login request. If there is a login failure, this will need to be
  // updated to a failed connection.
  current_event_.set_type(mcs_proto::ClientEvent::SUCCESSFUL_CONNECTION);
  current_event_.set_time_connection_established_ms(
      base::Time::Now().InMillisecondsSinceUnixEpoch());

  // A completed connection means that the old client event data has now been
  // sent to GCM. Delete old data.
  completed_events_.clear();
  number_discarded_events_ = 0;
}

void ConnectionEventTracker::ConnectionLoginFailed() {
  // A login failure would have originally been marked as a successful
  // connection, so now that it failed, that needs to be updated.
  DCHECK_EQ(current_event_.type(),
            mcs_proto::ClientEvent::SUCCESSFUL_CONNECTION);

  current_event_.set_type(mcs_proto::ClientEvent::FAILED_CONNECTION);
  current_event_.clear_time_connection_established_ms();
  current_event_.set_error_code(net::ERR_CONNECTION_RESET);
}

void ConnectionEventTracker::ConnectionAttemptFailed(int error) {
  DCHECK_NE(error, net::OK);

  current_event_.set_type(mcs_proto::ClientEvent::FAILED_CONNECTION);
  current_event_.set_error_code(error);
}

void ConnectionEventTracker::WriteToLoginRequest(
    mcs_proto::LoginRequest* request) {
  DCHECK(request);

  // Add an event to represented the discarded events if needed.
  if (number_discarded_events_ > 0) {
    mcs_proto::ClientEvent* event = request->add_client_event();
    event->set_type(mcs_proto::ClientEvent::DISCARDED_EVENTS);
    event->set_number_discarded_events(number_discarded_events_);
  }

  for (const mcs_proto::ClientEvent& event : completed_events_)
    request->add_client_event()->CopyFrom(event);
}

}  // namespace gcm