OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/devtools_http_handler/devtools_http_handler.h" | 5 #include "content/browser/devtools/devtools_http_handler.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
13 #include "base/files/scoped_temp_dir.h" | 13 #include "base/files/scoped_temp_dir.h" |
14 #include "base/location.h" | 14 #include "base/location.h" |
15 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
16 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
17 #include "base/single_thread_task_runner.h" | 17 #include "base/single_thread_task_runner.h" |
18 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
19 #include "base/threading/thread_task_runner_handle.h" | 19 #include "base/threading/thread_task_runner_handle.h" |
20 #include "base/values.h" | 20 #include "base/values.h" |
21 #include "components/devtools_http_handler/devtools_http_handler_delegate.h" | 21 #include "content/public/browser/devtools_manager_delegate.h" |
22 #include "content/public/browser/devtools_socket_factory.h" | 22 #include "content/public/browser/devtools_socket_factory.h" |
23 #include "content/public/test/test_browser_thread_bundle.h" | 23 #include "content/public/test/test_browser_thread_bundle.h" |
24 #include "content/public/test/test_utils.h" | 24 #include "content/public/test/test_utils.h" |
25 #include "net/base/ip_address.h" | 25 #include "net/base/ip_address.h" |
26 #include "net/base/ip_endpoint.h" | 26 #include "net/base/ip_endpoint.h" |
27 #include "net/base/net_errors.h" | 27 #include "net/base/net_errors.h" |
28 #include "net/socket/server_socket.h" | 28 #include "net/socket/server_socket.h" |
29 #include "testing/gtest/include/gtest/gtest.h" | 29 #include "testing/gtest/include/gtest/gtest.h" |
30 | 30 |
31 using content::BrowserThread; | 31 namespace content { |
32 | |
33 namespace devtools_http_handler { | |
34 namespace { | 32 namespace { |
35 | 33 |
36 const uint16_t kDummyPort = 4321; | 34 const uint16_t kDummyPort = 4321; |
37 const base::FilePath::CharType kDevToolsActivePortFileName[] = | 35 const base::FilePath::CharType kDevToolsActivePortFileName[] = |
38 FILE_PATH_LITERAL("DevToolsActivePort"); | 36 FILE_PATH_LITERAL("DevToolsActivePort"); |
39 | 37 |
40 class DummyServerSocket : public net::ServerSocket { | 38 class DummyServerSocket : public net::ServerSocket { |
41 public: | 39 public: |
42 DummyServerSocket() {} | 40 DummyServerSocket() {} |
43 | 41 |
(...skipping 10 matching lines...) Expand all Loading... |
54 int Accept(std::unique_ptr<net::StreamSocket>* socket, | 52 int Accept(std::unique_ptr<net::StreamSocket>* socket, |
55 const net::CompletionCallback& callback) override { | 53 const net::CompletionCallback& callback) override { |
56 return net::ERR_IO_PENDING; | 54 return net::ERR_IO_PENDING; |
57 } | 55 } |
58 }; | 56 }; |
59 | 57 |
60 void QuitFromHandlerThread(const base::Closure& quit_closure) { | 58 void QuitFromHandlerThread(const base::Closure& quit_closure) { |
61 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, quit_closure); | 59 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, quit_closure); |
62 } | 60 } |
63 | 61 |
64 class DummyServerSocketFactory : public content::DevToolsSocketFactory { | 62 class DummyServerSocketFactory : public DevToolsSocketFactory { |
65 public: | 63 public: |
66 DummyServerSocketFactory(base::Closure quit_closure_1, | 64 DummyServerSocketFactory(base::Closure quit_closure_1, |
67 base::Closure quit_closure_2) | 65 base::Closure quit_closure_2) |
68 : quit_closure_1_(quit_closure_1), | 66 : quit_closure_1_(quit_closure_1), |
69 quit_closure_2_(quit_closure_2) {} | 67 quit_closure_2_(quit_closure_2) {} |
70 | 68 |
71 ~DummyServerSocketFactory() override { | 69 ~DummyServerSocketFactory() override { |
72 BrowserThread::PostTask( | 70 BrowserThread::PostTask( |
73 BrowserThread::UI, FROM_HERE, quit_closure_2_); | 71 BrowserThread::UI, FROM_HERE, quit_closure_2_); |
74 } | 72 } |
(...skipping 22 matching lines...) Expand all Loading... |
97 } | 95 } |
98 | 96 |
99 private: | 97 private: |
100 std::unique_ptr<net::ServerSocket> CreateForHttpServer() override { | 98 std::unique_ptr<net::ServerSocket> CreateForHttpServer() override { |
101 base::ThreadTaskRunnerHandle::Get()->PostTask( | 99 base::ThreadTaskRunnerHandle::Get()->PostTask( |
102 FROM_HERE, base::Bind(&QuitFromHandlerThread, quit_closure_1_)); | 100 FROM_HERE, base::Bind(&QuitFromHandlerThread, quit_closure_1_)); |
103 return nullptr; | 101 return nullptr; |
104 } | 102 } |
105 }; | 103 }; |
106 | 104 |
107 class DummyDelegate : public DevToolsHttpHandlerDelegate { | 105 class DummyDelegate : public DevToolsManagerDelegate { |
108 public: | 106 public: |
109 std::string GetDiscoveryPageHTML() override { return std::string(); } | 107 void Inspect(DevToolsAgentHost* agent_host) override {} |
110 | 108 void DevToolsAgentStateChanged(DevToolsAgentHost* agent_host, |
| 109 bool attached) override {} |
| 110 std::string GetTargetType(RenderFrameHost* host) override { return ""; } |
| 111 std::string GetTargetTitle(RenderFrameHost* host) override { return ""; } |
| 112 scoped_refptr<DevToolsAgentHost> CreateNewTarget(const GURL& url) override { |
| 113 return nullptr; |
| 114 } |
| 115 base::DictionaryValue* HandleCommand( |
| 116 DevToolsAgentHost* agent_host, |
| 117 base::DictionaryValue* command) override { return nullptr; } |
| 118 std::string GetDiscoveryPageHTML() override { return ""; } |
111 std::string GetFrontendResource(const std::string& path) override { | 119 std::string GetFrontendResource(const std::string& path) override { |
112 return std::string(); | 120 return ""; |
113 } | 121 } |
114 }; | 122 }; |
115 | 123 |
116 } | 124 } |
117 | 125 |
118 class DevToolsHttpHandlerTest : public testing::Test { | 126 class DevToolsHttpHandlerTest : public testing::Test { |
119 public: | 127 public: |
120 DevToolsHttpHandlerTest() : testing::Test() { } | 128 DevToolsHttpHandlerTest() : testing::Test() { } |
121 | 129 |
122 private: | 130 private: |
123 content::TestBrowserThreadBundle thread_bundle_; | 131 content::TestBrowserThreadBundle thread_bundle_; |
124 }; | 132 }; |
125 | 133 |
126 TEST_F(DevToolsHttpHandlerTest, TestStartStop) { | 134 TEST_F(DevToolsHttpHandlerTest, TestStartStop) { |
127 base::RunLoop run_loop, run_loop_2; | 135 base::RunLoop run_loop, run_loop_2; |
128 std::unique_ptr<content::DevToolsSocketFactory> factory( | 136 std::unique_ptr<DevToolsSocketFactory> factory( |
129 new DummyServerSocketFactory(run_loop.QuitClosure(), | 137 new DummyServerSocketFactory(run_loop.QuitClosure(), |
130 run_loop_2.QuitClosure())); | 138 run_loop_2.QuitClosure())); |
131 std::unique_ptr<DevToolsHttpHandler> devtools_http_handler( | 139 DevToolsAgentHost::StartRemoteDebuggingServer( |
132 new DevToolsHttpHandler(std::move(factory), std::string(), | 140 std::move(factory), std::string(), base::FilePath(), base::FilePath(), |
133 new DummyDelegate(), base::FilePath(), | 141 std::string(), std::string()); |
134 base::FilePath(), std::string(), std::string())); | |
135 // Our dummy socket factory will post a quit message once the server will | 142 // Our dummy socket factory will post a quit message once the server will |
136 // become ready. | 143 // become ready. |
137 run_loop.Run(); | 144 run_loop.Run(); |
138 devtools_http_handler.reset(); | 145 DevToolsAgentHost::StopRemoteDebuggingServer(); |
139 // Make sure the handler actually stops. | 146 // Make sure the handler actually stops. |
140 run_loop_2.Run(); | 147 run_loop_2.Run(); |
141 } | 148 } |
142 | 149 |
143 TEST_F(DevToolsHttpHandlerTest, TestServerSocketFailed) { | 150 TEST_F(DevToolsHttpHandlerTest, TestServerSocketFailed) { |
144 base::RunLoop run_loop, run_loop_2; | 151 base::RunLoop run_loop, run_loop_2; |
145 std::unique_ptr<content::DevToolsSocketFactory> factory( | 152 std::unique_ptr<DevToolsSocketFactory> factory( |
146 new FailingServerSocketFactory(run_loop.QuitClosure(), | 153 new FailingServerSocketFactory(run_loop.QuitClosure(), |
147 run_loop_2.QuitClosure())); | 154 run_loop_2.QuitClosure())); |
148 std::unique_ptr<DevToolsHttpHandler> devtools_http_handler( | 155 DevToolsAgentHost::StartRemoteDebuggingServer( |
149 new DevToolsHttpHandler(std::move(factory), std::string(), | 156 std::move(factory), std::string(), base::FilePath(), base::FilePath(), |
150 new DummyDelegate(), base::FilePath(), | 157 std::string(), std::string()); |
151 base::FilePath(), std::string(), std::string())); | |
152 // Our dummy socket factory will post a quit message once the server will | 158 // Our dummy socket factory will post a quit message once the server will |
153 // become ready. | 159 // become ready. |
154 run_loop.Run(); | 160 run_loop.Run(); |
155 for (int i = 0; i < 5; i++) { | 161 for (int i = 0; i < 5; i++) { |
156 RunAllPendingInMessageLoop(BrowserThread::UI); | 162 RunAllPendingInMessageLoop(BrowserThread::UI); |
157 RunAllPendingInMessageLoop(BrowserThread::FILE); | 163 RunAllPendingInMessageLoop(BrowserThread::FILE); |
158 } | 164 } |
159 devtools_http_handler.reset(); | 165 DevToolsAgentHost::StopRemoteDebuggingServer(); |
160 // Make sure the handler actually stops. | 166 // Make sure the handler actually stops. |
161 run_loop_2.Run(); | 167 run_loop_2.Run(); |
162 } | 168 } |
163 | 169 |
164 | 170 |
165 TEST_F(DevToolsHttpHandlerTest, TestDevToolsActivePort) { | 171 TEST_F(DevToolsHttpHandlerTest, TestDevToolsActivePort) { |
166 base::RunLoop run_loop, run_loop_2; | 172 base::RunLoop run_loop, run_loop_2; |
167 base::ScopedTempDir temp_dir; | 173 base::ScopedTempDir temp_dir; |
168 EXPECT_TRUE(temp_dir.CreateUniqueTempDir()); | 174 EXPECT_TRUE(temp_dir.CreateUniqueTempDir()); |
169 std::unique_ptr<content::DevToolsSocketFactory> factory( | 175 std::unique_ptr<DevToolsSocketFactory> factory( |
170 new DummyServerSocketFactory(run_loop.QuitClosure(), | 176 new DummyServerSocketFactory(run_loop.QuitClosure(), |
171 run_loop_2.QuitClosure())); | 177 run_loop_2.QuitClosure())); |
172 std::unique_ptr<DevToolsHttpHandler> devtools_http_handler( | 178 |
173 new DevToolsHttpHandler(std::move(factory), std::string(), | 179 DevToolsAgentHost::StartRemoteDebuggingServer( |
174 new DummyDelegate(), temp_dir.path(), | 180 std::move(factory), std::string(), base::FilePath(), base::FilePath(), |
175 base::FilePath(), std::string(), std::string())); | 181 std::string(), std::string()); |
176 // Our dummy socket factory will post a quit message once the server will | 182 // Our dummy socket factory will post a quit message once the server will |
177 // become ready. | 183 // become ready. |
178 run_loop.Run(); | 184 run_loop.Run(); |
179 devtools_http_handler.reset(); | 185 DevToolsAgentHost::StopRemoteDebuggingServer(); |
180 // Make sure the handler actually stops. | 186 // Make sure the handler actually stops. |
181 run_loop_2.Run(); | 187 run_loop_2.Run(); |
182 | 188 |
183 // Now make sure the DevToolsActivePort was written into the | 189 // Now make sure the DevToolsActivePort was written into the |
184 // temporary directory and its contents are as expected. | 190 // temporary directory and its contents are as expected. |
185 base::FilePath active_port_file = temp_dir.path().Append( | 191 base::FilePath active_port_file = temp_dir.path().Append( |
186 kDevToolsActivePortFileName); | 192 kDevToolsActivePortFileName); |
187 EXPECT_TRUE(base::PathExists(active_port_file)); | 193 EXPECT_TRUE(base::PathExists(active_port_file)); |
188 std::string file_contents; | 194 std::string file_contents; |
189 EXPECT_TRUE(base::ReadFileToString(active_port_file, &file_contents)); | 195 EXPECT_TRUE(base::ReadFileToString(active_port_file, &file_contents)); |
190 int port = 0; | 196 int port = 0; |
191 EXPECT_TRUE(base::StringToInt(file_contents, &port)); | 197 EXPECT_TRUE(base::StringToInt(file_contents, &port)); |
192 EXPECT_EQ(static_cast<int>(kDummyPort), port); | 198 EXPECT_EQ(static_cast<int>(kDummyPort), port); |
193 } | 199 } |
194 | 200 |
195 } // namespace devtools_http_handler | 201 } // namespace content |
OLD | NEW |