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 "mojo/application/application_runner_chromium.h" |
| 7 #include "mojo/common/weak_binding_set.h" |
| 8 #include "mojo/public/c/system/main.h" |
| 9 #include "mojo/public/cpp/application/application_delegate.h" |
| 10 #include "mojo/public/cpp/application/application_impl.h" |
| 11 #include "services/tracing/trace_data_sink.h" |
| 12 #include "services/tracing/tracing.mojom.h" |
| 13 |
| 14 namespace tracing { |
| 15 |
| 16 class TracingApp : public mojo::ApplicationDelegate, |
| 17 public mojo::InterfaceFactory<TraceCoordinator>, |
| 18 public tracing::TraceCoordinator, |
| 19 public mojo::InterfaceFactory<TraceDataCollector>, |
| 20 public tracing::TraceDataCollector { |
| 21 public: |
| 22 TracingApp() {} |
| 23 ~TracingApp() override {} |
| 24 |
| 25 private: |
| 26 // mojo::ApplicationDelegate implementation. |
| 27 bool ConfigureIncomingConnection( |
| 28 mojo::ApplicationConnection* connection) override { |
| 29 connection->AddService<TraceCoordinator>(this); |
| 30 connection->AddService<TraceDataCollector>(this); |
| 31 return true; |
| 32 } |
| 33 |
| 34 // mojo::InterfaceFactory<TraceCoordinator> implementation. |
| 35 void Create(mojo::ApplicationConnection* connection, |
| 36 mojo::InterfaceRequest<TraceCoordinator> request) override { |
| 37 coordinator_bindings_.AddBinding(this, request.Pass()); |
| 38 } |
| 39 |
| 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. |
| 47 void Start(const mojo::String& base_name, |
| 48 const mojo::String& categories) override { |
| 49 base::FilePath base_name_path = |
| 50 base::FilePath::FromUTF8Unsafe(base_name.To<std::string>()); |
| 51 sink_.reset(new TraceDataSink(base_name_path)); |
| 52 collector_bindings_.ForAllBindings([categories]( |
| 53 TraceController* controller) { controller->StartTracing(categories); }); |
| 54 } |
| 55 void StopAndFlush() override { |
| 56 collector_bindings_.ForAllBindings( |
| 57 [](TraceController* controller) { controller->StopTracing(); }); |
| 58 |
| 59 // TODO: We really should keep track of how many connections we have here |
| 60 // and flush + reset the sink after we receive a EndTracing or a detect a |
| 61 // pipe closure on all pipes. |
| 62 base::MessageLoop::current()->PostDelayedTask( |
| 63 FROM_HERE, |
| 64 base::Bind(&TraceDataSink::Flush, base::Unretained(sink_.get())), |
| 65 base::TimeDelta::FromSeconds(1)); |
| 66 } |
| 67 |
| 68 // tracing::TraceDataCollector implementation. |
| 69 void DataCollected(const mojo::String& json) override { |
| 70 if (sink_) |
| 71 sink_->AddChunk(json.To<std::string>()); |
| 72 } |
| 73 // tracing::TraceDataCollector implementation. |
| 74 void EndTracing() override {} |
| 75 |
| 76 scoped_ptr<TraceDataSink> sink_; |
| 77 mojo::WeakBindingSet<TraceDataCollector> collector_bindings_; |
| 78 mojo::WeakBindingSet<TraceCoordinator> coordinator_bindings_; |
| 79 |
| 80 DISALLOW_COPY_AND_ASSIGN(TracingApp); |
| 81 }; |
| 82 |
| 83 } // namespace tracing |
| 84 |
| 85 MojoResult MojoMain(MojoHandle shell_handle) { |
| 86 mojo::ApplicationRunnerChromium runner(new tracing::TracingApp); |
| 87 return runner.Run(shell_handle); |
| 88 } |
OLD | NEW |