Skip to content

Commit

Permalink
[Lacros] Introduce a new shutdown notifier factory
Browse files Browse the repository at this point in the history
This change introduces a new shutdown notifier factory for the
MetricReportingManagerLacros component. This will enable metric
reporting components to perform cleanup tasks as necessary,

Bug: b:302033398
Change-Id: Ieb64f5d9b52fd6bbeee7f796577fdce7b5b02326
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4932892
Reviewed-by: Leonid Baraz <lbaraz@chromium.org>
Reviewed-by: Hong Xu <xuhong@google.com>
Commit-Queue: Vignesh Shenvi <vshenvi@google.com>
Cr-Commit-Position: refs/heads/main@{#1209244}
  • Loading branch information
Vignesh Shenvi authored and Chromium LUCI CQ committed Oct 13, 2023
1 parent 60c44ce commit acaa8f0
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 0 deletions.
3 changes: 3 additions & 0 deletions chrome/browser/chromeos/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,8 @@ source_set("chromeos") {
"reporting/metric_reporting_manager_lacros.h",
"reporting/metric_reporting_manager_lacros_factory.cc",
"reporting/metric_reporting_manager_lacros_factory.h",
"reporting/metric_reporting_manager_lacros_shutdown_notifier_factory.cc",
"reporting/metric_reporting_manager_lacros_shutdown_notifier_factory.h",
"reporting/websites/website_metrics_retriever_lacros.cc",
"reporting/websites/website_metrics_retriever_lacros.h",
]
Expand Down Expand Up @@ -663,6 +665,7 @@ source_set("unit_tests") {
"policy/dlp/dlp_files_controller_lacros_unittest.cc",
"reporting/device_reporting_settings_lacros_unittest.cc",
"reporting/metric_reporting_manager_lacros_factory_unittest.cc",
"reporting/metric_reporting_manager_lacros_shutdown_notifier_factory_unittest.cc",
"reporting/metric_reporting_manager_lacros_unittest.cc",
"reporting/websites/website_metrics_retriever_lacros_unittest.cc",
]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/chromeos/reporting/metric_reporting_manager_lacros_shutdown_notifier_factory.h"

#include "base/no_destructor.h"
#include "chrome/browser/chromeos/reporting/metric_reporting_manager_lacros_factory.h"
#include "components/keyed_service/content/browser_context_keyed_service_shutdown_notifier_factory.h"

namespace reporting::metrics {

// static
MetricReportingManagerLacrosShutdownNotifierFactory*
MetricReportingManagerLacrosShutdownNotifierFactory::GetInstance() {
static base::NoDestructor<MetricReportingManagerLacrosShutdownNotifierFactory>
g_factory;
return g_factory.get();
}

MetricReportingManagerLacrosShutdownNotifierFactory::
MetricReportingManagerLacrosShutdownNotifierFactory()
: BrowserContextKeyedServiceShutdownNotifierFactory(
"MetricReportingManagerLacrosShutdownNotifier") {
DependsOn(MetricReportingManagerLacrosFactory::GetInstance());
}

MetricReportingManagerLacrosShutdownNotifierFactory::
~MetricReportingManagerLacrosShutdownNotifierFactory() = default;

} // namespace reporting::metrics
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_CHROMEOS_REPORTING_METRIC_REPORTING_MANAGER_LACROS_SHUTDOWN_NOTIFIER_FACTORY_H_
#define CHROME_BROWSER_CHROMEOS_REPORTING_METRIC_REPORTING_MANAGER_LACROS_SHUTDOWN_NOTIFIER_FACTORY_H_

#include "base/no_destructor.h"
#include "build/chromeos_buildflags.h"
#include "components/keyed_service/content/browser_context_keyed_service_shutdown_notifier_factory.h"

static_assert(BUILDFLAG(IS_CHROMEOS_LACROS), "For Lacros only");

namespace reporting::metrics {

// Factory implementation that notifies subscribers of the
// `MetricReportingManagerLacros` component shutdown for a given context.
// Primarily used by metric reporting components in Lacros to perform cleanup
// tasks on shutdown.
class MetricReportingManagerLacrosShutdownNotifierFactory
: public BrowserContextKeyedServiceShutdownNotifierFactory {
public:
// Static helper that returns the singleton instance of the
// `MetricReportingManagerLacrosShutdownNotifierFactory`.
static MetricReportingManagerLacrosShutdownNotifierFactory* GetInstance();

MetricReportingManagerLacrosShutdownNotifierFactory(
const MetricReportingManagerLacrosShutdownNotifierFactory& other) =
delete;
MetricReportingManagerLacrosShutdownNotifierFactory& operator=(
const MetricReportingManagerLacrosShutdownNotifierFactory& other) =
delete;

private:
friend base::NoDestructor<
MetricReportingManagerLacrosShutdownNotifierFactory>;

MetricReportingManagerLacrosShutdownNotifierFactory();
~MetricReportingManagerLacrosShutdownNotifierFactory() override;
};

} // namespace reporting::metrics

#endif // CHROME_BROWSER_CHROMEOS_REPORTING_METRIC_REPORTING_MANAGER_LACROS_SHUTDOWN_NOTIFIER_FACTORY_H_
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/chromeos/reporting/metric_reporting_manager_lacros_shutdown_notifier_factory.h"

#include <memory>
#include <string_view>

#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/test/bind.h"
#include "chrome/browser/chromeos/reporting/device_reporting_settings_lacros.h"
#include "chrome/browser/chromeos/reporting/metric_reporting_manager_lacros.h"
#include "chrome/browser/chromeos/reporting/metric_reporting_manager_lacros_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/keyed_service/core/keyed_service_shutdown_notifier.h"
#include "content/public/browser/browser_context.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using ::testing::Eq;

namespace reporting::metrics {
namespace {

constexpr std::string_view kTestUserId = "123";

// Fake delegate implementation for the `MetricReportingManagerLacros`
// component. Used with the `MetricReportingManagerLacrosFactory` to block
// initialization of downstream components and simplify testing.
class FakeDelegate : public MetricReportingManagerLacros::Delegate {
public:
FakeDelegate() = default;
FakeDelegate(const FakeDelegate& other) = delete;
FakeDelegate& operator=(const FakeDelegate& other) = delete;
~FakeDelegate() override = default;

bool IsUserAffiliated(Profile& profile) const override { return false; }

std::unique_ptr<DeviceReportingSettingsLacros> CreateDeviceReportingSettings()
override {
return std::unique_ptr<DeviceReportingSettingsLacros>(nullptr);
}
};

class MetricReportingManagerLacrosShutdownNotifierFactoryTest
: public ::testing::Test {
protected:
MetricReportingManagerLacrosShutdownNotifierFactoryTest()
: profile_manager_(TestingBrowserProcess::GetGlobal()) {}

void SetUp() override {
ASSERT_TRUE(profile_manager_.SetUp());
dependency_manager_subscription_ =
BrowserContextDependencyManager::GetInstance()
->RegisterCreateServicesCallbackForTesting(base::BindRepeating(
&MetricReportingManagerLacrosShutdownNotifierFactoryTest::
SetTestingFactory,
base::Unretained(this)));

// Set up main user profile. Used to monitor `MetricReportingManagerLacros`
// component shutdown from the notifier.
profile_ = profile_manager_.CreateTestingProfile(std::string{kTestUserId});
profile_->SetIsMainProfile(true);
}

void SetTestingFactory(::content::BrowserContext* context) {
MetricReportingManagerLacrosFactory::GetInstance()->SetTestingFactory(
context, base::BindRepeating(
&MetricReportingManagerLacrosShutdownNotifierFactoryTest::
CreateMetricReportingManager,
base::Unretained(this)));
}

std::unique_ptr<KeyedService> CreateMetricReportingManager(
::content::BrowserContext* context) {
auto fake_delegate = std::make_unique<FakeDelegate>();
return std::make_unique<MetricReportingManagerLacros>(
static_cast<Profile*>(context), std::move(fake_delegate));
}

::content::BrowserTaskEnvironment task_environment_{
base::test::TaskEnvironment::TimeSource::MOCK_TIME};
TestingProfileManager profile_manager_;
base::CallbackListSubscription dependency_manager_subscription_;
raw_ptr<TestingProfile> profile_;
};

TEST_F(MetricReportingManagerLacrosShutdownNotifierFactoryTest,
NotifyOnShutdown) {
int callback_triggered_count = 0;
const base::CallbackListSubscription shutdown_subscription =
MetricReportingManagerLacrosShutdownNotifierFactory::GetInstance()
->Get(profile_.get())
->Subscribe(base::BindLambdaForTesting(
[&callback_triggered_count]() { ++callback_triggered_count; }));
ASSERT_THAT(callback_triggered_count, Eq(0));

// Delete profile to trigger shutdown of the metric reporting manager and
// verify callback was triggered.
profile_ = nullptr;
profile_manager_.DeleteAllTestingProfiles();
EXPECT_THAT(callback_triggered_count, Eq(1));
}

} // namespace
} // namespace reporting::metrics

0 comments on commit acaa8f0

Please sign in to comment.