1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/device_api/device_attribute_api.h"
#include "base/functional/callback.h"
#include "build/build_config.h"
#if BUILDFLAG(IS_CHROMEOS)
#include <optional>
#include <string_view>
#include "chrome/browser/ash/policy/core/browser_policy_connector_ash.h"
#include "chrome/browser/ash/policy/handlers/device_name_policy_handler.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/browser_process_platform_part.h"
#include "chromeos/ash/components/system/statistics_provider.h"
#endif
using blink::mojom::DeviceAPIService;
using blink::mojom::DeviceAttributeResultPtr;
namespace {
using Result = blink::mojom::DeviceAttributeResult;
constexpr char kNotAffiliatedErrorMessage[] =
"This web API is not allowed if the current profile is not affiliated.";
constexpr char kNotAllowedOriginErrorMessage[] =
"The current origin cannot use this web API because it is not allowed by "
"the DeviceAttributesAllowedForOrigins policy.";
#if !BUILDFLAG(IS_CHROMEOS)
const char kNotSupportedPlatformErrorMessage[] =
"This web API is not supported on the current platform.";
#endif
} // namespace
DeviceAttributeApiImpl::DeviceAttributeApiImpl() = default;
DeviceAttributeApiImpl::~DeviceAttributeApiImpl() = default;
void DeviceAttributeApiImpl::ReportNotAffiliatedError(
base::OnceCallback<void(DeviceAttributeResultPtr)> callback) {
std::move(callback).Run(Result::NewErrorMessage(kNotAffiliatedErrorMessage));
}
void DeviceAttributeApiImpl::ReportNotAllowedError(
base::OnceCallback<void(DeviceAttributeResultPtr)> callback) {
std::move(callback).Run(
Result::NewErrorMessage(kNotAllowedOriginErrorMessage));
}
void DeviceAttributeApiImpl::GetDirectoryId(
DeviceAPIService::GetDirectoryIdCallback callback) {
#if BUILDFLAG(IS_CHROMEOS)
const std::string attribute = g_browser_process->platform_part()
->browser_policy_connector_ash()
->GetDirectoryApiID();
if (attribute.empty()) {
std::move(callback).Run(Result::NewAttribute(std::optional<std::string>()));
} else {
std::move(callback).Run(Result::NewAttribute(attribute));
}
#else // Other platforms
std::move(callback).Run(
Result::NewErrorMessage(kNotSupportedPlatformErrorMessage));
#endif
}
void DeviceAttributeApiImpl::GetHostname(
DeviceAPIService::GetHostnameCallback callback) {
#if BUILDFLAG(IS_CHROMEOS)
const std::optional<std::string> attribute =
g_browser_process->platform_part()
->browser_policy_connector_ash()
->GetDeviceNamePolicyHandler()
->GetHostnameChosenByAdministrator();
std::move(callback).Run(Result::NewAttribute(attribute));
#else // Other platforms
std::move(callback).Run(
Result::NewErrorMessage(kNotSupportedPlatformErrorMessage));
#endif
}
void DeviceAttributeApiImpl::GetSerialNumber(
DeviceAPIService::GetSerialNumberCallback callback) {
#if BUILDFLAG(IS_CHROMEOS)
const std::optional<std::string_view> attribute =
ash::system::StatisticsProvider::GetInstance()->GetMachineID();
std::move(callback).Run(Result::NewAttribute(
attribute ? std::optional<std::string>(attribute.value())
: std::nullopt));
#else // Other platforms
std::move(callback).Run(
Result::NewErrorMessage(kNotSupportedPlatformErrorMessage));
#endif
}
void DeviceAttributeApiImpl::GetAnnotatedAssetId(
DeviceAPIService::GetAnnotatedAssetIdCallback callback) {
#if BUILDFLAG(IS_CHROMEOS)
const std::string attribute = g_browser_process->platform_part()
->browser_policy_connector_ash()
->GetDeviceAssetID();
if (attribute.empty()) {
std::move(callback).Run(Result::NewAttribute(std::optional<std::string>()));
} else {
std::move(callback).Run(Result::NewAttribute(attribute));
}
#else // Other platforms
std::move(callback).Run(
Result::NewErrorMessage(kNotSupportedPlatformErrorMessage));
#endif
}
void DeviceAttributeApiImpl::GetAnnotatedLocation(
DeviceAPIService::GetAnnotatedLocationCallback callback) {
#if BUILDFLAG(IS_CHROMEOS)
const std::string attribute = g_browser_process->platform_part()
->browser_policy_connector_ash()
->GetDeviceAnnotatedLocation();
if (attribute.empty()) {
std::move(callback).Run(Result::NewAttribute(std::optional<std::string>()));
} else {
std::move(callback).Run(Result::NewAttribute(attribute));
}
#else // Other platforms
std::move(callback).Run(
Result::NewErrorMessage(kNotSupportedPlatformErrorMessage));
#endif
}
|