electron/brightray/browser/devtools_manager_delegate.cc

230 lines
7.2 KiB
C++
Raw Normal View History

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>
#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"
#include "browser/browser_context.h"
#include "content/public/browser/devtools_agent_host.h"
#include "content/public/browser/devtools_http_handler.h"
#include "content/public/browser/devtools_target.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"
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"
using content::DevToolsAgentHost;
using content::RenderViewHost;
using content::WebContents;
using content::BrowserContext;
using content::DevToolsTarget;
using content::DevToolsHttpHandler;
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;
const char kTargetTypePage[] = "page";
2014-12-05 23:37:11 +00:00
const char kTargetTypeServiceWorker[] = "service_worker";
const char kTargetTypeOther[] = "other";
2014-07-30 03:40:17 +00:00
2014-12-05 23:37:11 +00:00
class TCPServerSocketFactory
: public content::DevToolsHttpHandler::ServerSocketFactory {
public:
TCPServerSocketFactory(const std::string& address, int port, int backlog)
: content::DevToolsHttpHandler::ServerSocketFactory(
address, port, backlog) {}
private:
// content::DevToolsHttpHandler::ServerSocketFactory.
scoped_ptr<net::ServerSocket> Create() const override {
return scoped_ptr<net::ServerSocket>(
new net::TCPServerSocket(NULL, net::NetLog::Source()));
}
DISALLOW_COPY_AND_ASSIGN(TCPServerSocketFactory);
};
scoped_ptr<content::DevToolsHttpHandler::ServerSocketFactory>
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;
}
}
2014-12-05 23:37:11 +00:00
return scoped_ptr<content::DevToolsHttpHandler::ServerSocketFactory>(
new TCPServerSocketFactory("127.0.0.1", port, 1));
2014-07-30 03:40:17 +00:00
}
class Target : public content::DevToolsTarget {
public:
2014-12-05 23:37:11 +00:00
explicit Target(scoped_refptr<DevToolsAgentHost> agent_host);
2015-01-10 01:14:52 +00:00
std::string GetId() const override { return agent_host_->GetId(); }
std::string GetParentId() const override { return std::string(); }
std::string GetType() const override {
2014-12-05 23:37:11 +00:00
switch (agent_host_->GetType()) {
case DevToolsAgentHost::TYPE_WEB_CONTENTS:
return kTargetTypePage;
case DevToolsAgentHost::TYPE_SERVICE_WORKER:
return kTargetTypeServiceWorker;
default:
break;
}
return kTargetTypeOther;
}
2015-01-10 01:14:52 +00:00
std::string GetTitle() const override { return agent_host_->GetTitle(); }
std::string GetDescription() const override { return std::string(); }
GURL GetURL() const override { return agent_host_->GetURL(); }
GURL GetFaviconURL() const override { return favicon_url_; }
base::TimeTicks GetLastActivityTime() const override {
2014-07-30 03:40:17 +00:00
return last_activity_time_;
}
2015-01-10 01:14:52 +00:00
bool IsAttached() const override { return agent_host_->IsAttached(); }
scoped_refptr<DevToolsAgentHost> GetAgentHost() const override {
2014-07-30 03:40:17 +00:00
return agent_host_;
}
2015-01-10 01:14:52 +00:00
bool Activate() const override;
bool Close() const override;
2014-07-30 03:40:17 +00:00
private:
scoped_refptr<DevToolsAgentHost> agent_host_;
GURL favicon_url_;
base::TimeTicks last_activity_time_;
};
2014-12-05 23:37:11 +00:00
Target::Target(scoped_refptr<DevToolsAgentHost> agent_host)
: agent_host_(agent_host) {
if (WebContents* web_contents = agent_host_->GetWebContents()) {
content::NavigationController& controller = web_contents->GetController();
content::NavigationEntry* entry = controller.GetActiveEntry();
if (entry != NULL && entry->GetURL().is_valid())
favicon_url_ = entry->GetFavicon().url;
last_activity_time_ = web_contents->GetLastActiveTime();
}
2014-07-30 03:40:17 +00:00
}
bool Target::Activate() const {
2014-12-05 23:37:11 +00:00
return agent_host_->Activate();
2014-07-30 03:40:17 +00:00
}
bool Target::Close() const {
2014-12-05 23:37:11 +00:00
return agent_host_->Close();
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-01-10 01:14:52 +00:00
class DevToolsDelegate : public content::DevToolsHttpHandlerDelegate {
public:
DevToolsDelegate();
virtual ~DevToolsDelegate();
2014-07-30 03:40:17 +00:00
2015-01-10 01:14:52 +00:00
// content::DevToolsHttpHandlerDelegate.
std::string GetDiscoveryPageHTML() override;
bool BundlesFrontendResources() override;
base::FilePath GetDebugFrontendDir() override;
2015-03-09 02:07:53 +00:00
scoped_ptr<net::ServerSocket> CreateSocketForTethering(
2015-01-10 01:14:52 +00:00
std::string* name) 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();
}
bool DevToolsDelegate::BundlesFrontendResources() {
return true;
}
base::FilePath DevToolsDelegate::GetDebugFrontendDir() {
return base::FilePath();
}
2015-03-09 02:07:53 +00:00
scoped_ptr<net::ServerSocket> DevToolsDelegate::CreateSocketForTethering(
2014-12-05 23:37:11 +00:00
std::string* name) {
2015-03-09 02:07:53 +00:00
return scoped_ptr<net::ServerSocket>();
2014-12-05 23:37:11 +00:00
}
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
content::DevToolsHttpHandler*
DevToolsManagerDelegate::CreateHttpHandler() {
return DevToolsHttpHandler::Start(CreateSocketFactory(),
std::string(),
new DevToolsDelegate,
base::FilePath());
}
DevToolsManagerDelegate::DevToolsManagerDelegate() {
2014-12-05 23:37:11 +00:00
}
DevToolsManagerDelegate::~DevToolsManagerDelegate() {
}
base::DictionaryValue* DevToolsManagerDelegate::HandleCommand(
content::DevToolsAgentHost* agent_host,
base::DictionaryValue* command) {
return NULL;
}
std::string DevToolsManagerDelegate::GetPageThumbnailData(
const GURL& url) {
2014-07-30 03:40:17 +00:00
return std::string();
}
2014-12-05 23:37:11 +00:00
scoped_ptr<content::DevToolsTarget>
DevToolsManagerDelegate::CreateNewTarget(const GURL& url) {
return scoped_ptr<content::DevToolsTarget>();
2014-07-30 03:40:17 +00:00
}
2014-12-05 23:37:11 +00:00
void DevToolsManagerDelegate::EnumerateTargets(TargetCallback callback) {
2014-07-30 03:40:17 +00:00
TargetList targets;
2015-01-10 01:14:52 +00:00
for (const auto& agent_host : DevToolsAgentHost::GetOrCreateAll()) {
targets.push_back(new Target(agent_host));
2014-07-30 03:40:17 +00:00
}
callback.Run(targets);
}
} // namespace brightray