Skip to content

Commit cfa08b5

Browse files
Owen MinChromium LUCI CQ
authored andcommitted
Initial Profile reporting for each profile
Each Profile will have KeyService that is created with the profile. It creates and owns the profile reporting ReportScheduler instance. Add two functions to ReportingSchedulerDelegate to get DM token and client id for profile reporting. Also improves the ReportSchedulerDesktop ctor to make it clear about the usage between device reporting and profile reporting. Change-Id: I6f82c9debcc7a21cde2f6b75d9259ed67664903a Bug: 1261945 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3489079 Reviewed-by: Fabio Tirelo <ftirelo@chromium.org> Reviewed-by: Alex Ilin <alexilin@chromium.org> Commit-Queue: Owen Min <zmin@chromium.org> Cr-Commit-Position: refs/heads/main@{#984961}
1 parent ffe18ea commit cfa08b5

21 files changed

+439
-37
lines changed

chrome/browser/BUILD.gn

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5037,6 +5037,10 @@ static_library("browser") {
50375037
}
50385038
} else { # Non - Ash.
50395039
sources += [
5040+
"enterprise/reporting/cloud_profile_reporting_service.cc",
5041+
"enterprise/reporting/cloud_profile_reporting_service.h",
5042+
"enterprise/reporting/cloud_profile_reporting_service_factory.cc",
5043+
"enterprise/reporting/cloud_profile_reporting_service_factory.h",
50405044
"fullscreen.h",
50415045
"policy/browser_signin_policy_handler.cc",
50425046
"policy/browser_signin_policy_handler.h",
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright 2022 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "build/build_config.h"
6+
#include "build/chromeos_buildflags.h"
7+
#include "chrome/browser/enterprise/reporting/cloud_profile_reporting_service.h"
8+
#include "chrome/browser/enterprise/reporting/cloud_profile_reporting_service_factory.h"
9+
#include "chrome/browser/profiles/profile.h"
10+
#include "chrome/test/base/chrome_test_utils.h"
11+
#include "components/enterprise/browser/reporting/common_pref_names.h"
12+
#include "components/policy/core/common/cloud/cloud_policy_store.h"
13+
#include "components/policy/core/common/cloud/user_cloud_policy_manager.h"
14+
#include "components/policy/proto/device_management_backend.pb.h"
15+
#include "components/prefs/pref_service.h"
16+
#include "content/public/test/browser_test.h"
17+
18+
#if BUILDFLAG(IS_CHROMEOS_LACROS)
19+
#include "components/policy/core/common/policy_loader_lacros.h"
20+
#endif
21+
22+
namespace enterprise_reporting {
23+
24+
namespace em = enterprise_management;
25+
26+
class CloudProfileReportingServiceTest : public PlatformBrowserTest {
27+
public:
28+
CloudProfileReportingServiceTest() = default;
29+
~CloudProfileReportingServiceTest() override = default;
30+
31+
void SetUpOnMainThread() override {
32+
Profile* profile = chrome_test_utils::GetProfile(this);
33+
EnableProfileManagement(profile);
34+
EnableReportingPolicy(profile);
35+
}
36+
37+
void EnableProfileManagement(Profile* profile) {
38+
em::PolicyData policy_data;
39+
policy_data.set_request_token("dm-token");
40+
policy_data.set_device_id("device-id");
41+
#if BUILDFLAG(IS_CHROMEOS_LACROS)
42+
ASSERT_TRUE(profile->IsMainProfile());
43+
policy::PolicyLoaderLacros::set_main_user_policy_data_for_testing(
44+
policy_data);
45+
#else
46+
profile->GetUserCloudPolicyManager()
47+
->core()
48+
->store()
49+
->set_policy_data_for_testing(
50+
std::make_unique<em::PolicyData>(policy_data));
51+
#endif
52+
}
53+
54+
void EnableReportingPolicy(Profile* profile) {
55+
profile->GetPrefs()->SetBoolean(kCloudProfileReportingEnabled, true);
56+
}
57+
};
58+
59+
IN_PROC_BROWSER_TEST_F(CloudProfileReportingServiceTest, LaunchTest) {
60+
ReportScheduler* report_scheduler =
61+
CloudProfileReportingServiceFactory::GetForProfile(
62+
chrome_test_utils::GetProfile(this))
63+
->report_scheduler();
64+
ASSERT_TRUE(report_scheduler);
65+
EXPECT_TRUE(report_scheduler->IsNextReportScheduledForTesting() ||
66+
report_scheduler->GetActiveTriggerForTesting() ==
67+
ReportScheduler::kTriggerTimer);
68+
}
69+
70+
} // namespace enterprise_reporting
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2022 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "chrome/browser/enterprise/reporting/cloud_profile_reporting_service.h"
6+
7+
#include <utility>
8+
9+
#include "base/strings/utf_string_conversions.h"
10+
#include "build/build_config.h"
11+
#include "build/chromeos_buildflags.h"
12+
#include "chrome/browser/browser_process.h"
13+
#include "chrome/browser/profiles/profile.h"
14+
#include "chrome/browser/profiles/profile_attributes_entry.h"
15+
#include "chrome/browser/profiles/profile_attributes_storage.h"
16+
#include "chrome/browser/profiles/profile_manager.h"
17+
#include "components/enterprise/browser/reporting/chrome_profile_request_generator.h"
18+
#include "components/enterprise/browser/reporting/report_scheduler.h"
19+
#include "components/policy/core/common/cloud/cloud_policy_client.h"
20+
#include "services/network/public/cpp/shared_url_loader_factory.h"
21+
22+
#if BUILDFLAG(IS_ANDROID)
23+
#include "chrome/browser/enterprise/reporting/reporting_delegate_factory_android.h"
24+
#else
25+
#include "chrome/browser/enterprise/reporting/reporting_delegate_factory_desktop.h"
26+
#endif
27+
28+
namespace enterprise_reporting {
29+
30+
namespace {
31+
32+
std::string GetProfileName(raw_ptr<Profile> profile) {
33+
ProfileManager* profile_manager = g_browser_process->profile_manager();
34+
// profile manager may not be available in test.
35+
if (!profile_manager)
36+
return std::string();
37+
ProfileAttributesStorage& storage =
38+
profile_manager->GetProfileAttributesStorage();
39+
ProfileAttributesEntry* entry =
40+
storage.GetProfileAttributesWithPath(profile->GetPath());
41+
if (!entry)
42+
return std::string();
43+
return base::UTF16ToUTF8(entry->GetName());
44+
}
45+
46+
} // namespace
47+
48+
CloudProfileReportingService::CloudProfileReportingService(
49+
raw_ptr<Profile> profile,
50+
raw_ptr<policy::DeviceManagementService> device_management_service,
51+
scoped_refptr<network::SharedURLLoaderFactory> system_url_loader_factory) {
52+
cloud_policy_client_ = std::make_unique<policy::CloudPolicyClient>(
53+
device_management_service, system_url_loader_factory,
54+
policy::CloudPolicyClient::DeviceDMTokenCallback());
55+
56+
#if BUILDFLAG(IS_ANDROID)
57+
ReportingDelegateFactoryAndroid delegate_factory;
58+
#else
59+
ReportingDelegateFactoryDesktop delegate_factory;
60+
#endif // !BUILDFLAG(IS_ANDROID)
61+
ReportScheduler::CreateParams params;
62+
params.client = cloud_policy_client_.get();
63+
params.delegate = delegate_factory.GetReportSchedulerDelegate(profile);
64+
params.profile_request_generator =
65+
std::make_unique<ChromeProfileRequestGenerator>(
66+
profile->GetPath(), GetProfileName(profile), &delegate_factory);
67+
report_scheduler_ = std::make_unique<ReportScheduler>(std::move(params));
68+
}
69+
70+
CloudProfileReportingService::~CloudProfileReportingService() = default;
71+
72+
} // namespace enterprise_reporting
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2022 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef CHROME_BROWSER_ENTERPRISE_REPORTING_CLOUD_PROFILE_REPORTING_SERVICE_H_
6+
#define CHROME_BROWSER_ENTERPRISE_REPORTING_CLOUD_PROFILE_REPORTING_SERVICE_H_
7+
8+
#include <memory>
9+
10+
#include "base/memory/raw_ptr.h"
11+
#include "components/enterprise/browser/reporting/report_scheduler.h"
12+
#include "components/keyed_service/core/keyed_service.h"
13+
#include "components/policy/core/common/cloud/cloud_policy_client.h"
14+
15+
class Profile;
16+
17+
namespace policy {
18+
class DeviceManagementService;
19+
}
20+
21+
namespace enterprise_reporting {
22+
23+
class CloudProfileReportingService : public KeyedService {
24+
public:
25+
CloudProfileReportingService(
26+
raw_ptr<Profile> profile,
27+
raw_ptr<policy::DeviceManagementService> device_management_service,
28+
scoped_refptr<network::SharedURLLoaderFactory> system_url_loader_factory);
29+
CloudProfileReportingService(const CloudProfileReportingService&) = delete;
30+
CloudProfileReportingService& operator=(const CloudProfileReportingService&) =
31+
delete;
32+
~CloudProfileReportingService() override;
33+
34+
ReportScheduler* report_scheduler() { return report_scheduler_.get(); }
35+
36+
private:
37+
std::unique_ptr<policy::CloudPolicyClient> cloud_policy_client_;
38+
std::unique_ptr<ReportScheduler> report_scheduler_;
39+
};
40+
41+
} // namespace enterprise_reporting
42+
43+
#endif // CHROME_BROWSER_ENTERPRISE_REPORTING_CLOUD_PROFILE_REPORTING_SERVICE_H_
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2022 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "chrome/browser/enterprise/reporting/cloud_profile_reporting_service_factory.h"
6+
7+
#include "chrome/browser/browser_process.h"
8+
#include "chrome/browser/enterprise/reporting/cloud_profile_reporting_service.h"
9+
#include "chrome/browser/policy/chrome_browser_policy_connector.h"
10+
#include "chrome/browser/profiles/profile.h"
11+
#include "components/enterprise/browser/reporting/report_scheduler.h"
12+
#include "components/keyed_service/content/browser_context_dependency_manager.h"
13+
#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
14+
#include "services/network/public/cpp/shared_url_loader_factory.h"
15+
16+
namespace enterprise_reporting {
17+
18+
// static
19+
CloudProfileReportingServiceFactory*
20+
CloudProfileReportingServiceFactory::GetInstance() {
21+
return base::Singleton<CloudProfileReportingServiceFactory>::get();
22+
}
23+
24+
// static
25+
CloudProfileReportingService*
26+
CloudProfileReportingServiceFactory::GetForProfile(Profile* profile) {
27+
return static_cast<CloudProfileReportingService*>(
28+
GetInstance()->GetServiceForBrowserContext(profile, /*create=*/true));
29+
}
30+
31+
KeyedService* CloudProfileReportingServiceFactory::BuildServiceInstanceFor(
32+
content::BrowserContext* context) const {
33+
Profile* profile = Profile::FromBrowserContext(context);
34+
if (!profile->IsRegularProfile())
35+
return nullptr;
36+
37+
return new CloudProfileReportingService(
38+
profile,
39+
g_browser_process->browser_policy_connector()
40+
->device_management_service(),
41+
g_browser_process->shared_url_loader_factory());
42+
}
43+
bool CloudProfileReportingServiceFactory::ServiceIsCreatedWithBrowserContext()
44+
const {
45+
return true;
46+
}
47+
48+
CloudProfileReportingServiceFactory::CloudProfileReportingServiceFactory()
49+
: BrowserContextKeyedServiceFactory(
50+
"CloudProfileReporting",
51+
BrowserContextDependencyManager::GetInstance()) {}
52+
53+
CloudProfileReportingServiceFactory::~CloudProfileReportingServiceFactory() =
54+
default;
55+
56+
} // namespace enterprise_reporting
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2022 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef CHROME_BROWSER_ENTERPRISE_REPORTING_CLOUD_PROFILE_REPORTING_SERVICE_FACTORY_H_
6+
#define CHROME_BROWSER_ENTERPRISE_REPORTING_CLOUD_PROFILE_REPORTING_SERVICE_FACTORY_H_
7+
8+
#include "base/memory/singleton.h"
9+
#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
10+
11+
class Profile;
12+
13+
namespace enterprise_reporting {
14+
15+
class CloudProfileReportingService;
16+
17+
class CloudProfileReportingServiceFactory
18+
: public BrowserContextKeyedServiceFactory {
19+
public:
20+
static CloudProfileReportingServiceFactory* GetInstance();
21+
22+
static CloudProfileReportingService* GetForProfile(Profile* profile);
23+
24+
CloudProfileReportingServiceFactory(
25+
const CloudProfileReportingServiceFactory&) = delete;
26+
CloudProfileReportingServiceFactory& operator=(
27+
const CloudProfileReportingServiceFactory&) = delete;
28+
29+
protected:
30+
// BrowserContextKeyedServiceFactory implementation.
31+
KeyedService* BuildServiceInstanceFor(
32+
content::BrowserContext* context) const override;
33+
bool ServiceIsCreatedWithBrowserContext() const override;
34+
35+
private:
36+
friend struct base::DefaultSingletonTraits<
37+
CloudProfileReportingServiceFactory>;
38+
39+
CloudProfileReportingServiceFactory();
40+
~CloudProfileReportingServiceFactory() override;
41+
};
42+
43+
} // namespace enterprise_reporting
44+
45+
#endif // CHROME_BROWSER_ENTERPRISE_REPORTING_CLOUD_PROFILE_REPORTING_SERVICE_FACTORY_H_

chrome/browser/enterprise/reporting/report_scheduler_android.cc

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@
55
#include "chrome/browser/enterprise/reporting/report_scheduler_android.h"
66

77
#include "chrome/browser/browser_process.h"
8+
#include "chrome/browser/profiles/profile.h"
9+
#include "chrome/browser/profiles/reporting_util.h"
10+
#include "components/policy/core/common/cloud/dm_token.h"
811
#include "components/prefs/pref_service.h"
912

1013
namespace enterprise_reporting {
1114

1215
ReportSchedulerAndroid::ReportSchedulerAndroid()
13-
: ReportSchedulerAndroid(g_browser_process->local_state()) {}
14-
ReportSchedulerAndroid::ReportSchedulerAndroid(raw_ptr<PrefService> prefs)
15-
: prefs_(prefs) {}
16+
: profile_(nullptr), prefs_(g_browser_process->local_state()) {}
17+
ReportSchedulerAndroid::ReportSchedulerAndroid(raw_ptr<Profile> profile)
18+
: profile_(profile), prefs_(profile_->GetPrefs()) {}
1619

1720
ReportSchedulerAndroid::~ReportSchedulerAndroid() = default;
1821

@@ -46,4 +49,15 @@ void ReportSchedulerAndroid::OnExtensionRequestUploaded() {
4649
// No-op because extensions are not supported on Android.
4750
}
4851

52+
policy::DMToken ReportSchedulerAndroid::GetProfileDMToken() {
53+
absl::optional<std::string> dm_token = reporting::GetUserDmToken(profile_);
54+
if (!dm_token || dm_token->empty())
55+
return policy::DMToken();
56+
return policy::DMToken(policy::DMToken::Status::kValid, *dm_token);
57+
}
58+
59+
std::string ReportSchedulerAndroid::GetProfileClientId() {
60+
return reporting::GetUserClientId(profile_).value_or(std::string());
61+
}
62+
4963
} // namespace enterprise_reporting

chrome/browser/enterprise/reporting/report_scheduler_android.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77

88
#include "components/enterprise/browser/reporting/report_scheduler.h"
99

10+
class Profile;
11+
1012
namespace enterprise_reporting {
1113

1214
// Android implementation of the ReportScheduler delegate.
1315
class ReportSchedulerAndroid : public ReportScheduler::Delegate {
1416
public:
1517
ReportSchedulerAndroid();
16-
explicit ReportSchedulerAndroid(raw_ptr<PrefService> prefs);
18+
explicit ReportSchedulerAndroid(raw_ptr<Profile> profile);
1719
ReportSchedulerAndroid(const ReportSchedulerAndroid&) = delete;
1820
ReportSchedulerAndroid& operator=(const ReportSchedulerAndroid&) = delete;
1921

@@ -28,8 +30,11 @@ class ReportSchedulerAndroid : public ReportScheduler::Delegate {
2830
void StartWatchingExtensionRequestIfNeeded() override;
2931
void StopWatchingExtensionRequest() override;
3032
void OnExtensionRequestUploaded() override;
33+
policy::DMToken GetProfileDMToken() override;
34+
std::string GetProfileClientId() override;
3135

3236
private:
37+
raw_ptr<Profile> profile_;
3338
raw_ptr<PrefService> prefs_;
3439
};
3540

0 commit comments

Comments
 (0)