Commit de775944 authored by Georges Khalil's avatar Georges Khalil Committed by Commit Bot

Remove native host dependency for reporting.

This change adds the necessary reported fields from within Chrome to remove the need for the native host.

BUG=847264

Change-Id: Id7e0489749c7e878c023d698e9f56ea0b2ef4ab9
Reviewed-on: https://chromium-review.googlesource.com/1074498
Commit-Queue: Georges Khalil <georgesak@chromium.org>
Reviewed-by: default avatarMarc-André Decoste <mad@chromium.org>
Reviewed-by: default avatarOwen Min <zmin@chromium.org>
Reviewed-by: default avatarJulian Pastarmov <pastarmovj@chromium.org>
Cr-Commit-Position: refs/heads/master@{#562540}
parent 7f21b029
......@@ -30,10 +30,7 @@ namespace em = enterprise_management;
namespace extensions {
namespace {
// JSON key in extension arguments.
const char kMachineName[] = "machineName";
const char kOSInfo[] = "osInfo";
const char kOSUser[] = "osUser";
// JSON keys in the extension arguments.
const char kBrowserReport[] = "browserReport";
const char kChromeUserProfileReport[] = "chromeUserProfileReport";
const char kChromeSignInUser[] = "chromeSignInUser";
......@@ -43,8 +40,9 @@ const char kSafeBrowsingWarnings[] = "safeBrowsingWarnings";
const char kSafeBrowsingWarningsClickThrough[] =
"safeBrowsingWarningsClickThrough";
// JSON key in the os_info field.
// JSON keys in the os_info field.
const char kOS[] = "os";
const char kOSArch[] = "arch";
const char kOSVersion[] = "os_version";
const char kDefaultDictionary[] = "{}";
......@@ -136,17 +134,23 @@ bool UpdateJSONEncodedStringEntry(const base::Value& dict_value,
return true;
}
bool UpdateOSInfoEntry(const base::Value& report, std::string* os_info_string) {
base::Value writable_os_info(base::Value::Type::DICTIONARY);
if (const base::Value* os_info = report.FindKey(kOSInfo)) {
if (!os_info->is_dict())
return false;
writable_os_info = os_info->Clone();
}
writable_os_info.SetKey(kOS, base::Value(policy::GetOSPlatform()));
writable_os_info.SetKey(kOSVersion, base::Value(policy::GetOSVersion()));
base::JSONWriter::Write(writable_os_info, os_info_string);
return true;
void AppendPlatformInformation(em::ChromeDesktopReportRequest* request) {
const char kComputerName[] = "computername";
const char kUsername[] = "username";
base::Value os_info = base::Value(base::Value::Type::DICTIONARY);
os_info.SetKey(kOS, base::Value(policy::GetOSPlatform()));
os_info.SetKey(kOSVersion, base::Value(policy::GetOSVersion()));
os_info.SetKey(kOSArch, base::Value(policy::GetOSArchitecture()));
base::JSONWriter::Write(os_info, request->mutable_os_info());
base::Value machine_name = base::Value(base::Value::Type::DICTIONARY);
machine_name.SetKey(kComputerName, base::Value(policy::GetMachineName()));
base::JSONWriter::Write(machine_name, request->mutable_machine_name());
base::Value os_user = base::Value(base::Value::Type::DICTIONARY);
os_user.SetKey(kUsername, base::Value(policy::GetOSUsername()));
base::JSONWriter::Write(os_user, request->mutable_os_user());
}
std::unique_ptr<em::ChromeUserProfileReport>
......@@ -192,13 +196,7 @@ GenerateChromeDesktopReportRequest(const base::DictionaryValue& report,
std::unique_ptr<em::ChromeDesktopReportRequest> request =
std::make_unique<em::ChromeDesktopReportRequest>();
if (!UpdateJSONEncodedStringEntry(
report, kMachineName, request->mutable_machine_name(), DICTIONARY) ||
!UpdateJSONEncodedStringEntry(report, kOSUser, request->mutable_os_user(),
DICTIONARY) ||
!UpdateOSInfoEntry(report, request->mutable_os_info())) {
return nullptr;
}
AppendPlatformInformation(request.get());
if (const base::Value* browser_report =
report.FindKeyOfType(kBrowserReport, base::Value::Type::DICTIONARY)) {
......
......@@ -5,6 +5,7 @@
#include "chrome/browser/extensions/api/enterprise_reporting_private/chrome_desktop_report_request_helper.h"
#include "base/json/json_reader.h"
#include "base/json/string_escape.h"
#include "base/values.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_profile.h"
......@@ -29,46 +30,41 @@ TEST_F(ChromeDesktopReportRequestGeneratorTest, OSInfo) {
std::unique_ptr<em::ChromeDesktopReportRequest> request;
std::string expected_os_info;
// Platform and its version will be the |os_info| by default.
expected_os_info = base::StringPrintf("{\"os\":\"%s\",\"os_version\":\"%s\"}",
policy::GetOSPlatform().c_str(),
policy::GetOSVersion().c_str());
expected_os_info = base::StringPrintf(
"{\"arch\":\"%s\",\"os\":\"%s\",\"os_version\":\"%s\"}",
policy::GetOSArchitecture().c_str(), policy::GetOSPlatform().c_str(),
policy::GetOSVersion().c_str());
request =
GenerateChromeDesktopReportRequest(base::DictionaryValue(), &profile_);
ASSERT_TRUE(request);
EXPECT_EQ(expected_os_info, request->os_info());
// Platform and its version will be merged with other |os_info|.
expected_os_info = base::StringPrintf(
"{\"os\":\"%s\",\"os_version\":\"%s\",\"other_info\":\"info\"}",
policy::GetOSPlatform().c_str(), policy::GetOSVersion().c_str());
std::unique_ptr<base::DictionaryValue> report = base::DictionaryValue::From(
base::JSONReader::Read("{\"osInfo\": {\"other_info\":\"info\"}}"));
ASSERT_TRUE(report);
request = GenerateChromeDesktopReportRequest(*report, &profile_);
ASSERT_TRUE(request);
EXPECT_EQ(expected_os_info, request->os_info());
}
TEST_F(ChromeDesktopReportRequestGeneratorTest, MachineName) {
std::unique_ptr<em::ChromeDesktopReportRequest> request;
std::string expected_machine_name;
// Machine name will be a empty dict by default.
expected_machine_name = "{}";
expected_machine_name = base::StringPrintf("{\"computername\":\"%s\"}",
policy::GetMachineName().c_str());
request =
GenerateChromeDesktopReportRequest(base::DictionaryValue(), &profile_);
ASSERT_TRUE(request);
EXPECT_EQ(expected_machine_name, request->machine_name());
}
// Machine name will be copied from the |report|.
expected_machine_name = "{\"computername\":\"name\"}";
std::unique_ptr<base::DictionaryValue> report = base::DictionaryValue::From(
base::JSONReader::Read("{\"machineName\":{\"computername\":\"name\"}}"));
ASSERT_TRUE(report);
request = GenerateChromeDesktopReportRequest(*report, &profile_);
TEST_F(ChromeDesktopReportRequestGeneratorTest, OSUsername) {
std::unique_ptr<em::ChromeDesktopReportRequest> request;
std::string expected_os_username, os_username_escaped;
// The username needs to be escaped as the name can contain slashes.
base::EscapeJSONString(policy::GetOSUsername(), false, &os_username_escaped);
expected_os_username =
base::StringPrintf("{\"username\":\"%s\"}", os_username_escaped.c_str());
request =
GenerateChromeDesktopReportRequest(base::DictionaryValue(), &profile_);
ASSERT_TRUE(request);
EXPECT_EQ(expected_machine_name, request->machine_name());
EXPECT_EQ(expected_os_username, request->os_user());
}
TEST_F(ChromeDesktopReportRequestGeneratorTest, ProfileName) {
......@@ -142,19 +138,15 @@ TEST_F(ChromeDesktopReportRequestGeneratorTest, ProfileID) {
TEST_F(ChromeDesktopReportRequestGeneratorTest, InvalidInput) {
// |request| will not be generated if the type of input is invalid.
std::unique_ptr<base::DictionaryValue> report;
report = base::DictionaryValue::From(
base::JSONReader::Read("{\"osInfo\": \"info\"}"));
ASSERT_TRUE(report);
EXPECT_FALSE(GenerateChromeDesktopReportRequest(*report, &profile_));
report = base::DictionaryValue::From(
base::JSONReader::Read("{\"machineName\": \"info\"}"));
report = base::DictionaryValue::From(base::JSONReader::Read(
"{\"browserReport\": "
"{\"chromeUserProfileReport\":[{\"extensionData\":{}}]}}"));
ASSERT_TRUE(report);
EXPECT_FALSE(GenerateChromeDesktopReportRequest(*report, &profile_));
report = base::DictionaryValue::From(base::JSONReader::Read(
"{\"browserReport\": "
"{\"chromeUserProfileReport\":[{\"extensionData\":{}}]}}"));
"{\"chromeUserProfileReport\":[{\"chromeSignInUser\":\"\"}]}}"));
ASSERT_TRUE(report);
EXPECT_FALSE(GenerateChromeDesktopReportRequest(*report, &profile_));
}
......
......@@ -21,7 +21,6 @@ namespace {
const char kFakeDMToken[] = "fake-dm-token";
const char kFakeClientId[] = "fake-client-id";
const char kFakeMachineNameReport[] = "{\"computername\":\"name\"}";
const char kFakeInvalidMachineNameReport[] = "\"invalid\"";
class MockCloudPolicyClient : public policy::MockCloudPolicyClient {
public:
......@@ -73,6 +72,14 @@ class EnterpriseReportingPrivateTest : public ExtensionApiUnittest {
return base::StringPrintf("[{\"machineName\":%s}]", name);
}
std::string GenerateInvalidReport() {
// This report is invalid as the chromeSignInUser dictionary should not be
// wrapped in a list.
return std::string(
"[{\"browserReport\": "
"{\"chromeUserProfileReport\":[{\"chromeSignInUser\":\"Name\"}]}}]");
}
MockCloudPolicyClient* client_;
private:
......@@ -90,7 +97,7 @@ TEST_F(EnterpriseReportingPrivateTest, ReportIsNotValid) {
ASSERT_EQ(enterprise_reporting::kInvalidInputErrorMessage,
RunFunctionAndReturnError(
CreateChromeDesktopReportingFunction(kFakeDMToken),
GenerateArgs(kFakeInvalidMachineNameReport)));
GenerateInvalidReport()));
}
TEST_F(EnterpriseReportingPrivateTest, UploadFailed) {
......
......@@ -8,6 +8,12 @@
#if defined(OS_WIN)
#include <Windows.h> // For GetComputerNameW()
// SECURITY_WIN32 must be defined in order to get
// EXTENDED_NAME_FORMAT enumeration.
#define SECURITY_WIN32 1
#include <security.h>
#undef SECURITY_WIN32
#include <wincred.h>
#endif
#if defined(OS_MACOSX)
......@@ -23,6 +29,7 @@
#include <utility>
#include "base/logging.h"
#include "base/sys_info.h"
#include "components/version_info/version_info.h"
#if defined(OS_WIN)
......@@ -113,4 +120,29 @@ std::string GetOSPlatform() {
return version_info::GetOSType();
}
std::string GetOSArchitecture() {
return base::SysInfo::OperatingSystemArchitecture();
}
std::string GetOSUsername() {
#if defined(OS_WIN)
WCHAR username[CREDUI_MAX_USERNAME_LENGTH + 1] = {};
DWORD username_length = sizeof(username);
// The SAM compatible username works on both standalone workstations and
// domain joined machines. The form is "DOMAIN\username", where DOMAIN is the
// the name of the machine for standalone workstations.
if (!::GetUserNameEx(::NameSamCompatible, username, &username_length) ||
username_length <= 0) {
return std::string();
}
return base::WideToUTF8(username);
#else
// TODO(crbug.com/847265): For now, this is only supported on Windows. Other
// platforms will be added when enabled.
return std::string();
#endif // OS_WIN
}
} // namespace policy
......@@ -11,15 +11,22 @@
namespace policy {
// Returns the name of the machine. This function is platform specific.
// Returns the name of the machine. This function is platform specific.
POLICY_EXPORT std::string GetMachineName();
// Returns the OS version of the machine. This function is platform specific.
// Returns the OS version of the machine. This function is platform specific.
POLICY_EXPORT std::string GetOSVersion();
// Returns the OS platform of the machine. This function is platform specific.
// Returns the OS platform of the machine. This function is platform specific.
POLICY_EXPORT std::string GetOSPlatform();
// Returns the bitness of the OS. This function is platform specific.
POLICY_EXPORT std::string GetOSArchitecture();
// Returns the username of the logged in user in the OS. This function is
// platform specific.
POLICY_EXPORT std::string GetOSUsername();
} // namespace policy
#endif // COMPONENTS_POLICY_CORE_COMMON_CLOUD_CLOUD_POLICY_UTIL_H_
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