Commit 89121b10 authored by Owen Min's avatar Owen Min Committed by Commit Bot

Add ExtensionRequestObserverFactory

It watches the Profile create/delete event to create ExtensionRequestObserver
for each Profile.

Bug: 1006899
Change-Id: I19a89427a957813d41f843b4e92256bfff0d1edd
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2028456
Commit-Queue: Owen Min <zmin@chromium.org>
Reviewed-by: default avatarJulian Pastarmov <pastarmovj@chromium.org>
Cr-Commit-Position: refs/heads/master@{#736969}
parent 454fd9a5
......@@ -3125,6 +3125,8 @@ jumbo_static_library("browser") {
"enterprise_reporting/notification/extension_request_notification.h",
"enterprise_reporting/notification/extension_request_observer.cc",
"enterprise_reporting/notification/extension_request_observer.h",
"enterprise_reporting/notification/extension_request_observer_factory.cc",
"enterprise_reporting/notification/extension_request_observer_factory.h",
"enterprise_reporting/policy_info.cc",
"enterprise_reporting/policy_info.h",
"enterprise_reporting/prefs.cc",
......
......@@ -46,8 +46,6 @@ constexpr int kNotificationBodies[] = {
} // namespace
ExtensionRequestNotification::ExtensionRequestNotification() = default;
ExtensionRequestNotification::ExtensionRequestNotification(
Profile* profile,
const NotifyType notify_type,
......
......@@ -28,7 +28,6 @@ class ExtensionRequestNotification
kNumberOfTypes = 3
};
ExtensionRequestNotification();
ExtensionRequestNotification(Profile* profile,
const NotifyType notify_type,
const ExtensionIds& extension_ids);
......
// Copyright 2020 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/notification/extension_request_observer_factory.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/enterprise_reporting/notification/extension_request_observer.h"
#include "chrome/browser/profiles/profile_manager.h"
namespace enterprise_reporting {
ExtensionRequestObserverFactory::ExtensionRequestObserverFactory() {
ProfileManager* profile_manager = g_browser_process->profile_manager();
profile_manager->AddObserver(this);
for (Profile* profile : profile_manager->GetLoadedProfiles())
OnProfileAdded(profile);
}
ExtensionRequestObserverFactory::~ExtensionRequestObserverFactory() {
if (g_browser_process->profile_manager())
g_browser_process->profile_manager()->RemoveObserver(this);
}
ExtensionRequestObserver*
ExtensionRequestObserverFactory::GetObserverByProfileForTesting(
Profile* profile) {
auto it = observers_.find(profile);
return it == observers_.end() ? nullptr : it->second.get();
}
int ExtensionRequestObserverFactory::GetNumberOfObserversForTesting() {
return observers_.size();
}
void ExtensionRequestObserverFactory::OnProfileAdded(Profile* profile) {
if (profile->IsSystemProfile() || profile->IsGuestSession() ||
profile->IsIncognitoProfile()) {
return;
}
observers_.emplace(profile,
std::make_unique<ExtensionRequestObserver>(profile));
}
void ExtensionRequestObserverFactory::OnProfileMarkedForPermanentDeletion(
Profile* profile) {
observers_.erase(profile);
}
} // namespace enterprise_reporting
// Copyright 2020 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_NOTIFICATION_EXTENSION_REQUEST_OBSERVER_FACTORY_H_
#define CHROME_BROWSER_ENTERPRISE_REPORTING_NOTIFICATION_EXTENSION_REQUEST_OBSERVER_FACTORY_H_
#include <map>
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager_observer.h"
namespace enterprise_reporting {
class ExtensionRequestObserver;
// Factory class for ExtensionRequestObserver. It creates
// ExtensionRequestObserver for each Profile.
class ExtensionRequestObserverFactory : public ProfileManagerObserver {
public:
ExtensionRequestObserverFactory();
ExtensionRequestObserverFactory(const ExtensionRequestObserverFactory&) =
delete;
ExtensionRequestObserverFactory& operator=(
const ExtensionRequestObserverFactory&) = delete;
~ExtensionRequestObserverFactory() override;
ExtensionRequestObserver* GetObserverByProfileForTesting(Profile* profile);
int GetNumberOfObserversForTesting();
private:
// ProfileManagerObserver
void OnProfileAdded(Profile* profile) override;
void OnProfileMarkedForPermanentDeletion(Profile* profile) override;
std::map<Profile*, std::unique_ptr<ExtensionRequestObserver>, ProfileCompare>
observers_;
};
} // namespace enterprise_reporting
#endif // CHROME_BROWSER_ENTERPRISE_REPORTING_NOTIFICATION_EXTENSION_REQUEST_OBSERVER_FACTORY_H_
// Copyright 2020 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/notification/extension_request_observer_factory.h"
#include "chrome/browser/enterprise_reporting/notification/extension_request_observer.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace enterprise_reporting {
constexpr char kProfile1[] = "profile-1";
constexpr char kProfile2[] = "profile-2";
class ExtensionRequestObserverFactoryTest : public ::testing::Test {
public:
void SetUp() override { ASSERT_TRUE(profile_manager_.SetUp()); }
TestingProfileManager* profile_manager() { return &profile_manager_; }
private:
content::BrowserTaskEnvironment task_environment_;
TestingProfileManager profile_manager_{TestingBrowserProcess::GetGlobal()};
};
TEST_F(ExtensionRequestObserverFactoryTest, LoadExistProfile) {
TestingProfile* profile = profile_manager()->CreateTestingProfile(kProfile1);
ExtensionRequestObserverFactory factory_;
EXPECT_TRUE(factory_.GetObserverByProfileForTesting(profile));
EXPECT_EQ(1, factory_.GetNumberOfObserversForTesting());
}
TEST_F(ExtensionRequestObserverFactoryTest, AddProfile) {
ExtensionRequestObserverFactory factory_;
EXPECT_EQ(0, factory_.GetNumberOfObserversForTesting());
TestingProfile* profile1 = profile_manager()->CreateTestingProfile(kProfile1);
EXPECT_TRUE(factory_.GetObserverByProfileForTesting(profile1));
EXPECT_EQ(1, factory_.GetNumberOfObserversForTesting());
TestingProfile* profile2 = profile_manager()->CreateTestingProfile(kProfile2);
EXPECT_TRUE(factory_.GetObserverByProfileForTesting(profile2));
EXPECT_EQ(2, factory_.GetNumberOfObserversForTesting());
}
TEST_F(ExtensionRequestObserverFactoryTest,
NoObserverForSystemAndGuestProfile) {
ExtensionRequestObserverFactory factory_;
EXPECT_EQ(0, factory_.GetNumberOfObserversForTesting());
TestingProfile* guest_profile = profile_manager()->CreateGuestProfile();
EXPECT_FALSE(factory_.GetObserverByProfileForTesting(guest_profile));
EXPECT_EQ(0, factory_.GetNumberOfObserversForTesting());
TestingProfile* system_profile = profile_manager()->CreateSystemProfile();
EXPECT_FALSE(factory_.GetObserverByProfileForTesting(system_profile));
EXPECT_EQ(0, factory_.GetNumberOfObserversForTesting());
}
} // namespace enterprise_reporting
......@@ -12,7 +12,7 @@
#include "base/containers/flat_set.h"
#include "base/macros.h"
#include "build/build_config.h"
#include "chrome/browser/enterprise_reporting/notification/extension_request_notification.h"
#include "chrome/browser/enterprise_reporting/notification/extension_request_observer_factory.h"
#include "chrome/browser/enterprise_reporting/report_generator.h"
#include "chrome/browser/enterprise_reporting/report_uploader.h"
#include "chrome/browser/profiles/profile_manager_observer.h"
......@@ -90,10 +90,7 @@ class ReportScheduler : public ProfileManagerObserver {
std::unique_ptr<base::flat_set<base::FilePath>> stale_profiles_;
// Create an un-used notification instance so that the resources won't be
// filtered out in the unit test. This will be removed once the whole
// implementation is finished.
ExtensionRequestNotification notification_;
ExtensionRequestObserverFactory extension_request_observer_factory_;
DISALLOW_COPY_AND_ASSIGN(ReportScheduler);
};
......
......@@ -3864,6 +3864,7 @@ test("unit_tests") {
"../browser/enterprise_reporting/browser_report_generator_unittest.cc",
"../browser/enterprise_reporting/extension_info_unittest.cc",
"../browser/enterprise_reporting/notification/extension_request_notification_unittest.cc",
"../browser/enterprise_reporting/notification/extension_request_observer_factory_unittest.cc",
"../browser/enterprise_reporting/notification/extension_request_observer_unittest.cc",
"../browser/enterprise_reporting/policy_info_unittest.cc",
"../browser/enterprise_reporting/profile_report_generator_unittest.cc",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment