1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/js_injection/browser/navigation_web_message_sender.h"
#include "base/debug/crash_logging.h"
#include "base/debug/dump_without_crashing.h"
#include "base/json/json_writer.h"
#include "base/no_destructor.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "components/back_forward_cache/back_forward_cache_disable.h"
#include "components/back_forward_cache/disabled_reason_id.h"
#include "components/js_injection/browser/web_message.h"
#include "components/js_injection/browser/web_message_host.h"
#include "components/js_injection/browser/web_message_host_factory.h"
#include "components/js_injection/browser/web_message_reply_proxy.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/page.h"
#include "content/public/browser/restore_type.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/url_constants.h"
#include "net/http/http_response_headers.h"
namespace {
// All navigations that are happening on the primary main frame.
std::set<int64_t>& GetOngoingPrimaryMainFrameNavigationIds() {
static base::NoDestructor<std::set<int64_t>> tracked_ongoing_navigation_ids;
return *tracked_ongoing_navigation_ids;
}
// All navigations, including those happening not on the primary main frame.
std::set<int64_t>& GetAllOngoingNavigationIds() {
static base::NoDestructor<std::set<int64_t>> all_ongoing_navigation_ids;
return *all_ongoing_navigation_ids;
}
base::Value::Dict CreateBaseMessageFromNavigationHandle(
content::NavigationHandle* navigation_handle) {
bool is_history = (navigation_handle->IsHistory());
return base::Value::Dict()
.Set("id", base::NumberToString(navigation_handle->GetNavigationId()))
.Set("url", navigation_handle->GetURL().spec())
.Set("isSameDocument", navigation_handle->IsSameDocument())
.Set("isPageInitiated", navigation_handle->IsRendererInitiated())
.Set("isReload",
navigation_handle->GetReloadType() != content::ReloadType::NONE)
.Set("isHistory", is_history)
.Set("isBack",
is_history && navigation_handle->GetNavigationEntryOffset() < 0)
.Set("isForward",
is_history && navigation_handle->GetNavigationEntryOffset() > 0)
.Set("isRestore", navigation_handle->GetRestoreType() ==
content::RestoreType::kRestored);
}
std::string GetURLTypeForCrashKey(const GURL& url) {
if (url == content::kUnreachableWebDataURL) {
return "error";
}
if (url == content::kBlockedURL) {
return "blocked";
}
if (url.IsAboutBlank()) {
return "about:blank";
}
if (url.IsAboutSrcdoc()) {
return "about:srcdoc";
}
if (url.is_empty()) {
return "empty";
}
if (!url.is_valid()) {
return "invalid";
}
return url.scheme();
}
void CheckNavigationIsInPrimaryOngoingList(
content::NavigationHandle* navigation_handle,
std::string message_type) {
if (GetOngoingPrimaryMainFrameNavigationIds().contains(
navigation_handle->GetNavigationId())) {
return;
}
SCOPED_CRASH_KEY_STRING256("NoTrackedNav", "message", message_type);
SCOPED_CRASH_KEY_NUMBER("NoTrackedNav", "nav_id",
navigation_handle->GetNavigationId());
SCOPED_CRASH_KEY_STRING32("NoTrackedNav", "url_type",
GetURLTypeForCrashKey(navigation_handle->GetURL()));
GURL prev_url = (navigation_handle->HasCommitted()
? navigation_handle->GetPreviousPrimaryMainFrameURL()
: navigation_handle->GetWebContents()
->GetPrimaryMainFrame()
->GetLastCommittedURL());
SCOPED_CRASH_KEY_STRING32("NoTrackedNav", "prev_url_type",
GetURLTypeForCrashKey(prev_url));
std::optional<content::NavigationDiscardReason> discard_reason =
navigation_handle->GetNavigationDiscardReason();
SCOPED_CRASH_KEY_NUMBER("NoTrackedNav", "discard_reason",
discard_reason.has_value()
? static_cast<int>(discard_reason.value())
: -1);
SCOPED_CRASH_KEY_NUMBER("NoTrackedNav", "primary_navs_size",
GetOngoingPrimaryMainFrameNavigationIds().size());
SCOPED_CRASH_KEY_NUMBER("NoTrackedNav", "all_navs_size",
GetAllOngoingNavigationIds().size());
SCOPED_CRASH_KEY_NUMBER("NoTrackedNav", "net_error_code",
navigation_handle->GetNetErrorCode());
SCOPED_CRASH_KEY_BOOL("NoTrackedNav", "has_committed",
navigation_handle->HasCommitted());
SCOPED_CRASH_KEY_BOOL("NoTrackedNav", "was_redirect",
navigation_handle->WasServerRedirect());
SCOPED_CRASH_KEY_BOOL("NoTrackedNav", "is_activation",
navigation_handle->IsPageActivation());
SCOPED_CRASH_KEY_BOOL("NoTrackedNav", "is_same_doc",
navigation_handle->IsSameDocument());
SCOPED_CRASH_KEY_BOOL("NoTrackedNav", "is_renderer",
navigation_handle->IsRendererInitiated());
SCOPED_CRASH_KEY_BOOL("NoTrackedNav", "is_history",
navigation_handle->IsHistory());
SCOPED_CRASH_KEY_BOOL(
"NoTrackedNav", "is_reload",
navigation_handle->GetReloadType() != content::ReloadType::NONE);
SCOPED_CRASH_KEY_BOOL(
"NoTrackedNav", "is_restore",
navigation_handle->GetRestoreType() == content::RestoreType::kRestored);
base::debug::DumpWithoutCrashing();
}
} // namespace
namespace features {
// Temporarily enable this feature while we work on moving the API to AndroidX.
// This should be safe as it is very unlikely for apps to accidentally register
// listeners with the same name as `kNavigationListenerAllowBFCacheObjectName` /
// `kNavigationListenerDisableBFCacheObjectName`.
BASE_FEATURE(kEnableNavigationListener,
"EnableNavigationListener",
base::FEATURE_ENABLED_BY_DEFAULT);
} // namespace features
namespace js_injection {
// An empty WebMessageReplyProxy used as a placeholder as there is no
// connection to the renderer. This object is 1:1 with the Page, so it
// can be used by the app to identify which Page an injected navigation
// message is associated with.
class EmptyReplyProxy : public WebMessageReplyProxy {
public:
explicit EmptyReplyProxy(content::Page& page) : page_(&page) {}
EmptyReplyProxy(const EmptyReplyProxy&) = delete;
EmptyReplyProxy& operator=(const EmptyReplyProxy&) = delete;
~EmptyReplyProxy() override = default;
// WebMessageReplyProxy:
void PostWebMessage(blink::WebMessagePayload message) override {
// Do nothing as there is no connection to the renderer.
}
content::Page& GetPage() override { return *page_; }
private:
raw_ptr<content::Page> page_;
};
const char16_t
NavigationWebMessageSender::kNavigationListenerAllowBFCacheObjectName[] =
u"experimentalWebViewNavigationListenerAllowBFCache";
const char16_t
NavigationWebMessageSender::kNavigationListenerDisableBFCacheObjectName[] =
u"experimentalWebViewNavigationListenerDisableBFCache";
const char NavigationWebMessageSender::kOptedInMessage[] =
"NAVIGATION_MESSAGE_OPTED_IN";
const char NavigationWebMessageSender::kNavigationStartedMessage[] =
"NAVIGATION_STARTED";
const char NavigationWebMessageSender::kNavigationRedirectedMessage[] =
"NAVIGATION_REDIRECTED";
const char NavigationWebMessageSender::kNavigationCompletedMessage[] =
"NAVIGATION_COMPLETED";
const char NavigationWebMessageSender::kPageLoadEndMessage[] = "PAGE_LOAD_END";
const char NavigationWebMessageSender::kDOMContentLoadedMessage[] =
"DOM_CONTENT_LOADED";
const char NavigationWebMessageSender::kFirstContentfulPaintMessage[] =
"FIRST_CONTENTFUL_PAINT";
const char NavigationWebMessageSender::kPageDeletedMessage[] = "PAGE_DELETED";
// static
bool NavigationWebMessageSender::IsNavigationListener(
const std::u16string& js_object_name) {
return base::FeatureList::IsEnabled(features::kEnableNavigationListener) &&
((js_object_name == kNavigationListenerAllowBFCacheObjectName) ||
(js_object_name == kNavigationListenerDisableBFCacheObjectName));
}
// static
void NavigationWebMessageSender::CreateForPageIfNeeded(
content::Page& page,
const std::u16string& js_object_name,
WebMessageHostFactory* factory) {
if (!IsNavigationListener(js_object_name)) {
return;
}
NavigationWebMessageSender::CreateForPage(page, js_object_name, factory);
}
NavigationWebMessageSender::NavigationWebMessageSender(
content::Page& page,
const std::u16string& js_object_name,
WebMessageHostFactory* factory)
: content::PageUserData<NavigationWebMessageSender>(page),
content::WebContentsObserver(
content::WebContents::FromRenderFrameHost(&page.GetMainDocument())) {
CHECK(base::FeatureList::IsEnabled(features::kEnableNavigationListener));
CHECK(page.IsPrimary());
const std::string origin_string =
page.GetMainDocument().GetLastCommittedOrigin().Serialize();
reply_proxy_ = std::make_unique<EmptyReplyProxy>(page);
host_ = factory->CreateHost(origin_string, origin_string,
/*is_main_frame=*/true, reply_proxy_.get());
if (js_object_name == kNavigationListenerDisableBFCacheObjectName) {
// Prevent this page to enter BFCache. See the comment for the definition of
// `kNavigationListenerDisableBFCacheObjectName` for why we want to do this.
content::BackForwardCache::DisableForRenderFrameHost(
&page.GetMainDocument(),
back_forward_cache::DisabledReason(
back_forward_cache::DisabledReasonId::kRequestedByWebViewClient));
}
}
NavigationWebMessageSender::~NavigationWebMessageSender() {
CHECK(!page().GetMainDocument().IsInLifecycleState(
content::RenderFrameHost::LifecycleState::kPendingCommit));
PostMessageWithType(kPageDeletedMessage);
}
void NavigationWebMessageSender::DispatchOptInMessage() {
CHECK(page().IsPrimary());
base::Value::Dict message_dict =
base::Value::Dict()
.Set("type", kOptedInMessage)
.Set("supports_start_and_redirect", true)
.Set("supports_history_details", true)
.Set("supports_dom_content_loaded", true)
.Set("supports_first_contentful_paint", true);
PostMessage(std::move(message_dict));
}
void NavigationWebMessageSender::DOMContentLoaded(
content::RenderFrameHost* render_frame_host) {
if (!ShouldSendMessageForRenderFrameHost(render_frame_host)) {
return;
}
PostMessageWithType(kDOMContentLoadedMessage);
}
void NavigationWebMessageSender::DidFinishLoad(
content::RenderFrameHost* render_frame_host,
const GURL& validated_url) {
if (!ShouldSendMessageForRenderFrameHost(render_frame_host)) {
return;
}
PostMessageWithType(kPageLoadEndMessage);
}
void NavigationWebMessageSender::OnFirstContentfulPaintInPrimaryMainFrame() {
if (!page().IsPrimary()) {
return;
}
PostMessageWithType(kFirstContentfulPaintMessage);
}
bool NavigationWebMessageSender::ShouldSendMessageForNavigation(
content::NavigationHandle* navigation_handle) {
// Only send navigation notifications for primary pages, and only from the
// associated NavigationWebMessageSender. Note that since
// `IsInPrimaryMainFrame()` can also be true when the navigation didn't
// commit / create a new page, it means that the messages for those
// navigations will be fired on the sender of the current primary page.
return page().IsPrimary() && navigation_handle->IsInPrimaryMainFrame();
}
bool NavigationWebMessageSender::ShouldSendMessageForRenderFrameHost(
content::RenderFrameHost* render_frame_host) {
return page().IsPrimary() && render_frame_host == &page().GetMainDocument();
}
void NavigationWebMessageSender::DidStartNavigation(
content::NavigationHandle* navigation_handle) {
if (page().IsPrimary()) {
// Add `navigation_handle` to the list of all ongoing navigations. Note that
// we only call this if the NavigationWebMessageSender is for the primary
// page, to ensure we only add it to the list once. The navigation itself
// might not be on the primary page, but it doesn't matter, since this list
// wants to capture all navigations.
GetAllOngoingNavigationIds().insert(navigation_handle->GetNavigationId());
}
if (!ShouldSendMessageForNavigation(navigation_handle)) {
return;
}
// Add `navigation_handle` to the list of ongoing primary main frame
// navigations.
GetOngoingPrimaryMainFrameNavigationIds().insert(
navigation_handle->GetNavigationId());
base::Value::Dict message_dict =
CreateBaseMessageFromNavigationHandle(navigation_handle)
.Set("type", kNavigationStartedMessage);
PostMessage(std::move(message_dict));
}
void NavigationWebMessageSender::DidRedirectNavigation(
content::NavigationHandle* navigation_handle) {
if (!ShouldSendMessageForNavigation(navigation_handle)) {
return;
}
CheckNavigationIsInPrimaryOngoingList(navigation_handle,
kNavigationRedirectedMessage);
base::Value::Dict message_dict =
CreateBaseMessageFromNavigationHandle(navigation_handle)
.Set("type", kNavigationRedirectedMessage);
PostMessage(std::move(message_dict));
}
void NavigationWebMessageSender::DidFinishNavigation(
content::NavigationHandle* navigation_handle) {
if (page().IsPrimary()) {
// Remove `navigation_handle` to the list of all ongoing navigations. Note
// that we only call this if the NavigationWebMessageSender is for the
// primary page, to ensure we only remove it from the list once.
GetAllOngoingNavigationIds().erase(navigation_handle->GetNavigationId());
}
if (!ShouldSendMessageForNavigation(navigation_handle)) {
return;
}
CheckNavigationIsInPrimaryOngoingList(navigation_handle,
kNavigationCompletedMessage);
GetOngoingPrimaryMainFrameNavigationIds().erase(
navigation_handle->GetNavigationId());
base::Value::Dict message_dict =
CreateBaseMessageFromNavigationHandle(navigation_handle)
.Set("type", kNavigationCompletedMessage)
.Set("isErrorPage", navigation_handle->IsErrorPage())
.Set("committed", navigation_handle->HasCommitted())
// Some navigations don't have HTTP responses. Default to 200 for
// those cases.
.Set("statusCode",
navigation_handle->GetResponseHeaders()
? navigation_handle->GetResponseHeaders()->response_code()
: 200);
PostMessage(std::move(message_dict));
}
std::unique_ptr<WebMessage> NavigationWebMessageSender::CreateWebMessage(
base::Value::Dict message_dict) {
base::Value message(std::move(message_dict));
std::string json_message;
base::JSONWriter::Write(message, &json_message);
std::unique_ptr<WebMessage> web_message = std::make_unique<WebMessage>();
web_message->message = base::UTF8ToUTF16(json_message);
return web_message;
}
void NavigationWebMessageSender::PostMessageWithType(std::string_view type) {
base::Value::Dict message_dict;
message_dict.Set("type", type);
PostMessage(std::move(message_dict));
}
void NavigationWebMessageSender::PostMessage(base::Value::Dict message_dict) {
host_->OnPostMessage(CreateWebMessage(std::move(message_dict)));
}
PAGE_USER_DATA_KEY_IMPL(NavigationWebMessageSender);
} // namespace js_injection
|