OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/memory/scoped_vector.h" |
6 #include "mojo/application/application_runner_chromium.h" | 7 #include "mojo/application/application_runner_chromium.h" |
7 #include "mojo/common/weak_binding_set.h" | 8 #include "mojo/common/weak_binding_set.h" |
| 9 #include "mojo/common/weak_interface_ptr_set.h" |
8 #include "mojo/public/c/system/main.h" | 10 #include "mojo/public/c/system/main.h" |
9 #include "mojo/public/cpp/application/application_delegate.h" | 11 #include "mojo/public/cpp/application/application_delegate.h" |
10 #include "mojo/public/cpp/application/application_impl.h" | 12 #include "mojo/public/cpp/application/application_impl.h" |
| 13 #include "mojo/public/cpp/bindings/strong_binding.h" |
11 #include "services/tracing/trace_data_sink.h" | 14 #include "services/tracing/trace_data_sink.h" |
12 #include "services/tracing/tracing.mojom.h" | 15 #include "services/tracing/tracing.mojom.h" |
13 | 16 |
14 namespace tracing { | 17 namespace tracing { |
15 | 18 |
| 19 namespace { |
| 20 |
| 21 class CollectorImpl : public TraceDataCollector { |
| 22 public: |
| 23 CollectorImpl(mojo::InterfaceRequest<TraceDataCollector> request, |
| 24 TraceDataSink* sink) |
| 25 : sink_(sink), binding_(this, request.Pass()) {} |
| 26 |
| 27 ~CollectorImpl() override {} |
| 28 |
| 29 // tracing::TraceDataCollector implementation. |
| 30 void DataCollected(const mojo::String& json) override { |
| 31 sink_->AddChunk(json.To<std::string>()); |
| 32 } |
| 33 |
| 34 private: |
| 35 TraceDataSink* sink_; |
| 36 mojo::Binding<TraceDataCollector> binding_; |
| 37 |
| 38 DISALLOW_COPY_AND_ASSIGN(CollectorImpl); |
| 39 }; |
| 40 |
| 41 } // namespace |
| 42 |
16 class TracingApp : public mojo::ApplicationDelegate, | 43 class TracingApp : public mojo::ApplicationDelegate, |
17 public mojo::InterfaceFactory<TraceCoordinator>, | 44 public mojo::InterfaceFactory<TraceCoordinator>, |
18 public tracing::TraceCoordinator, | 45 public TraceCoordinator { |
19 public mojo::InterfaceFactory<TraceDataCollector>, | |
20 public tracing::TraceDataCollector { | |
21 public: | 46 public: |
22 TracingApp() {} | 47 TracingApp() {} |
23 ~TracingApp() override {} | 48 ~TracingApp() override {} |
24 | 49 |
25 private: | 50 private: |
26 // mojo::ApplicationDelegate implementation. | 51 // mojo::ApplicationDelegate implementation. |
27 bool ConfigureIncomingConnection( | 52 bool ConfigureIncomingConnection( |
28 mojo::ApplicationConnection* connection) override { | 53 mojo::ApplicationConnection* connection) override { |
29 connection->AddService<TraceCoordinator>(this); | 54 connection->AddService<TraceCoordinator>(this); |
30 connection->AddService<TraceDataCollector>(this); | 55 |
| 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()); |
31 return true; | 64 return true; |
32 } | 65 } |
33 | 66 |
34 // mojo::InterfaceFactory<TraceCoordinator> implementation. | 67 // mojo::InterfaceFactory<TraceCoordinator> implementation. |
35 void Create(mojo::ApplicationConnection* connection, | 68 void Create(mojo::ApplicationConnection* connection, |
36 mojo::InterfaceRequest<TraceCoordinator> request) override { | 69 mojo::InterfaceRequest<TraceCoordinator> request) override { |
37 coordinator_bindings_.AddBinding(this, request.Pass()); | 70 coordinator_bindings_.AddBinding(this, request.Pass()); |
38 } | 71 } |
39 | 72 |
40 // mojo::InterfaceFactory<TraceDataCollector> implementation. | |
41 void Create(mojo::ApplicationConnection* connection, | |
42 mojo::InterfaceRequest<TraceDataCollector> request) override { | |
43 collector_bindings_.AddBinding(this, request.Pass()); | |
44 } | |
45 | |
46 // tracing::TraceCoordinator implementation. | 73 // tracing::TraceCoordinator implementation. |
47 void Start(mojo::ScopedDataPipeProducerHandle stream, | 74 void Start(mojo::ScopedDataPipeProducerHandle stream, |
48 const mojo::String& categories) override { | 75 const mojo::String& categories) override { |
49 sink_.reset(new TraceDataSink(stream.Pass())); | 76 sink_.reset(new TraceDataSink(stream.Pass())); |
50 collector_bindings_.ForAllBindings([categories]( | 77 controller_ptrs_.ForAllPtrs( |
51 TraceController* controller) { controller->StartTracing(categories); }); | 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()); |
| 83 }); |
52 } | 84 } |
53 void StopAndFlush() override { | 85 void StopAndFlush() override { |
54 collector_bindings_.ForAllBindings( | 86 controller_ptrs_.ForAllPtrs( |
55 [](TraceController* controller) { controller->StopTracing(); }); | 87 [](TraceController* controller) { controller->StopTracing(); }); |
56 | 88 |
57 // TODO: We really should keep track of how many connections we have here | 89 // TODO: We really should keep track of how many connections we have here |
58 // and flush + reset the sink after we receive a EndTracing or a detect a | 90 // and flush + reset the sink after we receive a EndTracing or a detect a |
59 // pipe closure on all pipes. | 91 // pipe closure on all pipes. |
60 base::MessageLoop::current()->PostDelayedTask( | 92 base::MessageLoop::current()->PostDelayedTask( |
61 FROM_HERE, | 93 FROM_HERE, |
62 base::Bind(&TraceDataSink::Flush, base::Unretained(sink_.get())), | 94 base::Bind(&TracingApp::AllDataCollected, base::Unretained(this)), |
63 base::TimeDelta::FromSeconds(1)); | 95 base::TimeDelta::FromSeconds(1)); |
64 } | 96 } |
65 | 97 |
66 // tracing::TraceDataCollector implementation. | 98 void AllDataCollected() { |
67 void DataCollected(const mojo::String& json) override { | 99 collector_impls_.clear(); |
68 if (sink_) | 100 sink_->Flush(); |
69 sink_->AddChunk(json.To<std::string>()); | |
70 } | 101 } |
71 // tracing::TraceDataCollector implementation. | |
72 void EndTracing() override {} | |
73 | 102 |
74 scoped_ptr<TraceDataSink> sink_; | 103 scoped_ptr<TraceDataSink> sink_; |
75 mojo::WeakBindingSet<TraceDataCollector> collector_bindings_; | 104 ScopedVector<CollectorImpl> collector_impls_; |
| 105 mojo::WeakInterfacePtrSet<TraceController> controller_ptrs_; |
76 mojo::WeakBindingSet<TraceCoordinator> coordinator_bindings_; | 106 mojo::WeakBindingSet<TraceCoordinator> coordinator_bindings_; |
77 | 107 |
78 DISALLOW_COPY_AND_ASSIGN(TracingApp); | 108 DISALLOW_COPY_AND_ASSIGN(TracingApp); |
79 }; | 109 }; |
80 | 110 |
81 } // namespace tracing | 111 } // namespace tracing |
82 | 112 |
83 MojoResult MojoMain(MojoHandle shell_handle) { | 113 MojoResult MojoMain(MojoHandle shell_handle) { |
84 mojo::ApplicationRunnerChromium runner(new tracing::TracingApp); | 114 mojo::ApplicationRunnerChromium runner(new tracing::TracingApp); |
85 return runner.Run(shell_handle); | 115 return runner.Run(shell_handle); |
86 } | 116 } |
OLD | NEW |