Commit e2bbdff7 authored by Askar Aitzhan's avatar Askar Aitzhan Committed by Commit Bot

Add profile.dmToken and device.dmToken to event reporting request

Chrome Reporting API requires those fields to be set, otherwise the
request get rejected.

Bug: 1006757
Change-Id: If39bd56287719579ba1067488a38c2216eed5ec4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1814830
Commit-Queue: Askar Aitzhan <askaraitzhan@google.com>
Reviewed-by: default avatarSergey Poromov <poromov@chromium.org>
Reviewed-by: default avatarMaksim Ivanov <emaxx@chromium.org>
Reviewed-by: default avatarStefan Kuhne <skuhne@chromium.org>
Cr-Commit-Position: refs/heads/master@{#701613}
parent 3ad73478
...@@ -158,7 +158,7 @@ class TestLogTaskRunnerWrapper ...@@ -158,7 +158,7 @@ class TestLogTaskRunnerWrapper
class AppInstallEventLogManagerTest : public testing::Test { class AppInstallEventLogManagerTest : public testing::Test {
protected: protected:
AppInstallEventLogManagerTest() AppInstallEventLogManagerTest()
: uploader_(&cloud_policy_client_, nullptr), : uploader_(&cloud_policy_client_, /*profile=*/nullptr),
log_task_runner_(log_task_runner_wrapper_.test_task_runner()), log_task_runner_(log_task_runner_wrapper_.test_task_runner()),
log_file_path_(profile_.GetPath().Append(kLogFileName)), log_file_path_(profile_.GetPath().Append(kLogFileName)),
packages_{std::begin(kPackageNames), std::end(kPackageNames)}, packages_{std::begin(kPackageNames), std::end(kPackageNames)},
...@@ -227,8 +227,8 @@ class AppInstallEventLogManagerTest : public testing::Test { ...@@ -227,8 +227,8 @@ class AppInstallEventLogManagerTest : public testing::Test {
} }
void BuildReport() { void BuildReport() {
base::Value event_list = ConvertEventsToValue(events_, nullptr); base::Value event_list = ConvertEventsToValue(events_, /*profile=*/nullptr);
base::Value context = reporting::GetContext(nullptr); base::Value context = reporting::GetContext(/*profile=*/nullptr);
AppendEventId(&event_list, context); AppendEventId(&event_list, context);
events_value_ = RealtimeReportingJobConfiguration::BuildReport( events_value_ = RealtimeReportingJobConfiguration::BuildReport(
......
...@@ -101,7 +101,8 @@ class AppInstallEventLogUploaderTest : public testing::Test { ...@@ -101,7 +101,8 @@ class AppInstallEventLogUploaderTest : public testing::Test {
} }
void CreateUploader() { void CreateUploader() {
uploader_ = std::make_unique<AppInstallEventLogUploader>(&client_, nullptr); uploader_ = std::make_unique<AppInstallEventLogUploader>(
&client_, /*profile=*/nullptr);
uploader_->SetDelegate(&delegate_); uploader_->SetDelegate(&delegate_);
} }
...@@ -129,7 +130,8 @@ class AppInstallEventLogUploaderTest : public testing::Test { ...@@ -129,7 +130,8 @@ class AppInstallEventLogUploaderTest : public testing::Test {
void CompleteUpload(bool success) { void CompleteUpload(bool success) {
ClearReportDict(); ClearReportDict();
value_report_ = RealtimeReportingJobConfiguration::BuildReport( value_report_ = RealtimeReportingJobConfiguration::BuildReport(
ConvertProtoToValue(&log_, nullptr), reporting::GetContext(nullptr)); ConvertProtoToValue(&log_, /*profile=*/nullptr),
reporting::GetContext(/*profile=*/nullptr));
EXPECT_CALL(client_, UploadRealtimeReport(MatchValue(&value_report_), _)) EXPECT_CALL(client_, UploadRealtimeReport(MatchValue(&value_report_), _))
.WillOnce(WithArgs<1>( .WillOnce(WithArgs<1>(
...@@ -141,7 +143,8 @@ class AppInstallEventLogUploaderTest : public testing::Test { ...@@ -141,7 +143,8 @@ class AppInstallEventLogUploaderTest : public testing::Test {
void CaptureUpload(CloudPolicyClient::StatusCallback* callback) { void CaptureUpload(CloudPolicyClient::StatusCallback* callback) {
ClearReportDict(); ClearReportDict();
value_report_ = RealtimeReportingJobConfiguration::BuildReport( value_report_ = RealtimeReportingJobConfiguration::BuildReport(
ConvertProtoToValue(&log_, nullptr), reporting::GetContext(nullptr)); ConvertProtoToValue(&log_, /*profile=*/nullptr),
reporting::GetContext(/*profile=*/nullptr));
CloudPolicyClient::StatusCallback status_callback; CloudPolicyClient::StatusCallback status_callback;
EXPECT_CALL(client_, UploadRealtimeReport(MatchValue(&value_report_), _)) EXPECT_CALL(client_, UploadRealtimeReport(MatchValue(&value_report_), _))
......
...@@ -4,6 +4,10 @@ ...@@ -4,6 +4,10 @@
#include "chrome/browser/profiles/reporting_util.h" #include "chrome/browser/profiles/reporting_util.h"
#include <string>
#include <vector>
#include "base/callback.h"
#include "base/values.h" #include "base/values.h"
#include "chrome/browser/browser_process.h" #include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_content_browser_client.h" #include "chrome/browser/chrome_content_browser_client.h"
...@@ -11,6 +15,91 @@ ...@@ -11,6 +15,91 @@
#include "chrome/browser/profiles/profile_attributes_entry.h" #include "chrome/browser/profiles/profile_attributes_entry.h"
#include "chrome/browser/profiles/profile_attributes_storage.h" #include "chrome/browser/profiles/profile_attributes_storage.h"
#include "chrome/browser/profiles/profile_manager.h" #include "chrome/browser/profiles/profile_manager.h"
#include "components/account_id/account_id.h"
#include "components/policy/core/common/cloud/cloud_policy_store.h"
#include "components/policy/proto/device_management_backend.pb.h"
#include "components/user_manager/user.h"
#include "components/user_manager/user_manager.h"
#ifdef OS_CHROMEOS
#include "chrome/browser/chromeos/login/users/affiliation.h"
#include "chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos.h"
#include "chrome/browser/chromeos/profiles/profile_helper.h"
#endif // OS_CHROMEOS
namespace {
#ifdef OS_CHROMEOS
// A callback which fetches device dm_token based on user affiliation.
using DeviceDMTokenCallback = base::RepeatingCallback<std::string(
const std::vector<std::string>& user_affiliation_ids)>;
// Returns policy for the given |profile|. If failed to get policy returns
// nullptr.
const enterprise_management::PolicyData* GetPolicyData(Profile* profile) {
if (!profile)
return nullptr;
policy::UserCloudPolicyManagerChromeOS* manager =
profile->GetUserCloudPolicyManagerChromeOS();
if (!manager)
return nullptr;
policy::CloudPolicyStore* store = manager->core()->store();
if (!store || !store->has_policy())
return nullptr;
return store->policy();
}
// Returns the Device DMToken for the given |profile| if:
// * |profile| is NOT incognito profile
// * user corresponding to a given |profile| is affiliated.
// Otherwise returns empty string. More about DMToken:
// go/dmserver-domain-model#dmtoken.
std::string GetDeviceDmToken(Profile* profile) {
if (!profile)
return std::string();
const user_manager::User* user =
chromeos::ProfileHelper::Get()->GetUserByProfile(profile);
if (!user)
return std::string();
DeviceDMTokenCallback device_dm_token_callback =
chromeos::GetDeviceDMTokenForUserPolicyGetter(user->GetAccountId());
if (!device_dm_token_callback)
return std::string();
const enterprise_management::PolicyData* policy = GetPolicyData(profile);
if (!policy)
return std::string();
std::vector<std::string> user_affiliation_ids(
policy->user_affiliation_ids().begin(),
policy->user_affiliation_ids().end());
return device_dm_token_callback.Run(user_affiliation_ids);
}
// Returns User DMToken for a given |profile| if:
// * |profile| is NOT incognito profile.
// * |profile| is NOT sign-in screen profile
// * user corresponding to a |profile| is managed.
// Otherwise returns empty string. More about DMToken:
// go/dmserver-domain-model#dmtoken.
std::string GetUserDmToken(Profile* profile) {
if (!profile)
return std::string();
const enterprise_management::PolicyData* policy = GetPolicyData(profile);
if (!policy || !policy->has_request_token())
return std::string();
return policy->request_token();
}
#endif // OS_CHROMEOS
} // namespace
namespace reporting { namespace reporting {
...@@ -31,6 +120,23 @@ base::Value GetContext(Profile* profile) { ...@@ -31,6 +120,23 @@ base::Value GetContext(Profile* profile) {
context.SetStringPath("profile.profilePath", profile->GetPath().value()); context.SetStringPath("profile.profilePath", profile->GetPath().value());
#ifdef OS_CHROMEOS
const enterprise_management::PolicyData* policy = GetPolicyData(profile);
if (policy) {
if (policy->has_device_id())
context.SetStringPath("profile.clientId", policy->device_id());
std::string device_dm_token = GetDeviceDmToken(profile);
if (!device_dm_token.empty())
context.SetStringPath("device.dmToken", device_dm_token);
std::string user_dm_token = GetUserDmToken(profile);
if (!user_dm_token.empty())
context.SetStringPath("profile.dmToken", user_dm_token);
}
#endif // OS_CHROMEOS
return context; return context;
} }
......
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