Chromium Code Reviews| 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 "mojo/application/application_runner_chromium.h" | 6 #include "mojo/application/application_runner_chromium.h" |
| 7 #include "mojo/common/weak_binding_set.h" | 7 #include "mojo/common/weak_binding_set.h" |
| 8 #include "mojo/common/weak_interface_ptr_set.h" | |
| 8 #include "mojo/public/c/system/main.h" | 9 #include "mojo/public/c/system/main.h" |
| 9 #include "mojo/public/cpp/application/application_delegate.h" | 10 #include "mojo/public/cpp/application/application_delegate.h" |
| 10 #include "mojo/public/cpp/application/application_impl.h" | 11 #include "mojo/public/cpp/application/application_impl.h" |
| 11 #include "services/tracing/trace_data_sink.h" | 12 #include "services/tracing/trace_data_sink.h" |
| 12 #include "services/tracing/tracing.mojom.h" | 13 #include "services/tracing/tracing.mojom.h" |
| 13 | 14 |
| 14 namespace tracing { | 15 namespace tracing { |
| 15 | 16 |
| 16 class TracingApp : public mojo::ApplicationDelegate, | 17 class TracingApp : public mojo::ApplicationDelegate, |
| 17 public mojo::InterfaceFactory<TraceCoordinator>, | 18 public mojo::InterfaceFactory<TraceCoordinator>, |
| 18 public tracing::TraceCoordinator, | 19 public tracing::TraceCoordinator, |
| 19 public mojo::InterfaceFactory<TraceDataCollector>, | |
| 20 public tracing::TraceDataCollector { | 20 public tracing::TraceDataCollector { |
| 21 public: | 21 public: |
| 22 TracingApp() {} | 22 TracingApp() {} |
| 23 ~TracingApp() override {} | 23 ~TracingApp() override {} |
| 24 | 24 |
| 25 private: | 25 private: |
| 26 // mojo::ApplicationDelegate implementation. | 26 // mojo::ApplicationDelegate implementation. |
| 27 bool ConfigureIncomingConnection( | 27 bool ConfigureIncomingConnection( |
| 28 mojo::ApplicationConnection* connection) override { | 28 mojo::ApplicationConnection* connection) override { |
| 29 connection->AddService<TraceCoordinator>(this); | 29 connection->AddService<TraceCoordinator>(this); |
| 30 connection->AddService<TraceDataCollector>(this); | 30 |
| 31 // If someone connects to us they may want to use the TraceCoordinator | |
| 32 // interface and/or they may want to expose themselves to be traced. Attempt | |
| 33 // to connect to the TraceController interface to see if the application | |
| 34 // connecting to us wants to be traced. They can refuse the connection or | |
| 35 // close the pipe if not. | |
| 36 mojo::InterfacePtr<TraceController> controller_ptr; | |
| 37 connection->ConnectToService(&controller_ptr); | |
| 38 controller_ptrs_.AddInterfacePtr(controller_ptr.Pass()); | |
| 31 return true; | 39 return true; |
| 32 } | 40 } |
| 33 | 41 |
| 34 // mojo::InterfaceFactory<TraceCoordinator> implementation. | 42 // mojo::InterfaceFactory<TraceCoordinator> implementation. |
| 35 void Create(mojo::ApplicationConnection* connection, | 43 void Create(mojo::ApplicationConnection* connection, |
| 36 mojo::InterfaceRequest<TraceCoordinator> request) override { | 44 mojo::InterfaceRequest<TraceCoordinator> request) override { |
| 37 coordinator_bindings_.AddBinding(this, request.Pass()); | 45 coordinator_bindings_.AddBinding(this, request.Pass()); |
| 38 } | 46 } |
| 39 | 47 |
| 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. | 48 // tracing::TraceCoordinator implementation. |
| 47 void Start(const mojo::String& base_name, | 49 void Start(const mojo::String& base_name, |
| 48 const mojo::String& categories) override { | 50 const mojo::String& categories) override { |
| 49 base::FilePath base_name_path = | 51 base::FilePath base_name_path = |
| 50 base::FilePath::FromUTF8Unsafe(base_name.To<std::string>()); | 52 base::FilePath::FromUTF8Unsafe(base_name.To<std::string>()); |
| 51 sink_.reset(new TraceDataSink(base_name_path)); | 53 sink_.reset(new TraceDataSink(base_name_path)); |
| 52 collector_bindings_.ForAllBindings([categories]( | 54 controller_ptrs_.ForAllPtrs( |
| 53 TraceController* controller) { controller->StartTracing(categories); }); | 55 [categories, this](TraceController* controller) { |
| 56 auto binding = new mojo::Binding<TraceDataCollector>(this); | |
| 57 TraceDataCollectorPtr ptr; | |
| 58 binding->Bind(&ptr); | |
| 59 collector_bindings_.push_back(binding); | |
| 60 controller->StartTracing(categories, ptr.Pass()); | |
| 61 }); | |
| 54 } | 62 } |
| 55 void StopAndFlush() override { | 63 void StopAndFlush() override { |
| 56 collector_bindings_.ForAllBindings( | 64 controller_ptrs_.ForAllPtrs( |
| 57 [](TraceController* controller) { controller->StopTracing(); }); | 65 [](TraceController* controller) { controller->StopTracing(); }); |
| 58 | 66 |
| 59 // TODO: We really should keep track of how many connections we have here | 67 // TODO: We really should keep track of how many connections we have here |
|
jamesr
2014/12/19 02:00:59
what i really want do to is join all controller me
| |
| 60 // and flush + reset the sink after we receive a EndTracing or a detect a | 68 // and flush + reset the sink after we receive a EndTracing or a detect a |
| 61 // pipe closure on all pipes. | 69 // pipe closure on all pipes. |
| 62 base::MessageLoop::current()->PostDelayedTask( | 70 base::MessageLoop::current()->PostDelayedTask( |
| 63 FROM_HERE, | 71 FROM_HERE, |
| 64 base::Bind(&TraceDataSink::Flush, base::Unretained(sink_.get())), | 72 base::Bind(&TracingApp::AllDataCollected, base::Unretained(this)), |
| 65 base::TimeDelta::FromSeconds(1)); | 73 base::TimeDelta::FromSeconds(1)); |
| 66 } | 74 } |
| 67 | 75 |
| 76 void AllDataCollected() { | |
| 77 for (auto p : collector_bindings_) | |
| 78 delete p; | |
| 79 collector_bindings_.clear(); | |
| 80 sink_->Flush(); | |
| 81 } | |
| 82 | |
| 68 // tracing::TraceDataCollector implementation. | 83 // tracing::TraceDataCollector implementation. |
| 69 void DataCollected(const mojo::String& json) override { | 84 void DataCollected(const mojo::String& json) override { |
| 70 if (sink_) | 85 if (sink_) |
| 71 sink_->AddChunk(json.To<std::string>()); | 86 sink_->AddChunk(json.To<std::string>()); |
| 72 } | 87 } |
| 73 // tracing::TraceDataCollector implementation. | |
| 74 void EndTracing() override {} | |
| 75 | 88 |
| 76 scoped_ptr<TraceDataSink> sink_; | 89 scoped_ptr<TraceDataSink> sink_; |
| 77 mojo::WeakBindingSet<TraceDataCollector> collector_bindings_; | 90 std::vector<mojo::Binding<TraceDataCollector>*> collector_bindings_; |
| 91 mojo::WeakInterfacePtrSet<TraceController> controller_ptrs_; | |
| 78 mojo::WeakBindingSet<TraceCoordinator> coordinator_bindings_; | 92 mojo::WeakBindingSet<TraceCoordinator> coordinator_bindings_; |
| 79 | 93 |
| 80 DISALLOW_COPY_AND_ASSIGN(TracingApp); | 94 DISALLOW_COPY_AND_ASSIGN(TracingApp); |
| 81 }; | 95 }; |
| 82 | 96 |
| 83 } // namespace tracing | 97 } // namespace tracing |
| 84 | 98 |
| 85 MojoResult MojoMain(MojoHandle shell_handle) { | 99 MojoResult MojoMain(MojoHandle shell_handle) { |
| 86 mojo::ApplicationRunnerChromium runner(new tracing::TracingApp); | 100 mojo::ApplicationRunnerChromium runner(new tracing::TracingApp); |
| 87 return runner.Run(shell_handle); | 101 return runner.Run(shell_handle); |
| 88 } | 102 } |
| OLD | NEW |