Chromium Code Reviews| Index: chrome/browser/chromeos/arc/trace/arc_trace_bridge.cc |
| diff --git a/chrome/browser/chromeos/arc/trace/arc_trace_bridge.cc b/chrome/browser/chromeos/arc/trace/arc_trace_bridge.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6368e267f28bf905551be1eb78c09e3988a2697d |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/arc/trace/arc_trace_bridge.cc |
| @@ -0,0 +1,111 @@ |
| +// 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 "chrome/browser/chromeos/arc/trace/arc_trace_bridge.h" |
| + |
| +#include <utility> |
| + |
| +#include "base/logging.h" |
| +#include "components/arc/arc_bridge_service.h" |
| +#include "components/arc/arc_service_manager.h" |
| +#include "content/public/browser/browser_thread.h" |
| + |
| +namespace arc { |
| + |
| +namespace { |
| + |
| +// 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 "); |
| + |
| +} // namespace |
| + |
| +ArcTraceBridge* ArcTraceBridge::Get() { |
| + return g_arc_trace_bridge; |
| +} |
| + |
| +ArcTraceBridge::ArcTraceBridge(ArcBridgeService* bridge_service) |
| + : ArcService(bridge_service), weak_ptr_factory_(this) { |
| + arc_bridge_service()->trace()->AddObserver(this); |
| + DCHECK(!g_arc_trace_bridge); |
| + 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() { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + 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( |
| + const std::vector<std::string>& categories) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + |
| + categories_.clear(); |
| + for (const auto& category : categories) { |
| + categories_.push_back( |
| + Category{category, std::string(kCategoryPrefix) + category}); |
| + // Show the category name in the selection UI. |
| + base::trace_event::TraceLog::GetCategoryGroupEnabled( |
| + categories_.back().full_name.c_str()); |
| + } |
| +} |
| + |
| +void ArcTraceBridge::StartTracing( |
| + const base::trace_event::TraceConfig& trace_config) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + |
| + auto* trace_instance = |
| + arc_bridge_service()->trace()->GetInstanceForMethod("StartTracing"); |
| + if (!trace_instance) |
| + return; |
| + |
| + std::vector<std::string> selected_categories; |
| + for (const auto& category : categories_) { |
| + if (trace_config.IsCategoryGroupEnabled(category.full_name.c_str())) |
| + selected_categories.push_back(category.name); |
| + } |
| + |
| + trace_instance->StartTracing(selected_categories, |
| + base::Bind(&ArcTraceBridge::OnTraceStart, |
| + weak_ptr_factory_.GetWeakPtr())); |
| +} |
| + |
| +void ArcTraceBridge::StopTracing() { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + 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) { |
|
Luis Héctor Chávez
2016/12/28 17:21:58
These two methods don't access members and can be
shunhsingou
2016/12/29 09:40:29
Done.
|
| + 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 |