2014-07-30 03:40:17 +00:00
|
|
|
// Copyright (c) 2014 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-CHROMIUM file.
|
|
|
|
|
2015-01-10 01:14:52 +00:00
|
|
|
#include "browser/devtools_manager_delegate.h"
|
2014-07-30 03:40:17 +00:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
2015-09-26 18:19:27 +00:00
|
|
|
#include "browser/net/devtools_network_protocol_handler.h"
|
|
|
|
|
2014-07-30 03:40:17 +00:00
|
|
|
#include "base/bind.h"
|
|
|
|
#include "base/command_line.h"
|
2015-01-10 01:14:52 +00:00
|
|
|
#include "base/files/file_path.h"
|
2014-07-30 03:40:17 +00:00
|
|
|
#include "base/strings/string_number_conversions.h"
|
|
|
|
#include "base/strings/stringprintf.h"
|
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2015-07-23 10:18:03 +00:00
|
|
|
#include "common/content_client.h"
|
|
|
|
#include "components/devtools_discovery/basic_target_descriptor.h"
|
2015-08-21 08:14:19 +00:00
|
|
|
#include "components/devtools_discovery/devtools_discovery_manager.h"
|
2015-07-23 10:18:03 +00:00
|
|
|
#include "components/devtools_http_handler/devtools_http_handler.h"
|
2014-07-30 03:40:17 +00:00
|
|
|
#include "content/public/browser/devtools_agent_host.h"
|
2015-07-23 10:18:03 +00:00
|
|
|
#include "content/public/browser/devtools_frontend_host.h"
|
2014-07-30 03:40:17 +00:00
|
|
|
#include "content/public/browser/favicon_status.h"
|
|
|
|
#include "content/public/browser/navigation_entry.h"
|
|
|
|
#include "content/public/common/content_switches.h"
|
|
|
|
#include "content/public/common/url_constants.h"
|
|
|
|
#include "content/public/common/user_agent.h"
|
2015-07-23 10:18:03 +00:00
|
|
|
#include "net/base/net_errors.h"
|
2014-12-05 23:37:11 +00:00
|
|
|
#include "net/socket/tcp_server_socket.h"
|
2015-03-09 02:07:53 +00:00
|
|
|
#include "net/socket/stream_socket.h"
|
2014-07-30 03:40:17 +00:00
|
|
|
#include "ui/base/resource/resource_bundle.h"
|
|
|
|
|
|
|
|
|
2015-01-10 01:14:52 +00:00
|
|
|
namespace brightray {
|
|
|
|
|
2014-07-30 03:40:17 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// A hack here:
|
|
|
|
// Copy from grit/shell_resources.h of chromium repository
|
|
|
|
// since libcontentchromium doesn't expose content_shell resources.
|
|
|
|
const int kIDR_CONTENT_SHELL_DEVTOOLS_DISCOVERY_PAGE = 25500;
|
|
|
|
|
2014-12-05 23:37:11 +00:00
|
|
|
class TCPServerSocketFactory
|
2015-07-23 10:18:03 +00:00
|
|
|
: public devtools_http_handler::DevToolsHttpHandler::ServerSocketFactory {
|
2014-12-05 23:37:11 +00:00
|
|
|
public:
|
2015-04-21 10:54:57 +00:00
|
|
|
TCPServerSocketFactory(const std::string& address, int port)
|
|
|
|
: address_(address), port_(port) {
|
|
|
|
}
|
2014-12-05 23:37:11 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
// content::DevToolsHttpHandler::ServerSocketFactory.
|
2015-04-21 10:54:57 +00:00
|
|
|
scoped_ptr<net::ServerSocket> CreateForHttpServer() override {
|
|
|
|
scoped_ptr<net::ServerSocket> socket(
|
|
|
|
new net::TCPServerSocket(nullptr, net::NetLog::Source()));
|
|
|
|
if (socket->ListenWithAddressAndPort(address_, port_, 10) != net::OK)
|
|
|
|
return scoped_ptr<net::ServerSocket>();
|
|
|
|
|
|
|
|
return socket;
|
2014-12-05 23:37:11 +00:00
|
|
|
}
|
|
|
|
|
2015-04-21 10:54:57 +00:00
|
|
|
std::string address_;
|
|
|
|
uint16 port_;
|
|
|
|
|
2014-12-05 23:37:11 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(TCPServerSocketFactory);
|
|
|
|
};
|
|
|
|
|
2015-07-23 10:18:03 +00:00
|
|
|
scoped_ptr<devtools_http_handler::DevToolsHttpHandler::ServerSocketFactory>
|
2014-12-05 23:37:11 +00:00
|
|
|
CreateSocketFactory() {
|
2015-03-09 02:07:53 +00:00
|
|
|
auto& command_line = *base::CommandLine::ForCurrentProcess();
|
2014-07-30 03:40:17 +00:00
|
|
|
// See if the user specified a port on the command line (useful for
|
|
|
|
// automation). If not, use an ephemeral port by specifying 0.
|
|
|
|
int 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 = temp_port;
|
|
|
|
} else {
|
|
|
|
DLOG(WARNING) << "Invalid http debugger port number " << temp_port;
|
|
|
|
}
|
|
|
|
}
|
2015-07-23 10:18:03 +00:00
|
|
|
return scoped_ptr<
|
|
|
|
devtools_http_handler::DevToolsHttpHandler::ServerSocketFactory>(
|
|
|
|
new TCPServerSocketFactory("127.0.0.1", port));
|
2014-07-30 03:40:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-10 01:14:52 +00:00
|
|
|
// DevToolsDelegate --------------------------------------------------------
|
2014-07-30 03:40:17 +00:00
|
|
|
|
2015-07-23 10:18:03 +00:00
|
|
|
class DevToolsDelegate :
|
|
|
|
public devtools_http_handler::DevToolsHttpHandlerDelegate {
|
2015-01-10 01:14:52 +00:00
|
|
|
public:
|
|
|
|
DevToolsDelegate();
|
|
|
|
virtual ~DevToolsDelegate();
|
2014-07-30 03:40:17 +00:00
|
|
|
|
2015-07-23 10:18:03 +00:00
|
|
|
// devtools_http_handler::DevToolsHttpHandlerDelegate.
|
2015-01-10 01:14:52 +00:00
|
|
|
std::string GetDiscoveryPageHTML() override;
|
2015-07-23 10:18:03 +00:00
|
|
|
std::string GetFrontendResource(const std::string& path) override;
|
|
|
|
std::string GetPageThumbnailData(const GURL& url) override;
|
2015-12-07 11:55:01 +00:00
|
|
|
content::DevToolsExternalAgentProxyDelegate* HandleWebSocketConnection(
|
|
|
|
const std::string& path) override;
|
2014-12-05 23:37:11 +00:00
|
|
|
|
2015-01-10 01:14:52 +00:00
|
|
|
private:
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(DevToolsDelegate);
|
|
|
|
};
|
2014-07-30 03:40:17 +00:00
|
|
|
|
2015-01-10 01:14:52 +00:00
|
|
|
DevToolsDelegate::DevToolsDelegate() {
|
2014-07-30 03:40:17 +00:00
|
|
|
}
|
|
|
|
|
2015-01-10 01:14:52 +00:00
|
|
|
DevToolsDelegate::~DevToolsDelegate() {
|
2014-07-30 03:40:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string DevToolsDelegate::GetDiscoveryPageHTML() {
|
|
|
|
return ResourceBundle::GetSharedInstance().GetRawDataResource(
|
|
|
|
kIDR_CONTENT_SHELL_DEVTOOLS_DISCOVERY_PAGE).as_string();
|
|
|
|
}
|
|
|
|
|
2015-07-23 10:18:03 +00:00
|
|
|
|
|
|
|
std::string DevToolsDelegate::GetFrontendResource(
|
|
|
|
const std::string& path) {
|
|
|
|
return content::DevToolsFrontendHost::GetFrontendResource(path).as_string();
|
2014-07-30 03:40:17 +00:00
|
|
|
}
|
|
|
|
|
2015-07-23 10:18:03 +00:00
|
|
|
std::string DevToolsDelegate::GetPageThumbnailData(const GURL& url) {
|
|
|
|
return std::string();
|
2014-07-30 03:40:17 +00:00
|
|
|
}
|
|
|
|
|
2015-12-07 11:55:01 +00:00
|
|
|
content::DevToolsExternalAgentProxyDelegate*
|
|
|
|
DevToolsDelegate::HandleWebSocketConnection(const std::string& path) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-01-10 01:14:52 +00:00
|
|
|
} // namespace
|
|
|
|
|
2014-12-05 23:37:11 +00:00
|
|
|
// DevToolsManagerDelegate ---------------------------------------------------
|
|
|
|
|
2015-01-10 01:14:52 +00:00
|
|
|
// static
|
2015-07-23 10:18:03 +00:00
|
|
|
devtools_http_handler::DevToolsHttpHandler*
|
2015-01-10 01:14:52 +00:00
|
|
|
DevToolsManagerDelegate::CreateHttpHandler() {
|
2015-07-23 10:18:03 +00:00
|
|
|
return new devtools_http_handler::DevToolsHttpHandler(
|
|
|
|
CreateSocketFactory(),
|
|
|
|
std::string(),
|
|
|
|
new DevToolsDelegate,
|
|
|
|
base::FilePath(),
|
|
|
|
base::FilePath(),
|
|
|
|
std::string(),
|
|
|
|
GetBrightrayUserAgent());
|
2015-01-10 01:14:52 +00:00
|
|
|
}
|
|
|
|
|
2015-09-26 18:19:27 +00:00
|
|
|
DevToolsManagerDelegate::DevToolsManagerDelegate()
|
|
|
|
: handler_(new DevToolsNetworkProtocolHandler) {
|
2015-08-21 08:14:19 +00:00
|
|
|
// NB(zcbenz): This call does nothing, the only purpose is to make sure the
|
|
|
|
// devtools_discovery module is linked into the final executable on Linux.
|
|
|
|
// Though it is possible to achieve this by modifying the gyp settings, it
|
|
|
|
// would greatly increase gyp file's complexity, so I chose to instead do
|
|
|
|
// this hack.
|
|
|
|
devtools_discovery::DevToolsDiscoveryManager::GetInstance();
|
2014-12-05 23:37:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DevToolsManagerDelegate::~DevToolsManagerDelegate() {
|
|
|
|
}
|
|
|
|
|
|
|
|
base::DictionaryValue* DevToolsManagerDelegate::HandleCommand(
|
|
|
|
content::DevToolsAgentHost* agent_host,
|
|
|
|
base::DictionaryValue* command) {
|
2015-09-26 18:19:27 +00:00
|
|
|
return handler_->HandleCommand(agent_host, command);
|
2014-12-05 23:37:11 +00:00
|
|
|
}
|
|
|
|
|
2014-07-30 03:40:17 +00:00
|
|
|
} // namespace brightray
|