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; ...@@ -30,10 +30,7 @@ namespace em = enterprise_management;
namespace extensions { namespace extensions {
namespace { namespace {
// JSON key in extension arguments. // JSON keys in the extension arguments.
const char kMachineName[] = "machineName";
const char kOSInfo[] = "osInfo";
const char kOSUser[] = "osUser";
const char kBrowserReport[] = "browserReport"; const char kBrowserReport[] = "browserReport";
const char kChromeUserProfileReport[] = "chromeUserProfileReport"; const char kChromeUserProfileReport[] = "chromeUserProfileReport";
const char kChromeSignInUser[] = "chromeSignInUser"; const char kChromeSignInUser[] = "chromeSignInUser";
...@@ -43,8 +40,9 @@ const char kSafeBrowsingWarnings[] = "safeBrowsingWarnings"; ...@@ -43,8 +40,9 @@ const char kSafeBrowsingWarnings[] = "safeBrowsingWarnings";
const char kSafeBrowsingWarningsClickThrough[] = const char kSafeBrowsingWarningsClickThrough[] =
"safeBrowsingWarningsClickThrough"; "safeBrowsingWarningsClickThrough";
// JSON key in the os_info field. // JSON keys in the os_info field.
const char kOS[] = "os"; const char kOS[] = "os";
const char kOSArch[] = "arch";
const char kOSVersion[] = "os_version"; const char kOSVersion[] = "os_version";
const char kDefaultDictionary[] = "{}"; const char kDefaultDictionary[] = "{}";
...@@ -136,17 +134,23 @@ bool UpdateJSONEncodedStringEntry(const base::Value& dict_value, ...@@ -136,17 +134,23 @@ bool UpdateJSONEncodedStringEntry(const base::Value& dict_value,
return true; return true;
} }
bool UpdateOSInfoEntry(const base::Value& report, std::string* os_info_string) { void AppendPlatformInformation(em::ChromeDesktopReportRequest* request) {
base::Value writable_os_info(base::Value::Type::DICTIONARY); const char kComputerName[] = "computername";
if (const base::Value* os_info = report.FindKey(kOSInfo)) { const char kUsername[] = "username";
if (!os_info->is_dict())
return false; base::Value os_info = base::Value(base::Value::Type::DICTIONARY);
writable_os_info = os_info->Clone(); os_info.SetKey(kOS, base::Value(policy::GetOSPlatform()));
} os_info.SetKey(kOSVersion, base::Value(policy::GetOSVersion()));
writable_os_info.SetKey(kOS, base::Value(policy::GetOSPlatform())); os_info.SetKey(kOSArch, base::Value(policy::GetOSArchitecture()));
writable_os_info.SetKey(kOSVersion, base::Value(policy::GetOSVersion())); base::JSONWriter::Write(os_info, request->mutable_os_info());
base::JSONWriter::Write(writable_os_info, os_info_string);
return true; 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> std::unique_ptr<em::ChromeUserProfileReport>
...@@ -192,13 +196,7 @@ GenerateChromeDesktopReportRequest(const base::DictionaryValue& report, ...@@ -192,13 +196,7 @@ GenerateChromeDesktopReportRequest(const base::DictionaryValue& report,
std::unique_ptr<em::ChromeDesktopReportRequest> request = std::unique_ptr<em::ChromeDesktopReportRequest> request =
std::make_unique<em::ChromeDesktopReportRequest>(); std::make_unique<em::ChromeDesktopReportRequest>();
if (!UpdateJSONEncodedStringEntry( AppendPlatformInformation(request.get());
report, kMachineName, request->mutable_machine_name(), DICTIONARY) ||
!UpdateJSONEncodedStringEntry(report, kOSUser, request->mutable_os_user(),
DICTIONARY) ||
!UpdateOSInfoEntry(report, request->mutable_os_info())) {
return nullptr;
}
if (const base::Value* browser_report = if (const base::Value* browser_report =
report.FindKeyOfType(kBrowserReport, base::Value::Type::DICTIONARY)) { report.FindKeyOfType(kBrowserReport, base::Value::Type::DICTIONARY)) {
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "chrome/browser/extensions/api/enterprise_reporting_private/chrome_desktop_report_request_helper.h" #include "chrome/browser/extensions/api/enterprise_reporting_private/chrome_desktop_report_request_helper.h"
#include "base/json/json_reader.h" #include "base/json/json_reader.h"
#include "base/json/string_escape.h"
#include "base/values.h" #include "base/values.h"
#include "chrome/common/pref_names.h" #include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_profile.h" #include "chrome/test/base/testing_profile.h"
...@@ -29,46 +30,41 @@ TEST_F(ChromeDesktopReportRequestGeneratorTest, OSInfo) { ...@@ -29,46 +30,41 @@ TEST_F(ChromeDesktopReportRequestGeneratorTest, OSInfo) {
std::unique_ptr<em::ChromeDesktopReportRequest> request; std::unique_ptr<em::ChromeDesktopReportRequest> request;
std::string expected_os_info; std::string expected_os_info;
// Platform and its version will be the |os_info| by default. expected_os_info = base::StringPrintf(
expected_os_info = base::StringPrintf("{\"os\":\"%s\",\"os_version\":\"%s\"}", "{\"arch\":\"%s\",\"os\":\"%s\",\"os_version\":\"%s\"}",
policy::GetOSPlatform().c_str(), policy::GetOSArchitecture().c_str(), policy::GetOSPlatform().c_str(),
policy::GetOSVersion().c_str()); policy::GetOSVersion().c_str());
request = request =
GenerateChromeDesktopReportRequest(base::DictionaryValue(), &profile_); GenerateChromeDesktopReportRequest(base::DictionaryValue(), &profile_);
ASSERT_TRUE(request); ASSERT_TRUE(request);
EXPECT_EQ(expected_os_info, request->os_info()); 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) { TEST_F(ChromeDesktopReportRequestGeneratorTest, MachineName) {
std::unique_ptr<em::ChromeDesktopReportRequest> request; std::unique_ptr<em::ChromeDesktopReportRequest> request;
std::string expected_machine_name; std::string expected_machine_name;
// Machine name will be a empty dict by default. expected_machine_name = base::StringPrintf("{\"computername\":\"%s\"}",
expected_machine_name = "{}"; policy::GetMachineName().c_str());
request = request =
GenerateChromeDesktopReportRequest(base::DictionaryValue(), &profile_); GenerateChromeDesktopReportRequest(base::DictionaryValue(), &profile_);
ASSERT_TRUE(request); ASSERT_TRUE(request);
EXPECT_EQ(expected_machine_name, request->machine_name()); EXPECT_EQ(expected_machine_name, request->machine_name());
}
// Machine name will be copied from the |report|. TEST_F(ChromeDesktopReportRequestGeneratorTest, OSUsername) {
expected_machine_name = "{\"computername\":\"name\"}"; std::unique_ptr<em::ChromeDesktopReportRequest> request;
std::unique_ptr<base::DictionaryValue> report = base::DictionaryValue::From( std::string expected_os_username, os_username_escaped;
base::JSONReader::Read("{\"machineName\":{\"computername\":\"name\"}}"));
ASSERT_TRUE(report); // The username needs to be escaped as the name can contain slashes.
request = GenerateChromeDesktopReportRequest(*report, &profile_); 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); ASSERT_TRUE(request);
EXPECT_EQ(expected_machine_name, request->machine_name()); EXPECT_EQ(expected_os_username, request->os_user());
} }
TEST_F(ChromeDesktopReportRequestGeneratorTest, ProfileName) { TEST_F(ChromeDesktopReportRequestGeneratorTest, ProfileName) {
...@@ -142,19 +138,15 @@ TEST_F(ChromeDesktopReportRequestGeneratorTest, ProfileID) { ...@@ -142,19 +138,15 @@ TEST_F(ChromeDesktopReportRequestGeneratorTest, ProfileID) {
TEST_F(ChromeDesktopReportRequestGeneratorTest, InvalidInput) { TEST_F(ChromeDesktopReportRequestGeneratorTest, InvalidInput) {
// |request| will not be generated if the type of input is invalid. // |request| will not be generated if the type of input is invalid.
std::unique_ptr<base::DictionaryValue> report; std::unique_ptr<base::DictionaryValue> report;
report = base::DictionaryValue::From( report = base::DictionaryValue::From(base::JSONReader::Read(
base::JSONReader::Read("{\"osInfo\": \"info\"}")); "{\"browserReport\": "
ASSERT_TRUE(report); "{\"chromeUserProfileReport\":[{\"extensionData\":{}}]}}"));
EXPECT_FALSE(GenerateChromeDesktopReportRequest(*report, &profile_));
report = base::DictionaryValue::From(
base::JSONReader::Read("{\"machineName\": \"info\"}"));
ASSERT_TRUE(report); ASSERT_TRUE(report);
EXPECT_FALSE(GenerateChromeDesktopReportRequest(*report, &profile_)); EXPECT_FALSE(GenerateChromeDesktopReportRequest(*report, &profile_));
report = base::DictionaryValue::From(base::JSONReader::Read( report = base::DictionaryValue::From(base::JSONReader::Read(
"{\"browserReport\": " "{\"browserReport\": "
"{\"chromeUserProfileReport\":[{\"extensionData\":{}}]}}")); "{\"chromeUserProfileReport\":[{\"chromeSignInUser\":\"\"}]}}"));
ASSERT_TRUE(report); ASSERT_TRUE(report);
EXPECT_FALSE(GenerateChromeDesktopReportRequest(*report, &profile_)); EXPECT_FALSE(GenerateChromeDesktopReportRequest(*report, &profile_));
} }
......
...@@ -21,7 +21,6 @@ namespace { ...@@ -21,7 +21,6 @@ namespace {
const char kFakeDMToken[] = "fake-dm-token"; const char kFakeDMToken[] = "fake-dm-token";
const char kFakeClientId[] = "fake-client-id"; const char kFakeClientId[] = "fake-client-id";
const char kFakeMachineNameReport[] = "{\"computername\":\"name\"}"; const char kFakeMachineNameReport[] = "{\"computername\":\"name\"}";
const char kFakeInvalidMachineNameReport[] = "\"invalid\"";
class MockCloudPolicyClient : public policy::MockCloudPolicyClient { class MockCloudPolicyClient : public policy::MockCloudPolicyClient {
public: public:
...@@ -73,6 +72,14 @@ class EnterpriseReportingPrivateTest : public ExtensionApiUnittest { ...@@ -73,6 +72,14 @@ class EnterpriseReportingPrivateTest : public ExtensionApiUnittest {
return base::StringPrintf("[{\"machineName\":%s}]", name); 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_; MockCloudPolicyClient* client_;
private: private:
...@@ -90,7 +97,7 @@ TEST_F(EnterpriseReportingPrivateTest, ReportIsNotValid) { ...@@ -90,7 +97,7 @@ TEST_F(EnterpriseReportingPrivateTest, ReportIsNotValid) {
ASSERT_EQ(enterprise_reporting::kInvalidInputErrorMessage, ASSERT_EQ(enterprise_reporting::kInvalidInputErrorMessage,
RunFunctionAndReturnError( RunFunctionAndReturnError(
CreateChromeDesktopReportingFunction(kFakeDMToken), CreateChromeDesktopReportingFunction(kFakeDMToken),
GenerateArgs(kFakeInvalidMachineNameReport))); GenerateInvalidReport()));
} }
TEST_F(EnterpriseReportingPrivateTest, UploadFailed) { TEST_F(EnterpriseReportingPrivateTest, UploadFailed) {
......
...@@ -8,6 +8,12 @@ ...@@ -8,6 +8,12 @@
#if defined(OS_WIN) #if defined(OS_WIN)
#include <Windows.h> // For GetComputerNameW() #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 #endif
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
...@@ -23,6 +29,7 @@ ...@@ -23,6 +29,7 @@
#include <utility> #include <utility>
#include "base/logging.h" #include "base/logging.h"
#include "base/sys_info.h"
#include "components/version_info/version_info.h" #include "components/version_info/version_info.h"
#if defined(OS_WIN) #if defined(OS_WIN)
...@@ -113,4 +120,29 @@ std::string GetOSPlatform() { ...@@ -113,4 +120,29 @@ std::string GetOSPlatform() {
return version_info::GetOSType(); 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 } // namespace policy
...@@ -20,6 +20,13 @@ POLICY_EXPORT std::string GetOSVersion(); ...@@ -20,6 +20,13 @@ 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(); 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 } // namespace policy
#endif // COMPONENTS_POLICY_CORE_COMMON_CLOUD_CLOUD_POLICY_UTIL_H_ #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