Skip to content

Commit

Permalink
Initial Profile reporting for each profile
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 21 changed files with 439 additions and 37 deletions.
4 changes: 4 additions & 0 deletions chrome/browser/BUILD.gn
Expand Up @@ -5037,6 +5037,10 @@ static_library("browser") {
}
} else { # Non - Ash.
sources += [
"enterprise/reporting/cloud_profile_reporting_service.cc",
"enterprise/reporting/cloud_profile_reporting_service.h",
"enterprise/reporting/cloud_profile_reporting_service_factory.cc",
"enterprise/reporting/cloud_profile_reporting_service_factory.h",
"fullscreen.h",
"policy/browser_signin_policy_handler.cc",
"policy/browser_signin_policy_handler.h",
Expand Down
@@ -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
@@ -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
@@ -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_
@@ -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
@@ -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_
20 changes: 17 additions & 3 deletions chrome/browser/enterprise/reporting/report_scheduler_android.cc
Expand Up @@ -5,14 +5,17 @@
#include "chrome/browser/enterprise/reporting/report_scheduler_android.h"

#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/reporting_util.h"
#include "components/policy/core/common/cloud/dm_token.h"
#include "components/prefs/pref_service.h"

namespace enterprise_reporting {

ReportSchedulerAndroid::ReportSchedulerAndroid()
: ReportSchedulerAndroid(g_browser_process->local_state()) {}
ReportSchedulerAndroid::ReportSchedulerAndroid(raw_ptr<PrefService> prefs)
: prefs_(prefs) {}
: profile_(nullptr), prefs_(g_browser_process->local_state()) {}
ReportSchedulerAndroid::ReportSchedulerAndroid(raw_ptr<Profile> profile)
: profile_(profile), prefs_(profile_->GetPrefs()) {}

ReportSchedulerAndroid::~ReportSchedulerAndroid() = default;

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

policy::DMToken ReportSchedulerAndroid::GetProfileDMToken() {
absl::optional<std::string> dm_token = reporting::GetUserDmToken(profile_);
if (!dm_token || dm_token->empty())
return policy::DMToken();
return policy::DMToken(policy::DMToken::Status::kValid, *dm_token);
}

std::string ReportSchedulerAndroid::GetProfileClientId() {
return reporting::GetUserClientId(profile_).value_or(std::string());
}

} // namespace enterprise_reporting
Expand Up @@ -7,13 +7,15 @@

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

class Profile;

namespace enterprise_reporting {

// Android implementation of the ReportScheduler delegate.
class ReportSchedulerAndroid : public ReportScheduler::Delegate {
public:
ReportSchedulerAndroid();
explicit ReportSchedulerAndroid(raw_ptr<PrefService> prefs);
explicit ReportSchedulerAndroid(raw_ptr<Profile> profile);
ReportSchedulerAndroid(const ReportSchedulerAndroid&) = delete;
ReportSchedulerAndroid& operator=(const ReportSchedulerAndroid&) = delete;

Expand All @@ -28,8 +30,11 @@ class ReportSchedulerAndroid : public ReportScheduler::Delegate {
void StartWatchingExtensionRequestIfNeeded() override;
void StopWatchingExtensionRequest() override;
void OnExtensionRequestUploaded() override;
policy::DMToken GetProfileDMToken() override;
std::string GetProfileClientId() override;

private:
raw_ptr<Profile> profile_;
raw_ptr<PrefService> prefs_;
};

Expand Down

0 comments on commit cfa08b5

Please sign in to comment.