Commit 5b510464 authored by Evan Stade's avatar Evan Stade Committed by Commit Bot

Eagerly create UserScriptListener.

Lazy creation meant that sometimes, at least in tests,
UserScriptListener wasn't created early enough to do its job (profiles
were added and user script update notifications were sent before
UserScriptListener was created).

Bug: none
Change-Id: Id0a3c394da2d5bd2c58da5d26118f4448c68107d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1853912
Commit-Queue: Evan Stade <estade@chromium.org>
Reviewed-by: default avatarDevlin <rdevlin.cronin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#708079}
parent 07de4276
......@@ -33,7 +33,6 @@
#include "chrome/browser/extensions/extension_util.h"
#include "chrome/browser/extensions/menu_manager.h"
#include "chrome/browser/extensions/updater/chrome_update_client_config.h"
#include "chrome/browser/extensions/user_script_listener.h"
#include "chrome/browser/external_protocol/external_protocol_handler.h"
#include "chrome/browser/net/system_network_context_manager.h"
#include "chrome/browser/profiles/profile.h"
......@@ -520,11 +519,7 @@ ChromeExtensionsBrowserClient::GetSystemNetworkContext() {
}
UserScriptListener* ChromeExtensionsBrowserClient::GetUserScriptListener() {
// Create lazily since this accesses g_browser_process which may not be set up
// when ChromeExtensionsBrowserClient is created.
if (!user_script_listener_)
user_script_listener_ = std::make_unique<UserScriptListener>();
return user_script_listener_.get();
return &user_script_listener_;
}
std::string ChromeExtensionsBrowserClient::GetUserAgent() const {
......
......@@ -14,6 +14,7 @@
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "build/build_config.h"
#include "chrome/browser/extensions/user_script_listener.h"
#include "extensions/browser/extensions_browser_client.h"
#include "extensions/browser/kiosk/kiosk_delegate.h"
......@@ -165,7 +166,7 @@ class ChromeExtensionsBrowserClient : public ExtensionsBrowserClient {
std::unique_ptr<KioskDelegate> kiosk_delegate_;
std::unique_ptr<UserScriptListener> user_script_listener_;
UserScriptListener user_script_listener_;
DISALLOW_COPY_AND_ASSIGN(ChromeExtensionsBrowserClient);
};
......
......@@ -179,9 +179,7 @@ void UserScriptListener::AppendNewURLPatterns(content::BrowserContext* context,
void UserScriptListener::ReplaceURLPatterns(content::BrowserContext* context,
const URLPatterns& patterns) {
// TODO(estade): enable this check once it no longer fails.
// DCHECK_EQ(1U, profile_data_.count(context));
DCHECK_EQ(1U, profile_data_.count(context));
profile_data_[context].url_patterns = patterns;
}
......
......@@ -23,7 +23,9 @@
#include "chrome/browser/ui/toolbar/toolbar_actions_bar_unittest.h"
#include "chrome/grit/chromium_strings.h"
#include "chrome/grit/generated_resources.h"
#include "content/public/browser/notification_service.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/notification_types.h"
#include "extensions/common/extension_builder.h"
#include "extensions/common/user_script.h"
#include "ui/base/l10n/l10n_util.h"
......@@ -256,6 +258,13 @@ void ExtensionActionViewControllerGrayscaleTest::RunGrayscaleTest(
permissions_modifier.SetWithholdHostPermissions(true);
ASSERT_EQ(1u, toolbar_actions_bar()->GetIconCount());
const GURL kUrl("https://www.google.com/");
// Make sure UserScriptListener doesn't hold up the navigation.
content::NotificationService::current()->Notify(
extensions::NOTIFICATION_USER_SCRIPTS_UPDATED,
content::Source<Profile>(browser()->profile()),
content::NotificationService::NoDetails());
AddTab(browser(), kUrl);
enum class ActionState {
......
......@@ -70,7 +70,12 @@ TestingBrowserProcess* TestingBrowserProcess::GetGlobal() {
// static
void TestingBrowserProcess::CreateInstance() {
DCHECK(!g_browser_process);
g_browser_process = new TestingBrowserProcess;
TestingBrowserProcess* process = new TestingBrowserProcess;
// Set |g_browser_process| before initializing the TestingBrowserProcess
// because some members may depend on |g_browser_process| (in particular,
// ChromeExtensionsBrowserClient).
g_browser_process = process;
process->Init();
}
// static
......@@ -84,28 +89,7 @@ void TestingBrowserProcess::DeleteInstance() {
TestingBrowserProcess::TestingBrowserProcess()
: notification_service_(content::NotificationService::Create()),
app_locale_("en"),
is_shutting_down_(false),
local_state_(nullptr),
rappor_service_(nullptr),
platform_part_(new TestingBrowserProcessPlatformPart()),
test_network_connection_tracker_(
network::TestNetworkConnectionTracker::CreateInstance()) {
content::SetNetworkConnectionTrackerForTesting(
test_network_connection_tracker_.get());
#if BUILDFLAG(ENABLE_EXTENSIONS)
extensions_browser_client_.reset(
new extensions::ChromeExtensionsBrowserClient);
extensions_browser_client_->AddAPIProvider(
std::make_unique<chrome_apps::ChromeAppsBrowserAPIProvider>());
extensions::AppWindowClient::Set(ChromeAppWindowClient::GetInstance());
extensions::ExtensionsBrowserClient::Set(extensions_browser_client_.get());
#endif
#if !defined(OS_ANDROID)
KeepAliveRegistry::GetInstance()->SetIsShuttingDown(false);
#endif
}
platform_part_(new TestingBrowserProcessPlatformPart()) {}
TestingBrowserProcess::~TestingBrowserProcess() {
EXPECT_FALSE(local_state_);
......@@ -122,6 +106,26 @@ TestingBrowserProcess::~TestingBrowserProcess() {
DCHECK_EQ(static_cast<BrowserProcess*>(nullptr), g_browser_process);
}
void TestingBrowserProcess::Init() {
test_network_connection_tracker_ =
network::TestNetworkConnectionTracker::CreateInstance();
content::SetNetworkConnectionTrackerForTesting(
test_network_connection_tracker_.get());
#if BUILDFLAG(ENABLE_EXTENSIONS)
extensions_browser_client_ =
std::make_unique<extensions::ChromeExtensionsBrowserClient>();
extensions_browser_client_->AddAPIProvider(
std::make_unique<chrome_apps::ChromeAppsBrowserAPIProvider>());
extensions::AppWindowClient::Set(ChromeAppWindowClient::GetInstance());
extensions::ExtensionsBrowserClient::Set(extensions_browser_client_.get());
#endif
#if !defined(OS_ANDROID)
KeepAliveRegistry::GetInstance()->SetIsShuttingDown(false);
#endif
}
void TestingBrowserProcess::FlushLocalStateAndReply(base::OnceClosure reply) {
// This could be implemented the same way as in BrowserProcessImpl but it's
// not currently expected to be used by TestingBrowserProcess users so we
......
......@@ -163,9 +163,11 @@ class TestingBrowserProcess : public BrowserProcess {
TestingBrowserProcess();
~TestingBrowserProcess() override;
void Init();
std::unique_ptr<content::NotificationService> notification_service_;
std::string app_locale_;
bool is_shutting_down_;
bool is_shutting_down_ = false;
std::unique_ptr<policy::ChromeBrowserPolicyConnector>
browser_policy_connector_;
......@@ -198,9 +200,9 @@ class TestingBrowserProcess : public BrowserProcess {
std::unique_ptr<network_time::NetworkTimeTracker> network_time_tracker_;
// The following objects are not owned by TestingBrowserProcess:
PrefService* local_state_;
PrefService* local_state_ = nullptr;
scoped_refptr<network::SharedURLLoaderFactory> shared_url_loader_factory_;
rappor::RapporServiceImpl* rappor_service_;
rappor::RapporServiceImpl* rappor_service_ = nullptr;
std::unique_ptr<BrowserProcessPlatformPart> platform_part_;
std::unique_ptr<network::TestNetworkConnectionTracker>
......
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