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.
6 #include "base/memory/scoped_vector.h"
7 #include "mojo/application/application_runner_chromium.h"
8 #include "mojo/common/weak_binding_set.h"
9 #include "mojo/common/weak_interface_ptr_set.h"
10 #include "mojo/public/c/system/main.h"
11 #include "mojo/public/cpp/application/application_delegate.h"
12 #include "mojo/public/cpp/application/application_impl.h"
13 #include "mojo/public/cpp/bindings/strong_binding.h"
14 #include "mojo/services/tracing/trace_data_sink.h"
15 #include "mojo/services/tracing/tracing.mojom.h"
21 class CollectorImpl
: public TraceDataCollector
{
23 CollectorImpl(mojo::InterfaceRequest
<TraceDataCollector
> request
,
25 : sink_(sink
), binding_(this, request
.Pass()) {}
27 ~CollectorImpl() override
{}
29 // tracing::TraceDataCollector implementation.
30 void DataCollected(const mojo::String
& json
) override
{
31 sink_
->AddChunk(json
.To
<std::string
>());
36 mojo::Binding
<TraceDataCollector
> binding_
;
38 DISALLOW_COPY_AND_ASSIGN(CollectorImpl
);
43 class TracingApp
: public mojo::ApplicationDelegate
,
44 public mojo::InterfaceFactory
<TraceCoordinator
>,
45 public TraceCoordinator
{
48 ~TracingApp() override
{}
51 // mojo::ApplicationDelegate implementation.
52 bool ConfigureIncomingConnection(
53 mojo::ApplicationConnection
* connection
) override
{
54 connection
->AddService
<TraceCoordinator
>(this);
56 // If someone connects to us they may want to use the TraceCoordinator
57 // interface and/or they may want to expose themselves to be traced. Attempt
58 // to connect to the TraceController interface to see if the application
59 // connecting to us wants to be traced. They can refuse the connection or
60 // close the pipe if not.
61 TraceControllerPtr controller_ptr
;
62 connection
->ConnectToService(&controller_ptr
);
63 controller_ptrs_
.AddInterfacePtr(controller_ptr
.Pass());
67 // mojo::InterfaceFactory<TraceCoordinator> implementation.
68 void Create(mojo::ApplicationConnection
* connection
,
69 mojo::InterfaceRequest
<TraceCoordinator
> request
) override
{
70 coordinator_bindings_
.AddBinding(this, request
.Pass());
73 // tracing::TraceCoordinator implementation.
74 void Start(mojo::ScopedDataPipeProducerHandle stream
,
75 const mojo::String
& categories
) override
{
76 sink_
.reset(new TraceDataSink(stream
.Pass()));
77 controller_ptrs_
.ForAllPtrs(
78 [categories
, this](TraceController
* controller
) {
79 TraceDataCollectorPtr ptr
;
80 collector_impls_
.push_back(
81 new CollectorImpl(GetProxy(&ptr
), sink_
.get()));
82 controller
->StartTracing(categories
, ptr
.Pass());
85 void StopAndFlush() override
{
86 controller_ptrs_
.ForAllPtrs(
87 [](TraceController
* controller
) { controller
->StopTracing(); });
89 // TODO: We really should keep track of how many connections we have here
90 // and flush + reset the sink after we receive a EndTracing or a detect a
91 // pipe closure on all pipes.
92 base::MessageLoop::current()->PostDelayedTask(
94 base::Bind(&TracingApp::AllDataCollected
, base::Unretained(this)),
95 base::TimeDelta::FromSeconds(1));
98 void AllDataCollected() {
99 collector_impls_
.clear();
103 scoped_ptr
<TraceDataSink
> sink_
;
104 ScopedVector
<CollectorImpl
> collector_impls_
;
105 mojo::WeakInterfacePtrSet
<TraceController
> controller_ptrs_
;
106 mojo::WeakBindingSet
<TraceCoordinator
> coordinator_bindings_
;
108 DISALLOW_COPY_AND_ASSIGN(TracingApp
);
111 } // namespace tracing
113 MojoResult
MojoMain(MojoHandle shell_handle
) {
114 mojo::ApplicationRunnerChromium
runner(new tracing::TracingApp
);
115 return runner
.Run(shell_handle
);