Skip to content

Commit

Permalink
Implement Hardware Platform API
Browse files Browse the repository at this point in the history
Authorization via policy settings will be added in a follow-up CL before
enabling by default.

[email protected]

(cherry picked from commit 433c521)

Bug: 860311
Change-Id: Ifadaa08b1a312f750654ebe51b862a8733b998a5
Reviewed-on: https://chromium-review.googlesource.com/1183195
Commit-Queue: Guido Urdaneta <[email protected]>
Reviewed-by: Devlin <[email protected]>
Cr-Original-Commit-Position: refs/heads/master@{#588245}
Reviewed-on: https://chromium-review.googlesource.com/1199405
Reviewed-by: Guido Urdaneta <[email protected]>
Cr-Commit-Position: refs/branch-heads/3538@{#83}
Cr-Branched-From: 79f7c91-refs/heads/master@{#587811}
  • Loading branch information
Guido Urdaneta committed Sep 6, 2018
1 parent ec82e30 commit bb5d6c7
Show file tree
Hide file tree
Showing 16 changed files with 210 additions and 1 deletion.
3 changes: 3 additions & 0 deletions chrome/app/generated_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -3501,6 +3501,9 @@ are declared in tools/grit/grit_rule.gni.
<message name="IDS_EXTENSION_PROMPT_WARNING_DOCUMENT_SCAN" desc="Permission string for access to document scanning.">
Access document scanners attached via USB or on the local network
</message>
<message name="IDS_EXTENSION_PROMPT_WARNING_ENTERPRISE_HARDWARE_PLATFORM" desc="Permission string for access to hardware platform information.">
Read the manufacturer and model of this computer
</message>
<message name="IDS_EXTENSION_PROMPT_WARNING_FAVICON" desc="Permission string for access to favicons.">
Read the icons of the websites you visit
</message>
Expand Down
2 changes: 2 additions & 0 deletions chrome/browser/extensions/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[email protected]
Original file line number Diff line number Diff line change
@@ -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 <utility>

#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
Original file line number Diff line number Diff line change
@@ -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_
Original file line number Diff line number Diff line change
@@ -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 <memory>
#include <string>

#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<TestBrowserWindow>();
Browser::CreateParams params(profile(), true);
params.type = Browser::TYPE_TABBED;
params.window = browser_window_.get();
browser_ = std::make_unique<Browser>(params);
}

void TearDown() override {
browser_.reset();
browser_window_.reset();
ExtensionServiceTestWithInstall::TearDown();
}

std::unique_ptr<TestBrowserWindow> browser_window_;
std::unique_ptr<Browser> browser_;

DISALLOW_COPY_AND_ASSIGN(EnterpriseHardwarePlatformAPITest);
};

TEST_F(EnterpriseHardwarePlatformAPITest, GetHardwarePlatformInfo) {
scoped_refptr<const Extension> extension = ExtensionBuilder("Test").Build();
scoped_refptr<EnterpriseHardwarePlatformGetHardwarePlatformInfoFunction>
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<base::Value> 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
4 changes: 4 additions & 0 deletions chrome/common/extensions/api/_api_features.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
5 changes: 5 additions & 0 deletions chrome/common/extensions/api/_permission_features.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
1 change: 1 addition & 0 deletions chrome/common/extensions/api/api_sources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
23 changes: 23 additions & 0 deletions chrome/common/extensions/api/enterprise_hardware_platform.idl
Original file line number Diff line number Diff line change
@@ -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 <code>chrome.enterprise.hardwarePlatform</code> 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);
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ChromePermissionMessageRule>(
Expand Down
1 change: 1 addition & 0 deletions chrome/test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions extensions/browser/extension_function_histogram_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion extensions/common/permissions/api_permission.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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).
Expand Down
3 changes: 3 additions & 0 deletions tools/metrics/histograms/enums.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16555,6 +16555,8 @@ Called by update_net_error_codes.py.-->
<int value="1266" label="ARCAPPSPRIVATE_LAUNCHAPP"/>
<int value="1267" label="AUTOTESTPRIVATE_RUNCROSTINIINSTALLER"/>
<int value="1268" label="AUTOFILLPRIVATE_MIGRATECREDITCARDS"/>
<int value="1271"
label="ENTERPRISE_HARDWAREPLATFORM_GETHARDWAREPLATFORMINFO"/>
</enum>

<enum name="ExtensionIconState">
Expand Down Expand Up @@ -17001,6 +17003,7 @@ Called by update_net_error_codes.py.-->
<int value="213" label="kFileSystemRequestDownloads"/>
<int value="214" label="kSystemPowerSource"/>
<int value="215" label="kArcAppsPrivate"/>
<int value="216" label="kEnterpriseHardwarePlatform"/>
</enum>

<enum name="ExtensionServiceVerifyAllSuccess">
Expand Down

0 comments on commit bb5d6c7

Please sign in to comment.