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 <stddef.h> | 5 #include <stddef.h> |
6 #include <stdint.h> | 6 #include <stdint.h> |
7 | 7 |
8 #include <algorithm> | 8 #include <algorithm> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 // added back pressure on the TraceComplete message protocol - crbug.com/456845. | 76 // added back pressure on the TraceComplete message protocol - crbug.com/456845. |
77 const int32_t kSendBufferSizeForDevTools = 256 * 1024 * 1024; // 256Mb | 77 const int32_t kSendBufferSizeForDevTools = 256 * 1024 * 1024; // 256Mb |
78 | 78 |
79 } // namespace | 79 } // namespace |
80 | 80 |
81 // ServerWrapper ------------------------------------------------------------- | 81 // ServerWrapper ------------------------------------------------------------- |
82 // All methods in this class are only called on handler thread. | 82 // All methods in this class are only called on handler thread. |
83 class ServerWrapper : net::HttpServer::Delegate { | 83 class ServerWrapper : net::HttpServer::Delegate { |
84 public: | 84 public: |
85 ServerWrapper(base::WeakPtr<DevToolsHttpHandler> handler, | 85 ServerWrapper(base::WeakPtr<DevToolsHttpHandler> handler, |
86 scoped_ptr<net::ServerSocket> socket, | 86 std::unique_ptr<net::ServerSocket> socket, |
87 const base::FilePath& frontend_dir, | 87 const base::FilePath& frontend_dir, |
88 bool bundles_resources); | 88 bool bundles_resources); |
89 | 89 |
90 int GetLocalAddress(net::IPEndPoint* address); | 90 int GetLocalAddress(net::IPEndPoint* address); |
91 | 91 |
92 void AcceptWebSocket(int connection_id, | 92 void AcceptWebSocket(int connection_id, |
93 const net::HttpServerRequestInfo& request); | 93 const net::HttpServerRequestInfo& request); |
94 void SendOverWebSocket(int connection_id, const std::string& message); | 94 void SendOverWebSocket(int connection_id, const std::string& message); |
95 void SendResponse(int connection_id, | 95 void SendResponse(int connection_id, |
96 const net::HttpServerResponseInfo& response); | 96 const net::HttpServerResponseInfo& response); |
(...skipping 13 matching lines...) Expand all Loading... |
110 void OnConnect(int connection_id) override {} | 110 void OnConnect(int connection_id) override {} |
111 void OnHttpRequest(int connection_id, | 111 void OnHttpRequest(int connection_id, |
112 const net::HttpServerRequestInfo& info) override; | 112 const net::HttpServerRequestInfo& info) override; |
113 void OnWebSocketRequest(int connection_id, | 113 void OnWebSocketRequest(int connection_id, |
114 const net::HttpServerRequestInfo& info) override; | 114 const net::HttpServerRequestInfo& info) override; |
115 void OnWebSocketMessage(int connection_id, | 115 void OnWebSocketMessage(int connection_id, |
116 const std::string& data) override; | 116 const std::string& data) override; |
117 void OnClose(int connection_id) override; | 117 void OnClose(int connection_id) override; |
118 | 118 |
119 base::WeakPtr<DevToolsHttpHandler> handler_; | 119 base::WeakPtr<DevToolsHttpHandler> handler_; |
120 scoped_ptr<net::HttpServer> server_; | 120 std::unique_ptr<net::HttpServer> server_; |
121 base::FilePath frontend_dir_; | 121 base::FilePath frontend_dir_; |
122 bool bundles_resources_; | 122 bool bundles_resources_; |
123 }; | 123 }; |
124 | 124 |
125 ServerWrapper::ServerWrapper(base::WeakPtr<DevToolsHttpHandler> handler, | 125 ServerWrapper::ServerWrapper(base::WeakPtr<DevToolsHttpHandler> handler, |
126 scoped_ptr<net::ServerSocket> socket, | 126 std::unique_ptr<net::ServerSocket> socket, |
127 const base::FilePath& frontend_dir, | 127 const base::FilePath& frontend_dir, |
128 bool bundles_resources) | 128 bool bundles_resources) |
129 : handler_(handler), | 129 : handler_(handler), |
130 server_(new net::HttpServer(std::move(socket), this)), | 130 server_(new net::HttpServer(std::move(socket), this)), |
131 frontend_dir_(frontend_dir), | 131 frontend_dir_(frontend_dir), |
132 bundles_resources_(bundles_resources) {} | 132 bundles_resources_(bundles_resources) {} |
133 | 133 |
134 int ServerWrapper::GetLocalAddress(net::IPEndPoint* address) { | 134 int ServerWrapper::GetLocalAddress(net::IPEndPoint* address) { |
135 return server_->GetLocalAddress(address); | 135 return server_->GetLocalAddress(address); |
136 } | 136 } |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 } | 182 } |
183 if (socket_factory) { | 183 if (socket_factory) { |
184 DCHECK(thread); | 184 DCHECK(thread); |
185 thread->message_loop()->DeleteSoon(FROM_HERE, socket_factory); | 185 thread->message_loop()->DeleteSoon(FROM_HERE, socket_factory); |
186 } | 186 } |
187 if (thread) { | 187 if (thread) { |
188 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, thread); | 188 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, thread); |
189 } | 189 } |
190 } | 190 } |
191 | 191 |
192 void ServerStartedOnUI( | 192 void ServerStartedOnUI(base::WeakPtr<DevToolsHttpHandler> handler, |
193 base::WeakPtr<DevToolsHttpHandler> handler, | 193 base::Thread* thread, |
194 base::Thread* thread, | 194 ServerWrapper* server_wrapper, |
195 ServerWrapper* server_wrapper, | 195 DevToolsHttpHandler::ServerSocketFactory* socket_factory, |
196 DevToolsHttpHandler::ServerSocketFactory* socket_factory, | 196 std::unique_ptr<net::IPEndPoint> ip_address) { |
197 scoped_ptr<net::IPEndPoint> ip_address) { | |
198 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 197 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
199 if (handler && thread && server_wrapper) { | 198 if (handler && thread && server_wrapper) { |
200 handler->ServerStarted(thread, server_wrapper, socket_factory, | 199 handler->ServerStarted(thread, server_wrapper, socket_factory, |
201 std::move(ip_address)); | 200 std::move(ip_address)); |
202 } else { | 201 } else { |
203 TerminateOnUI(thread, server_wrapper, socket_factory); | 202 TerminateOnUI(thread, server_wrapper, socket_factory); |
204 } | 203 } |
205 } | 204 } |
206 | 205 |
207 void StartServerOnHandlerThread( | 206 void StartServerOnHandlerThread( |
208 base::WeakPtr<DevToolsHttpHandler> handler, | 207 base::WeakPtr<DevToolsHttpHandler> handler, |
209 base::Thread* thread, | 208 base::Thread* thread, |
210 DevToolsHttpHandler::ServerSocketFactory* server_socket_factory, | 209 DevToolsHttpHandler::ServerSocketFactory* server_socket_factory, |
211 const base::FilePath& output_directory, | 210 const base::FilePath& output_directory, |
212 const base::FilePath& frontend_dir, | 211 const base::FilePath& frontend_dir, |
213 bool bundles_resources) { | 212 bool bundles_resources) { |
214 DCHECK_EQ(thread->message_loop(), base::MessageLoop::current()); | 213 DCHECK_EQ(thread->message_loop(), base::MessageLoop::current()); |
215 ServerWrapper* server_wrapper = nullptr; | 214 ServerWrapper* server_wrapper = nullptr; |
216 scoped_ptr<net::ServerSocket> server_socket = | 215 std::unique_ptr<net::ServerSocket> server_socket = |
217 server_socket_factory->CreateForHttpServer(); | 216 server_socket_factory->CreateForHttpServer(); |
218 scoped_ptr<net::IPEndPoint> ip_address(new net::IPEndPoint); | 217 std::unique_ptr<net::IPEndPoint> ip_address(new net::IPEndPoint); |
219 if (server_socket) { | 218 if (server_socket) { |
220 server_wrapper = new ServerWrapper(handler, std::move(server_socket), | 219 server_wrapper = new ServerWrapper(handler, std::move(server_socket), |
221 frontend_dir, bundles_resources); | 220 frontend_dir, bundles_resources); |
222 if (!output_directory.empty()) | 221 if (!output_directory.empty()) |
223 server_wrapper->WriteActivePortToUserProfile(output_directory); | 222 server_wrapper->WriteActivePortToUserProfile(output_directory); |
224 | 223 |
225 if (server_wrapper->GetLocalAddress(ip_address.get()) != net::OK) | 224 if (server_wrapper->GetLocalAddress(ip_address.get()) != net::OK) |
226 ip_address.reset(); | 225 ip_address.reset(); |
227 } else { | 226 } else { |
228 ip_address.reset(); | 227 ip_address.reset(); |
229 LOG(ERROR) << "Cannot start http server for devtools. Stop devtools."; | 228 LOG(ERROR) << "Cannot start http server for devtools. Stop devtools."; |
230 } | 229 } |
231 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 230 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
232 base::Bind(&ServerStartedOnUI, | 231 base::Bind(&ServerStartedOnUI, |
233 handler, | 232 handler, |
234 thread, | 233 thread, |
235 server_wrapper, | 234 server_wrapper, |
236 server_socket_factory, | 235 server_socket_factory, |
237 base::Passed(&ip_address))); | 236 base::Passed(&ip_address))); |
238 } | 237 } |
239 | 238 |
240 void StartServerOnFile( | 239 void StartServerOnFile( |
241 base::WeakPtr<DevToolsHttpHandler> handler, | 240 base::WeakPtr<DevToolsHttpHandler> handler, |
242 DevToolsHttpHandler::ServerSocketFactory* server_socket_factory, | 241 DevToolsHttpHandler::ServerSocketFactory* server_socket_factory, |
243 const base::FilePath& output_directory, | 242 const base::FilePath& output_directory, |
244 const base::FilePath& frontend_dir, | 243 const base::FilePath& frontend_dir, |
245 bool bundles_resources) { | 244 bool bundles_resources) { |
246 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 245 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
247 scoped_ptr<base::Thread> thread(new base::Thread(kDevToolsHandlerThreadName)); | 246 std::unique_ptr<base::Thread> thread( |
| 247 new base::Thread(kDevToolsHandlerThreadName)); |
248 base::Thread::Options options; | 248 base::Thread::Options options; |
249 options.message_loop_type = base::MessageLoop::TYPE_IO; | 249 options.message_loop_type = base::MessageLoop::TYPE_IO; |
250 if (thread->StartWithOptions(options)) { | 250 if (thread->StartWithOptions(options)) { |
251 base::MessageLoop* message_loop = thread->message_loop(); | 251 base::MessageLoop* message_loop = thread->message_loop(); |
252 message_loop->task_runner()->PostTask( | 252 message_loop->task_runner()->PostTask( |
253 FROM_HERE, | 253 FROM_HERE, |
254 base::Bind(&StartServerOnHandlerThread, handler, | 254 base::Bind(&StartServerOnHandlerThread, handler, |
255 base::Unretained(thread.release()), server_socket_factory, | 255 base::Unretained(thread.release()), server_socket_factory, |
256 output_directory, frontend_dir, bundles_resources)); | 256 output_directory, frontend_dir, bundles_resources)); |
257 } | 257 } |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
322 scoped_refptr<DevToolsAgentHost> agent_host_; | 322 scoped_refptr<DevToolsAgentHost> agent_host_; |
323 }; | 323 }; |
324 | 324 |
325 static bool TimeComparator(const DevToolsTargetDescriptor* desc1, | 325 static bool TimeComparator(const DevToolsTargetDescriptor* desc1, |
326 const DevToolsTargetDescriptor* desc2) { | 326 const DevToolsTargetDescriptor* desc2) { |
327 return desc1->GetLastActivityTime() > desc2->GetLastActivityTime(); | 327 return desc1->GetLastActivityTime() > desc2->GetLastActivityTime(); |
328 } | 328 } |
329 | 329 |
330 // DevToolsHttpHandler::ServerSocketFactory ---------------------------------- | 330 // DevToolsHttpHandler::ServerSocketFactory ---------------------------------- |
331 | 331 |
332 scoped_ptr<net::ServerSocket> | 332 std::unique_ptr<net::ServerSocket> |
333 DevToolsHttpHandler::ServerSocketFactory::CreateForHttpServer() { | 333 DevToolsHttpHandler::ServerSocketFactory::CreateForHttpServer() { |
334 return scoped_ptr<net::ServerSocket>(); | 334 return nullptr; |
335 } | 335 } |
336 | 336 |
337 scoped_ptr<net::ServerSocket> | 337 std::unique_ptr<net::ServerSocket> |
338 DevToolsHttpHandler::ServerSocketFactory::CreateForTethering( | 338 DevToolsHttpHandler::ServerSocketFactory::CreateForTethering( |
339 std::string* name) { | 339 std::string* name) { |
340 return scoped_ptr<net::ServerSocket>(); | 340 return nullptr; |
341 } | 341 } |
342 | 342 |
343 // DevToolsHttpHandler ------------------------------------------------------- | 343 // DevToolsHttpHandler ------------------------------------------------------- |
344 | 344 |
345 DevToolsHttpHandler::~DevToolsHttpHandler() { | 345 DevToolsHttpHandler::~DevToolsHttpHandler() { |
346 TerminateOnUI(thread_, server_wrapper_, socket_factory_); | 346 TerminateOnUI(thread_, server_wrapper_, socket_factory_); |
347 STLDeleteValues(&descriptor_map_); | 347 STLDeleteValues(&descriptor_map_); |
348 STLDeleteValues(&connection_to_client_); | 348 STLDeleteValues(&connection_to_client_); |
349 } | 349 } |
350 | 350 |
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
589 SendJson(connection_id, net::HTTP_OK, &list_value, std::string()); | 589 SendJson(connection_id, net::HTTP_OK, &list_value, std::string()); |
590 return; | 590 return; |
591 } | 591 } |
592 | 592 |
593 if (command == "new") { | 593 if (command == "new") { |
594 GURL url(net::UnescapeURLComponent( | 594 GURL url(net::UnescapeURLComponent( |
595 query, net::UnescapeRule::URL_SPECIAL_CHARS_EXCEPT_PATH_SEPARATORS | | 595 query, net::UnescapeRule::URL_SPECIAL_CHARS_EXCEPT_PATH_SEPARATORS | |
596 net::UnescapeRule::PATH_SEPARATORS)); | 596 net::UnescapeRule::PATH_SEPARATORS)); |
597 if (!url.is_valid()) | 597 if (!url.is_valid()) |
598 url = GURL(url::kAboutBlankURL); | 598 url = GURL(url::kAboutBlankURL); |
599 scoped_ptr<DevToolsTargetDescriptor> descriptor = | 599 std::unique_ptr<DevToolsTargetDescriptor> descriptor = |
600 devtools_discovery::DevToolsDiscoveryManager::GetInstance()-> | 600 devtools_discovery::DevToolsDiscoveryManager::GetInstance()->CreateNew( |
601 CreateNew(url); | 601 url); |
602 if (!descriptor) { | 602 if (!descriptor) { |
603 SendJson(connection_id, | 603 SendJson(connection_id, |
604 net::HTTP_INTERNAL_SERVER_ERROR, | 604 net::HTTP_INTERNAL_SERVER_ERROR, |
605 NULL, | 605 NULL, |
606 "Could not create new page"); | 606 "Could not create new page"); |
607 return; | 607 return; |
608 } | 608 } |
609 std::string host = info.headers["host"]; | 609 std::string host = info.headers["host"]; |
610 scoped_ptr<base::DictionaryValue> dictionary( | 610 std::unique_ptr<base::DictionaryValue> dictionary( |
611 SerializeDescriptor(*descriptor.get(), host)); | 611 SerializeDescriptor(*descriptor.get(), host)); |
612 SendJson(connection_id, net::HTTP_OK, dictionary.get(), std::string()); | 612 SendJson(connection_id, net::HTTP_OK, dictionary.get(), std::string()); |
613 const std::string target_id = descriptor->GetId(); | 613 const std::string target_id = descriptor->GetId(); |
614 descriptor_map_[target_id] = descriptor.release(); | 614 descriptor_map_[target_id] = descriptor.release(); |
615 return; | 615 return; |
616 } | 616 } |
617 | 617 |
618 if (command == "activate" || command == "close") { | 618 if (command == "activate" || command == "close") { |
619 DevToolsTargetDescriptor* descriptor = GetDescriptor(target_id); | 619 DevToolsTargetDescriptor* descriptor = GetDescriptor(target_id); |
620 if (!descriptor) { | 620 if (!descriptor) { |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
761 void DevToolsHttpHandler::OnClose(int connection_id) { | 761 void DevToolsHttpHandler::OnClose(int connection_id) { |
762 ConnectionToClientMap::iterator it = | 762 ConnectionToClientMap::iterator it = |
763 connection_to_client_.find(connection_id); | 763 connection_to_client_.find(connection_id); |
764 if (it != connection_to_client_.end()) { | 764 if (it != connection_to_client_.end()) { |
765 delete it->second; | 765 delete it->second; |
766 connection_to_client_.erase(connection_id); | 766 connection_to_client_.erase(connection_id); |
767 } | 767 } |
768 } | 768 } |
769 | 769 |
770 DevToolsHttpHandler::DevToolsHttpHandler( | 770 DevToolsHttpHandler::DevToolsHttpHandler( |
771 scoped_ptr<ServerSocketFactory> server_socket_factory, | 771 std::unique_ptr<ServerSocketFactory> server_socket_factory, |
772 const std::string& frontend_url, | 772 const std::string& frontend_url, |
773 DevToolsHttpHandlerDelegate* delegate, | 773 DevToolsHttpHandlerDelegate* delegate, |
774 const base::FilePath& output_directory, | 774 const base::FilePath& output_directory, |
775 const base::FilePath& debug_frontend_dir, | 775 const base::FilePath& debug_frontend_dir, |
776 const std::string& product_name, | 776 const std::string& product_name, |
777 const std::string& user_agent) | 777 const std::string& user_agent) |
778 : thread_(nullptr), | 778 : thread_(nullptr), |
779 frontend_url_(frontend_url), | 779 frontend_url_(frontend_url), |
780 product_name_(product_name), | 780 product_name_(product_name), |
781 user_agent_(user_agent), | 781 user_agent_(user_agent), |
(...skipping 12 matching lines...) Expand all Loading... |
794 server_socket_factory.release(), | 794 server_socket_factory.release(), |
795 output_directory, | 795 output_directory, |
796 debug_frontend_dir, | 796 debug_frontend_dir, |
797 bundles_resources)); | 797 bundles_resources)); |
798 } | 798 } |
799 | 799 |
800 void DevToolsHttpHandler::ServerStarted( | 800 void DevToolsHttpHandler::ServerStarted( |
801 base::Thread* thread, | 801 base::Thread* thread, |
802 ServerWrapper* server_wrapper, | 802 ServerWrapper* server_wrapper, |
803 ServerSocketFactory* socket_factory, | 803 ServerSocketFactory* socket_factory, |
804 scoped_ptr<net::IPEndPoint> ip_address) { | 804 std::unique_ptr<net::IPEndPoint> ip_address) { |
805 thread_ = thread; | 805 thread_ = thread; |
806 server_wrapper_ = server_wrapper; | 806 server_wrapper_ = server_wrapper; |
807 socket_factory_ = socket_factory; | 807 socket_factory_ = socket_factory; |
808 server_ip_address_.swap(ip_address); | 808 server_ip_address_.swap(ip_address); |
809 } | 809 } |
810 | 810 |
811 void ServerWrapper::WriteActivePortToUserProfile( | 811 void ServerWrapper::WriteActivePortToUserProfile( |
812 const base::FilePath& output_directory) { | 812 const base::FilePath& output_directory) { |
813 DCHECK(!output_directory.empty()); | 813 DCHECK(!output_directory.empty()); |
814 net::IPEndPoint endpoint; | 814 net::IPEndPoint endpoint; |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
930 id.c_str(), | 930 id.c_str(), |
931 host); | 931 host); |
932 dictionary->SetString( | 932 dictionary->SetString( |
933 kTargetDevtoolsFrontendUrlField, devtools_frontend_url); | 933 kTargetDevtoolsFrontendUrlField, devtools_frontend_url); |
934 } | 934 } |
935 | 935 |
936 return dictionary; | 936 return dictionary; |
937 } | 937 } |
938 | 938 |
939 } // namespace devtools_http_handler | 939 } // namespace devtools_http_handler |
OLD | NEW |