Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/engine/connection_event_tracker.h" | 5 #include "google_apis/gcm/engine/connection_event_tracker.h" |
| 6 | 6 |
| 7 #include "base/time/time.h" | 7 #include "base/time/time.h" |
| 8 #include "net/base/network_change_notifier.h" | 8 #include "net/base/network_change_notifier.h" |
| 9 | 9 |
| 10 namespace { | 10 namespace { |
| 11 | 11 |
| 12 // The maxiumum number of events which are stored before deleting old ones. | 12 // The maxiumum number of events which are stored before deleting old ones. |
| 13 // This mirrors the behaviour of the GMS Core connection tracking. | 13 // This mirrors the behaviour of the GMS Core connection tracking. |
| 14 constexpr size_t kMaxClientEvents = 30; | 14 constexpr size_t kMaxClientEvents = 30; |
| 15 | 15 |
| 16 } // namespace | 16 } // namespace |
| 17 | 17 |
| 18 namespace gcm { | 18 namespace gcm { |
| 19 | 19 |
| 20 ConnectionEventTracker::ConnectionEventTracker() = default; | 20 ConnectionEventTracker::ConnectionEventTracker() = default; |
| 21 | 21 |
| 22 ConnectionEventTracker::~ConnectionEventTracker() = default; | 22 ConnectionEventTracker::~ConnectionEventTracker() = default; |
| 23 | 23 |
| 24 bool ConnectionEventTracker::EventInProgress() { | |
| 25 return current_event_.has_time_connection_started_ms(); | |
| 26 } | |
| 27 | |
| 24 void ConnectionEventTracker::StartConnectionAttempt() { | 28 void ConnectionEventTracker::StartConnectionAttempt() { |
| 25 // TODO(harkness): Can we dcheck here that there is not an in progress | 29 // TODO(harkness): Can we dcheck here that there is not an in progress |
| 26 // connection? | 30 // connection? |
| 27 current_event_.set_time_connection_started_ms(base::Time::Now().ToJavaTime()); | 31 current_event_.set_time_connection_started_ms(base::Time::Now().ToJavaTime()); |
| 28 // The connection type is passed to the server and stored there, so the | 32 // The connection type is passed to the server and stored there, so the |
| 29 // values should remain consistent. | 33 // values should remain consistent. |
| 30 current_event_.set_network_type( | 34 current_event_.set_network_type( |
| 31 static_cast<int>(net::NetworkChangeNotifier::GetConnectionType())); | 35 static_cast<int>(net::NetworkChangeNotifier::GetConnectionType())); |
| 32 } | 36 } |
| 33 | 37 |
| 34 void ConnectionEventTracker::EndConnectionAttempt() { | 38 void ConnectionEventTracker::EndConnectionAttempt() { |
| 35 // TODO(harkness): Modify tests so that we can put a DCHECK here. | 39 // EndConnectionAttempt should only be called if there was a previous |
| 40 // StartConnectionAttempt. | |
|
Peter Beverloo
2017/01/12 16:03:27
optional nit: I'd drop the comment - that's obviou
harkness
2017/01/16 12:20:37
Done.
| |
| 41 DCHECK(EventInProgress()); | |
| 42 | |
| 36 if (completed_events_.size() == kMaxClientEvents) { | 43 if (completed_events_.size() == kMaxClientEvents) { |
| 37 // Don't let the completed events grow beyond the max. | 44 // Don't let the completed events grow beyond the max. |
| 38 completed_events_.pop_front(); | 45 completed_events_.pop_front(); |
| 39 number_discarded_events_++; | 46 number_discarded_events_++; |
| 40 } | 47 } |
| 41 | 48 |
| 42 // Current event is now completed, so add it to our list of completed events. | 49 // Current event is finished, so add it to our list of completed events. |
| 43 current_event_.set_time_connection_ended_ms(base::Time::Now().ToJavaTime()); | 50 current_event_.set_time_connection_ended_ms(base::Time::Now().ToJavaTime()); |
| 44 completed_events_.push_back(current_event_); | 51 completed_events_.push_back(current_event_); |
| 45 current_event_.Clear(); | 52 current_event_.Clear(); |
| 46 } | 53 } |
| 47 | 54 |
| 48 void ConnectionEventTracker::ConnectionAttemptSucceeded() { | 55 void ConnectionEventTracker::ConnectionAttemptSucceeded() { |
| 49 // Record the successful connection so information about it can be sent in the | 56 // Record the successful connection so information about it can be sent in the |
| 50 // next login request. If there is a login failure, this will need to be | 57 // next login request. If there is a login failure, this will need to be |
| 51 // updated to a failed connection. | 58 // updated to a failed connection. |
| 52 current_event_.set_type(mcs_proto::ClientEvent::SUCCESSFUL_CONNECTION); | 59 current_event_.set_type(mcs_proto::ClientEvent::SUCCESSFUL_CONNECTION); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 86 mcs_proto::ClientEvent* event = request->add_client_event(); | 93 mcs_proto::ClientEvent* event = request->add_client_event(); |
| 87 event->set_type(mcs_proto::ClientEvent::DISCARDED_EVENTS); | 94 event->set_type(mcs_proto::ClientEvent::DISCARDED_EVENTS); |
| 88 event->set_number_discarded_events(number_discarded_events_); | 95 event->set_number_discarded_events(number_discarded_events_); |
| 89 } | 96 } |
| 90 | 97 |
| 91 for (const mcs_proto::ClientEvent& event : completed_events_) | 98 for (const mcs_proto::ClientEvent& event : completed_events_) |
| 92 request->add_client_event()->CopyFrom(event); | 99 request->add_client_event()->CopyFrom(event); |
| 93 } | 100 } |
| 94 | 101 |
| 95 } // namespace gcm | 102 } // namespace gcm |
| OLD | NEW |