Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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}
- Loading branch information
Owen Min
authored and
Chromium LUCI CQ
committed
Mar 24, 2022
1 parent
ffe18ea
commit cfa08b5
Showing
21 changed files
with
439 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
chrome/browser/enterprise/reporting/cloud_profile_reporting_browsertest.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright 2022 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "build/build_config.h" | ||
#include "build/chromeos_buildflags.h" | ||
#include "chrome/browser/enterprise/reporting/cloud_profile_reporting_service.h" | ||
#include "chrome/browser/enterprise/reporting/cloud_profile_reporting_service_factory.h" | ||
#include "chrome/browser/profiles/profile.h" | ||
#include "chrome/test/base/chrome_test_utils.h" | ||
#include "components/enterprise/browser/reporting/common_pref_names.h" | ||
#include "components/policy/core/common/cloud/cloud_policy_store.h" | ||
#include "components/policy/core/common/cloud/user_cloud_policy_manager.h" | ||
#include "components/policy/proto/device_management_backend.pb.h" | ||
#include "components/prefs/pref_service.h" | ||
#include "content/public/test/browser_test.h" | ||
|
||
#if BUILDFLAG(IS_CHROMEOS_LACROS) | ||
#include "components/policy/core/common/policy_loader_lacros.h" | ||
#endif | ||
|
||
namespace enterprise_reporting { | ||
|
||
namespace em = enterprise_management; | ||
|
||
class CloudProfileReportingServiceTest : public PlatformBrowserTest { | ||
public: | ||
CloudProfileReportingServiceTest() = default; | ||
~CloudProfileReportingServiceTest() override = default; | ||
|
||
void SetUpOnMainThread() override { | ||
Profile* profile = chrome_test_utils::GetProfile(this); | ||
EnableProfileManagement(profile); | ||
EnableReportingPolicy(profile); | ||
} | ||
|
||
void EnableProfileManagement(Profile* profile) { | ||
em::PolicyData policy_data; | ||
policy_data.set_request_token("dm-token"); | ||
policy_data.set_device_id("device-id"); | ||
#if BUILDFLAG(IS_CHROMEOS_LACROS) | ||
ASSERT_TRUE(profile->IsMainProfile()); | ||
policy::PolicyLoaderLacros::set_main_user_policy_data_for_testing( | ||
policy_data); | ||
#else | ||
profile->GetUserCloudPolicyManager() | ||
->core() | ||
->store() | ||
->set_policy_data_for_testing( | ||
std::make_unique<em::PolicyData>(policy_data)); | ||
#endif | ||
} | ||
|
||
void EnableReportingPolicy(Profile* profile) { | ||
profile->GetPrefs()->SetBoolean(kCloudProfileReportingEnabled, true); | ||
} | ||
}; | ||
|
||
IN_PROC_BROWSER_TEST_F(CloudProfileReportingServiceTest, LaunchTest) { | ||
ReportScheduler* report_scheduler = | ||
CloudProfileReportingServiceFactory::GetForProfile( | ||
chrome_test_utils::GetProfile(this)) | ||
->report_scheduler(); | ||
ASSERT_TRUE(report_scheduler); | ||
EXPECT_TRUE(report_scheduler->IsNextReportScheduledForTesting() || | ||
report_scheduler->GetActiveTriggerForTesting() == | ||
ReportScheduler::kTriggerTimer); | ||
} | ||
|
||
} // namespace enterprise_reporting |
72 changes: 72 additions & 0 deletions
72
chrome/browser/enterprise/reporting/cloud_profile_reporting_service.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright 2022 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "chrome/browser/enterprise/reporting/cloud_profile_reporting_service.h" | ||
|
||
#include <utility> | ||
|
||
#include "base/strings/utf_string_conversions.h" | ||
#include "build/build_config.h" | ||
#include "build/chromeos_buildflags.h" | ||
#include "chrome/browser/browser_process.h" | ||
#include "chrome/browser/profiles/profile.h" | ||
#include "chrome/browser/profiles/profile_attributes_entry.h" | ||
#include "chrome/browser/profiles/profile_attributes_storage.h" | ||
#include "chrome/browser/profiles/profile_manager.h" | ||
#include "components/enterprise/browser/reporting/chrome_profile_request_generator.h" | ||
#include "components/enterprise/browser/reporting/report_scheduler.h" | ||
#include "components/policy/core/common/cloud/cloud_policy_client.h" | ||
#include "services/network/public/cpp/shared_url_loader_factory.h" | ||
|
||
#if BUILDFLAG(IS_ANDROID) | ||
#include "chrome/browser/enterprise/reporting/reporting_delegate_factory_android.h" | ||
#else | ||
#include "chrome/browser/enterprise/reporting/reporting_delegate_factory_desktop.h" | ||
#endif | ||
|
||
namespace enterprise_reporting { | ||
|
||
namespace { | ||
|
||
std::string GetProfileName(raw_ptr<Profile> profile) { | ||
ProfileManager* profile_manager = g_browser_process->profile_manager(); | ||
// profile manager may not be available in test. | ||
if (!profile_manager) | ||
return std::string(); | ||
ProfileAttributesStorage& storage = | ||
profile_manager->GetProfileAttributesStorage(); | ||
ProfileAttributesEntry* entry = | ||
storage.GetProfileAttributesWithPath(profile->GetPath()); | ||
if (!entry) | ||
return std::string(); | ||
return base::UTF16ToUTF8(entry->GetName()); | ||
} | ||
|
||
} // namespace | ||
|
||
CloudProfileReportingService::CloudProfileReportingService( | ||
raw_ptr<Profile> profile, | ||
raw_ptr<policy::DeviceManagementService> device_management_service, | ||
scoped_refptr<network::SharedURLLoaderFactory> system_url_loader_factory) { | ||
cloud_policy_client_ = std::make_unique<policy::CloudPolicyClient>( | ||
device_management_service, system_url_loader_factory, | ||
policy::CloudPolicyClient::DeviceDMTokenCallback()); | ||
|
||
#if BUILDFLAG(IS_ANDROID) | ||
ReportingDelegateFactoryAndroid delegate_factory; | ||
#else | ||
ReportingDelegateFactoryDesktop delegate_factory; | ||
#endif // !BUILDFLAG(IS_ANDROID) | ||
ReportScheduler::CreateParams params; | ||
params.client = cloud_policy_client_.get(); | ||
params.delegate = delegate_factory.GetReportSchedulerDelegate(profile); | ||
params.profile_request_generator = | ||
std::make_unique<ChromeProfileRequestGenerator>( | ||
profile->GetPath(), GetProfileName(profile), &delegate_factory); | ||
report_scheduler_ = std::make_unique<ReportScheduler>(std::move(params)); | ||
} | ||
|
||
CloudProfileReportingService::~CloudProfileReportingService() = default; | ||
|
||
} // namespace enterprise_reporting |
43 changes: 43 additions & 0 deletions
43
chrome/browser/enterprise/reporting/cloud_profile_reporting_service.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright 2022 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef CHROME_BROWSER_ENTERPRISE_REPORTING_CLOUD_PROFILE_REPORTING_SERVICE_H_ | ||
#define CHROME_BROWSER_ENTERPRISE_REPORTING_CLOUD_PROFILE_REPORTING_SERVICE_H_ | ||
|
||
#include <memory> | ||
|
||
#include "base/memory/raw_ptr.h" | ||
#include "components/enterprise/browser/reporting/report_scheduler.h" | ||
#include "components/keyed_service/core/keyed_service.h" | ||
#include "components/policy/core/common/cloud/cloud_policy_client.h" | ||
|
||
class Profile; | ||
|
||
namespace policy { | ||
class DeviceManagementService; | ||
} | ||
|
||
namespace enterprise_reporting { | ||
|
||
class CloudProfileReportingService : public KeyedService { | ||
public: | ||
CloudProfileReportingService( | ||
raw_ptr<Profile> profile, | ||
raw_ptr<policy::DeviceManagementService> device_management_service, | ||
scoped_refptr<network::SharedURLLoaderFactory> system_url_loader_factory); | ||
CloudProfileReportingService(const CloudProfileReportingService&) = delete; | ||
CloudProfileReportingService& operator=(const CloudProfileReportingService&) = | ||
delete; | ||
~CloudProfileReportingService() override; | ||
|
||
ReportScheduler* report_scheduler() { return report_scheduler_.get(); } | ||
|
||
private: | ||
std::unique_ptr<policy::CloudPolicyClient> cloud_policy_client_; | ||
std::unique_ptr<ReportScheduler> report_scheduler_; | ||
}; | ||
|
||
} // namespace enterprise_reporting | ||
|
||
#endif // CHROME_BROWSER_ENTERPRISE_REPORTING_CLOUD_PROFILE_REPORTING_SERVICE_H_ |
56 changes: 56 additions & 0 deletions
56
chrome/browser/enterprise/reporting/cloud_profile_reporting_service_factory.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright 2022 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "chrome/browser/enterprise/reporting/cloud_profile_reporting_service_factory.h" | ||
|
||
#include "chrome/browser/browser_process.h" | ||
#include "chrome/browser/enterprise/reporting/cloud_profile_reporting_service.h" | ||
#include "chrome/browser/policy/chrome_browser_policy_connector.h" | ||
#include "chrome/browser/profiles/profile.h" | ||
#include "components/enterprise/browser/reporting/report_scheduler.h" | ||
#include "components/keyed_service/content/browser_context_dependency_manager.h" | ||
#include "components/keyed_service/content/browser_context_keyed_service_factory.h" | ||
#include "services/network/public/cpp/shared_url_loader_factory.h" | ||
|
||
namespace enterprise_reporting { | ||
|
||
// static | ||
CloudProfileReportingServiceFactory* | ||
CloudProfileReportingServiceFactory::GetInstance() { | ||
return base::Singleton<CloudProfileReportingServiceFactory>::get(); | ||
} | ||
|
||
// static | ||
CloudProfileReportingService* | ||
CloudProfileReportingServiceFactory::GetForProfile(Profile* profile) { | ||
return static_cast<CloudProfileReportingService*>( | ||
GetInstance()->GetServiceForBrowserContext(profile, /*create=*/true)); | ||
} | ||
|
||
KeyedService* CloudProfileReportingServiceFactory::BuildServiceInstanceFor( | ||
content::BrowserContext* context) const { | ||
Profile* profile = Profile::FromBrowserContext(context); | ||
if (!profile->IsRegularProfile()) | ||
return nullptr; | ||
|
||
return new CloudProfileReportingService( | ||
profile, | ||
g_browser_process->browser_policy_connector() | ||
->device_management_service(), | ||
g_browser_process->shared_url_loader_factory()); | ||
} | ||
bool CloudProfileReportingServiceFactory::ServiceIsCreatedWithBrowserContext() | ||
const { | ||
return true; | ||
} | ||
|
||
CloudProfileReportingServiceFactory::CloudProfileReportingServiceFactory() | ||
: BrowserContextKeyedServiceFactory( | ||
"CloudProfileReporting", | ||
BrowserContextDependencyManager::GetInstance()) {} | ||
|
||
CloudProfileReportingServiceFactory::~CloudProfileReportingServiceFactory() = | ||
default; | ||
|
||
} // namespace enterprise_reporting |
45 changes: 45 additions & 0 deletions
45
chrome/browser/enterprise/reporting/cloud_profile_reporting_service_factory.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright 2022 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef CHROME_BROWSER_ENTERPRISE_REPORTING_CLOUD_PROFILE_REPORTING_SERVICE_FACTORY_H_ | ||
#define CHROME_BROWSER_ENTERPRISE_REPORTING_CLOUD_PROFILE_REPORTING_SERVICE_FACTORY_H_ | ||
|
||
#include "base/memory/singleton.h" | ||
#include "components/keyed_service/content/browser_context_keyed_service_factory.h" | ||
|
||
class Profile; | ||
|
||
namespace enterprise_reporting { | ||
|
||
class CloudProfileReportingService; | ||
|
||
class CloudProfileReportingServiceFactory | ||
: public BrowserContextKeyedServiceFactory { | ||
public: | ||
static CloudProfileReportingServiceFactory* GetInstance(); | ||
|
||
static CloudProfileReportingService* GetForProfile(Profile* profile); | ||
|
||
CloudProfileReportingServiceFactory( | ||
const CloudProfileReportingServiceFactory&) = delete; | ||
CloudProfileReportingServiceFactory& operator=( | ||
const CloudProfileReportingServiceFactory&) = delete; | ||
|
||
protected: | ||
// BrowserContextKeyedServiceFactory implementation. | ||
KeyedService* BuildServiceInstanceFor( | ||
content::BrowserContext* context) const override; | ||
bool ServiceIsCreatedWithBrowserContext() const override; | ||
|
||
private: | ||
friend struct base::DefaultSingletonTraits< | ||
CloudProfileReportingServiceFactory>; | ||
|
||
CloudProfileReportingServiceFactory(); | ||
~CloudProfileReportingServiceFactory() override; | ||
}; | ||
|
||
} // namespace enterprise_reporting | ||
|
||
#endif // CHROME_BROWSER_ENTERPRISE_REPORTING_CLOUD_PROFILE_REPORTING_SERVICE_FACTORY_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.