| 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 "content/browser/devtools/devtools_http_handler_impl.h" | 5 #include "content/browser/devtools/devtools_http_handler_impl.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 return; | 311 return; |
| 312 } | 312 } |
| 313 | 313 |
| 314 thread_->message_loop()->PostTask( | 314 thread_->message_loop()->PostTask( |
| 315 FROM_HERE, | 315 FROM_HERE, |
| 316 base::Bind(&DevToolsHttpHandlerImpl::Init, this)); | 316 base::Bind(&DevToolsHttpHandlerImpl::Init, this)); |
| 317 } | 317 } |
| 318 | 318 |
| 319 void DevToolsHttpHandlerImpl::ResetHandlerThread() { | 319 void DevToolsHttpHandlerImpl::ResetHandlerThread() { |
| 320 thread_.reset(); | 320 thread_.reset(); |
| 321 server_ip_address_.reset(); |
| 321 } | 322 } |
| 322 | 323 |
| 323 void DevToolsHttpHandlerImpl::ResetHandlerThreadAndRelease() { | 324 void DevToolsHttpHandlerImpl::ResetHandlerThreadAndRelease() { |
| 324 ResetHandlerThread(); | 325 ResetHandlerThread(); |
| 325 Release(); | 326 Release(); |
| 326 } | 327 } |
| 327 | 328 |
| 328 void DevToolsHttpHandlerImpl::Stop() { | 329 void DevToolsHttpHandlerImpl::Stop() { |
| 329 if (!thread_) | 330 if (!thread_) |
| 330 return; | 331 return; |
| 331 BrowserThread::PostTaskAndReply( | 332 BrowserThread::PostTaskAndReply( |
| 332 BrowserThread::FILE, FROM_HERE, | 333 BrowserThread::FILE, FROM_HERE, |
| 333 base::Bind(&DevToolsHttpHandlerImpl::StopHandlerThread, this), | 334 base::Bind(&DevToolsHttpHandlerImpl::StopHandlerThread, this), |
| 334 base::Bind(&DevToolsHttpHandlerImpl::ResetHandlerThreadAndRelease, this)); | 335 base::Bind(&DevToolsHttpHandlerImpl::ResetHandlerThreadAndRelease, this)); |
| 335 } | 336 } |
| 336 | 337 |
| 337 void DevToolsHttpHandlerImpl::StopWithoutRelease() { | 338 void DevToolsHttpHandlerImpl::StopWithoutRelease() { |
| 338 if (!thread_) | 339 if (!thread_) |
| 339 return; | 340 return; |
| 340 BrowserThread::PostTaskAndReply( | 341 BrowserThread::PostTaskAndReply( |
| 341 BrowserThread::FILE, FROM_HERE, | 342 BrowserThread::FILE, FROM_HERE, |
| 342 base::Bind(&DevToolsHttpHandlerImpl::StopHandlerThread, this), | 343 base::Bind(&DevToolsHttpHandlerImpl::StopHandlerThread, this), |
| 343 base::Bind(&DevToolsHttpHandlerImpl::ResetHandlerThread, this)); | 344 base::Bind(&DevToolsHttpHandlerImpl::ResetHandlerThread, this)); |
| 344 } | 345 } |
| 345 | 346 |
| 346 GURL DevToolsHttpHandlerImpl::GetFrontendURL() { | 347 GURL DevToolsHttpHandlerImpl::GetFrontendURL() { |
| 347 net::IPEndPoint ip_address; | 348 if (!server_ip_address_) |
| 348 if (server_ && server_->GetLocalAddress(&ip_address)) | |
| 349 return GURL(); | 349 return GURL(); |
| 350 return GURL(std::string("http://") + ip_address.ToString() + frontend_url_); | 350 return GURL(std::string("http://") + server_ip_address_->ToString() + frontend
_url_); |
| 351 } | 351 } |
| 352 | 352 |
| 353 static std::string PathWithoutParams(const std::string& path) { | 353 static std::string PathWithoutParams(const std::string& path) { |
| 354 size_t query_position = path.find("?"); | 354 size_t query_position = path.find("?"); |
| 355 if (query_position != std::string::npos) | 355 if (query_position != std::string::npos) |
| 356 return path.substr(0, query_position); | 356 return path.substr(0, query_position); |
| 357 return path; | 357 return path; |
| 358 } | 358 } |
| 359 | 359 |
| 360 static std::string GetMimeType(const std::string& filename) { | 360 static std::string GetMimeType(const std::string& filename) { |
| (...skipping 408 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 769 ConnectionToClientMap::iterator it = | 769 ConnectionToClientMap::iterator it = |
| 770 connection_to_client_ui_.find(connection_id); | 770 connection_to_client_ui_.find(connection_id); |
| 771 if (it != connection_to_client_ui_.end()) { | 771 if (it != connection_to_client_ui_.end()) { |
| 772 DevToolsAgentHostClientImpl* client = | 772 DevToolsAgentHostClientImpl* client = |
| 773 static_cast<DevToolsAgentHostClientImpl*>(it->second); | 773 static_cast<DevToolsAgentHostClientImpl*>(it->second); |
| 774 delete client; | 774 delete client; |
| 775 connection_to_client_ui_.erase(connection_id); | 775 connection_to_client_ui_.erase(connection_id); |
| 776 } | 776 } |
| 777 } | 777 } |
| 778 | 778 |
| 779 void DevToolsHttpHandlerImpl::OnHttpServerInitialized( |
| 780 const net::IPEndPoint& ip_address) { |
| 781 server_ip_address_.reset(new net::IPEndPoint(ip_address)); |
| 782 } |
| 783 |
| 779 DevToolsHttpHandlerImpl::DevToolsHttpHandlerImpl( | 784 DevToolsHttpHandlerImpl::DevToolsHttpHandlerImpl( |
| 780 scoped_ptr<ServerSocketFactory> server_socket_factory, | 785 scoped_ptr<ServerSocketFactory> server_socket_factory, |
| 781 const std::string& frontend_url, | 786 const std::string& frontend_url, |
| 782 DevToolsHttpHandlerDelegate* delegate, | 787 DevToolsHttpHandlerDelegate* delegate, |
| 783 const base::FilePath& active_port_output_directory) | 788 const base::FilePath& active_port_output_directory) |
| 784 : frontend_url_(frontend_url), | 789 : frontend_url_(frontend_url), |
| 785 server_socket_factory_(server_socket_factory.Pass()), | 790 server_socket_factory_(server_socket_factory.Pass()), |
| 786 delegate_(delegate), | 791 delegate_(delegate), |
| 787 active_port_output_directory_(active_port_output_directory) { | 792 active_port_output_directory_(active_port_output_directory) { |
| 788 if (frontend_url_.empty()) | 793 if (frontend_url_.empty()) |
| 789 frontend_url_ = "/devtools/devtools.html"; | 794 frontend_url_ = "/devtools/devtools.html"; |
| 790 | 795 |
| 791 // Balanced in ResetHandlerThreadAndRelease(). | 796 // Balanced in ResetHandlerThreadAndRelease(). |
| 792 AddRef(); | 797 AddRef(); |
| 793 } | 798 } |
| 794 | 799 |
| 795 // Runs on the handler thread | 800 // Runs on the handler thread |
| 796 void DevToolsHttpHandlerImpl::Init() { | 801 void DevToolsHttpHandlerImpl::Init() { |
| 797 scoped_ptr<net::ServerSocket> server_socket = | 802 scoped_ptr<net::ServerSocket> server_socket = |
| 798 server_socket_factory_->CreateAndListen(); | 803 server_socket_factory_->CreateAndListen(); |
| 799 if (!server_socket) { | 804 if (!server_socket) { |
| 800 LOG(ERROR) << "Cannot start http server for devtools. Stop devtools."; | 805 LOG(ERROR) << "Cannot start http server for devtools. Stop devtools."; |
| 801 BrowserThread::PostTask( | 806 BrowserThread::PostTask( |
| 802 BrowserThread::UI, FROM_HERE, | 807 BrowserThread::UI, FROM_HERE, |
| 803 base::Bind(&DevToolsHttpHandlerImpl::StopWithoutRelease, this)); | 808 base::Bind(&DevToolsHttpHandlerImpl::StopWithoutRelease, this)); |
| 804 return; | 809 return; |
| 805 } | 810 } |
| 806 | 811 |
| 807 server_.reset(new net::HttpServer(server_socket.Pass(), this)); | 812 server_.reset(new net::HttpServer(server_socket.Pass(), this)); |
| 813 net::IPEndPoint ip_address; |
| 814 server_->GetLocalAddress(&ip_address); |
| 815 BrowserThread::PostTask( |
| 816 BrowserThread::UI, FROM_HERE, |
| 817 base::Bind(&DevToolsHttpHandlerImpl::OnHttpServerInitialized, |
| 818 this, ip_address)); |
| 808 if (!active_port_output_directory_.empty()) | 819 if (!active_port_output_directory_.empty()) |
| 809 WriteActivePortToUserProfile(); | 820 WriteActivePortToUserProfile(); |
| 810 } | 821 } |
| 811 | 822 |
| 812 // Runs on the handler thread | 823 // Runs on the handler thread |
| 813 void DevToolsHttpHandlerImpl::Teardown() { | 824 void DevToolsHttpHandlerImpl::Teardown() { |
| 814 server_.reset(NULL); | 825 server_.reset(); |
| 815 } | 826 } |
| 816 | 827 |
| 817 // Runs on FILE thread to make sure that it is serialized against | 828 // Runs on FILE thread to make sure that it is serialized against |
| 818 // {Start|Stop}HandlerThread and to allow calling pthread_join. | 829 // {Start|Stop}HandlerThread and to allow calling pthread_join. |
| 819 void DevToolsHttpHandlerImpl::StopHandlerThread() { | 830 void DevToolsHttpHandlerImpl::StopHandlerThread() { |
| 820 if (!thread_->message_loop()) | 831 if (!thread_->message_loop()) |
| 821 return; | 832 return; |
| 822 thread_->message_loop()->PostTask( | 833 thread_->message_loop()->PostTask( |
| 823 FROM_HERE, | 834 FROM_HERE, |
| 824 base::Bind(&DevToolsHttpHandlerImpl::Teardown, this)); | 835 base::Bind(&DevToolsHttpHandlerImpl::Teardown, this)); |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 969 id.c_str(), | 980 id.c_str(), |
| 970 host); | 981 host); |
| 971 dictionary->SetString( | 982 dictionary->SetString( |
| 972 kTargetDevtoolsFrontendUrlField, devtools_frontend_url); | 983 kTargetDevtoolsFrontendUrlField, devtools_frontend_url); |
| 973 } | 984 } |
| 974 | 985 |
| 975 return dictionary; | 986 return dictionary; |
| 976 } | 987 } |
| 977 | 988 |
| 978 } // namespace content | 989 } // namespace content |
| OLD | NEW |