-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Lacros] Introduce a new shutdown notifier factory
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
Showing
4 changed files
with
193 additions
and
0 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
31 changes: 31 additions & 0 deletions
31
...e/browser/chromeos/reporting/metric_reporting_manager_lacros_shutdown_notifier_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,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 |
44 changes: 44 additions & 0 deletions
44
...me/browser/chromeos/reporting/metric_reporting_manager_lacros_shutdown_notifier_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,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_ |
115 changes: 115 additions & 0 deletions
115
.../chromeos/reporting/metric_reporting_manager_lacros_shutdown_notifier_factory_unittest.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,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 |