http_handler.h revision ca12bfac764ba476d6cd062bf1dde12cc64c3f40
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/compiler_specific.h"
12#include "base/gtest_prod_util.h"
13#include "base/memory/scoped_ptr.h"
14#include "base/threading/thread.h"
15#include "chrome/test/chromedriver/command.h"
16#include "chrome/test/chromedriver/element_commands.h"
17#include "chrome/test/chromedriver/net/sync_websocket_factory.h"
18#include "chrome/test/chromedriver/session_commands.h"
19#include "chrome/test/chromedriver/session_map.h"
20#include "chrome/test/chromedriver/window_commands.h"
21
22namespace base {
23class DictionaryValue;
24}
25
26namespace net {
27class HttpServerRequestInfo;
28}
29
30class Adb;
31class DeviceManager;
32class Log;
33class HttpResponse;
34class URLRequestContextGetter;
35
36enum HttpMethod {
37  kGet,
38  kPost,
39  kDelete,
40};
41
42struct CommandMapping {
43  CommandMapping(HttpMethod method,
44                 const std::string& path_pattern,
45                 const Command& command);
46  ~CommandMapping();
47
48  HttpMethod method;
49  std::string path_pattern;
50  Command command;
51};
52
53class HttpHandler {
54 public:
55  HttpHandler(Log* log, const std::string& url_base);
56  ~HttpHandler();
57
58  void Handle(const net::HttpServerRequestInfo& request,
59              HttpResponse* response);
60  bool ShouldShutdown(const net::HttpServerRequestInfo& request);
61
62 private:
63  FRIEND_TEST_ALL_PREFIXES(HttpHandlerTest, HandleUnknownCommand);
64  FRIEND_TEST_ALL_PREFIXES(HttpHandlerTest, HandleNewSession);
65  FRIEND_TEST_ALL_PREFIXES(HttpHandlerTest, HandleInvalidPost);
66  FRIEND_TEST_ALL_PREFIXES(HttpHandlerTest, HandleUnimplementedCommand);
67  FRIEND_TEST_ALL_PREFIXES(HttpHandlerTest, HandleCommand);
68  typedef std::vector<CommandMapping> CommandMap;
69
70  Command WrapToCommand(const SessionCommand& session_command);
71  Command WrapToCommand(const WindowCommand& window_command);
72  Command WrapToCommand(const ElementCommand& element_command);
73  void HandleInternal(const net::HttpServerRequestInfo& request,
74                      HttpResponse* response);
75  bool HandleWebDriverCommand(
76      const net::HttpServerRequestInfo& request,
77      const std::string& trimmed_path,
78      HttpResponse* response);
79
80  Log* log_;
81  base::Thread io_thread_;
82  std::string url_base_;
83  scoped_refptr<URLRequestContextGetter> context_getter_;
84  SyncWebSocketFactory socket_factory_;
85  SessionMap session_map_;
86  scoped_ptr<CommandMap> command_map_;
87  scoped_ptr<Adb> adb_;
88  scoped_ptr<DeviceManager> device_manager_;
89
90  DISALLOW_COPY_AND_ASSIGN(HttpHandler);
91};
92
93namespace internal {
94
95extern const char kNewSessionPathPattern[];
96
97bool MatchesCommand(const std::string& method,
98                    const std::string& path,
99                    const CommandMapping& command,
100                    std::string* session_id,
101                    base::DictionaryValue* out_params);
102
103}  // namespace internal
104
105#endif  // CHROME_TEST_CHROMEDRIVER_SERVER_HTTP_HANDLER_H_
106