Index: components/arc/trace/arc_trace_bridge.cc |
diff --git a/components/arc/trace/arc_trace_bridge.cc b/components/arc/trace/arc_trace_bridge.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..3ed7f8e957bf5d19eb6fd9907af4e96fae0a2599 |
--- /dev/null |
+++ b/components/arc/trace/arc_trace_bridge.cc |
@@ -0,0 +1,107 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "components/arc/trace/arc_trace_bridge.h" |
+ |
+#include <string> |
Yusuke Sato
2016/10/12 05:58:13
remove.
https://google.github.io/styleguide/cppgu
shunhsingou
2016/12/22 04:04:35
Done.
|
+#include <utility> |
+ |
+#include "base/logging.h" |
+#include "base/trace_event/trace_event.h" |
Yusuke Sato
2016/10/12 05:58:13
remove
shunhsingou
2016/12/22 04:04:34
Done.
|
+#include "components/arc/arc_bridge_service.h" |
+ |
+namespace arc { |
+ |
+namespace { |
Yusuke Sato
2016/10/12 05:58:13
space between line 16 and 17.
shunhsingou
2016/12/22 04:04:35
Done.
|
+// This class is owned by ArcServiceManager so that it is safe to use this raw |
+// pointer as the singleton reference. |
+ArcTraceBridge* g_arc_trace_bridge = nullptr; |
+ |
+// The prefix of the categories to be shown on the trace selection UI. |
+constexpr char kCategoryPrefix[] = TRACE_DISABLED_BY_DEFAULT("android "); |
+ |
+} // namcespace |
+ |
+ArcTraceBridge* ArcTraceBridge::Get() { |
+ DCHECK(g_arc_trace_bridge); |
Yusuke Sato
2016/10/12 05:58:13
Does this make sense? Your c/b/chromeos/ code has
shunhsingou
2016/12/22 04:04:35
Done.
|
+ return g_arc_trace_bridge; |
+} |
+ |
+ArcTraceBridge::ArcTraceBridge(ArcBridgeService* bridge_service) |
+ : ArcService(bridge_service), binding_(this), weak_ptr_factory_(this) { |
+ arc_bridge_service()->trace()->AddObserver(this); |
+ g_arc_trace_bridge = this; |
+} |
+ |
+ArcTraceBridge::~ArcTraceBridge() { |
+ arc_bridge_service()->trace()->RemoveObserver(this); |
+ g_arc_trace_bridge = nullptr; |
+} |
+ |
+void ArcTraceBridge::OnInstanceReady() { |
+ QueryAvailableCategories(); |
+} |
+ |
+void ArcTraceBridge::QueryAvailableCategories() { |
+ auto* trace_instance = arc_bridge_service()->trace()->GetInstanceForMethod( |
+ "QueryAvailableCategories"); |
+ if (!trace_instance) |
+ return; |
+ trace_instance->QueryAvailableCategories(base::Bind( |
+ &ArcTraceBridge::OnCategoriesReady, weak_ptr_factory_.GetWeakPtr())); |
+} |
+ |
+void ArcTraceBridge::OnCategoriesReady(mojo::Array<mojo::String> categories) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ |
+ for (const auto& category : categories) { |
+ categories_.push_back(Category( |
+ category, std::string(kCategoryPrefix) + std::string(category))); |
Yusuke Sato
2016/10/12 05:58:13
Converting both to std::string seems redundant.
shunhsingou
2016/12/22 04:04:34
Done.
|
+ // Shows the category name in the selection UI. |
Yusuke Sato
2016/10/12 05:58:13
nit: Show (imperative form)
shunhsingou
2016/12/22 04:04:35
Done.
|
+ base::trace_event::TraceLog::GetCategoryGroupEnabled( |
+ categories_.back().full_name.c_str()); |
+ } |
+} |
+ |
+void ArcTraceBridge::StartTracing( |
+ const base::trace_event::TraceConfig& trace_config) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ |
+ auto* trace_instance = |
+ arc_bridge_service()->trace()->GetInstanceForMethod("StartTracing"); |
+ if (!trace_instance) |
+ return; |
+ |
+ mojo::Array<mojo::String> selected_categories; |
+ for (const auto& category : categories_) { |
+ if (trace_config.IsCategoryGroupEnabled(category.full_name.c_str())) { |
Yusuke Sato
2016/10/12 05:58:13
nit: no {} for one-line if.
shunhsingou
2016/12/22 04:04:35
Done.
|
+ selected_categories.push_back(category.name); |
+ } |
+ } |
+ |
+ trace_instance->StartTracing(std::move(selected_categories), |
+ base::Bind(&ArcTraceBridge::OnTraceStart, |
+ weak_ptr_factory_.GetWeakPtr())); |
+} |
+ |
+void ArcTraceBridge::StopTracing() { |
+ auto* trace_instance = |
+ arc_bridge_service()->trace()->GetInstanceForMethod("StopTracing"); |
+ if (!trace_instance) |
+ return; |
+ trace_instance->StopTracing( |
+ base::Bind(&ArcTraceBridge::OnTraceStop, weak_ptr_factory_.GetWeakPtr())); |
+} |
+ |
+void ArcTraceBridge::OnTraceStart(bool success) { |
+ if (!success) |
+ LOG(ERROR) << "Failed to start tracing in the container."; |
+} |
+ |
+void ArcTraceBridge::OnTraceStop(bool success) { |
+ if (!success) |
+ LOG(ERROR) << "Failed to stop tracing in the container."; |
+} |
+ |
+} // namespace arc |