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
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/shell/browser/shell_devtools_manager_delegate.h"
#include <stdint.h>
#include <vector>
#include "base/atomicops.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/macros.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/devtools_agent_host.h"
#include "content/public/browser/devtools_socket_factory.h"
#include "content/public/browser/favicon_status.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/url_constants.h"
#include "content/public/common/user_agent.h"
#include "content/shell/browser/shell.h"
#include "content/shell/browser/web_test/secondary_test_window_observer.h"
#include "content/shell/common/shell_content_client.h"
#include "content/shell/common/shell_switches.h"
#include "content/shell/grit/shell_resources.h"
#include "net/base/net_errors.h"
#include "net/log/net_log_source.h"
#include "net/socket/tcp_server_socket.h"
#include "ui/base/resource/resource_bundle.h"
#if !defined(OS_ANDROID)
#include "content/public/browser/devtools_frontend_host.h"
#endif
#if defined(OS_ANDROID)
#include "content/public/browser/android/devtools_auth.h"
#include "net/socket/unix_domain_server_socket_posix.h"
#endif
namespace content {
namespace {
const int kBackLog = 10;
base::subtle::Atomic32 g_last_used_port;
#if defined(OS_ANDROID)
class UnixDomainServerSocketFactory : public content::DevToolsSocketFactory {
public:
explicit UnixDomainServerSocketFactory(const std::string& socket_name)
: socket_name_(socket_name) {}
private:
// content::DevToolsSocketFactory.
std::unique_ptr<net::ServerSocket> CreateForHttpServer() override {
std::unique_ptr<net::UnixDomainServerSocket> socket(
new net::UnixDomainServerSocket(base::Bind(&CanUserConnectToDevTools),
true /* use_abstract_namespace */));
if (socket->BindAndListen(socket_name_, kBackLog) != net::OK)
return std::unique_ptr<net::ServerSocket>();
return std::move(socket);
}
std::unique_ptr<net::ServerSocket> CreateForTethering(
std::string* out_name) override {
return nullptr;
}
std::string socket_name_;
DISALLOW_COPY_AND_ASSIGN(UnixDomainServerSocketFactory);
};
#else
class TCPServerSocketFactory : public content::DevToolsSocketFactory {
public:
TCPServerSocketFactory(const std::string& address, uint16_t port)
: address_(address), port_(port) {}
private:
// content::DevToolsSocketFactory.
std::unique_ptr<net::ServerSocket> CreateForHttpServer() override {
std::unique_ptr<net::ServerSocket> socket(
new net::TCPServerSocket(nullptr, net::NetLogSource()));
if (socket->ListenWithAddressAndPort(address_, port_, kBackLog) != net::OK)
return std::unique_ptr<net::ServerSocket>();
net::IPEndPoint endpoint;
if (socket->GetLocalAddress(&endpoint) == net::OK)
base::subtle::NoBarrier_Store(&g_last_used_port, endpoint.port());
return socket;
}
std::unique_ptr<net::ServerSocket> CreateForTethering(
std::string* out_name) override {
return nullptr;
}
std::string address_;
uint16_t port_;
DISALLOW_COPY_AND_ASSIGN(TCPServerSocketFactory);
};
#endif
std::unique_ptr<content::DevToolsSocketFactory> CreateSocketFactory() {
const base::CommandLine& command_line =
*base::CommandLine::ForCurrentProcess();
#if defined(OS_ANDROID)
std::string socket_name = "content_shell_devtools_remote";
if (command_line.HasSwitch(switches::kRemoteDebuggingSocketName)) {
socket_name = command_line.GetSwitchValueASCII(
switches::kRemoteDebuggingSocketName);
}
return std::unique_ptr<content::DevToolsSocketFactory>(
new UnixDomainServerSocketFactory(socket_name));
#else
// See if the user specified a port on the command line (useful for
// automation). If not, use an ephemeral port by specifying 0.
uint16_t port = 0;
if (command_line.HasSwitch(switches::kRemoteDebuggingPort)) {
int temp_port;
std::string port_str =
command_line.GetSwitchValueASCII(switches::kRemoteDebuggingPort);
if (base::StringToInt(port_str, &temp_port) &&
temp_port >= 0 && temp_port < 65535) {
port = static_cast<uint16_t>(temp_port);
} else {
DLOG(WARNING) << "Invalid http debugger port number " << temp_port;
}
}
return std::unique_ptr<content::DevToolsSocketFactory>(
new TCPServerSocketFactory("127.0.0.1", port));
#endif
}
} // namespace
// ShellDevToolsManagerDelegate ----------------------------------------------
// static
int ShellDevToolsManagerDelegate::GetHttpHandlerPort() {
return base::subtle::NoBarrier_Load(&g_last_used_port);
}
// static
void ShellDevToolsManagerDelegate::StartHttpHandler(
BrowserContext* browser_context) {
std::string frontend_url;
DevToolsAgentHost::StartRemoteDebuggingServer(
CreateSocketFactory(), browser_context->GetPath(), base::FilePath());
const base::CommandLine& command_line =
*base::CommandLine::ForCurrentProcess();
if (command_line.HasSwitch(switches::kRemoteDebuggingPipe))
DevToolsAgentHost::StartRemoteDebuggingPipeHandler();
}
// static
void ShellDevToolsManagerDelegate::StopHttpHandler() {
DevToolsAgentHost::StopRemoteDebuggingServer();
}
ShellDevToolsManagerDelegate::ShellDevToolsManagerDelegate(
BrowserContext* browser_context)
: browser_context_(browser_context) {
}
ShellDevToolsManagerDelegate::~ShellDevToolsManagerDelegate() {
}
BrowserContext* ShellDevToolsManagerDelegate::GetDefaultBrowserContext() {
return browser_context_;
}
void ShellDevToolsManagerDelegate::ClientAttached(
content::DevToolsAgentHost* agent_host,
content::DevToolsAgentHostClient* client) {
// Make sure we don't receive notifications twice for the same client.
CHECK(clients_.find(client) == clients_.end());
clients_.insert(client);
}
void ShellDevToolsManagerDelegate::ClientDetached(
content::DevToolsAgentHost* agent_host,
content::DevToolsAgentHostClient* client) {
clients_.erase(client);
}
scoped_refptr<DevToolsAgentHost>
ShellDevToolsManagerDelegate::CreateNewTarget(const GURL& url) {
Shell* shell = Shell::CreateNewWindow(browser_context_,
url,
nullptr,
gfx::Size());
if (switches::IsRunWebTestsSwitchPresent())
SecondaryTestWindowObserver::CreateForWebContents(shell->web_contents());
return DevToolsAgentHost::GetOrCreateFor(shell->web_contents());
}
std::string ShellDevToolsManagerDelegate::GetDiscoveryPageHTML() {
#if defined(OS_ANDROID)
return std::string();
#else
return ui::ResourceBundle::GetSharedInstance()
.GetRawDataResource(IDR_CONTENT_SHELL_DEVTOOLS_DISCOVERY_PAGE)
.as_string();
#endif
}
bool ShellDevToolsManagerDelegate::HasBundledFrontendResources() {
#if defined(OS_ANDROID)
return false;
#endif
return true;
}
} // namespace content
|