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 Log;
     39 class URLRequestContextGetter;
     40 
     41 enum HttpMethod {
     42   kGet,
     43   kPost,
     44   kDelete,
     45 };
     46 
     47 struct CommandMapping {
     48   CommandMapping(HttpMethod method,
     49                  const std::string& path_pattern,
     50                  const Command& command);
     51   ~CommandMapping();
     52 
     53   HttpMethod method;
     54   std::string path_pattern;
     55   Command command;
     56 };
     57 
     58 typedef base::Callback<void(scoped_ptr<net::HttpServerResponseInfo>)>
     59     HttpResponseSenderFunc;
     60 
     61 class HttpHandler {
     62  public:
     63   HttpHandler(Log* log, const std::string& url_base);
     64   HttpHandler(const base::Closure& quit_func,
     65               const scoped_refptr<base::SingleThreadTaskRunner> io_task_runner,
     66               Log* log,
     67               const std::string& url_base);
     68   ~HttpHandler();
     69 
     70   void Handle(const net::HttpServerRequestInfo& request,
     71               const HttpResponseSenderFunc& send_response_func);
     72 
     73  private:
     74   FRIEND_TEST_ALL_PREFIXES(HttpHandlerTest, HandleUnknownCommand);
     75   FRIEND_TEST_ALL_PREFIXES(HttpHandlerTest, HandleNewSession);
     76   FRIEND_TEST_ALL_PREFIXES(HttpHandlerTest, HandleInvalidPost);
     77   FRIEND_TEST_ALL_PREFIXES(HttpHandlerTest, HandleUnimplementedCommand);
     78   FRIEND_TEST_ALL_PREFIXES(HttpHandlerTest, HandleCommand);
     79   typedef std::vector<CommandMapping> CommandMap;
     80 
     81   Command WrapToCommand(const SessionCommand& session_command);
     82   Command WrapToCommand(const WindowCommand& window_command);
     83   Command WrapToCommand(const ElementCommand& element_command);
     84   void HandleCommand(const net::HttpServerRequestInfo& request,
     85                      const std::string& trimmed_path,
     86                      const HttpResponseSenderFunc& send_response_func);
     87   void PrepareResponse(const std::string& trimmed_path,
     88                        const HttpResponseSenderFunc& send_response_func,
     89                        const Status& status,
     90                        scoped_ptr<base::Value> value,
     91                        const std::string& session_id);
     92   scoped_ptr<net::HttpServerResponseInfo> PrepareResponseHelper(
     93       const std::string& trimmed_path,
     94       const Status& status,
     95       scoped_ptr<base::Value> value,
     96       const std::string& session_id);
     97 
     98   base::ThreadChecker thread_checker_;
     99   base::Closure quit_func_;
    100   Log* log_;
    101   std::string url_base_;
    102   bool received_shutdown_;
    103   scoped_refptr<URLRequestContextGetter> context_getter_;
    104   SyncWebSocketFactory socket_factory_;
    105   SessionThreadMap session_thread_map_;
    106   scoped_ptr<CommandMap> command_map_;
    107   scoped_ptr<Adb> adb_;
    108   scoped_ptr<DeviceManager> device_manager_;
    109 
    110   base::WeakPtrFactory<HttpHandler> weak_ptr_factory_;
    111 
    112   DISALLOW_COPY_AND_ASSIGN(HttpHandler);
    113 };
    114 
    115 namespace internal {
    116 
    117 extern const char kNewSessionPathPattern[];
    118 
    119 bool MatchesCommand(const std::string& method,
    120                     const std::string& path,
    121                     const CommandMapping& command,
    122                     std::string* session_id,
    123                     base::DictionaryValue* out_params);
    124 
    125 }  // namespace internal
    126 
    127 #endif  // CHROME_TEST_CHROMEDRIVER_SERVER_HTTP_HANDLER_H_
    128