Skip to content

Commit

Permalink
[Extensions] Create WebContentsObservers with ExtensionsBrowserClient
Browse files Browse the repository at this point in the history
There are different variations of the ExtensionWebContentsObserver based
on the embedder (i.e., one for Shell and one for Chrome). Instead of
having individual callers know about this distinction, instantiate these
WebContentsObservers through the ExtensionsBrowserClient by introducing
ExtensionsBrowserClient::CreateExtensionWebContentsObserver(). The
ExtensionsBrowserClient already had knowledge of these for the
related GetExtensionWebContentsObserver() method, and this allows more
code to be shared across different build configurations.

This CL should have no behavior change.

Bug: 356905053
Change-Id: I75acd6cb2a0a151d193c596d97509c5b26700910
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5825636
Reviewed-by: Lei Zhang <[email protected]>
Reviewed-by: David Bertoni <[email protected]>
Commit-Queue: Devlin Cronin <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1350962}
  • Loading branch information
rdcronin authored and Chromium LUCI CQ committed Sep 4, 2024
1 parent 0604e74 commit f9ffc4e
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 8 deletions.
9 changes: 4 additions & 5 deletions chrome/browser/chrome_content_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,7 @@
#include "extensions/browser/extension_protocols.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_util.h"
#include "extensions/browser/extension_web_contents_observer.h"
#include "extensions/browser/process_map.h"
#include "extensions/browser/script_injection_tracker.h"
#include "extensions/common/constants.h"
Expand All @@ -681,7 +682,6 @@

#if BUILDFLAG(ENABLE_EXTENSIONS)
#include "chrome/browser/accessibility/animation_policy_prefs.h"
#include "chrome/browser/extensions/chrome_extension_web_contents_observer.h"
#include "chrome/browser/extensions/extension_util.h"
#include "chrome/browser/extensions/user_script_listener.h"
#include "chrome/browser/speech/extension_api/tts_engine_extension_api.h"
Expand Down Expand Up @@ -6523,11 +6523,10 @@ void AddChromeSchemeFactories(
/*allowed_webui_hosts=*/base::flat_set<std::string>()));
}

extensions::ChromeExtensionWebContentsObserver* web_observer =
extensions::ChromeExtensionWebContentsObserver::FromWebContents(
web_contents);
extensions::ExtensionWebContentsObserver* web_observer =
extensions::ExtensionWebContentsObserver::GetForWebContents(web_contents);

// There is nothing to do if no ChromeExtensionWebContentsObserver is attached
// There is nothing to do if no ExtensionWebContentsObserver is attached
// to the |web_contents| or no enabled extension exists.
if (!web_observer || !extension)
return;
Expand Down
5 changes: 5 additions & 0 deletions chrome/browser/extensions/chrome_extensions_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,11 @@ bool ChromeExtensionsBrowserClient::IsMinBrowserVersionSupported(
browser_min_version.CompareTo(browser_version) <= 0;
}

void ChromeExtensionsBrowserClient::CreateExtensionWebContentsObserver(
content::WebContents* web_contents) {
ChromeExtensionWebContentsObserver::CreateForWebContents(web_contents);
}

