Home | History | Annotate | Download | only in server
      1 // Copyright (c) 2013 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 CHROME_TEST_CHROMEDRIVER_SERVER_HTTP_HANDLER_H_
      6 #define CHROME_TEST_CHROMEDRIVER_SERVER_HTTP_HANDLER_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/callback.h"
     12 #include "base/compiler_specific.h"
     13 #include "base/gtest_prod_util.h"
     14 #include "base/memory/ref_counted.h"
     15 #include "base/memory/scoped_ptr.h"
     16 #include "base/memory/weak_ptr.h"
     17 #include "base/threading/thread_checker.h"
     18 #include "chrome/test/chromedriver/command.h"
     19 #include "chrome/test/chromedriver/commands.h"
     20 #include "chrome/test/chromedriver/element_commands.h"
     21 #include "chrome/test/chromedriver/net/sync_websocket_factory.h"
     22 #include "chrome/test/chromedriver/session_commands.h"
     23 #include "chrome/test/chromedriver/session_thread_map.h"
     24 #include "chrome/test/chromedriver/window_commands.h"
     25 
     26 namespace base {
     27 class DictionaryValue;
     28 class SingleThreadTaskRunner;
     29 }
     30 
     31 namespace net {
     32 class HttpServerRequestInfo;
     33 class HttpServerResponseInfo;
     34 }
     35 
     36 class Adb;
     37 class DeviceManager;
     38 class PortManager;
     39 class PortServer;
     40 class URLRequestContextGetter;
     41 
     42 enum HttpMethod {
     43   kGet,
     44   kPost,
     45   kDelete,
     46 };
     47 
     48 struct CommandMapping {
     49   CommandMapping(HttpMethod method,
     50                  const std::string& path_pattern,
     51                  const Command& command);
     52   ~CommandMapping();
     53 
     54   HttpMethod method;
     55   std::string path_pattern;
     56   Command command;
     57 };
     58 
     59 typedef base::Callback<void(scoped_ptr<net::HttpServerResponseInfo>)>
     60     HttpResponseSenderFunc;
     61 
     62 class HttpHandler {
     63  public:
     64   explicit HttpHandler(const std::string& url_base);
     65   HttpHandler(const base::Closure& quit_func,
     66               const scoped_refptr<base::SingleThreadTaskRunner> io_task_runner,
     67               const std::string& url_base,
     68               int adb_port,
     69               scoped_ptr<PortServer> port_server);
     70   ~HttpHandler();
     71 
     72   void Handle(const net::HttpServerRequestInfo& request,
     73               const HttpResponseSenderFunc& send_response_func);
     74 
     75  private:
     76   FRIEND_TEST_ALL_PREFIXES(HttpHandlerTest, HandleUnknownCommand);
     77   FRIEND_TEST_ALL_PREFIXES(HttpHandlerTest, HandleNewSession);
     78   FRIEND_TEST_ALL_PREFIXES(HttpHandlerTest, HandleInvalidPost);
     79   FRIEND_TEST_ALL_PREFIXES(HttpHandlerTest, HandleUnimplementedCommand);
     80   FRIEND_TEST_ALL_PREFIXES(HttpHandlerTest, HandleCommand);
     81   typedef std::vector<CommandMapping> CommandMap;
     82 
     83   Command WrapToCommand(const char* name,
     84                         const SessionCommand& session_command);
     85   Command WrapToCommand(const char* name, const WindowCommand& window_command);
     86   Command WrapToCommand(const char* name,
     87                         const ElementCommand& element_command);
     88   void HandleCommand(const net::HttpServerRequestInfo& request,
     89                      const std::string& trimmed_path,
     90                      const HttpResponseSenderFunc& send_response_func);
     91   void PrepareResponse(const std::string& trimmed_path,
     92                        const HttpResponseSenderFunc& send_response_func,
     93                        const Status& status,
     94                        scoped_ptr<base::Value> value,
     95                        const std::string& session_id);
     96   scoped_ptr<net::HttpServerResponseInfo> PrepareResponseHelper(
     97       const std::string& trimmed_path,
     98       const Status& status,
     99       scoped_ptr<base::Value> value,
    100       const std::string& session_id);
    101 
    102   base::ThreadChecker thread_checker_;
    103   base::Closure quit_func_;
    104   std::string url_base_;
    105   bool received_shutdown_;
    106   scoped_refptr<URLRequestContextGetter> context_getter_;
    107   SyncWebSocketFactory socket_factory_;
    108   SessionThreadMap session_thread_map_;
    109   scoped_ptr<CommandMap> command_map_;
    110   scoped_ptr<Adb> adb_;
    111   scoped_ptr<DeviceManager> device_manager_;
    112   scoped_ptr<PortServer> port_server_;
    113   scoped_ptr<PortManager> port_manager_;
    114 
    115   base::WeakPtrFactory<HttpHandler> weak_ptr_factory_;
    116 
    117   DISALLOW_COPY_AND_ASSIGN(HttpHandler);
    118 };
    119 
    120 namespace internal {
    121 
    122 extern const char kNewSessionPathPattern[];
    123 
    124 bool MatchesCommand(const std::string& method,
    125                     const std::string& path,
    126                     const CommandMapping& command,
    127                     std::string* session_id,
    128                     base::DictionaryValue* out_params);
    129 
    130 }  // namespace internal
    131 
    132 #endif  // CHROME_TEST_CHROMEDRIVER_SERVER_HTTP_HANDLER_H_
    133