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 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/devtools/web_contents_devtools_agent_host.h"
#include "base/memory/raw_ptr.h"
#include "base/unguessable_token.h"
#include "content/browser/devtools/protocol/io_handler.h"
#include "content/browser/devtools/protocol/target_auto_attacher.h"
#include "content/browser/devtools/protocol/target_handler.h"
#include "content/browser/devtools/protocol/tracing_handler.h"
#include "content/browser/devtools/render_frame_devtools_agent_host.h"
#include "content/browser/renderer_host/frame_tree_node.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/common/content_features.h"
namespace content {
namespace {
using WebContentsDevToolsMap =
std::map<WebContents*, WebContentsDevToolsAgentHost*>;
base::LazyInstance<WebContentsDevToolsMap>::Leaky g_agent_host_instances =
LAZY_INSTANCE_INITIALIZER;
WebContentsDevToolsAgentHost* FindAgentHost(WebContents* wc) {
if (!g_agent_host_instances.IsCreated())
return nullptr;
auto it = g_agent_host_instances.Get().find(wc);
return it == g_agent_host_instances.Get().end() ? nullptr : it->second;
}
} // namespace
// static
scoped_refptr<DevToolsAgentHost> DevToolsAgentHost::GetForTab(WebContents* wc) {
return WebContentsDevToolsAgentHost::GetFor(wc);
}
// static
scoped_refptr<DevToolsAgentHost> DevToolsAgentHost::GetOrCreateForTab(
WebContents* wc) {
return WebContentsDevToolsAgentHost::GetOrCreateFor(wc);
}
class WebContentsDevToolsAgentHost::AutoAttacher
: public protocol::TargetAutoAttacher {
public:
AutoAttacher() = default;
void UpdateChildFrameTrees(bool update_target_info) {
if (!auto_attach())
return;
base::flat_set<scoped_refptr<DevToolsAgentHost>> pages =
UpdateAssociatedPages();
if (update_target_info) {
for (auto& page : pages)
DispatchTargetInfoChanged(page.get());
}
}
void WillInitiatePrerender(FrameTreeNode* ftn) {
if (!auto_attach())
return;
auto host = RenderFrameDevToolsAgentHost::GetOrCreateFor(ftn);
DispatchAutoAttach(host.get(), wait_for_debugger_on_start());
}
void SetWebContents(WebContents* wc) {
web_contents_ = wc;
UpdateAssociatedPages();
}
private:
void UpdateAutoAttach(base::OnceClosure callback) override {
UpdateAssociatedPages();
protocol::TargetAutoAttacher::UpdateAutoAttach(std::move(callback));
}
base::flat_set<scoped_refptr<DevToolsAgentHost>> UpdateAssociatedPages() {
base::flat_set<scoped_refptr<DevToolsAgentHost>> hosts;
if (auto_attach() && web_contents_) {
auto* rfh = static_cast<RenderFrameHostImpl*>(
web_contents_->GetPrimaryMainFrame());
web_contents_->ForEachRenderFrameHost(
[&hosts](RenderFrameHost* rfh) { AddFrame(hosts, rfh); });
// In case the primary main frame has been filtered out by some criteria
// in AddFrame(), ensure it's added.
if (!base::FeatureList::IsEnabled(features::kGuestViewMPArch)) {
hosts.insert(RenderFrameDevToolsAgentHost::GetOrCreateFor(
rfh->frame_tree_node()));
}
}
DispatchSetAttachedTargetsOfType(hosts, DevToolsAgentHost::kTypePage);
return hosts;
}
static void AddFrame(base::flat_set<scoped_refptr<DevToolsAgentHost>>& hosts,
RenderFrameHost* rfh) {
RenderFrameHostImpl* rfhi = static_cast<RenderFrameHostImpl*>(rfh);
// We do not expose cached hosts as separate targets for now.
if (rfhi->IsInBackForwardCache())
return;
FrameTreeNode* ftn = rfhi->frame_tree_node();
// We're interested only in main frames, with the exception of fenced frames
// and (MPArch-based) guests that are reported as regular subframes via
// FrameAutoAttacher.
if (!ftn->IsMainFrame())
return;
if (ftn->IsFencedFrameRoot())
return;
// Ignore other kinds of embedders such as GuestViews.
if (ftn->render_manager()->GetOuterDelegateNode()) {
return;
}
hosts.insert(RenderFrameDevToolsAgentHost::GetOrCreateFor(ftn));
}
raw_ptr<WebContents> web_contents_ = nullptr;
};
// static
WebContentsDevToolsAgentHost* WebContentsDevToolsAgentHost::GetFor(
WebContents* web_contents) {
return FindAgentHost(web_contents);
}
// static
WebContentsDevToolsAgentHost* WebContentsDevToolsAgentHost::GetOrCreateFor(
WebContents* web_contents) {
if (auto* host = FindAgentHost(web_contents))
return host;
return new WebContentsDevToolsAgentHost(web_contents);
}
// static
bool WebContentsDevToolsAgentHost::IsDebuggerAttached(
WebContents* web_contents) {
WebContentsDevToolsAgentHost* host = FindAgentHost(web_contents);
return host && host->IsAttached();
}
// static
void WebContentsDevToolsAgentHost::AddAllAgentHosts(
DevToolsAgentHost::List* result) {
for (WebContentsImpl* wc : WebContentsImpl::GetAllWebContents()) {
result->push_back(GetOrCreateFor(wc));
}
}
WebContentsDevToolsAgentHost::WebContentsDevToolsAgentHost(WebContents* wc)
: DevToolsAgentHostImpl(base::UnguessableToken::Create().ToString()),
auto_attacher_(std::make_unique<AutoAttacher>()) {
InnerAttach(wc);
NotifyCreated();
}
void WebContentsDevToolsAgentHost::InnerAttach(WebContents* wc) {
CHECK(!web_contents());
// With ConnectWebContents(), we may be attaching to a WC that has
// a different host created.
// TODO(caseq): find a better solution. See also a similar comment in
// RenderFrameDevToolsAgentHost::SetFrameTreeNode();
auto prev_entry = g_agent_host_instances.Get().find(wc);
if (prev_entry != g_agent_host_instances.Get().end()) {
CHECK_NE(prev_entry->second, this);
prev_entry->second->InnerDetach();
}
const bool inserted =
g_agent_host_instances.Get().insert(std::make_pair(wc, this)).second;
CHECK(inserted);
auto_attacher_->SetWebContents(wc);
Observe(wc);
// Once created, persist till underlying WC is detached, so that
// the target id is retained.
AddRef();
}
void WebContentsDevToolsAgentHost::InnerDetach() {
DCHECK_EQ(this, FindAgentHost(web_contents()));
auto_attacher_->SetWebContents(nullptr);
g_agent_host_instances.Get().erase(web_contents());
Observe(nullptr);
// We may or may not be destruced here, depending on embedders
// potentially retaining references.
Release();
}
void WebContentsDevToolsAgentHost::WillInitiatePrerender(FrameTreeNode* ftn) {
auto_attacher_->WillInitiatePrerender(ftn);
for (auto* tracing : protocol::TracingHandler::ForAgentHost(this)) {
tracing->WillInitiatePrerender(ftn);
}
}
void WebContentsDevToolsAgentHost::UpdateChildFrameTrees(
bool update_target_info) {
auto_attacher_->UpdateChildFrameTrees(update_target_info);
}
void WebContentsDevToolsAgentHost::InspectElement(RenderFrameHost* frame_host,
int x,
int y) {
if (auto host = GetOrCreatePrimaryFrameAgent()) {
host->InspectElement(frame_host, x, y);
}
}
WebContentsDevToolsAgentHost::~WebContentsDevToolsAgentHost() {
DCHECK(!web_contents());
}
void WebContentsDevToolsAgentHost::DisconnectWebContents() {
InnerDetach();
}
void WebContentsDevToolsAgentHost::ConnectWebContents(
WebContents* web_contents) {
InnerAttach(web_contents);
}
BrowserContext* WebContentsDevToolsAgentHost::GetBrowserContext() {
return web_contents()->GetBrowserContext();
}
WebContents* WebContentsDevToolsAgentHost::GetWebContents() {
return web_contents();
}
std::string WebContentsDevToolsAgentHost::GetParentId() {
return std::string();
}
std::string WebContentsDevToolsAgentHost::GetOpenerId() {
if (DevToolsAgentHost* host = GetPrimaryFrameAgent())
return host->GetOpenerId();
return "";
}
std::string WebContentsDevToolsAgentHost::GetOpenerFrameId() {
if (DevToolsAgentHost* host = GetPrimaryFrameAgent())
return host->GetOpenerFrameId();
return "";
}
bool WebContentsDevToolsAgentHost::CanAccessOpener() {
if (DevToolsAgentHost* host = GetPrimaryFrameAgent())
return host->CanAccessOpener();
return false;
}
std::string WebContentsDevToolsAgentHost::GetType() {
return kTypeTab;
}
std::string WebContentsDevToolsAgentHost::GetTitle() {
if (DevToolsAgentHost* host = GetPrimaryFrameAgent())
return host->GetTitle();
return "";
}
std::string WebContentsDevToolsAgentHost::GetDescription() {
if (DevToolsAgentHost* host = GetPrimaryFrameAgent())
return host->GetDescription();
return "";
}
GURL WebContentsDevToolsAgentHost::GetURL() {
if (DevToolsAgentHost* host = GetPrimaryFrameAgent())
return host->GetURL();
return GURL();
}
GURL WebContentsDevToolsAgentHost::GetFaviconURL() {
if (DevToolsAgentHost* host = GetPrimaryFrameAgent())
return host->GetFaviconURL();
return GURL();
}
bool WebContentsDevToolsAgentHost::Activate() {
if (auto host = GetOrCreatePrimaryFrameAgent()) {
return host->Activate();
}
return false;
}
void WebContentsDevToolsAgentHost::Reload() {
if (auto host = GetOrCreatePrimaryFrameAgent()) {
host->Reload();
}
}
bool WebContentsDevToolsAgentHost::Close() {
if (auto host = GetOrCreatePrimaryFrameAgent()) {
return host->Close();
}
return false;
}
base::TimeTicks WebContentsDevToolsAgentHost::GetLastActivityTime() {
if (DevToolsAgentHost* host = GetPrimaryFrameAgent())
return host->GetLastActivityTime();
return base::TimeTicks();
}
std::optional<network::CrossOriginEmbedderPolicy>
WebContentsDevToolsAgentHost::cross_origin_embedder_policy(
const std::string& id) {
NOTREACHED();
}
std::optional<network::CrossOriginOpenerPolicy>
WebContentsDevToolsAgentHost::cross_origin_opener_policy(
const std::string& id) {
NOTREACHED();
}
DevToolsAgentHostImpl* WebContentsDevToolsAgentHost::GetPrimaryFrameAgent() {
if (WebContents* wc = web_contents()) {
return RenderFrameDevToolsAgentHost::GetFor(
static_cast<RenderFrameHostImpl*>(wc->GetPrimaryMainFrame()));
}
return nullptr;
}
scoped_refptr<DevToolsAgentHost>
WebContentsDevToolsAgentHost::GetOrCreatePrimaryFrameAgent() {
if (WebContents* wc = web_contents()) {
return RenderFrameDevToolsAgentHost::GetOrCreateFor(
static_cast<WebContentsImpl*>(wc)->GetPrimaryFrameTree().root());
}
return nullptr;
}
void WebContentsDevToolsAgentHost::WebContentsDestroyed() {
auto retain_this = ForceDetachAllSessionsImpl();
InnerDetach();
}
void WebContentsDevToolsAgentHost::RenderFrameHostChanged(
RenderFrameHost* old_host,
RenderFrameHost* new_host) {
CHECK(web_contents());
if (new_host == web_contents()->GetPrimaryMainFrame()) {
std::ignore = RevalidateSessionAccess();
// `this` is invalid at this point!
}
}
void WebContentsDevToolsAgentHost::ReadyToCommitNavigation(
NavigationHandle* navigation_handle) {
CHECK(web_contents());
NavigationRequest* request = NavigationRequest::From(navigation_handle);
for (auto* tracing : protocol::TracingHandler::ForAgentHost(this)) {
tracing->ReadyToCommitNavigation(request);
}
}
void WebContentsDevToolsAgentHost::FrameDeleted(
FrameTreeNodeId frame_tree_node_id) {
for (auto* tracing : protocol::TracingHandler::ForAgentHost(this)) {
tracing->FrameDeleted(frame_tree_node_id);
}
}
// DevToolsAgentHostImpl overrides.
DevToolsSession::Mode WebContentsDevToolsAgentHost::GetSessionMode() {
return DevToolsSession::Mode::kSupportsTabTarget;
}
bool WebContentsDevToolsAgentHost::AttachSession(DevToolsSession* session) {
if (web_contents() && !RenderFrameDevToolsAgentHost::ShouldAllowSession(
web_contents()->GetPrimaryMainFrame(), session)) {
return false;
}
session->SetBrowserOnly(true);
const bool may_attach_to_browser = session->GetClient()->IsTrusted();
session->CreateAndAddHandler<protocol::TargetHandler>(
may_attach_to_browser
? protocol::TargetHandler::AccessMode::kRegular
: protocol::TargetHandler::AccessMode::kAutoAttachOnly,
GetId(), auto_attacher_.get(), session);
DevToolsSession* root_session = session->GetRootSession();
CHECK(root_session);
session->CreateAndAddHandler<protocol::IOHandler>(GetIOContext());
session->CreateAndAddHandler<protocol::TracingHandler>(this, GetIOContext(),
root_session);
return true;
}
protocol::TargetAutoAttacher* WebContentsDevToolsAgentHost::auto_attacher() {
DCHECK(auto_attacher_);
return auto_attacher_.get();
}
scoped_refptr<WebContentsDevToolsAgentHost>
WebContentsDevToolsAgentHost::RevalidateSessionAccess() {
scoped_refptr<WebContentsDevToolsAgentHost> retain_this(this);
WebContents* wc = web_contents();
if (!wc) {
return retain_this;
}
std::vector<DevToolsSession*> restricted_sessions;
for (DevToolsSession* session : sessions()) {
if (!RenderFrameDevToolsAgentHost::ShouldAllowSession(
wc->GetPrimaryMainFrame(), session)) {
restricted_sessions.push_back(session);
}
}
if (!restricted_sessions.empty()) {
ForceDetachRestrictedSessions(restricted_sessions);
}
return retain_this;
}
} // namespace content
|