Home | History | Annotate | Download | only in devtools
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_HTTP_HANDLER_IMPL_H_
      6 #define CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_HTTP_HANDLER_IMPL_H_
      7 
      8 #include <map>
      9 #include <set>
     10 #include <string>
     11 #include <vector>
     12 
     13 #include "base/memory/ref_counted.h"
     14 #include "base/memory/scoped_ptr.h"
     15 #include "content/common/content_export.h"
     16 #include "content/public/browser/devtools_http_handler.h"
     17 #include "content/public/browser/devtools_http_handler_delegate.h"
     18 #include "net/http/http_status_code.h"
     19 #include "net/server/http_server.h"
     20 
     21 namespace base {
     22 class DictionaryValue;
     23 class ListValue;
     24 class Thread;
     25 class Value;
     26 }
     27 
     28 namespace net {
     29 class StreamListenSocketFactory;
     30 class URLRequestContextGetter;
     31 }
     32 
     33 namespace content {
     34 
     35 class DevToolsBrowserTarget;
     36 class DevToolsClientHost;
     37 
     38 class DevToolsHttpHandlerImpl
     39     : public DevToolsHttpHandler,
     40       public base::RefCountedThreadSafe<DevToolsHttpHandlerImpl>,
     41       public net::HttpServer::Delegate {
     42  private:
     43   friend class base::RefCountedThreadSafe<DevToolsHttpHandlerImpl>;
     44   friend class DevToolsHttpHandler;
     45 
     46   // Takes ownership over |socket_factory|.
     47   DevToolsHttpHandlerImpl(const net::StreamListenSocketFactory* socket_factory,
     48                           const std::string& frontend_url,
     49                           DevToolsHttpHandlerDelegate* delegate);
     50   virtual ~DevToolsHttpHandlerImpl();
     51   void Start();
     52 
     53   // DevToolsHttpHandler implementation.
     54   virtual void Stop() OVERRIDE;
     55   virtual GURL GetFrontendURL() OVERRIDE;
     56 
     57   // net::HttpServer::Delegate implementation.
     58   virtual void OnHttpRequest(int connection_id,
     59                              const net::HttpServerRequestInfo& info) OVERRIDE;
     60   virtual void OnWebSocketRequest(
     61       int connection_id,
     62       const net::HttpServerRequestInfo& info) OVERRIDE;
     63   virtual void OnWebSocketMessage(int connection_id,
     64                                   const std::string& data) OVERRIDE;
     65   virtual void OnClose(int connection_id) OVERRIDE;
     66 
     67   void OnJsonRequestUI(int connection_id,
     68                        const net::HttpServerRequestInfo& info);
     69   void OnThumbnailRequestUI(int connection_id, const GURL& page_url);
     70   void OnDiscoveryPageRequestUI(int connection_id);
     71 
     72   void OnWebSocketRequestUI(int connection_id,
     73                             const net::HttpServerRequestInfo& info);
     74   void OnWebSocketMessageUI(int connection_id, const std::string& data);
     75   void OnCloseUI(int connection_id);
     76 
     77   void ResetHandlerThread();
     78   void ResetHandlerThreadAndRelease();
     79 
     80   void OnTargetListReceived(
     81       int connection_id,
     82       const std::string& host,
     83       const DevToolsHttpHandlerDelegate::TargetList& targets);
     84 
     85   DevToolsTarget* GetTarget(const std::string& id);
     86 
     87   void Init();
     88   void Teardown();
     89 
     90   void StartHandlerThread();
     91   void StopHandlerThread();
     92 
     93   void SendJson(int connection_id,
     94                 net::HttpStatusCode status_code,
     95                 base::Value* value,
     96                 const std::string& message);
     97   void Send200(int connection_id,
     98                const std::string& data,
     99                const std::string& mime_type);
    100   void Send404(int connection_id);
    101   void Send500(int connection_id,
    102                const std::string& message);
    103   void AcceptWebSocket(int connection_id,
    104                        const net::HttpServerRequestInfo& request);
    105 
    106   // Returns the front end url without the host at the beginning.
    107   std::string GetFrontendURLInternal(const std::string target_id,
    108                                      const std::string& host);
    109 
    110   base::DictionaryValue* SerializeTarget(const DevToolsTarget& target,
    111                                          const std::string& host);
    112 
    113   // The thread used by the devtools handler to run server socket.
    114   scoped_ptr<base::Thread> thread_;
    115 
    116   std::string overridden_frontend_url_;
    117   scoped_ptr<const net::StreamListenSocketFactory> socket_factory_;
    118   scoped_refptr<net::HttpServer> server_;
    119   typedef std::map<int, DevToolsClientHost*> ConnectionToClientHostMap;
    120   ConnectionToClientHostMap connection_to_client_host_ui_;
    121   scoped_ptr<DevToolsHttpHandlerDelegate> delegate_;
    122   typedef std::map<std::string, DevToolsTarget*> TargetMap;
    123   TargetMap target_map_;
    124   scoped_refptr<DevToolsBrowserTarget> browser_target_;
    125   DISALLOW_COPY_AND_ASSIGN(DevToolsHttpHandlerImpl);
    126 };
    127 
    128 }  // namespace content
    129 
    130 #endif  // CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_HTTP_HANDLER_IMPL_H_
    131