From a87c85e91d2b3b93b421cfcd19f1d943dd99738f Mon Sep 17 00:00:00 2001 From: esmael Date: Wed, 15 Jan 2025 12:12:29 -0800 Subject: [PATCH] Add top-level Cobalt WebContentsObserver. b/384979128 --- cobalt/BUILD.gn | 2 ++ cobalt/android/BUILD.gn | 2 ++ cobalt/cobalt_content_browser_client.cc | 5 ++++ cobalt/cobalt_content_browser_client.h | 6 +++++ cobalt/cobalt_web_contents_observer.cc | 24 ++++++++++++++++++ cobalt/cobalt_web_contents_observer.h | 33 +++++++++++++++++++++++++ 6 files changed, 72 insertions(+) create mode 100644 cobalt/cobalt_web_contents_observer.cc create mode 100644 cobalt/cobalt_web_contents_observer.h diff --git a/cobalt/BUILD.gn b/cobalt/BUILD.gn index b51b6ad8ac3d..8da714a12e42 100644 --- a/cobalt/BUILD.gn +++ b/cobalt/BUILD.gn @@ -40,6 +40,8 @@ if (!is_android) { "cobalt_content_browser_client.h", "cobalt_main_delegate.cc", "cobalt_main_delegate.h", + "cobalt_web_contents_observer.cc", + "cobalt_web_contents_observer.h", ] defines = [] diff --git a/cobalt/android/BUILD.gn b/cobalt/android/BUILD.gn index e946a2cdca66..e598c9459519 100644 --- a/cobalt/android/BUILD.gn +++ b/cobalt/android/BUILD.gn @@ -268,6 +268,8 @@ shared_library("libcobalt_content_shell_content_view") { "//cobalt/cobalt_content_browser_client.h", "//cobalt/cobalt_main_delegate.cc", "//cobalt/cobalt_main_delegate.h", + "//cobalt/cobalt_web_contents_observer.cc", + "//cobalt/cobalt_web_contents_observer.h", "cobalt_library_loader.cc", ] configs -= [ "//build/config/android:hide_all_but_jni_onload" ] diff --git a/cobalt/cobalt_content_browser_client.cc b/cobalt/cobalt_content_browser_client.cc index 8b612b359872..9e9dd549888e 100644 --- a/cobalt/cobalt_content_browser_client.cc +++ b/cobalt/cobalt_content_browser_client.cc @@ -91,4 +91,9 @@ void CobaltContentBrowserClient::OverrideWebkitPrefs( content::ShellContentBrowserClient::OverrideWebkitPrefs(web_contents, prefs); } +void CobaltContentBrowserClient::OnWebContentsCreated( + content::WebContents* web_contents) { + web_contents_observer_.reset(new CobaltWebContentsObserver(web_contents)); +} + } // namespace cobalt diff --git a/cobalt/cobalt_content_browser_client.h b/cobalt/cobalt_content_browser_client.h index c98af65f29bc..80170477dee5 100644 --- a/cobalt/cobalt_content_browser_client.h +++ b/cobalt/cobalt_content_browser_client.h @@ -15,6 +15,8 @@ #ifndef COBALT_COBALT_CONTENT_BROWSER_CLIENT_H_ #define COBALT_COBALT_CONTENT_BROWSER_CLIENT_H_ +#include "cobalt/cobalt_web_contents_observer.h" +#include "content/public/browser/web_contents.h" #include "content/shell/browser/shell_content_browser_client.h" namespace cobalt { @@ -31,6 +33,10 @@ class CobaltContentBrowserClient : public content::ShellContentBrowserClient { blink::UserAgentMetadata GetUserAgentMetadata() override; void OverrideWebkitPrefs(content::WebContents* web_contents, blink::web_pref::WebPreferences* prefs) override; + void OnWebContentsCreated(content::WebContents* web_contents); + + private: + std::unique_ptr web_contents_observer_; }; } // namespace cobalt diff --git a/cobalt/cobalt_web_contents_observer.cc b/cobalt/cobalt_web_contents_observer.cc new file mode 100644 index 000000000000..e7b18bf0fab6 --- /dev/null +++ b/cobalt/cobalt_web_contents_observer.cc @@ -0,0 +1,24 @@ +// Copyright 2025 The Cobalt Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "cobalt/cobalt_web_contents_observer.h" + +namespace cobalt { + +// Placeholder for a WebContentsObserver override +void CobaltWebContentsObserver::PrimaryMainDocumentElementAvailable() { + LOG(INFO) << "Cobalt::PrimaryMainDocumentElementAvailable"; +} + +} // namespace cobalt diff --git a/cobalt/cobalt_web_contents_observer.h b/cobalt/cobalt_web_contents_observer.h new file mode 100644 index 000000000000..82f34a4d627c --- /dev/null +++ b/cobalt/cobalt_web_contents_observer.h @@ -0,0 +1,33 @@ +// Copyright 2025 The Cobalt Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef COBALT_COBALT_WEB_CONTENTS_OBSERVER_H_ +#define COBALT_COBALT_WEB_CONTENTS_OBSERVER_H_ + +#include "content/public/browser/web_contents_observer.h" +#include "content/shell/browser/shell_content_browser_client.h" + +namespace cobalt { + +class CobaltWebContentsObserver : public content::WebContentsObserver { + public: + explicit CobaltWebContentsObserver(content::WebContents* web_contents) + : content::WebContentsObserver(web_contents) {} + + void PrimaryMainDocumentElementAvailable() override; +}; + +} // namespace cobalt + +#endif // COBALT_COBALT_WEB_CONTENTS_OBSERVER_H_