Commit ce1ecbdc authored by Andrei-Laurențiu Olteanu's avatar Andrei-Laurențiu Olteanu Committed by Commit Bot

[Telemetry SWX] Add convert_ptr.h

ConvertPtr method is used by Probe and Diagnostics services. To
avoid duplicated code, this method is moved to a common place.

Move Probe and Diagnostics to the same namespace.

Bug: b:162051831, b:158658869
Change-Id: I5b735eee3ef4c0aa9a58762c0aac074226646763
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2362934
Commit-Queue: Laurențiu Olteanu <lolteanu@google.com>
Reviewed-by: default avatarMahmoud Gawad <mgawad@google.com>
Reviewed-by: default avatarOleh Lamzin <lamzin@google.com>
Cr-Commit-Position: refs/heads/master@{#802140}
parent 6213b9d5
...@@ -8,6 +8,7 @@ assert(!is_official_build, ...@@ -8,6 +8,7 @@ assert(!is_official_build,
source_set("telemetry_extension_ui") { source_set("telemetry_extension_ui") {
sources = [ sources = [
"convert_ptr.h",
"diagnostics_service.cc", "diagnostics_service.cc",
"diagnostics_service.h", "diagnostics_service.h",
"diagnostics_service_converters.cc", "diagnostics_service_converters.cc",
...@@ -41,6 +42,7 @@ source_set("telemetry_extension_ui") { ...@@ -41,6 +42,7 @@ source_set("telemetry_extension_ui") {
source_set("unit_tests") { source_set("unit_tests") {
testonly = true testonly = true
sources = [ sources = [
"convert_ptr_unittest.cc",
"diagnostics_service_converters_unittest.cc", "diagnostics_service_converters_unittest.cc",
"probe_service_converters_unittest.cc", "probe_service_converters_unittest.cc",
"probe_service_unittest.cc", "probe_service_unittest.cc",
......
// Copyright 2020 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.
#ifndef CHROMEOS_COMPONENTS_TELEMETRY_EXTENSION_UI_CONVERT_PTR_H_
#define CHROMEOS_COMPONENTS_TELEMETRY_EXTENSION_UI_CONVERT_PTR_H_
#if defined(OFFICIAL_BUILD)
#error ConvertPtr should only be included in unofficial builds.
#endif
#include <utility>
#include "chromeos/components/telemetry_extension_ui/diagnostics_service_converters.h"
#include "chromeos/components/telemetry_extension_ui/probe_service_converters.h"
// To use ConvertPtr with other functions, headers to function definitions
// must be included in this file.
namespace chromeos {
namespace converters {
template <class InputT>
auto ConvertPtr(InputT input) {
return (!input.is_null()) ? unchecked::UncheckedConvertPtr(std::move(input))
: nullptr;
}
} // namespace converters
} // namespace chromeos
#endif // CHROMEOS_COMPONENTS_TELEMETRY_EXTENSION_UI_CONVERT_PTR_H_
// Copyright 2020 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 "chromeos/components/telemetry_extension_ui/convert_ptr.h"
#include "chromeos/components/telemetry_extension_ui/mojom/probe_service.mojom.h"
#include "chromeos/services/cros_healthd/public/mojom/cros_healthd_probe.mojom.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chromeos {
namespace converters {
// Tests that |ConvertPtr| function returns nullptr if input is nullptr.
// ConvertPtr is a template, so we can test this function with any valid type.
TEST(TelemetryConvertPtr, ConvertPtrTakesNullPtr) {
EXPECT_TRUE(ConvertPtr(cros_healthd::mojom::ProbeErrorPtr()).is_null());
}
} // namespace converters
} // namespace chromeos
\ No newline at end of file
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include <vector> #include <vector>
#include "base/bind.h" #include "base/bind.h"
#include "chromeos/components/telemetry_extension_ui/convert_ptr.h"
#include "chromeos/components/telemetry_extension_ui/diagnostics_service_converters.h" #include "chromeos/components/telemetry_extension_ui/diagnostics_service_converters.h"
#include "chromeos/services/cros_healthd/public/cpp/service_connection.h" #include "chromeos/services/cros_healthd/public/cpp/service_connection.h"
#include "chromeos/services/cros_healthd/public/mojom/cros_healthd_diagnostics.mojom.h" #include "chromeos/services/cros_healthd/public/mojom/cros_healthd_diagnostics.mojom.h"
...@@ -42,8 +43,7 @@ void DiagnosticsService::GetAvailableRoutines( ...@@ -42,8 +43,7 @@ void DiagnosticsService::GetAvailableRoutines(
callback, callback,
const std::vector<cros_healthd::mojom::DiagnosticRoutineEnum>& const std::vector<cros_healthd::mojom::DiagnosticRoutineEnum>&
routines) { routines) {
std::move(callback).Run( std::move(callback).Run(converters::Convert(routines));
diagnostics_service_converters::Convert(routines));
}, },
std::move(callback))); std::move(callback)));
} }
...@@ -54,13 +54,12 @@ void DiagnosticsService::GetRoutineUpdate( ...@@ -54,13 +54,12 @@ void DiagnosticsService::GetRoutineUpdate(
bool include_output, bool include_output,
GetRoutineUpdateCallback callback) { GetRoutineUpdateCallback callback) {
GetService()->GetRoutineUpdate( GetService()->GetRoutineUpdate(
id, diagnostics_service_converters::Convert(command), include_output, id, converters::Convert(command), include_output,
base::BindOnce( base::BindOnce(
[](health::mojom::DiagnosticsService::GetRoutineUpdateCallback [](health::mojom::DiagnosticsService::GetRoutineUpdateCallback
callback, callback,
cros_healthd::mojom::RoutineUpdatePtr ptr) { cros_healthd::mojom::RoutineUpdatePtr ptr) {
std::move(callback).Run( std::move(callback).Run(converters::ConvertPtr(std::move(ptr)));
diagnostics_service_converters::ConvertPtr(std::move(ptr)));
}, },
std::move(callback))); std::move(callback)));
} }
......
...@@ -7,11 +7,12 @@ ...@@ -7,11 +7,12 @@
#include "base/notreached.h" #include "base/notreached.h"
#include "base/optional.h" #include "base/optional.h"
#include "chrome/browser/chromeos/wilco_dtc_supportd/mojo_utils.h" #include "chrome/browser/chromeos/wilco_dtc_supportd/mojo_utils.h"
#include "chromeos/components/telemetry_extension_ui/convert_ptr.h"
#include "chromeos/components/telemetry_extension_ui/mojom/diagnostics_service.mojom.h" #include "chromeos/components/telemetry_extension_ui/mojom/diagnostics_service.mojom.h"
#include "chromeos/services/cros_healthd/public/mojom/cros_healthd_diagnostics.mojom.h" #include "chromeos/services/cros_healthd/public/mojom/cros_healthd_diagnostics.mojom.h"
namespace chromeos { namespace chromeos {
namespace diagnostics_service_converters { namespace converters {
namespace unchecked { namespace unchecked {
...@@ -168,5 +169,5 @@ std::vector<health::mojom::DiagnosticRoutineEnum> Convert( ...@@ -168,5 +169,5 @@ std::vector<health::mojom::DiagnosticRoutineEnum> Convert(
return output; return output;
} }
} // namespace diagnostics_service_converters } // namespace converters
} // namespace chromeos } // namespace chromeos
...@@ -14,11 +14,11 @@ ...@@ -14,11 +14,11 @@
#include <vector> #include <vector>
#include "chromeos/components/telemetry_extension_ui/mojom/diagnostics_service.mojom-forward.h" #include "chromeos/components/telemetry_extension_ui/mojom/diagnostics_service.mojom-forward.h"
#include "chromeos/services/cros_healthd/public/mojom/cros_healthd_diagnostics.mojom-forward.h" #include "chromeos/services/cros_healthd/public/mojom/cros_healthd_diagnostics.mojom.h"
#include "mojo/public/cpp/system/handle.h" #include "mojo/public/cpp/system/handle.h"
namespace chromeos { namespace chromeos {
namespace diagnostics_service_converters { namespace converters {
// This file contains helper functions used by DiagnosticsService to convert its // This file contains helper functions used by DiagnosticsService to convert its
// types to/from cros_healthd DiagnosticsService types. // types to/from cros_healthd DiagnosticsService types.
...@@ -53,13 +53,7 @@ cros_healthd::mojom::DiagnosticRoutineCommandEnum Convert( ...@@ -53,13 +53,7 @@ cros_healthd::mojom::DiagnosticRoutineCommandEnum Convert(
std::string Convert(mojo::ScopedHandle handle); std::string Convert(mojo::ScopedHandle handle);
template <class InputT> } // namespace converters
auto ConvertPtr(InputT input) {
return (!input.is_null()) ? unchecked::UncheckedConvertPtr(std::move(input))
: nullptr;
}
} // namespace diagnostics_service_converters
} // namespace chromeos } // namespace chromeos
#endif // CHROMEOS_COMPONENTS_TELEMETRY_EXTENSION_UI_DIAGNOSTICS_SERVICE_CONVERTERS_H_ #endif // CHROMEOS_COMPONENTS_TELEMETRY_EXTENSION_UI_DIAGNOSTICS_SERVICE_CONVERTERS_H_
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
namespace chromeos { namespace chromeos {
namespace diagnostics_service_converters { namespace converters {
TEST(DiagnosticsServiceConvertersTest, ConvertDiagnosticRoutineStatusEnum) { TEST(DiagnosticsServiceConvertersTest, ConvertDiagnosticRoutineStatusEnum) {
namespace cros_healthd = ::chromeos::cros_healthd::mojom; namespace cros_healthd = ::chromeos::cros_healthd::mojom;
...@@ -66,5 +66,5 @@ TEST(DiagnosticsServiceConvertersTest, ConvertDiagnosticRoutineCommandEnum) { ...@@ -66,5 +66,5 @@ TEST(DiagnosticsServiceConvertersTest, ConvertDiagnosticRoutineCommandEnum) {
cros_healthd::DiagnosticRoutineCommandEnum::kRemove); cros_healthd::DiagnosticRoutineCommandEnum::kRemove);
} }
} // namespace diagnostics_service_converters } // namespace converters
} // namespace chromeos } // namespace chromeos
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <utility> #include <utility>
#include "base/bind.h" #include "base/bind.h"
#include "chromeos/components/telemetry_extension_ui/convert_ptr.h"
#include "chromeos/components/telemetry_extension_ui/probe_service_converters.h" #include "chromeos/components/telemetry_extension_ui/probe_service_converters.h"
#include "chromeos/services/cros_healthd/public/cpp/service_connection.h" #include "chromeos/services/cros_healthd/public/cpp/service_connection.h"
#include "chromeos/services/cros_healthd/public/mojom/cros_healthd_probe.mojom.h" #include "chromeos/services/cros_healthd/public/mojom/cros_healthd_probe.mojom.h"
...@@ -23,12 +24,11 @@ void ProbeService::ProbeTelemetryInfo( ...@@ -23,12 +24,11 @@ void ProbeService::ProbeTelemetryInfo(
const std::vector<health::mojom::ProbeCategoryEnum>& categories, const std::vector<health::mojom::ProbeCategoryEnum>& categories,
ProbeTelemetryInfoCallback callback) { ProbeTelemetryInfoCallback callback) {
GetService()->ProbeTelemetryInfo( GetService()->ProbeTelemetryInfo(
probe_service_converters::ConvertCategoryVector(categories), converters::ConvertCategoryVector(categories),
base::BindOnce( base::BindOnce(
[](health::mojom::ProbeService::ProbeTelemetryInfoCallback callback, [](health::mojom::ProbeService::ProbeTelemetryInfoCallback callback,
cros_healthd::mojom::TelemetryInfoPtr ptr) { cros_healthd::mojom::TelemetryInfoPtr ptr) {
std::move(callback).Run( std::move(callback).Run(converters::ConvertPtr(std::move(ptr)));
probe_service_converters::ConvertPtr(std::move(ptr)));
}, },
std::move(callback))); std::move(callback)));
} }
......
...@@ -9,11 +9,12 @@ ...@@ -9,11 +9,12 @@
#include "base/notreached.h" #include "base/notreached.h"
#include "base/strings/string_number_conversions.h" #include "base/strings/string_number_conversions.h"
#include "chromeos/components/telemetry_extension_ui/convert_ptr.h"
#include "chromeos/components/telemetry_extension_ui/mojom/probe_service.mojom.h" #include "chromeos/components/telemetry_extension_ui/mojom/probe_service.mojom.h"
#include "chromeos/services/cros_healthd/public/mojom/cros_healthd_probe.mojom.h" #include "chromeos/services/cros_healthd/public/mojom/cros_healthd_probe.mojom.h"
namespace chromeos { namespace chromeos {
namespace probe_service_converters { namespace converters {
namespace { namespace {
...@@ -386,5 +387,5 @@ std::vector<cros_healthd::mojom::ProbeCategoryEnum> ConvertCategoryVector( ...@@ -386,5 +387,5 @@ std::vector<cros_healthd::mojom::ProbeCategoryEnum> ConvertCategoryVector(
return output; return output;
} }
} // namespace probe_service_converters } // namespace converters
} // namespace chromeos } // namespace chromeos
...@@ -14,10 +14,10 @@ ...@@ -14,10 +14,10 @@
#include "base/check.h" #include "base/check.h"
#include "chromeos/components/telemetry_extension_ui/mojom/probe_service.mojom-forward.h" #include "chromeos/components/telemetry_extension_ui/mojom/probe_service.mojom-forward.h"
#include "chromeos/services/cros_healthd/public/mojom/cros_healthd_probe.mojom-forward.h" #include "chromeos/services/cros_healthd/public/mojom/cros_healthd_probe.mojom.h"
namespace chromeos { namespace chromeos {
namespace probe_service_converters { namespace converters {
// This file contains helper functions used by ProbeService to convert its // This file contains helper functions used by ProbeService to convert its
// types to/from cros_healthd ProbeService types. // types to/from cros_healthd ProbeService types.
...@@ -127,12 +127,6 @@ health::mojom::UInt32ValuePtr Convert(uint32_t input); ...@@ -127,12 +127,6 @@ health::mojom::UInt32ValuePtr Convert(uint32_t input);
health::mojom::UInt64ValuePtr Convert(uint64_t input); health::mojom::UInt64ValuePtr Convert(uint64_t input);
template <class InputT>
auto ConvertPtr(InputT input) {
return (!input.is_null()) ? unchecked::UncheckedConvertPtr(std::move(input))
: nullptr;
}
template <class OutputT, class InputT> template <class OutputT, class InputT>
std::vector<OutputT> ConvertPtrVector(std::vector<InputT> input) { std::vector<OutputT> ConvertPtrVector(std::vector<InputT> input) {
std::vector<OutputT> output; std::vector<OutputT> output;
...@@ -146,7 +140,7 @@ std::vector<OutputT> ConvertPtrVector(std::vector<InputT> input) { ...@@ -146,7 +140,7 @@ std::vector<OutputT> ConvertPtrVector(std::vector<InputT> input) {
std::vector<cros_healthd::mojom::ProbeCategoryEnum> ConvertCategoryVector( std::vector<cros_healthd::mojom::ProbeCategoryEnum> ConvertCategoryVector(
const std::vector<health::mojom::ProbeCategoryEnum>& input); const std::vector<health::mojom::ProbeCategoryEnum>& input);
} // namespace probe_service_converters } // namespace converters
} // namespace chromeos } // namespace chromeos
#endif // CHROMEOS_COMPONENTS_TELEMETRY_EXTENSION_UI_PROBE_SERVICE_CONVERTERS_H_ #endif // CHROMEOS_COMPONENTS_TELEMETRY_EXTENSION_UI_PROBE_SERVICE_CONVERTERS_H_
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <cstdint> #include <cstdint>
#include <vector> #include <vector>
#include "chromeos/components/telemetry_extension_ui/convert_ptr.h"
#include "chromeos/components/telemetry_extension_ui/mojom/probe_service.mojom.h" #include "chromeos/components/telemetry_extension_ui/mojom/probe_service.mojom.h"
#include "chromeos/services/cros_healthd/public/mojom/cros_healthd_probe.mojom.h" #include "chromeos/services/cros_healthd/public/mojom/cros_healthd_probe.mojom.h"
#include "testing/gmock/include/gmock/gmock.h" #include "testing/gmock/include/gmock/gmock.h"
...@@ -15,7 +16,7 @@ ...@@ -15,7 +16,7 @@
using testing::ElementsAre; using testing::ElementsAre;
namespace chromeos { namespace chromeos {
namespace probe_service_converters { namespace converters {
// Note: in some tests we intentionally use New() with no arguments for // Note: in some tests we intentionally use New() with no arguments for
// cros_healthd::mojom types, because there can be some fields that we don't // cros_healthd::mojom types, because there can be some fields that we don't
...@@ -50,12 +51,6 @@ TEST(ProbeServiceConvertors, ConvertCategoryVector) { ...@@ -50,12 +51,6 @@ TEST(ProbeServiceConvertors, ConvertCategoryVector) {
cros_healthd::mojom::ProbeCategoryEnum::kBluetooth)); cros_healthd::mojom::ProbeCategoryEnum::kBluetooth));
} }
// Tests that |ConvertPtr| function returns nullptr if input is nullptr.
// ConvertPtr is a template, so we can test this function with any valid type.
TEST(ProbeServiceConvertors, ConvertPtrTakesNullPtr) {
EXPECT_TRUE(ConvertPtr(cros_healthd::mojom::ProbeErrorPtr()).is_null());
}
TEST(ProbeServiceConvertors, ErrorType) { TEST(ProbeServiceConvertors, ErrorType) {
EXPECT_EQ(Convert(cros_healthd::mojom::ErrorType::kFileReadError), EXPECT_EQ(Convert(cros_healthd::mojom::ErrorType::kFileReadError),
health::mojom::ErrorType::kFileReadError); health::mojom::ErrorType::kFileReadError);
...@@ -698,5 +693,5 @@ TEST(ProbeServiceConvertors, TelemetryInfoPtrWithNullFields) { ...@@ -698,5 +693,5 @@ TEST(ProbeServiceConvertors, TelemetryInfoPtrWithNullFields) {
health::mojom::BluetoothResultPtr(nullptr))); health::mojom::BluetoothResultPtr(nullptr)));
} }
} // namespace probe_service_converters } // namespace converters
} // namespace chromeos } // namespace chromeos
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment