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 "net/test/embedded_test_server/embedded_test_server.h" | 5 #include "net/test/embedded_test_server/embedded_test_server.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 | 92 |
93 void HttpListenSocket::Listen() { | 93 void HttpListenSocket::Listen() { |
94 DCHECK(thread_checker_.CalledOnValidThread()); | 94 DCHECK(thread_checker_.CalledOnValidThread()); |
95 TCPListenSocket::Listen(); | 95 TCPListenSocket::Listen(); |
96 } | 96 } |
97 | 97 |
98 HttpListenSocket::~HttpListenSocket() { | 98 HttpListenSocket::~HttpListenSocket() { |
99 DCHECK(thread_checker_.CalledOnValidThread()); | 99 DCHECK(thread_checker_.CalledOnValidThread()); |
100 } | 100 } |
101 | 101 |
| 102 void HttpListenSocket::DetachFromThread() { |
| 103 thread_checker_.DetachFromThread(); |
| 104 } |
| 105 |
102 EmbeddedTestServer::EmbeddedTestServer() | 106 EmbeddedTestServer::EmbeddedTestServer() |
103 : port_(-1), | 107 : port_(-1), |
104 weak_factory_(this) { | 108 weak_factory_(this) { |
105 DCHECK(thread_checker_.CalledOnValidThread()); | 109 DCHECK(thread_checker_.CalledOnValidThread()); |
106 } | 110 } |
107 | 111 |
108 EmbeddedTestServer::~EmbeddedTestServer() { | 112 EmbeddedTestServer::~EmbeddedTestServer() { |
109 DCHECK(thread_checker_.CalledOnValidThread()); | 113 DCHECK(thread_checker_.CalledOnValidThread()); |
110 | 114 |
111 if (Started() && !ShutdownAndWaitUntilComplete()) { | 115 if (Started() && !ShutdownAndWaitUntilComplete()) { |
112 LOG(ERROR) << "EmbeddedTestServer failed to shut down."; | 116 LOG(ERROR) << "EmbeddedTestServer failed to shut down."; |
113 } | 117 } |
114 } | 118 } |
115 | 119 |
116 bool EmbeddedTestServer::InitializeAndWaitUntilReady() { | 120 bool EmbeddedTestServer::InitializeAndWaitUntilReady() { |
117 base::Thread::Options thread_options; | 121 StartThread(); |
118 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; | |
119 io_thread_.reset(new base::Thread("EmbeddedTestServer io thread")); | |
120 CHECK(io_thread_->StartWithOptions(thread_options)); | |
121 | |
122 DCHECK(thread_checker_.CalledOnValidThread()); | 122 DCHECK(thread_checker_.CalledOnValidThread()); |
123 | 123 |
124 if (!PostTaskToIOThreadAndWait(base::Bind( | 124 if (!PostTaskToIOThreadAndWait(base::Bind( |
125 &EmbeddedTestServer::InitializeOnIOThread, base::Unretained(this)))) { | 125 &EmbeddedTestServer::InitializeOnIOThread, base::Unretained(this)))) { |
126 return false; | 126 return false; |
127 } | 127 } |
| 128 return Started() && base_url_.is_valid(); |
| 129 } |
128 | 130 |
129 return Started() && base_url_.is_valid(); | 131 void EmbeddedTestServer::StartThread() { |
| 132 DCHECK(!io_thread_.get()); |
| 133 base::Thread::Options thread_options; |
| 134 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; |
| 135 io_thread_.reset(new base::Thread("EmbeddedTestServer io thread")); |
| 136 CHECK(io_thread_->StartWithOptions(thread_options)); |
| 137 } |
| 138 |
| 139 void EmbeddedTestServer::StopThread() { |
| 140 io_thread_->Stop(); |
| 141 io_thread_.reset(); |
| 142 thread_checker_.DetachFromThread(); |
| 143 listen_socket_->DetachFromThread(); |
130 } | 144 } |
131 | 145 |
132 bool EmbeddedTestServer::ShutdownAndWaitUntilComplete() { | 146 bool EmbeddedTestServer::ShutdownAndWaitUntilComplete() { |
133 DCHECK(thread_checker_.CalledOnValidThread()); | 147 DCHECK(thread_checker_.CalledOnValidThread()); |
134 | 148 |
135 return PostTaskToIOThreadAndWait(base::Bind( | 149 return PostTaskToIOThreadAndWait(base::Bind( |
136 &EmbeddedTestServer::ShutdownOnIOThread, base::Unretained(this))); | 150 &EmbeddedTestServer::ShutdownOnIOThread, base::Unretained(this))); |
137 } | 151 } |
138 | 152 |
139 void EmbeddedTestServer::InitializeOnIOThread() { | 153 void EmbeddedTestServer::InitializeOnIOThread() { |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 FROM_HERE, closure, run_loop.QuitClosure())) { | 297 FROM_HERE, closure, run_loop.QuitClosure())) { |
284 return false; | 298 return false; |
285 } | 299 } |
286 run_loop.Run(); | 300 run_loop.Run(); |
287 | 301 |
288 return true; | 302 return true; |
289 } | 303 } |
290 | 304 |
291 } // namespace test_server | 305 } // namespace test_server |
292 } // namespace net | 306 } // namespace net |
OLD | NEW |