| 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/content_browser_client.h" |
| 22 #include "content/public/browser/devtools_manager_delegate.h" |
| 22 #include "content/public/browser/devtools_socket_factory.h" | 23 #include "content/public/browser/devtools_socket_factory.h" |
| 23 #include "content/public/test/test_browser_thread_bundle.h" | 24 #include "content/public/test/test_browser_thread_bundle.h" |
| 24 #include "content/public/test/test_utils.h" | 25 #include "content/public/test/test_utils.h" |
| 25 #include "net/base/ip_address.h" | 26 #include "net/base/ip_address.h" |
| 26 #include "net/base/ip_endpoint.h" | 27 #include "net/base/ip_endpoint.h" |
| 27 #include "net/base/net_errors.h" | 28 #include "net/base/net_errors.h" |
| 28 #include "net/socket/server_socket.h" | 29 #include "net/socket/server_socket.h" |
| 29 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
| 30 | 31 |
| 31 using content::BrowserThread; | 32 namespace content { |
| 32 | |
| 33 namespace devtools_http_handler { | |
| 34 namespace { | 33 namespace { |
| 35 | 34 |
| 36 const uint16_t kDummyPort = 4321; | 35 const uint16_t kDummyPort = 4321; |
| 37 const base::FilePath::CharType kDevToolsActivePortFileName[] = | 36 const base::FilePath::CharType kDevToolsActivePortFileName[] = |
| 38 FILE_PATH_LITERAL("DevToolsActivePort"); | 37 FILE_PATH_LITERAL("DevToolsActivePort"); |
| 39 | 38 |
| 40 class DummyServerSocket : public net::ServerSocket { | 39 class DummyServerSocket : public net::ServerSocket { |
| 41 public: | 40 public: |
| 42 DummyServerSocket() {} | 41 DummyServerSocket() {} |
| 43 | 42 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 54 int Accept(std::unique_ptr<net::StreamSocket>* socket, | 53 int Accept(std::unique_ptr<net::StreamSocket>* socket, |
| 55 const net::CompletionCallback& callback) override { | 54 const net::CompletionCallback& callback) override { |
| 56 return net::ERR_IO_PENDING; | 55 return net::ERR_IO_PENDING; |
| 57 } | 56 } |
| 58 }; | 57 }; |
| 59 | 58 |
| 60 void QuitFromHandlerThread(const base::Closure& quit_closure) { | 59 void QuitFromHandlerThread(const base::Closure& quit_closure) { |
| 61 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, quit_closure); | 60 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, quit_closure); |
| 62 } | 61 } |
| 63 | 62 |
| 64 class DummyServerSocketFactory : public content::DevToolsSocketFactory { | 63 class DummyServerSocketFactory : public DevToolsSocketFactory { |
| 65 public: | 64 public: |
| 66 DummyServerSocketFactory(base::Closure quit_closure_1, | 65 DummyServerSocketFactory(base::Closure quit_closure_1, |
| 67 base::Closure quit_closure_2) | 66 base::Closure quit_closure_2) |
| 68 : quit_closure_1_(quit_closure_1), | 67 : quit_closure_1_(quit_closure_1), |
| 69 quit_closure_2_(quit_closure_2) {} | 68 quit_closure_2_(quit_closure_2) {} |
| 70 | 69 |
| 71 ~DummyServerSocketFactory() override { | 70 ~DummyServerSocketFactory() override { |
| 72 BrowserThread::PostTask( | 71 BrowserThread::PostTask( |
| 73 BrowserThread::UI, FROM_HERE, quit_closure_2_); | 72 BrowserThread::UI, FROM_HERE, quit_closure_2_); |
| 74 } | 73 } |
| (...skipping 22 matching lines...) Expand all Loading... |
| 97 } | 96 } |
| 98 | 97 |
| 99 private: | 98 private: |
| 100 std::unique_ptr<net::ServerSocket> CreateForHttpServer() override { | 99 std::unique_ptr<net::ServerSocket> CreateForHttpServer() override { |
| 101 base::ThreadTaskRunnerHandle::Get()->PostTask( | 100 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 102 FROM_HERE, base::Bind(&QuitFromHandlerThread, quit_closure_1_)); | 101 FROM_HERE, base::Bind(&QuitFromHandlerThread, quit_closure_1_)); |
| 103 return nullptr; | 102 return nullptr; |
| 104 } | 103 } |
| 105 }; | 104 }; |
| 106 | 105 |
| 107 class DummyDelegate : public DevToolsHttpHandlerDelegate { | 106 class BrowserClient : public ContentBrowserClient { |
| 108 public: | 107 public: |
| 109 std::string GetDiscoveryPageHTML() override { return std::string(); } | 108 BrowserClient() {} |
| 110 | 109 ~BrowserClient() override {} |
| 111 std::string GetFrontendResource(const std::string& path) override { | 110 DevToolsManagerDelegate* GetDevToolsManagerDelegate() override { |
| 112 return std::string(); | 111 return new DevToolsManagerDelegate(); |
| 113 } | 112 } |
| 114 }; | 113 }; |
| 115 | 114 |
| 116 } | 115 } |
| 117 | 116 |
| 118 class DevToolsHttpHandlerTest : public testing::Test { | 117 class DevToolsHttpHandlerTest : public testing::Test { |
| 119 public: | 118 public: |
| 120 DevToolsHttpHandlerTest() : testing::Test() { } | 119 DevToolsHttpHandlerTest() : testing::Test() { } |
| 121 | 120 |
| 121 void SetUp() override { |
| 122 content_client_.reset(new ContentClient()); |
| 123 browser_content_client_.reset(new BrowserClient()); |
| 124 SetBrowserClientForTesting(browser_content_client_.get()); |
| 125 } |
| 126 |
| 122 private: | 127 private: |
| 128 std::unique_ptr<ContentClient> content_client_; |
| 129 std::unique_ptr<ContentBrowserClient> browser_content_client_; |
| 123 content::TestBrowserThreadBundle thread_bundle_; | 130 content::TestBrowserThreadBundle thread_bundle_; |
| 124 }; | 131 }; |
| 125 | 132 |
| 126 TEST_F(DevToolsHttpHandlerTest, TestStartStop) { | 133 TEST_F(DevToolsHttpHandlerTest, TestStartStop) { |
| 127 base::RunLoop run_loop, run_loop_2; | 134 base::RunLoop run_loop, run_loop_2; |
| 128 std::unique_ptr<content::DevToolsSocketFactory> factory( | 135 std::unique_ptr<DevToolsSocketFactory> factory( |
| 129 new DummyServerSocketFactory(run_loop.QuitClosure(), | 136 new DummyServerSocketFactory(run_loop.QuitClosure(), |
| 130 run_loop_2.QuitClosure())); | 137 run_loop_2.QuitClosure())); |
| 131 std::unique_ptr<DevToolsHttpHandler> devtools_http_handler( | 138 DevToolsAgentHost::StartRemoteDebuggingServer( |
| 132 new DevToolsHttpHandler(std::move(factory), std::string(), | 139 std::move(factory), std::string(), base::FilePath(), base::FilePath(), |
| 133 new DummyDelegate(), base::FilePath(), | 140 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 | 141 // Our dummy socket factory will post a quit message once the server will |
| 136 // become ready. | 142 // become ready. |
| 137 run_loop.Run(); | 143 run_loop.Run(); |
| 138 devtools_http_handler.reset(); | 144 DevToolsAgentHost::StopRemoteDebuggingServer(); |
| 139 // Make sure the handler actually stops. | 145 // Make sure the handler actually stops. |
| 140 run_loop_2.Run(); | 146 run_loop_2.Run(); |
| 141 } | 147 } |
| 142 | 148 |
| 143 TEST_F(DevToolsHttpHandlerTest, TestServerSocketFailed) { | 149 TEST_F(DevToolsHttpHandlerTest, TestServerSocketFailed) { |
| 144 base::RunLoop run_loop, run_loop_2; | 150 base::RunLoop run_loop, run_loop_2; |
| 145 std::unique_ptr<content::DevToolsSocketFactory> factory( | 151 std::unique_ptr<DevToolsSocketFactory> factory( |
| 146 new FailingServerSocketFactory(run_loop.QuitClosure(), | 152 new FailingServerSocketFactory(run_loop.QuitClosure(), |
| 147 run_loop_2.QuitClosure())); | 153 run_loop_2.QuitClosure())); |
| 148 std::unique_ptr<DevToolsHttpHandler> devtools_http_handler( | 154 DevToolsAgentHost::StartRemoteDebuggingServer( |
| 149 new DevToolsHttpHandler(std::move(factory), std::string(), | 155 std::move(factory), std::string(), base::FilePath(), base::FilePath(), |
| 150 new DummyDelegate(), base::FilePath(), | 156 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 | 157 // Our dummy socket factory will post a quit message once the server will |
| 153 // become ready. | 158 // become ready. |
| 154 run_loop.Run(); | 159 run_loop.Run(); |
| 155 for (int i = 0; i < 5; i++) { | 160 for (int i = 0; i < 5; i++) { |
| 156 RunAllPendingInMessageLoop(BrowserThread::UI); | 161 RunAllPendingInMessageLoop(BrowserThread::UI); |
| 157 RunAllPendingInMessageLoop(BrowserThread::FILE); | 162 RunAllPendingInMessageLoop(BrowserThread::FILE); |
| 158 } | 163 } |
| 159 devtools_http_handler.reset(); | 164 DevToolsAgentHost::StopRemoteDebuggingServer(); |
| 160 // Make sure the handler actually stops. | 165 // Make sure the handler actually stops. |
| 161 run_loop_2.Run(); | 166 run_loop_2.Run(); |
| 162 } | 167 } |
| 163 | 168 |
| 164 | 169 |
| 165 TEST_F(DevToolsHttpHandlerTest, TestDevToolsActivePort) { | 170 TEST_F(DevToolsHttpHandlerTest, TestDevToolsActivePort) { |
| 166 base::RunLoop run_loop, run_loop_2; | 171 base::RunLoop run_loop, run_loop_2; |
| 167 base::ScopedTempDir temp_dir; | 172 base::ScopedTempDir temp_dir; |
| 168 EXPECT_TRUE(temp_dir.CreateUniqueTempDir()); | 173 EXPECT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 169 std::unique_ptr<content::DevToolsSocketFactory> factory( | 174 std::unique_ptr<DevToolsSocketFactory> factory( |
| 170 new DummyServerSocketFactory(run_loop.QuitClosure(), | 175 new DummyServerSocketFactory(run_loop.QuitClosure(), |
| 171 run_loop_2.QuitClosure())); | 176 run_loop_2.QuitClosure())); |
| 172 std::unique_ptr<DevToolsHttpHandler> devtools_http_handler( | 177 |
| 173 new DevToolsHttpHandler(std::move(factory), std::string(), | 178 DevToolsAgentHost::StartRemoteDebuggingServer( |
| 174 new DummyDelegate(), temp_dir.path(), | 179 std::move(factory), std::string(), temp_dir.path(), base::FilePath(), |
| 175 base::FilePath(), std::string(), std::string())); | 180 std::string(), std::string()); |
| 176 // Our dummy socket factory will post a quit message once the server will | 181 // Our dummy socket factory will post a quit message once the server will |
| 177 // become ready. | 182 // become ready. |
| 178 run_loop.Run(); | 183 run_loop.Run(); |
| 179 devtools_http_handler.reset(); | 184 DevToolsAgentHost::StopRemoteDebuggingServer(); |
| 180 // Make sure the handler actually stops. | 185 // Make sure the handler actually stops. |
| 181 run_loop_2.Run(); | 186 run_loop_2.Run(); |
| 182 | 187 |
| 183 // Now make sure the DevToolsActivePort was written into the | 188 // Now make sure the DevToolsActivePort was written into the |
| 184 // temporary directory and its contents are as expected. | 189 // temporary directory and its contents are as expected. |
| 185 base::FilePath active_port_file = temp_dir.path().Append( | 190 base::FilePath active_port_file = temp_dir.path().Append( |
| 186 kDevToolsActivePortFileName); | 191 kDevToolsActivePortFileName); |
| 187 EXPECT_TRUE(base::PathExists(active_port_file)); | 192 EXPECT_TRUE(base::PathExists(active_port_file)); |
| 188 std::string file_contents; | 193 std::string file_contents; |
| 189 EXPECT_TRUE(base::ReadFileToString(active_port_file, &file_contents)); | 194 EXPECT_TRUE(base::ReadFileToString(active_port_file, &file_contents)); |
| 190 int port = 0; | 195 int port = 0; |
| 191 EXPECT_TRUE(base::StringToInt(file_contents, &port)); | 196 EXPECT_TRUE(base::StringToInt(file_contents, &port)); |
| 192 EXPECT_EQ(static_cast<int>(kDummyPort), port); | 197 EXPECT_EQ(static_cast<int>(kDummyPort), port); |
| 193 } | 198 } |
| 194 | 199 |
| 195 } // namespace devtools_http_handler | 200 } // namespace content |
| OLD | NEW |