ExtensionWebContentsObserver*
ChromeExtensionsBrowserClient::GetExtensionWebContentsObserver(
content::WebContents* web_contents) {
Expand Down
2 changes: 2 additions & 0 deletions chrome/browser/extensions/chrome_extensions_browser_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ class ChromeExtensionsBrowserClient : public ExtensionsBrowserClient {
ExtensionCache* GetExtensionCache() override;
bool IsBackgroundUpdateAllowed() override;
bool IsMinBrowserVersionSupported(const std::string& min_version) override;
void CreateExtensionWebContentsObserver(
content::WebContents* web_contents) override;
ExtensionWebContentsObserver* GetExtensionWebContentsObserver(
content::WebContents* web_contents) override;
void ReportError(content::BrowserContext* context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,9 @@ bool DesktopAndroidExtensionsBrowserClient::IsMinBrowserVersionSupported(
return true;
}

void DesktopAndroidExtensionsBrowserClient::CreateExtensionWebContentsObserver(
content::WebContents* web_contents) {}

ExtensionWebContentsObserver*
DesktopAndroidExtensionsBrowserClient::GetExtensionWebContentsObserver(
content::WebContents* web_contents) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ class DesktopAndroidExtensionsBrowserClient : public ExtensionsBrowserClient {
ExtensionCache* GetExtensionCache() override;
bool IsBackgroundUpdateAllowed() override;
bool IsMinBrowserVersionSupported(const std::string& min_version) override;
void CreateExtensionWebContentsObserver(
content::WebContents* web_contents) override;
ExtensionWebContentsObserver* GetExtensionWebContentsObserver(
content::WebContents* web_contents) override;
KioskDelegate* GetKioskDelegate() override;
Expand Down
7 changes: 4 additions & 3 deletions chrome/browser/universal_web_contents_observers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

#include "chrome/browser/extensions/chrome_content_browser_client_extensions_part.h"
#include "components/performance_manager/embedder/performance_manager_registry.h"
#include "content/public/browser/web_contents.h"
#include "extensions/buildflags/buildflags.h"

#if BUILDFLAG(ENABLE_EXTENSIONS)
#include "chrome/browser/extensions/chrome_extension_web_contents_observer.h"
#include "extensions/browser/extensions_browser_client.h"
#endif

void AttachUniversalWebContentsObservers(content::WebContents* web_contents) {
Expand All @@ -29,8 +30,8 @@ void AttachUniversalWebContentsObservers(content::WebContents* web_contents) {
return;
}

extensions::ChromeExtensionWebContentsObserver::CreateForWebContents(
web_contents);
extensions::ExtensionsBrowserClient::Get()
->CreateExtensionWebContentsObserver(web_contents);
#endif

if (auto* pm_registry =
Expand Down
5 changes: 5 additions & 0 deletions extensions/browser/extensions_browser_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,11 @@ class ExtensionsBrowserClient {
virtual void ReportError(content::BrowserContext* context,
std::unique_ptr<ExtensionError> error);

// Creates a new instance of an ExtensionWebContentsObserver and attaches it
// to the given `web_contents`.
virtual void CreateExtensionWebContentsObserver(
content::WebContents* web_contents) = 0;

// Returns the ExtensionWebContentsObserver for the given |web_contents|.
virtual ExtensionWebContentsObserver* GetExtensionWebContentsObserver(
content::WebContents* web_contents) = 0;
Expand Down
3 changes: 3 additions & 0 deletions extensions/browser/test_extensions_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,9 @@ bool TestExtensionsBrowserClient::IsMinBrowserVersionSupported(
return true;
}

void TestExtensionsBrowserClient::CreateExtensionWebContentsObserver(
content::WebContents* web_contents) {}

ExtensionWebContentsObserver*
TestExtensionsBrowserClient::GetExtensionWebContentsObserver(
content::WebContents* web_contents) {
Expand Down
2 changes: 2 additions & 0 deletions extensions/browser/test_extensions_browser_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ class TestExtensionsBrowserClient : public ExtensionsBrowserClient {
ExtensionCache* GetExtensionCache() override;
bool IsBackgroundUpdateAllowed() override;
bool IsMinBrowserVersionSupported(const std::string& min_version) override;
void CreateExtensionWebContentsObserver(
content::WebContents* web_contents) override;
ExtensionWebContentsObserver* GetExtensionWebContentsObserver(
content::WebContents* web_contents) override;
KioskDelegate* GetKioskDelegate() override;
Expand Down
5 changes: 5 additions & 0 deletions extensions/shell/browser/shell_extensions_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,11 @@ void ShellExtensionsBrowserClient::SetAPIClientForTest(
api_client_.reset(api_client);
}

void ShellExtensionsBrowserClient::CreateExtensionWebContentsObserver(
content::WebContents* web_contents) {
ShellExtensionWebContentsObserver::CreateForWebContents(web_contents);
}

ExtensionWebContentsObserver*
ShellExtensionsBrowserClient::GetExtensionWebContentsObserver(
content::WebContents* web_contents) {
Expand Down
2 changes: 2 additions & 0 deletions extensions/shell/browser/shell_extensions_browser_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ class ShellExtensionsBrowserClient : public ExtensionsBrowserClient {
ExtensionCache* GetExtensionCache() override;
bool IsBackgroundUpdateAllowed() override;
bool IsMinBrowserVersionSupported(const std::string& min_version) override;
void CreateExtensionWebContentsObserver(
content::WebContents* web_contents) override;
ExtensionWebContentsObserver* GetExtensionWebContentsObserver(
content::WebContents* web_contents) override;
KioskDelegate* GetKioskDelegate() override;
Expand Down

0 comments on commit f9ffc4e

Please sign in to comment.