Commit 8bcacdd7 authored by Omar Morsi's avatar Omar Morsi Committed by Commit Bot

Add enterprise.deviceAttributes.getDeviceHostname

Extend the enterprise.deviceAttributes API to provide the device
hostname. Device hostname can be set by administrators via
DeviceHostnameTemplate policy.

Bug: 1043612
Test: browser_tests --gtest_filter=*EnterpriseDeviceAttributes*
Change-Id: Ib9c40e1fcd5c95e6a76d8fb5501e4f8d8b559f61
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2003855
Commit-Queue: Omar Morsi <omorsi@google.com>
Reviewed-by: default avatarDevlin <rdevlin.cronin@chromium.org>
Reviewed-by: default avatarMaksim Ivanov <emaxx@chromium.org>
Reviewed-by: default avatarPavol Marko <pmarko@chromium.org>
Cr-Commit-Position: refs/heads/master@{#745009}
parent 782ef07a
......@@ -176,6 +176,10 @@ class BrowserPolicyConnectorChromeOS
return system_proxy_settings_policy_handler_.get();
}
HostnameHandler* GetHostnameHandler() const {
return hostname_handler_.get();
}
// Returns device's market segment.
MarketSegment GetEnterpriseMarketSegment() const;
......
......@@ -70,6 +70,10 @@ void HostnameHandler::Shutdown() {
}
}
const std::string& HostnameHandler::GetDeviceHostname() const {
return hostname_;
}
// static
std::string HostnameHandler::FormatHostname(const std::string& name_template,
const std::string& asset_id,
......@@ -147,8 +151,9 @@ void HostnameHandler::
}
}
handler->SetHostname(FormatHostname(hostname_template, asset_id, serial, mac,
machine_name, location));
hostname_ = FormatHostname(hostname_template, asset_id, serial, mac,
machine_name, location);
handler->SetHostname(hostname_);
}
} // namespace policy
......@@ -6,6 +6,7 @@
#define CHROME_BROWSER_CHROMEOS_POLICY_HOSTNAME_HANDLER_H_
#include <memory>
#include <string>
#include "base/macros.h"
#include "base/memory/ref_counted.h"
......@@ -28,6 +29,10 @@ class HostnameHandler : public chromeos::NetworkStateHandlerObserver {
void Shutdown();
// Returns the device hostname that HostnameHandler has last set in shill.
// This is the hostname after formatting (by FormatHostname()).
const std::string& GetDeviceHostname() const;
private:
friend class HostnameHandlerTest;
......@@ -48,6 +53,7 @@ class HostnameHandler : public chromeos::NetworkStateHandlerObserver {
chromeos::CrosSettings* cros_settings_;
std::unique_ptr<chromeos::CrosSettings::ObserverSubscription>
policy_subscription_;
std::string hostname_;
base::WeakPtrFactory<HostnameHandler> weak_factory_{this};
DISALLOW_COPY_AND_ASSIGN(HostnameHandler);
......
......@@ -9,6 +9,7 @@
#include "chrome/browser/browser_process.h"
#include "chrome/browser/browser_process_platform_part.h"
#include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
#include "chrome/browser/chromeos/policy/hostname_handler.h"
#include "chrome/browser/chromeos/profiles/profile_helper.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/extensions/api/enterprise_device_attributes.h"
......@@ -20,6 +21,9 @@ namespace extensions {
namespace {
// TODO(http://crbug.com/1056550): Return an error if the user is not permitted
// to get device attributes instead of an empty string.
// Checks for the current browser context if the user is affiliated or belongs
// to the sign-in profile.
bool IsPermittedToGetDeviceAttributes(content::BrowserContext* context) {
......@@ -110,4 +114,24 @@ EnterpriseDeviceAttributesGetDeviceAnnotatedLocationFunction::Run() {
Create(annotated_location)));
}
EnterpriseDeviceAttributesGetDeviceHostnameFunction::
EnterpriseDeviceAttributesGetDeviceHostnameFunction() = default;
EnterpriseDeviceAttributesGetDeviceHostnameFunction::
~EnterpriseDeviceAttributesGetDeviceHostnameFunction() = default;
ExtensionFunction::ResponseAction
EnterpriseDeviceAttributesGetDeviceHostnameFunction::Run() {
std::string hostname;
if (IsPermittedToGetDeviceAttributes(browser_context())) {
hostname = g_browser_process->platform_part()
->browser_policy_connector_chromeos()
->GetHostnameHandler()
->GetDeviceHostname();
}
return RespondNow(ArgumentList(
api::enterprise_device_attributes::GetDeviceHostname::Results::Create(
hostname)));
}
} // namespace extensions
......@@ -71,5 +71,20 @@ class EnterpriseDeviceAttributesGetDeviceAnnotatedLocationFunction
ENTERPRISE_DEVICEATTRIBUTES_GETDEVICEANNOTATEDLOCATION)
};
class EnterpriseDeviceAttributesGetDeviceHostnameFunction
: public ExtensionFunction {
public:
EnterpriseDeviceAttributesGetDeviceHostnameFunction();
protected:
~EnterpriseDeviceAttributesGetDeviceHostnameFunction() override;
ResponseAction Run() override;
private:
DECLARE_EXTENSION_FUNCTION("enterprise.deviceAttributes.getDeviceHostname",
ENTERPRISE_DEVICEATTRIBUTES_GETDEVICEHOSTNAME)
};
} // namespace extensions
#endif // CHROME_BROWSER_EXTENSIONS_API_ENTERPRISE_DEVICE_ATTRIBUTES_ENTERPRISE_DEVICE_ATTRIBUTES_API_H_
......@@ -40,6 +40,7 @@ constexpr char kDeviceId[] = "device_id";
constexpr char kSerialNumber[] = "serial_number";
constexpr char kAssetId[] = "asset_id";
constexpr char kAnnotatedLocation[] = "annotated_location";
constexpr char kHostname[] = "hostname";
constexpr char kUpdateManifestPath[] =
"/extensions/api_test/enterprise_device_attributes/update_manifest.xml";
......@@ -68,7 +69,8 @@ std::string PrintParam(testing::TestParamInfo<Params> param_info) {
base::Value BuildCustomArg(const std::string& expected_directory_device_id,
const std::string& expected_serial_number,
const std::string& expected_asset_id,
const std::string& expected_annotated_location) {
const std::string& expected_annotated_location,
const std::string& expected_hostname) {
base::Value custom_arg(base::Value::Type::DICTIONARY);
custom_arg.SetKey("expectedDirectoryDeviceId",
base::Value(expected_directory_device_id));
......@@ -77,6 +79,7 @@ base::Value BuildCustomArg(const std::string& expected_directory_device_id,
custom_arg.SetKey("expectedAssetId", base::Value(expected_asset_id));
custom_arg.SetKey("expectedAnnotatedLocation",
base::Value(expected_annotated_location));
custom_arg.SetKey("expectedHostname", base::Value(expected_hostname));
return custom_arg;
}
......@@ -134,6 +137,9 @@ class EnterpriseDeviceAttributesTest
device_policy->policy_data().set_directory_api_id(kDeviceId);
device_policy->policy_data().set_annotated_asset_id(kAssetId);
device_policy->policy_data().set_annotated_location(kAnnotatedLocation);
enterprise_management::NetworkHostnameProto* proto =
device_policy->payload().mutable_network_hostname();
proto->set_device_hostname_template(kHostname);
device_policy->Build();
chromeos::FakeSessionManagerClient::Get()->set_device_policy(
......@@ -226,13 +232,15 @@ IN_PROC_BROWSER_TEST_P(EnterpriseDeviceAttributesTest, Success) {
std::string expected_asset_id = GetParam().affiliated ? kAssetId : "";
std::string expected_annotated_location =
GetParam().affiliated ? kAnnotatedLocation : "";
std::string expected_hostname = GetParam().affiliated ? kHostname : "";
// Pass the expected value (device_id) to test.
ASSERT_TRUE(TestExtension(
CreateBrowser(profile()),
base::StringPrintf("chrome-extension://%s/basic.html", kTestExtensionID),
BuildCustomArg(expected_directory_device_id, expected_serial_number,
expected_asset_id, expected_annotated_location)))
expected_asset_id, expected_annotated_location,
expected_hostname)))
<< message_;
}
......
......@@ -14,6 +14,8 @@ namespace enterprise.deviceAttributes {
callback GetDeviceAnnotatedLocationCallback = void (DOMString annotatedLocation);
callback GetDeviceHostnameCallback = void (DOMString hostname);
interface Functions {
// Fetches the value of
// <a href="https://developers.google.com/admin-sdk/directory/v1/guides/manage-chrome-devices">the device identifier of the directory API</a>,
......@@ -43,6 +45,12 @@ namespace enterprise.deviceAttributes {
// set by the administrator, returns an empty string.
// |callback| : Called with the Annotated Location of the device.
void getDeviceAnnotatedLocation(GetDeviceAnnotatedLocationCallback callback);
// Fetches the device's hostname as set by DeviceHostnameTemplate policy.
// If the current user is not affiliated or no hostname has been set by the
// the enterprise policy, returns an empty string.
// |callback| : Called with hostname of the device.
void getDeviceHostname(GetDeviceHostnameCallback callback);
};
};
......@@ -11,6 +11,7 @@ chrome.test.getConfig(function(config) {
var expectedSerialNumber = customArg.expectedSerialNumber;
var expectedAssetId = customArg.expectedAssetId;
var expectedAnnotatedLocation = customArg.expectedAnnotatedLocation;
var expectedHostname = customArg.expectedHostname;
chrome.test.runTests([
function testDirectoryDeviceId() {
......@@ -40,6 +41,12 @@ chrome.test.getConfig(function(config) {
chrome.test.assertEq(expectedAnnotatedLocation, annotatedLocation);
chrome.test.succeed();
});
},
function testDeviceHostname() {
chrome.enterprise.deviceAttributes.getDeviceHostname(function(hostname) {
chrome.test.assertEq(expectedHostname, hostname);
chrome.test.succeed();
});
}
]);
});
......@@ -1507,6 +1507,7 @@ enum HistogramValue {
AUTOTESTPRIVATE_PINSHELFICON = 1444,
AUTOTESTPRIVATE_WAITFOROVERVIEWSTATE = 1445,
AUTOTESTPRIVATE_GETSCROLLABLESHELFINFOFORSTATE = 1446,
ENTERPRISE_DEVICEATTRIBUTES_GETDEVICEHOSTNAME = 1447,
// Last entry: Add new entries above, then run:
// python tools/metrics/histograms/update_extension_histograms.py
ENUM_BOUNDARY
......
......@@ -22343,6 +22343,7 @@ to ensure that the crash string is shown properly on the user-facing crash UI.
<int value="1444" label="AUTOTESTPRIVATE_PINSHELFICON"/>
<int value="1445" label="AUTOTESTPRIVATE_WAITFOROVERVIEWSTATE"/>
<int value="1446" label="AUTOTESTPRIVATE_GETSCROLLABLESHELFINFOFORSTATE"/>
<int value="1447" label="ENTERPRISE_DEVICEATTRIBUTES_GETDEVICEHOSTNAME"/>
</enum>
<enum name="ExtensionIconState">
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