#include "chrome/browser/extensions/api/debugger/debugger_api.h"
#include <stddef.h>
#include <map>
#include <memory>
#include <optional>
#include <set>
#include <string_view>
#include <utility>
#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/lazy_instance.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/singleton.h"
#include "base/metrics/histogram_functions.h"
#include "base/ranges/algorithm.h"
#include "base/scoped_observation.h"
#include "base/strings/string_number_conversions.h"
#include "base/types/optional_util.h"
#include "base/values.h"
#include "chrome/browser/devtools/chrome_devtools_manager_delegate.h"
#include "chrome/browser/extensions/api/debugger/extension_dev_tools_infobar_delegate.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_tab_util.h"
#include "chrome/browser/lifetime/termination_notification.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_observer.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "components/security_interstitials/content/security_interstitial_tab_helper.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/devtools_agent_host.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_widget_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_client.h"
#include "content/public/common/url_constants.h"
#include "content/public/common/url_utils.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/extension_host.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_registry_observer.h"
#include "extensions/browser/extension_util.h"
#include "extensions/browser/guest_view/mime_handler_view/mime_handler_view_guest.h"
#include "extensions/common/constants.h"
#include "extensions/common/error_utils.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_id.h"
#include "extensions/common/manifest_constants.h"
#include "extensions/common/permissions/permissions_data.h"
#include "extensions/common/switches.h"
#include "pdf/buildflags.h"
#include "url/origin.h"
#if BUILDFLAG(ENABLE_PDF)
#include "components/pdf/common/pdf_util.h"
#include "pdf/pdf_features.h"
#endif
DevToolsAgentHost;
RenderProcessHost;
RenderWidgetHost;
WebContents;
Attach;
Detach;
OnDetach;
OnEvent;
SendCommand;
namespace extensions {
class ExtensionRegistry;
class ExtensionDevToolsClientHost;
namespace {
constexpr char kAlreadyAttachedError[] = …;
constexpr char kNoTargetError[] = …;
constexpr char kInvalidTargetError[] = …;
constexpr char kNotAttachedError[] = …;
constexpr char kProtocolVersionNotSupportedError[] = …;
constexpr char kRestrictedError[] = …;
constexpr char kDetachedWhileHandlingError[] = …;
constexpr char kTabTargetType[] = …;
constexpr char kBackgroundPageTargetType[] = …;
constexpr char kOpaqueTargetType[] = …;
void CopyDebuggee(Debuggee* dst, const Debuggee& src) { … }
void DebuggerSessionFromDebugee(DebuggerSession& dst,
const Debuggee& src,
std::string* maybe_session_id) { … }
void DebuggeeFromDebuggerSession(Debuggee& dst, const DebuggerSession& src) { … }
#if BUILDFLAG(ENABLE_PDF)
bool IsPdfExtensionUrl(const GURL& url) { … }
#endif
bool ExtensionMayAttachToTargetProfile(Profile* extension_profile,
bool allow_incognito_access,
DevToolsAgentHost& agent_host) { … }
bool ExtensionMayAttachToURL(const Extension& extension,
Profile* extension_profile,
const GURL& url,
std::string* error) { … }
bool ExtensionMayAttachToURLOrInnerURL(const Extension& extension,
Profile* extension_profile,
const GURL& url,
std::string* error) { … }
constexpr char kBrowserTargetId[] = …;
constexpr char kPerfettoUIExtensionId[] = …;
bool ExtensionIsTrusted(const Extension& extension) { … }
bool ExtensionMayAttachToRenderFrameHost(
const Extension& extension,
Profile* extension_profile,
content::RenderFrameHost* render_frame_host,
std::string* error) { … }
bool ExtensionMayAttachToWebContents(const Extension& extension,
Profile* extension_profile,
WebContents& web_contents,
std::string* error) { … }
bool ExtensionMayAttachToAgentHost(const Extension& extension,
bool allow_incognito_access,
Profile* extension_profile,
DevToolsAgentHost& agent_host,
std::string* error) { … }
}
AttachedClientHosts;
base::LazyInstance<AttachedClientHosts>::Leaky g_attached_client_hosts = …;
class ExtensionDevToolsClientHost : public content::DevToolsAgentHostClient,
public ExtensionRegistryObserver,
public ProfileObserver { … };
ExtensionDevToolsClientHost::ExtensionDevToolsClientHost(
Profile* profile,
DevToolsAgentHost* agent_host,
scoped_refptr<const Extension> extension,
std::optional<WorkerId> extension_service_worker_id,
const Debuggee& debuggee)
: … { … }
bool ExtensionDevToolsClientHost::Attach() { … }
ExtensionDevToolsClientHost::~ExtensionDevToolsClientHost() { … }
void ExtensionDevToolsClientHost::AgentHostClosed(
DevToolsAgentHost* agent_host) { … }
void ExtensionDevToolsClientHost::Close() { … }
void ExtensionDevToolsClientHost::SendMessageToBackend(
DebuggerSendCommandFunction* function,
const std::string& method,
SendCommand::Params::CommandParams* command_params,
std::optional<std::string> session_id) { … }
void ExtensionDevToolsClientHost::InfoBarDestroyed() { … }
void ExtensionDevToolsClientHost::RespondDetachedToPendingRequests() { … }
void ExtensionDevToolsClientHost::SendDetachedEvent() { … }
void ExtensionDevToolsClientHost::OnProfileWillBeDestroyed(Profile* profile) { … }
void ExtensionDevToolsClientHost::OnExtensionUnloaded(
content::BrowserContext* browser_context,
const Extension* extension,
UnloadedExtensionReason reason) { … }
void ExtensionDevToolsClientHost::OnAppTerminating() { … }
void ExtensionDevToolsClientHost::DispatchProtocolMessage(
DevToolsAgentHost* agent_host,
base::span<const uint8_t> message) { … }
bool ExtensionDevToolsClientHost::MayAttachToRenderFrameHost(
content::RenderFrameHost* render_frame_host) { … }
bool ExtensionDevToolsClientHost::MayAttachToURL(const GURL& url,
bool is_webui) { … }
bool ExtensionDevToolsClientHost::IsTrusted() { … }
bool ExtensionDevToolsClientHost::MayReadLocalFiles() { … }
bool ExtensionDevToolsClientHost::MayWriteLocalFiles() { … }
std::optional<url::Origin>
ExtensionDevToolsClientHost::GetNavigationInitiatorOrigin() { … }
DebuggerFunction::DebuggerFunction() : … { … }
DebuggerFunction::~DebuggerFunction() = default;
std::string DebuggerFunction::FormatErrorMessage(const std::string& format) { … }
bool DebuggerFunction::InitAgentHost(std::string* error) { … }
bool DebuggerFunction::InitClientHost(std::string* error) { … }
ExtensionDevToolsClientHost* DebuggerFunction::FindClientHost() { … }
DebuggerAttachFunction::DebuggerAttachFunction() = default;
DebuggerAttachFunction::~DebuggerAttachFunction() = default;
ExtensionFunction::ResponseAction DebuggerAttachFunction::Run() { … }
DebuggerDetachFunction::DebuggerDetachFunction() = default;
DebuggerDetachFunction::~DebuggerDetachFunction() = default;
ExtensionFunction::ResponseAction DebuggerDetachFunction::Run() { … }
DebuggerSendCommandFunction::DebuggerSendCommandFunction() = default;
DebuggerSendCommandFunction::~DebuggerSendCommandFunction() = default;
ExtensionFunction::ResponseAction DebuggerSendCommandFunction::Run() { … }
void DebuggerSendCommandFunction::SendResponseBody(base::Value response) { … }
void DebuggerSendCommandFunction::SendDetachedError() { … }
namespace {
const char kTargetIdField[] = …;
const char kTargetTypeField[] = …;
const char kTargetTitleField[] = …;
const char kTargetAttachedField[] = …;
const char kTargetUrlField[] = …;
const char kTargetFaviconUrlField[] = …;
const char kTargetTabIdField[] = …;
const char kTargetExtensionIdField[] = …;
const char kTargetTypePage[] = …;
const char kTargetTypeBackgroundPage[] = …;
const char kTargetTypeWorker[] = …;
const char kTargetTypeOther[] = …;
base::Value::Dict SerializeTarget(scoped_refptr<DevToolsAgentHost> host) { … }
}
DebuggerGetTargetsFunction::DebuggerGetTargetsFunction() = default;
DebuggerGetTargetsFunction::~DebuggerGetTargetsFunction() = default;
ExtensionFunction::ResponseAction DebuggerGetTargetsFunction::Run() { … }
}