Chromium Code Reviews| 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/trace/arc_trace_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 constexpr char kCategoryPrefix[] = TRACE_DISABLED_BY_DEFAULT("android "); | |
| 20 | |
| 21 } // namespace | |
| 22 | |
| 23 ArcTraceBridge::ArcTraceBridge(ArcBridgeService* bridge_service) | |
| 24 : ArcService(bridge_service), weak_ptr_factory_(this) { | |
| 25 arc_bridge_service()->trace()->AddObserver(this); | |
| 26 chromeos::ArcTraceAgent::GetInstance()->SetDelegate(this); | |
| 27 } | |
| 28 | |
| 29 ArcTraceBridge::~ArcTraceBridge() { | |
| 30 chromeos::ArcTraceAgent::GetInstance()->SetDelegate(nullptr); | |
| 31 arc_bridge_service()->trace()->RemoveObserver(this); | |
| 32 } | |
| 33 | |
| 34 void ArcTraceBridge::OnInstanceReady() { | |
| 35 QueryAvailableCategories(); | |
|
Luis Héctor Chávez
2017/03/02 18:45:50
This looks unnecessary: how about moving the imple
Earl Ou
2017/03/03 05:37:50
Done.
| |
| 36 } | |
| 37 | |
| 38 void ArcTraceBridge::QueryAvailableCategories() { | |
| 39 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 40 auto* trace_instance = ARC_GET_INSTANCE_FOR_METHOD( | |
| 41 arc_bridge_service()->trace(), QueryAvailableCategories); | |
| 42 if (!trace_instance) | |
| 43 return; | |
| 44 trace_instance->QueryAvailableCategories(base::Bind( | |
| 45 &ArcTraceBridge::OnCategoriesReady, weak_ptr_factory_.GetWeakPtr())); | |
| 46 } | |
| 47 | |
| 48 void ArcTraceBridge::OnCategoriesReady( | |
| 49 const std::vector<std::string>& categories) { | |
| 50 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 51 | |
| 52 // There is no API in TraceLog to remove a category from the UI. As an | |
| 53 // alternative, the old category that is no longer in |categories_| will be | |
| 54 // ignored when calling |StartTracing|. | |
| 55 categories_.clear(); | |
| 56 for (const auto& category : categories) { | |
| 57 categories_.push_back( | |
| 58 Category{category, std::string(kCategoryPrefix) + category}); | |
| 59 // Show the category name in the selection UI. | |
| 60 base::trace_event::TraceLog::GetCategoryGroupEnabled( | |
| 61 categories_.back().full_name.c_str()); | |
| 62 } | |
| 63 } | |
| 64 | |
| 65 void ArcTraceBridge::StartTracing( | |
| 66 const base::trace_event::TraceConfig& trace_config, | |
| 67 const StartTracingCallback& callback) { | |
| 68 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 69 | |
| 70 auto* trace_instance = | |
| 71 ARC_GET_INSTANCE_FOR_METHOD(arc_bridge_service()->trace(), StartTracing); | |
| 72 if (!trace_instance) { | |
| 73 // Use PostTask as the convention of TraceAgent. The caller expects | |
| 74 // callback to be called after this function returns. | |
| 75 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | |
| 76 base::Bind(callback, false)); | |
| 77 return; | |
| 78 } | |
| 79 | |
| 80 std::vector<std::string> selected_categories; | |
| 81 for (const auto& category : categories_) { | |
| 82 if (trace_config.IsCategoryGroupEnabled(category.full_name.c_str())) | |
| 83 selected_categories.push_back(category.name); | |
| 84 } | |
| 85 | |
| 86 trace_instance->StartTracing(selected_categories, callback); | |
| 87 } | |
| 88 | |
| 89 void ArcTraceBridge::StopTracing(const StopTracingCallback& callback) { | |
| 90 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 91 | |
| 92 auto* trace_instance = | |
| 93 ARC_GET_INSTANCE_FOR_METHOD(arc_bridge_service()->trace(), StopTracing); | |
| 94 if (!trace_instance) { | |
| 95 callback.Run(false); | |
| 96 return; | |
| 97 } | |
| 98 trace_instance->StopTracing(callback); | |
| 99 } | |
| 100 | |
| 101 } // namespace arc | |
| OLD | NEW |