From bb5d6c7b33eb5b40a83623f26462d9121c134aee Mon Sep 17 00:00:00 2001 From: Guido Urdaneta Date: Thu, 6 Sep 2018 09:45:50 +0000 Subject: [PATCH] Implement Hardware Platform API Authorization via policy settings will be added in a follow-up CL before enabling by default. TBR=guidou@chromium.org (cherry picked from commit 433c5217f47a20596078a1002bac15e001784fd1) Bug: 860311 Change-Id: Ifadaa08b1a312f750654ebe51b862a8733b998a5 Reviewed-on: https://chromium-review.googlesource.com/1183195 Commit-Queue: Guido Urdaneta Reviewed-by: Devlin Cr-Original-Commit-Position: refs/heads/master@{#588245} Reviewed-on: https://chromium-review.googlesource.com/1199405 Reviewed-by: Guido Urdaneta Cr-Commit-Position: refs/branch-heads/3538@{#83} Cr-Branched-From: 79f7c91a2b2a2932cd447fa6f865cb6662fa8fa6-refs/heads/master@{#587811} --- chrome/app/generated_resources.grd | 3 + chrome/browser/extensions/BUILD.gn | 2 + .../api/enterprise_hardware_platform/OWNERS | 1 + .../enterprise_hardware_platform_api.cc | 38 +++++++++ .../enterprise_hardware_platform_api.h | 37 ++++++++ ...terprise_hardware_platform_api_unittest.cc | 84 +++++++++++++++++++ .../common/extensions/api/_api_features.json | 4 + .../extensions/api/_permission_features.json | 5 ++ chrome/common/extensions/api/api_sources.gni | 1 + .../api/enterprise_hardware_platform.idl | 23 +++++ .../permissions/chrome_api_permissions.cc | 2 + .../chrome_permission_message_rules.cc | 3 + chrome/test/BUILD.gn | 1 + .../extension_function_histogram_value.h | 1 + .../common/permissions/api_permission.h | 3 +- tools/metrics/histograms/enums.xml | 3 + 16 files changed, 210 insertions(+), 1 deletion(-) create mode 100644 chrome/browser/extensions/api/enterprise_hardware_platform/OWNERS create mode 100644 chrome/browser/extensions/api/enterprise_hardware_platform/enterprise_hardware_platform_api.cc create mode 100644 chrome/browser/extensions/api/enterprise_hardware_platform/enterprise_hardware_platform_api.h create mode 100644 chrome/browser/extensions/api/enterprise_hardware_platform/enterprise_hardware_platform_api_unittest.cc create mode 100644 chrome/common/extensions/api/enterprise_hardware_platform.idl diff --git a/chrome/app/generated_resources.grd b/chrome/app/generated_resources.grd index b615103c64c1..cf5e67fa9c69 100644 --- a/chrome/app/generated_resources.grd +++ b/chrome/app/generated_resources.grd @@ -3501,6 +3501,9 @@ are declared in tools/grit/grit_rule.gni. Access document scanners attached via USB or on the local network + + Read the manufacturer and model of this computer + Read the icons of the websites you visit diff --git a/chrome/browser/extensions/BUILD.gn b/chrome/browser/extensions/BUILD.gn index 614f27924e77..f25d5aafd709 100644 --- a/chrome/browser/extensions/BUILD.gn +++ b/chrome/browser/extensions/BUILD.gn @@ -160,6 +160,8 @@ jumbo_static_library("extensions") { "api/downloads/downloads_api.h", "api/downloads_internal/downloads_internal_api.cc", "api/downloads_internal/downloads_internal_api.h", + "api/enterprise_hardware_platform/enterprise_hardware_platform_api.cc", + "api/enterprise_hardware_platform/enterprise_hardware_platform_api.h", "api/extension_action/extension_action_api.cc", "api/extension_action/extension_action_api.h", "api/extension_action/extension_page_actions_api_constants.cc", diff --git a/chrome/browser/extensions/api/enterprise_hardware_platform/OWNERS b/chrome/browser/extensions/api/enterprise_hardware_platform/OWNERS new file mode 100644 index 000000000000..05cb9bd3c026 --- /dev/null +++ b/chrome/browser/extensions/api/enterprise_hardware_platform/OWNERS @@ -0,0 +1 @@ +guidou@chromium.org diff --git a/chrome/browser/extensions/api/enterprise_hardware_platform/enterprise_hardware_platform_api.cc b/chrome/browser/extensions/api/enterprise_hardware_platform/enterprise_hardware_platform_api.cc new file mode 100644 index 000000000000..3a75d928dd48 --- /dev/null +++ b/chrome/browser/extensions/api/enterprise_hardware_platform/enterprise_hardware_platform_api.cc @@ -0,0 +1,38 @@ +// Copyright 2018 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/extensions/api/enterprise_hardware_platform/enterprise_hardware_platform_api.h" + +#include + +#include "base/bind.h" +#include "chrome/common/extensions/api/enterprise_hardware_platform.h" + +namespace extensions { + +EnterpriseHardwarePlatformGetHardwarePlatformInfoFunction:: + EnterpriseHardwarePlatformGetHardwarePlatformInfoFunction() = default; + +EnterpriseHardwarePlatformGetHardwarePlatformInfoFunction:: + ~EnterpriseHardwarePlatformGetHardwarePlatformInfoFunction() = default; + +ExtensionFunction::ResponseAction +EnterpriseHardwarePlatformGetHardwarePlatformInfoFunction::Run() { + base::SysInfo::GetHardwareInfo(base::BindOnce( + &EnterpriseHardwarePlatformGetHardwarePlatformInfoFunction:: + OnHardwarePlatformInfo, + this)); + return RespondLater(); +} + +void EnterpriseHardwarePlatformGetHardwarePlatformInfoFunction:: + OnHardwarePlatformInfo(base::SysInfo::HardwareInfo info) { + api::enterprise_hardware_platform::HardwarePlatformInfo result; + result.manufacturer = std::move(info.manufacturer); + result.model = std::move(info.model); + Respond(ArgumentList(api::enterprise_hardware_platform:: + GetHardwarePlatformInfo::Results::Create(result))); +} + +} // namespace extensions diff --git a/chrome/browser/extensions/api/enterprise_hardware_platform/enterprise_hardware_platform_api.h b/chrome/browser/extensions/api/enterprise_hardware_platform/enterprise_hardware_platform_api.h new file mode 100644 index 000000000000..70726063ac15 --- /dev/null +++ b/chrome/browser/extensions/api/enterprise_hardware_platform/enterprise_hardware_platform_api.h @@ -0,0 +1,37 @@ +// Copyright 2018 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 CHROME_BROWSER_EXTENSIONS_API_ENTERPRISE_HARDWARE_PLATFORM_ENTERPRISE_HARDWARE_PLATFORM_API_H_ +#define CHROME_BROWSER_EXTENSIONS_API_ENTERPRISE_HARDWARE_PLATFORM_ENTERPRISE_HARDWARE_PLATFORM_API_H_ + +#include "base/macros.h" +#include "base/sys_info.h" +#include "extensions/browser/extension_function.h" + +namespace extensions { + +class EnterpriseHardwarePlatformGetHardwarePlatformInfoFunction + : public UIThreadExtensionFunction { + public: + EnterpriseHardwarePlatformGetHardwarePlatformInfoFunction(); + + protected: + ~EnterpriseHardwarePlatformGetHardwarePlatformInfoFunction() override; + + ResponseAction Run() override; + + private: + DECLARE_EXTENSION_FUNCTION( + "enterprise.hardwarePlatform.getHardwarePlatformInfo", + ENTERPRISE_HARDWAREPLATFORM_GETHARDWAREPLATFORMINFO); + + void OnHardwarePlatformInfo(base::SysInfo::HardwareInfo info); + + DISALLOW_COPY_AND_ASSIGN( + EnterpriseHardwarePlatformGetHardwarePlatformInfoFunction); +}; + +} // namespace extensions + +#endif // CHROME_BROWSER_EXTENSIONS_API_ENTERPRISE_HARDWARE_PLATFORM_ENTERPRISE_HARDWARE_PLATFORM_API_H_ diff --git a/chrome/browser/extensions/api/enterprise_hardware_platform/enterprise_hardware_platform_api_unittest.cc b/chrome/browser/extensions/api/enterprise_hardware_platform/enterprise_hardware_platform_api_unittest.cc new file mode 100644 index 000000000000..285c63683a11 --- /dev/null +++ b/chrome/browser/extensions/api/enterprise_hardware_platform/enterprise_hardware_platform_api_unittest.cc @@ -0,0 +1,84 @@ +// Copyright 2018 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/extensions/api/enterprise_hardware_platform/enterprise_hardware_platform_api.h" + +#include +#include + +#include "base/json/json_writer.h" +#include "chrome/browser/extensions/extension_api_unittest.h" +#include "chrome/browser/extensions/extension_function_test_utils.h" +#include "chrome/browser/extensions/extension_service.h" +#include "chrome/browser/extensions/extension_service_test_with_install.h" +#include "components/crx_file/id_util.h" +#include "extensions/common/extension_builder.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace extensions { + +class EnterpriseHardwarePlatformAPITest + : public ExtensionServiceTestWithInstall { + public: + EnterpriseHardwarePlatformAPITest() = default; + ~EnterpriseHardwarePlatformAPITest() override = default; + Browser* browser() { return browser_.get(); } + + private: + void SetUp() override { + ExtensionServiceTestWithInstall::SetUp(); + InitializeEmptyExtensionService(); + browser_window_ = std::make_unique(); + Browser::CreateParams params(profile(), true); + params.type = Browser::TYPE_TABBED; + params.window = browser_window_.get(); + browser_ = std::make_unique(params); + } + + void TearDown() override { + browser_.reset(); + browser_window_.reset(); + ExtensionServiceTestWithInstall::TearDown(); + } + + std::unique_ptr browser_window_; + std::unique_ptr browser_; + + DISALLOW_COPY_AND_ASSIGN(EnterpriseHardwarePlatformAPITest); +}; + +TEST_F(EnterpriseHardwarePlatformAPITest, GetHardwarePlatformInfo) { + scoped_refptr extension = ExtensionBuilder("Test").Build(); + scoped_refptr + function = + new EnterpriseHardwarePlatformGetHardwarePlatformInfoFunction(); + function->set_extension(extension.get()); + function->set_has_callback(true); + + std::string args; + base::JSONWriter::Write(base::ListValue(), &args); + + std::unique_ptr result( + extension_function_test_utils::RunFunctionAndReturnSingleResult( + function.get(), args, browser())); + base::RunLoop().RunUntilIdle(); + + ASSERT_TRUE(result); + ASSERT_TRUE(result->is_dict()); + ASSERT_EQ(result->DictSize(), 2u); + + const base::Value* val = + result->FindKeyOfType("manufacturer", base::Value::Type::STRING); + ASSERT_TRUE(val); + const std::string& manufacturer = val->GetString(); + + val = result->FindKeyOfType("model", base::Value::Type::STRING); + ASSERT_TRUE(val); + const std::string& model = val->GetString(); + + EXPECT_FALSE(manufacturer.empty()); + EXPECT_FALSE(model.empty()); +} + +} // namespace extensions diff --git a/chrome/common/extensions/api/_api_features.json b/chrome/common/extensions/api/_api_features.json index 931689042da5..c23d625b063d 100644 --- a/chrome/common/extensions/api/_api_features.json +++ b/chrome/common/extensions/api/_api_features.json @@ -372,6 +372,10 @@ "dependencies": ["permission:echoPrivate"], "contexts": ["blessed_extension"] }, + "enterprise.hardwarePlatform": { + "dependencies": ["permission:enterprise.hardwarePlatform"], + "contexts": ["blessed_extension"] + }, "enterprise.deviceAttributes": { "dependencies": ["permission:enterprise.deviceAttributes"], "contexts": ["blessed_extension"] diff --git a/chrome/common/extensions/api/_permission_features.json b/chrome/common/extensions/api/_permission_features.json index 8da1ccc15876..345e58da0221 100644 --- a/chrome/common/extensions/api/_permission_features.json +++ b/chrome/common/extensions/api/_permission_features.json @@ -292,6 +292,11 @@ "extension_types": ["extension", "platform_app"], "location": "policy" }, + "enterprise.hardwarePlatform": { + "channel": "canary", + "extension_types": ["extension"], + "location": "policy" + }, "enterprise.platformKeys": [{ "channel": "stable", "platforms": ["chromeos"], diff --git a/chrome/common/extensions/api/api_sources.gni b/chrome/common/extensions/api/api_sources.gni index ea34622b0a6f..a26827fa30d6 100644 --- a/chrome/common/extensions/api/api_sources.gni +++ b/chrome/common/extensions/api/api_sources.gni @@ -37,6 +37,7 @@ schema_sources_ = [ "developer_private.idl", "downloads.idl", "downloads_internal.idl", + "enterprise_hardware_platform.idl", "font_settings.json", "gcm.json", "history.json", diff --git a/chrome/common/extensions/api/enterprise_hardware_platform.idl b/chrome/common/extensions/api/enterprise_hardware_platform.idl new file mode 100644 index 000000000000..9c5504c7254b --- /dev/null +++ b/chrome/common/extensions/api/enterprise_hardware_platform.idl @@ -0,0 +1,23 @@ +// Copyright 2018 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. + +// Use the chrome.enterprise.hardwarePlatform API to get the +// manufacturer and model of the hardware platform where the browser runs. +// Note: This API is only available to extensions installed by enterprise +// policy. +namespace enterprise.hardwarePlatform { + dictionary HardwarePlatformInfo { + DOMString model; + DOMString manufacturer; + }; + + callback HardwarePlatformInfoCallback = void(HardwarePlatformInfo info); + + interface Functions { + // Obtains the manufacturer and model for the hardware platform and, if + // the extension is authorized, returns it via |callback|. + // |callback|: Called with the hardware platform info. + static void getHardwarePlatformInfo(HardwarePlatformInfoCallback callback); + }; +}; diff --git a/chrome/common/extensions/permissions/chrome_api_permissions.cc b/chrome/common/extensions/permissions/chrome_api_permissions.cc index 83af3c749cfb..85f9ea1a6f05 100644 --- a/chrome/common/extensions/permissions/chrome_api_permissions.cc +++ b/chrome/common/extensions/permissions/chrome_api_permissions.cc @@ -74,6 +74,8 @@ ChromeAPIPermissions::GetAllPermissions() const { APIPermissionInfo::kFlagCannotBeOptional}, {APIPermission::kEnterpriseDeviceAttributes, "enterprise.deviceAttributes"}, + {APIPermission::kEnterpriseHardwarePlatform, + "enterprise.hardwarePlatform"}, {APIPermission::kEnterprisePlatformKeys, "enterprise.platformKeys"}, {APIPermission::kFileBrowserHandler, "fileBrowserHandler", APIPermissionInfo::kFlagCannotBeOptional}, diff --git a/chrome/common/extensions/permissions/chrome_permission_message_rules.cc b/chrome/common/extensions/permissions/chrome_permission_message_rules.cc index 96302805ee71..9f3471a24cd5 100644 --- a/chrome/common/extensions/permissions/chrome_permission_message_rules.cc +++ b/chrome/common/extensions/permissions/chrome_permission_message_rules.cc @@ -639,6 +639,9 @@ ChromePermissionMessageRule::GetAllRules() { {IDS_EXTENSION_PROMPT_WARNING_DISPLAY_SOURCE, {APIPermission::kDisplaySource}, {}}, + {IDS_EXTENSION_PROMPT_WARNING_ENTERPRISE_HARDWARE_PLATFORM, + {APIPermission::kEnterpriseHardwarePlatform}, + {}}, }; return std::vector( diff --git a/chrome/test/BUILD.gn b/chrome/test/BUILD.gn index 0e7209082c6c..791a98a297f8 100644 --- a/chrome/test/BUILD.gn +++ b/chrome/test/BUILD.gn @@ -3509,6 +3509,7 @@ test("unit_tests") { "../browser/extensions/api/developer_private/extension_info_generator_unittest.cc", "../browser/extensions/api/device_permissions_manager_unittest.cc", "../browser/extensions/api/downloads/downloads_api_unittest.cc", + "../browser/extensions/api/enterprise_hardware_platform/enterprise_hardware_platform_api_unittest.cc", "../browser/extensions/api/extension_action/browser_action_unittest.cc", "../browser/extensions/api/extension_action/extension_action_prefs_unittest.cc", "../browser/extensions/api/file_system/file_system_api_unittest.cc", diff --git a/extensions/browser/extension_function_histogram_value.h b/extensions/browser/extension_function_histogram_value.h index 34852cc946ab..98079d339060 100644 --- a/extensions/browser/extension_function_histogram_value.h +++ b/extensions/browser/extension_function_histogram_value.h @@ -1329,6 +1329,7 @@ enum HistogramValue { ARCAPPSPRIVATE_LAUNCHAPP = 1266, AUTOTESTPRIVATE_RUNCROSTINIINSTALLER = 1267, AUTOFILLPRIVATE_MIGRATECREDITCARDS = 1268, + ENTERPRISE_HARDWAREPLATFORM_GETHARDWAREPLATFORMINFO = 1271, // Last entry: Add new entries above, then run: // python tools/metrics/histograms/update_extension_histograms.py ENUM_BOUNDARY diff --git a/extensions/common/permissions/api_permission.h b/extensions/common/permissions/api_permission.h index 8281f3f860e2..05e1d7422a4f 100644 --- a/extensions/common/permissions/api_permission.h +++ b/extensions/common/permissions/api_permission.h @@ -197,7 +197,7 @@ class APIPermission { kWallpaper = 153, kWallpaperPrivate = 154, kWebcamPrivate = 155, - kWebConnectable = 156, // for externally_connectable manifest key + kWebConnectable = 156, // for externally_connectable manifest key kWebNavigation = 157, kWebRequest = 158, kWebRequestBlocking = 159, @@ -257,6 +257,7 @@ class APIPermission { kFileSystemRequestDownloads = 213, kSystemPowerSource = 214, kArcAppsPrivate = 215, + kEnterpriseHardwarePlatform = 216, // Last entry: Add new entries above and ensure to update the // "ExtensionPermission3" enum in tools/metrics/histograms/histograms.xml // (by running update_extension_permission.py). diff --git a/tools/metrics/histograms/enums.xml b/tools/metrics/histograms/enums.xml index 8850910ab4e9..876f4cf80771 100644 --- a/tools/metrics/histograms/enums.xml +++ b/tools/metrics/histograms/enums.xml @@ -16555,6 +16555,8 @@ Called by update_net_error_codes.py.--> + @@ -17001,6 +17003,7 @@ Called by update_net_error_codes.py.--> +