| Index: headless/lib/browser/headless_devtools.cc
 | 
| diff --git a/headless/lib/browser/headless_devtools.cc b/headless/lib/browser/headless_devtools.cc
 | 
| new file mode 100644
 | 
| index 0000000000000000000000000000000000000000..000ca937c8e5b83c145dfcc63a21db0956097519
 | 
| --- /dev/null
 | 
| +++ b/headless/lib/browser/headless_devtools.cc
 | 
| @@ -0,0 +1,141 @@
 | 
| +// Copyright 2015 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 "headless/lib/browser/headless_devtools.h"
 | 
| +
 | 
| +#include <vector>
 | 
| +
 | 
| +#include "base/bind.h"
 | 
| +#include "base/command_line.h"
 | 
| +#include "base/files/file_path.h"
 | 
| +#include "base/strings/string_number_conversions.h"
 | 
| +#include "base/strings/stringprintf.h"
 | 
| +#include "base/strings/utf_string_conversions.h"
 | 
| +#include "components/devtools_discovery/basic_target_descriptor.h"
 | 
| +#include "components/devtools_discovery/devtools_discovery_manager.h"
 | 
| +#include "components/devtools_http_handler/devtools_http_handler.h"
 | 
| +#include "content/public/browser/browser_context.h"
 | 
| +#include "content/public/browser/devtools_agent_host.h"
 | 
| +#include "content/public/browser/devtools_frontend_host.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 "net/base/net_errors.h"
 | 
| +#include "net/socket/tcp_server_socket.h"
 | 
| +#include "ui/base/resource/resource_bundle.h"
 | 
| +#include "components/devtools_http_handler/devtools_http_handler_delegate.h"
 | 
| +
 | 
| +using devtools_http_handler::DevToolsHttpHandler;
 | 
| +
 | 
| +namespace headless {
 | 
| +
 | 
| +namespace {
 | 
| +
 | 
| +const int kBackLog = 10;
 | 
| +
 | 
| +class TCPServerSocketFactory
 | 
| +    : public DevToolsHttpHandler::ServerSocketFactory {
 | 
| + public:
 | 
| +  TCPServerSocketFactory(const std::string& address, uint16 port)
 | 
| +      : address_(address), port_(port) {
 | 
| +  }
 | 
| +
 | 
| + private:
 | 
| +  // DevToolsHttpHandler::ServerSocketFactory.
 | 
| +  scoped_ptr<net::ServerSocket> CreateForHttpServer() override {
 | 
| +    scoped_ptr<net::ServerSocket> socket(
 | 
| +        new net::TCPServerSocket(nullptr, net::NetLog::Source()));
 | 
| +    if (socket->ListenWithAddressAndPort(address_, port_, kBackLog) != net::OK)
 | 
| +      return scoped_ptr<net::ServerSocket>();
 | 
| +
 | 
| +    return socket;
 | 
| +  }
 | 
| +
 | 
| +  std::string address_;
 | 
| +  uint16 port_;
 | 
| +
 | 
| +  DISALLOW_COPY_AND_ASSIGN(TCPServerSocketFactory);
 | 
| +};
 | 
| +
 | 
| +scoped_ptr<DevToolsHttpHandler::ServerSocketFactory>
 | 
| +CreateSocketFactory() {
 | 
| +  const base::CommandLine& command_line =
 | 
| +      *base::CommandLine::ForCurrentProcess();
 | 
| +  // See if the user specified a port on the command line (useful for
 | 
| +  // automation). If not, use an ephemeral port by specifying 0.
 | 
| +  uint16 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>(temp_port);
 | 
| +    } else {
 | 
| +      DLOG(WARNING) << "Invalid http debugger port number " << temp_port;
 | 
| +    }
 | 
| +  }
 | 
| +  return scoped_ptr<DevToolsHttpHandler::ServerSocketFactory>(
 | 
| +      new TCPServerSocketFactory("127.0.0.1", port));
 | 
| +}
 | 
| +
 | 
| +
 | 
| +class HeadlessDevToolsDelegate :
 | 
| +    public devtools_http_handler::DevToolsHttpHandlerDelegate {
 | 
| + public:
 | 
| +  explicit HeadlessDevToolsDelegate();
 | 
| +  ~HeadlessDevToolsDelegate() override;
 | 
| +
 | 
| +  // devtools_http_handler::DevToolsHttpHandlerDelegate implementation.
 | 
| +  std::string GetDiscoveryPageHTML() override;
 | 
| +  std::string GetFrontendResource(const std::string& path) override;
 | 
| +  std::string GetPageThumbnailData(const GURL& url) override;
 | 
| +  content::DevToolsExternalAgentProxyDelegate*
 | 
| +      HandleWebSocketConnection(const std::string& path) override;
 | 
| +
 | 
| + private:
 | 
| +  DISALLOW_COPY_AND_ASSIGN(HeadlessDevToolsDelegate);
 | 
| +};
 | 
| +
 | 
| +HeadlessDevToolsDelegate::HeadlessDevToolsDelegate() { }
 | 
| +
 | 
| +HeadlessDevToolsDelegate::~HeadlessDevToolsDelegate() { }
 | 
| +
 | 
| +std::string HeadlessDevToolsDelegate::GetDiscoveryPageHTML() {
 | 
| +  return std::string();
 | 
| +}
 | 
| +
 | 
| +std::string HeadlessDevToolsDelegate::GetFrontendResource(
 | 
| +    const std::string& path) {
 | 
| +  return content::DevToolsFrontendHost::GetFrontendResource(path).as_string();
 | 
| +}
 | 
| +
 | 
| +std::string HeadlessDevToolsDelegate::GetPageThumbnailData(const GURL& url) {
 | 
| +  return std::string();
 | 
| +}
 | 
| +
 | 
| +content::DevToolsExternalAgentProxyDelegate*
 | 
| +HeadlessDevToolsDelegate::HandleWebSocketConnection(const std::string& path) {
 | 
| +  return nullptr;
 | 
| +}
 | 
| +
 | 
| +}  // namespace
 | 
| +
 | 
| +scoped_ptr<DevToolsHttpHandler>
 | 
| +CreateHttpHandler(content::BrowserContext* browser_context) {
 | 
| +  return make_scoped_ptr(new DevToolsHttpHandler(
 | 
| +      CreateSocketFactory(),
 | 
| +      std::string(),
 | 
| +      new HeadlessDevToolsDelegate(),
 | 
| +      browser_context->GetPath(),
 | 
| +      base::FilePath(),
 | 
| +      std::string(),
 | 
| +      "UserAgent")); // TODO(altimin): support user agent properly.
 | 
| +}
 | 
| +
 | 
| +}  // namespace headless
 | 
| 
 |