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.
5 #ifndef CHROME_TEST_CHROMEDRIVER_SERVER_HTTP_HANDLER_H_
6 #define CHROME_TEST_CHROMEDRIVER_SERVER_HTTP_HANDLER_H_
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"
27 class DictionaryValue
;
28 class SingleThreadTaskRunner
;
32 class HttpServerRequestInfo
;
33 class HttpServerResponseInfo
;
40 class URLRequestContextGetter
;
48 struct CommandMapping
{
49 CommandMapping(HttpMethod method
,
50 const std::string
& path_pattern
,
51 const Command
& command
);
55 std::string path_pattern
;
59 typedef base::Callback
<void(scoped_ptr
<net::HttpServerResponseInfo
>)>
60 HttpResponseSenderFunc
;
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
,
69 scoped_ptr
<PortServer
> port_server
);
72 void Handle(const net::HttpServerRequestInfo
& request
,
73 const HttpResponseSenderFunc
& send_response_func
);
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
;
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
,
94 scoped_ptr
<base::Value
> value
,
95 const std::string
& session_id
);
96 scoped_ptr
<net::HttpServerResponseInfo
> PrepareResponseHelper(
97 const std::string
& trimmed_path
,
99 scoped_ptr
<base::Value
> value
,
100 const std::string
& session_id
);
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_
;
115 base::WeakPtrFactory
<HttpHandler
> weak_ptr_factory_
;
117 DISALLOW_COPY_AND_ASSIGN(HttpHandler
);
122 extern const char kNewSessionPathPattern
[];
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
);
130 } // namespace internal
132 #endif // CHROME_TEST_CHROMEDRIVER_SERVER_HTTP_HANDLER_H_