Skip to content

Commit

Permalink
Add top-level Cobalt WebContentsObserver. (#4699)
Browse files Browse the repository at this point in the history
b/384979128
  • Loading branch information
aee-google authored Jan 15, 2025
1 parent b597c1c commit d08c8cd
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cobalt/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
2 changes: 2 additions & 0 deletions cobalt/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]
Expand Down
5 changes: 5 additions & 0 deletions cobalt/cobalt_content_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions cobalt/cobalt_content_browser_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<CobaltWebContentsObserver> web_contents_observer_;
};

} // namespace cobalt
Expand Down
24 changes: 24 additions & 0 deletions cobalt/cobalt_web_contents_observer.cc
Original file line number Diff line number Diff line change
@@ -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
33 changes: 33 additions & 0 deletions cobalt/cobalt_web_contents_observer.h
Original file line number Diff line number Diff line change
@@ -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_

0 comments on commit d08c8cd

Please sign in to comment.