cr: Add 'gn' command
[chromium-blink-merge.git] / mojo / services / test_service / tracked_service.cc
bloba2fedfc9bb6ce6a9dbcf958b8e9511a325f198d3
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
3 // found in the LICENSE file.
5 #include "mojo/services/test_service/tracked_service.h"
7 #include "base/bind.h"
9 namespace mojo {
10 namespace test {
12 TrackedService::TrackedService(TestRequestTrackerPtr tracker,
13 const std::string& service_name,
14 const mojo::Callback<void()>& callback)
15 : id_(0u),
16 requests_since_upload_(0u),
17 service_name_(service_name),
18 tracker_(tracker.Pass()),
19 tracking_connected_callback_(callback) {
20 tracker_->SetNameAndReturnId(
21 service_name, base::Bind(&TrackedService::SetId, base::Unretained(this)));
24 TrackedService::~TrackedService() {
27 void TrackedService::RecordNewRequest() {
28 requests_since_upload_++;
29 if (id_ == 0u)
30 return;
31 SendStats();
34 void TrackedService::SendStats() {
35 ServiceStatsPtr stats(ServiceStats::New());
36 stats->num_new_requests = requests_since_upload_;
37 stats->health = 0.7;
38 tracker_->RecordStats(id_, stats.Pass());
39 requests_since_upload_ = 0u;
42 void TrackedService::SetId(uint64_t id) {
43 assert(id != 0u);
44 assert(id_ == 0u);
45 id_ = id;
46 tracking_connected_callback_.Run();
47 if (requests_since_upload_ == 0u)
48 return;
49 SendStats();
52 } // namespace test
53 } // namespace mojo