2012-04-03 03:34:16 +02:00
|
|
|
// Copyright (c) 2012 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 "libcef/browser/devtools_delegate.h"
|
2012-05-18 17:04:56 +02:00
|
|
|
#include "libcef/browser/devtools_scheme_handler.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
#include <algorithm>
|
2012-04-27 00:20:18 +02:00
|
|
|
#include <string>
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2012-04-27 00:20:18 +02:00
|
|
|
#include "base/command_line.h"
|
|
|
|
#include "base/md5.h"
|
|
|
|
#include "base/rand_util.h"
|
|
|
|
#include "base/stringprintf.h"
|
|
|
|
#include "base/string_number_conversions.h"
|
|
|
|
#include "base/time.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "content/public/browser/devtools_http_handler.h"
|
2012-04-27 00:20:18 +02:00
|
|
|
#include "content/public/browser/render_process_host.h"
|
|
|
|
#include "content/public/browser/render_view_host.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "content/public/common/content_client.h"
|
2012-04-27 00:20:18 +02:00
|
|
|
#include "content/public/common/content_switches.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "grit/cef_resources.h"
|
2012-05-31 17:19:33 +02:00
|
|
|
#include "net/base/tcp_listen_socket.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "net/url_request/url_request_context_getter.h"
|
2012-05-31 17:19:33 +02:00
|
|
|
#include "ui/base/layout.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "ui/base/resource/resource_bundle.h"
|
|
|
|
|
2012-04-27 00:20:18 +02:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class CefDevToolsBindingHandler
|
|
|
|
: public content::DevToolsHttpHandler::RenderViewHostBinding {
|
|
|
|
public:
|
|
|
|
CefDevToolsBindingHandler() {
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual std::string GetIdentifier(content::RenderViewHost* rvh) OVERRIDE {
|
|
|
|
int process_id = rvh->GetProcess()->GetID();
|
|
|
|
int routing_id = rvh->GetRoutingID();
|
|
|
|
|
|
|
|
if (random_seed_.empty()) {
|
|
|
|
// Generate a random seed that is used to make identifier guessing more
|
|
|
|
// difficult.
|
2012-05-02 18:28:22 +02:00
|
|
|
random_seed_ = base::StringPrintf("%lf|%u",
|
|
|
|
base::Time::Now().ToDoubleT(), base::RandInt(0, INT_MAX));
|
2012-04-27 00:20:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a key that combines RVH IDs and the random seed.
|
|
|
|
std::string key = base::StringPrintf("%d|%d|%s",
|
|
|
|
process_id,
|
|
|
|
routing_id,
|
|
|
|
random_seed_.c_str());
|
|
|
|
|
|
|
|
// Return an MD5 hash of the key.
|
|
|
|
return base::MD5String(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual content::RenderViewHost* ForIdentifier(
|
|
|
|
const std::string& identifier) OVERRIDE {
|
|
|
|
// Iterate through the existing RVH instances to find a match.
|
|
|
|
for (content::RenderProcessHost::iterator it(
|
|
|
|
content::RenderProcessHost::AllHostsIterator());
|
|
|
|
!it.IsAtEnd(); it.Advance()) {
|
|
|
|
content::RenderProcessHost* render_process_host = it.GetCurrentValue();
|
|
|
|
DCHECK(render_process_host);
|
|
|
|
|
|
|
|
// Ignore processes that don't have a connection, such as crashed
|
|
|
|
// contents.
|
|
|
|
if (!render_process_host->HasConnection())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
content::RenderProcessHost::RenderWidgetHostsIterator rwit(
|
|
|
|
render_process_host->GetRenderWidgetHostsIterator());
|
|
|
|
for (; !rwit.IsAtEnd(); rwit.Advance()) {
|
|
|
|
const content::RenderWidgetHost* widget = rwit.GetCurrentValue();
|
|
|
|
DCHECK(widget);
|
|
|
|
if (!widget || !widget->IsRenderView())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
content::RenderViewHost* host =
|
|
|
|
content::RenderViewHost::From(
|
|
|
|
const_cast<content::RenderWidgetHost*>(widget));
|
|
|
|
if (GetIdentifier(host) == identifier)
|
|
|
|
return host;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string random_seed_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
CefDevToolsDelegate::CefDevToolsDelegate(
|
|
|
|
int port,
|
2012-04-04 20:18:09 +02:00
|
|
|
net::URLRequestContextGetter* context_getter) {
|
2012-04-03 03:34:16 +02:00
|
|
|
devtools_http_handler_ = content::DevToolsHttpHandler::Start(
|
2012-05-31 17:19:33 +02:00
|
|
|
new net::TCPListenSocketFactory("127.0.0.1", port),
|
2012-04-03 03:34:16 +02:00
|
|
|
"",
|
2012-04-04 20:18:09 +02:00
|
|
|
context_getter,
|
2012-04-03 03:34:16 +02:00
|
|
|
this);
|
2012-04-27 00:20:18 +02:00
|
|
|
|
|
|
|
binding_.reset(new CefDevToolsBindingHandler());
|
|
|
|
devtools_http_handler_->SetRenderViewHostBinding(binding_.get());
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefDevToolsDelegate::~CefDevToolsDelegate() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefDevToolsDelegate::Stop() {
|
|
|
|
// The call below destroys this.
|
|
|
|
devtools_http_handler_->Stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CefDevToolsDelegate::GetDiscoveryPageHTML() {
|
|
|
|
return content::GetContentClient()->GetDataResource(
|
2012-05-31 17:19:33 +02:00
|
|
|
IDR_CEF_DEVTOOLS_DISCOVERY_PAGE, ui::SCALE_FACTOR_NONE).as_string();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefDevToolsDelegate::BundlesFrontendResources() {
|
2012-05-18 17:04:56 +02:00
|
|
|
return false;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string CefDevToolsDelegate::GetFrontendResourcesBaseURL() {
|
2012-05-18 17:04:56 +02:00
|
|
|
return kChromeDevToolsURL;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
2012-04-27 00:20:18 +02:00
|
|
|
|
2012-05-18 17:04:56 +02:00
|
|
|
std::string CefDevToolsDelegate::GetDevToolsURL(content::RenderViewHost* rvh,
|
|
|
|
bool http_scheme) {
|
2012-04-27 00:20:18 +02:00
|
|
|
const CommandLine& command_line = *CommandLine::ForCurrentProcess();
|
|
|
|
std::string port_str =
|
|
|
|
command_line.GetSwitchValueASCII(switches::kRemoteDebuggingPort);
|
|
|
|
DCHECK(!port_str.empty());
|
|
|
|
int port;
|
|
|
|
if (!base::StringToInt(port_str, &port))
|
|
|
|
return std::string();
|
|
|
|
|
|
|
|
std::string page_id = binding_->GetIdentifier(rvh);
|
2012-05-18 17:04:56 +02:00
|
|
|
std::string host = http_scheme ?
|
|
|
|
base::StringPrintf("http://localhost:%d/devtools/", port) :
|
|
|
|
kChromeDevToolsURL;
|
2012-04-27 00:20:18 +02:00
|
|
|
|
|
|
|
return base::StringPrintf(
|
2012-05-18 17:04:56 +02:00
|
|
|
"%sdevtools.html?ws=localhost:%d/devtools/page/%s",
|
|
|
|
host.c_str(),
|
2012-04-27 00:20:18 +02:00
|
|
|
port,
|
|
|
|
page_id.c_str());
|
|
|
|
}
|