blob: 1d1483db2887f4832302331845fd5824691d74f3 [file] [log] [blame]
Przemyslaw Pietrzkiewicz1d909942015-08-28 16:27:56 +02001// Copyright 2015 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.
4
5#include "apps/benchmark/trace_collector_client.h"
6
7TraceCollectorClient::TraceCollectorClient(Receiver* receiver,
8 tracing::TraceCollectorPtr collector)
9 : receiver_(receiver),
10 collector_(collector.Pass()),
11 currently_tracing_(false) {}
12
13TraceCollectorClient::~TraceCollectorClient() {}
14
15void TraceCollectorClient::Start(const std::string& categories) {
16 DCHECK(!currently_tracing_);
17 currently_tracing_ = true;
18 mojo::DataPipe data_pipe;
19 collector_->Start(data_pipe.producer_handle.Pass(), categories);
20 drainer_.reset(new mojo::common::DataPipeDrainer(
21 this, data_pipe.consumer_handle.Pass()));
22 trace_data_.clear();
23 trace_data_ += "[";
24}
25
26void TraceCollectorClient::Stop() {
27 DCHECK(currently_tracing_);
28 currently_tracing_ = false;
29 collector_->StopAndFlush();
30}
31
32void TraceCollectorClient::OnDataAvailable(const void* data, size_t num_bytes) {
33 const char* chars = static_cast<const char*>(data);
34 trace_data_.append(chars, num_bytes);
35}
36
37void TraceCollectorClient::OnDataComplete() {
38 drainer_.reset();
39 collector_.reset();
40 trace_data_ += "]";
41 receiver_->OnTraceCollected(trace_data_);
42}