OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "base/bind.h" |
| 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" |
| 16 |
| 17 namespace tracing { |
| 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 |
| 43 class TracingApp : public mojo::ApplicationDelegate, |
| 44 public mojo::InterfaceFactory<TraceCoordinator>, |
| 45 public TraceCoordinator { |
| 46 public: |
| 47 TracingApp() {} |
| 48 ~TracingApp() override {} |
| 49 |
| 50 private: |
| 51 // mojo::ApplicationDelegate implementation. |
| 52 bool ConfigureIncomingConnection( |
| 53 mojo::ApplicationConnection* connection) override { |
| 54 connection->AddService<TraceCoordinator>(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()); |
| 64 return true; |
| 65 } |
| 66 |
| 67 // mojo::InterfaceFactory<TraceCoordinator> implementation. |
| 68 void Create(mojo::ApplicationConnection* connection, |
| 69 mojo::InterfaceRequest<TraceCoordinator> request) override { |
| 70 coordinator_bindings_.AddBinding(this, request.Pass()); |
| 71 } |
| 72 |
| 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()); |
| 83 }); |
| 84 } |
| 85 void StopAndFlush() override { |
| 86 controller_ptrs_.ForAllPtrs( |
| 87 [](TraceController* controller) { controller->StopTracing(); }); |
| 88 |
| 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( |
| 93 FROM_HERE, |
| 94 base::Bind(&TracingApp::AllDataCollected, base::Unretained(this)), |
| 95 base::TimeDelta::FromSeconds(1)); |
| 96 } |
| 97 |
| 98 void AllDataCollected() { |
| 99 collector_impls_.clear(); |
| 100 sink_->Flush(); |
| 101 } |
| 102 |
| 103 scoped_ptr<TraceDataSink> sink_; |
| 104 ScopedVector<CollectorImpl> collector_impls_; |
| 105 mojo::WeakInterfacePtrSet<TraceController> controller_ptrs_; |
| 106 mojo::WeakBindingSet<TraceCoordinator> coordinator_bindings_; |
| 107 |
| 108 DISALLOW_COPY_AND_ASSIGN(TracingApp); |
| 109 }; |
| 110 |
| 111 } // namespace tracing |
| 112 |
| 113 MojoResult MojoMain(MojoHandle shell_handle) { |
| 114 mojo::ApplicationRunnerChromium runner(new tracing::TracingApp); |
| 115 return runner.Run(shell_handle); |
| 116 } |
OLD | NEW |