| OLD | NEW |
| (Empty) |
| 1 // Copyright 2017 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 "chrome/browser/chromeos/arc/tracing/arc_tracing_bridge.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/threading/thread_task_runner_handle.h" | |
| 10 #include "components/arc/arc_bridge_service.h" | |
| 11 #include "components/arc/arc_service_manager.h" | |
| 12 #include "content/public/browser/browser_thread.h" | |
| 13 | |
| 14 namespace arc { | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 // The prefix of the categories to be shown on the trace selection UI. | |
| 19 // The space at the end of the string is intentional as the separator between | |
| 20 // the prefix and the real categories. | |
| 21 constexpr char kCategoryPrefix[] = TRACE_DISABLED_BY_DEFAULT("android "); | |
| 22 | |
| 23 } // namespace | |
| 24 | |
| 25 struct ArcTracingBridge::Category { | |
| 26 // The name used by Android to trigger tracing. | |
| 27 std::string name; | |
| 28 // The full name shown in the tracing UI in chrome://tracing. | |
| 29 std::string full_name; | |
| 30 }; | |
| 31 | |
| 32 ArcTracingBridge::ArcTracingBridge(ArcBridgeService* bridge_service) | |
| 33 : ArcService(bridge_service), weak_ptr_factory_(this) { | |
| 34 arc_bridge_service()->tracing()->AddObserver(this); | |
| 35 content::ArcTracingAgent::GetInstance()->SetDelegate(this); | |
| 36 } | |
| 37 | |
| 38 ArcTracingBridge::~ArcTracingBridge() { | |
| 39 content::ArcTracingAgent::GetInstance()->SetDelegate(nullptr); | |
| 40 arc_bridge_service()->tracing()->RemoveObserver(this); | |
| 41 } | |
| 42 | |
| 43 void ArcTracingBridge::OnInstanceReady() { | |
| 44 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 45 mojom::TracingInstance* tracing_instance = ARC_GET_INSTANCE_FOR_METHOD( | |
| 46 arc_bridge_service()->tracing(), QueryAvailableCategories); | |
| 47 if (!tracing_instance) | |
| 48 return; | |
| 49 tracing_instance->QueryAvailableCategories(base::Bind( | |
| 50 &ArcTracingBridge::OnCategoriesReady, weak_ptr_factory_.GetWeakPtr())); | |
| 51 } | |
| 52 | |
| 53 void ArcTracingBridge::OnCategoriesReady( | |
| 54 const std::vector<std::string>& categories) { | |
| 55 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 56 | |
| 57 // There is no API in TraceLog to remove a category from the UI. As an | |
| 58 // alternative, the old category that is no longer in |categories_| will be | |
| 59 // ignored when calling |StartTracing|. | |
| 60 categories_.clear(); | |
| 61 for (const auto& category : categories) { | |
| 62 categories_.push_back({category, kCategoryPrefix + category}); | |
| 63 // Show the category name in the selection UI. | |
| 64 base::trace_event::TraceLog::GetCategoryGroupEnabled( | |
| 65 categories_.back().full_name.c_str()); | |
| 66 } | |
| 67 } | |
| 68 | |
| 69 void ArcTracingBridge::StartTracing( | |
| 70 const base::trace_event::TraceConfig& trace_config, | |
| 71 const StartTracingCallback& callback) { | |
| 72 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 73 | |
| 74 mojom::TracingInstance* tracing_instance = ARC_GET_INSTANCE_FOR_METHOD( | |
| 75 arc_bridge_service()->tracing(), StartTracing); | |
| 76 if (!tracing_instance) { | |
| 77 // Use PostTask as the convention of TracingAgent. The caller expects | |
| 78 // callback to be called after this function returns. | |
| 79 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | |
| 80 base::Bind(callback, false)); | |
| 81 return; | |
| 82 } | |
| 83 | |
| 84 std::vector<std::string> selected_categories; | |
| 85 for (const auto& category : categories_) { | |
| 86 if (trace_config.IsCategoryGroupEnabled(category.full_name.c_str())) | |
| 87 selected_categories.push_back(category.name); | |
| 88 } | |
| 89 | |
| 90 tracing_instance->StartTracing(selected_categories, callback); | |
| 91 } | |
| 92 | |
| 93 void ArcTracingBridge::StopTracing(const StopTracingCallback& callback) { | |
| 94 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 95 | |
| 96 mojom::TracingInstance* tracing_instance = | |
| 97 ARC_GET_INSTANCE_FOR_METHOD(arc_bridge_service()->tracing(), StopTracing); | |
| 98 if (!tracing_instance) { | |
| 99 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | |
| 100 base::Bind(callback, false)); | |
| 101 return; | |
| 102 } | |
| 103 tracing_instance->StopTracing(callback); | |
| 104 } | |
| 105 | |
| 106 } // namespace arc | |
| OLD | NEW |