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 "chrome/browser/android/dev_tools_server.h" | 5 #include "chrome/browser/android/dev_tools_server.h" |
6 | 6 |
7 #include <pwd.h> | 7 #include <pwd.h> |
8 #include <cstring> | 8 #include <cstring> |
9 | 9 |
10 #include "base/android/jni_string.h" | 10 #include "base/android/jni_string.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 #include "content/public/browser/favicon_status.h" | 33 #include "content/public/browser/favicon_status.h" |
34 #include "content/public/browser/navigation_entry.h" | 34 #include "content/public/browser/navigation_entry.h" |
35 #include "content/public/browser/render_view_host.h" | 35 #include "content/public/browser/render_view_host.h" |
36 #include "content/public/browser/web_contents.h" | 36 #include "content/public/browser/web_contents.h" |
37 #include "content/public/browser/web_contents_delegate.h" | 37 #include "content/public/browser/web_contents_delegate.h" |
38 #include "content/public/common/content_switches.h" | 38 #include "content/public/common/content_switches.h" |
39 #include "content/public/common/url_constants.h" | 39 #include "content/public/common/url_constants.h" |
40 #include "content/public/common/user_agent.h" | 40 #include "content/public/common/user_agent.h" |
41 #include "grit/browser_resources.h" | 41 #include "grit/browser_resources.h" |
42 #include "jni/DevToolsServer_jni.h" | 42 #include "jni/DevToolsServer_jni.h" |
| 43 #include "net/base/net_errors.h" |
43 #include "net/socket/unix_domain_listen_socket_posix.h" | 44 #include "net/socket/unix_domain_listen_socket_posix.h" |
| 45 #include "net/socket/unix_domain_server_socket_posix.h" |
44 #include "net/url_request/url_request_context_getter.h" | 46 #include "net/url_request/url_request_context_getter.h" |
45 #include "ui/base/resource/resource_bundle.h" | 47 #include "ui/base/resource/resource_bundle.h" |
46 | 48 |
47 using content::DevToolsAgentHost; | 49 using content::DevToolsAgentHost; |
48 using content::RenderViewHost; | 50 using content::RenderViewHost; |
49 using content::WebContents; | 51 using content::WebContents; |
50 | 52 |
51 namespace { | 53 namespace { |
52 | 54 |
53 // TL;DR: Do not change this string. | 55 // TL;DR: Do not change this string. |
(...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
386 if (top_sites) | 388 if (top_sites) |
387 top_sites->SyncWithHistory(); | 389 top_sites->SyncWithHistory(); |
388 } | 390 } |
389 | 391 |
390 int last_tethering_socket_; | 392 int last_tethering_socket_; |
391 const net::UnixDomainServerSocket::AuthCallback auth_callback_; | 393 const net::UnixDomainServerSocket::AuthCallback auth_callback_; |
392 | 394 |
393 DISALLOW_COPY_AND_ASSIGN(DevToolsServerDelegate); | 395 DISALLOW_COPY_AND_ASSIGN(DevToolsServerDelegate); |
394 }; | 396 }; |
395 | 397 |
| 398 // Factory for UnixDomainServerSocket. It tries a fallback socket when |
| 399 // original socket doesn't work. |
| 400 class UnixDomainServerSocketFactory |
| 401 : public content::DevToolsHttpHandler::ServerSocketFactory { |
| 402 public: |
| 403 explicit UnixDomainServerSocketFactory(const std::string& socket_name) |
| 404 : content::DevToolsHttpHandler::ServerSocketFactory(socket_name, 0, 1) {} |
| 405 |
| 406 private: |
| 407 // content::DevToolsHttpHandler::ServerSocketFactory. |
| 408 virtual scoped_ptr<net::ServerSocket> Create() const OVERRIDE { |
| 409 return scoped_ptr<net::ServerSocket>( |
| 410 new net::UnixDomainServerSocket( |
| 411 base::Bind(&content::CanUserConnectToDevTools), |
| 412 true)); |
| 413 } |
| 414 |
| 415 virtual scoped_ptr<net::ServerSocket> CreateAndListen() const OVERRIDE { |
| 416 scoped_ptr<net::ServerSocket> socket = Create(); |
| 417 if (!socket) |
| 418 return scoped_ptr<net::ServerSocket>(); |
| 419 |
| 420 if (socket->ListenWithAddressAndPort(address_, port_, backlog_) == net::OK) |
| 421 return socket.Pass(); |
| 422 |
| 423 // Try a fallback socket name. |
| 424 const std::string fallback_address( |
| 425 base::StringPrintf("%s_%d", address_.c_str(), getpid())); |
| 426 if (socket->ListenWithAddressAndPort(fallback_address, port_, backlog_) |
| 427 == net::OK) |
| 428 return socket.Pass(); |
| 429 |
| 430 return scoped_ptr<net::ServerSocket>(); |
| 431 } |
| 432 |
| 433 DISALLOW_COPY_AND_ASSIGN(UnixDomainServerSocketFactory); |
| 434 }; |
| 435 |
396 } // namespace | 436 } // namespace |
397 | 437 |
398 DevToolsServer::DevToolsServer(const std::string& socket_name_prefix) | 438 DevToolsServer::DevToolsServer(const std::string& socket_name_prefix) |
399 : socket_name_(base::StringPrintf(kDevToolsChannelNameFormat, | 439 : socket_name_(base::StringPrintf(kDevToolsChannelNameFormat, |
400 socket_name_prefix.c_str())), | 440 socket_name_prefix.c_str())), |
401 protocol_handler_(NULL) { | 441 protocol_handler_(NULL) { |
402 // Override the socket name if one is specified on the command line. | 442 // Override the socket name if one is specified on the command line. |
403 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 443 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
404 if (command_line.HasSwitch(switches::kRemoteDebuggingSocketName)) { | 444 if (command_line.HasSwitch(switches::kRemoteDebuggingSocketName)) { |
405 socket_name_ = command_line.GetSwitchValueASCII( | 445 socket_name_ = command_line.GetSwitchValueASCII( |
406 switches::kRemoteDebuggingSocketName); | 446 switches::kRemoteDebuggingSocketName); |
407 } | 447 } |
408 } | 448 } |
409 | 449 |
410 DevToolsServer::~DevToolsServer() { | 450 DevToolsServer::~DevToolsServer() { |
411 Stop(); | 451 Stop(); |
412 } | 452 } |
413 | 453 |
414 void DevToolsServer::Start(bool allow_debug_permission) { | 454 void DevToolsServer::Start(bool allow_debug_permission) { |
415 if (protocol_handler_) | 455 if (protocol_handler_) |
416 return; | 456 return; |
417 | 457 |
| 458 scoped_ptr<content::DevToolsHttpHandler::ServerSocketFactory> factory( |
| 459 new UnixDomainServerSocketFactory(socket_name_)); |
418 net::UnixDomainServerSocket::AuthCallback auth_callback = | 460 net::UnixDomainServerSocket::AuthCallback auth_callback = |
419 allow_debug_permission ? | 461 allow_debug_permission ? |
420 base::Bind(&AuthorizeSocketAccessWithDebugPermission) : | 462 base::Bind(&AuthorizeSocketAccessWithDebugPermission) : |
421 base::Bind(&content::CanUserConnectToDevTools); | 463 base::Bind(&content::CanUserConnectToDevTools); |
422 | |
423 protocol_handler_ = content::DevToolsHttpHandler::Start( | 464 protocol_handler_ = content::DevToolsHttpHandler::Start( |
424 new net::deprecated::UnixDomainListenSocketWithAbstractNamespaceFactory( | 465 factory.Pass(), |
425 socket_name_, | |
426 base::StringPrintf("%s_%d", socket_name_.c_str(), getpid()), | |
427 auth_callback), | |
428 base::StringPrintf(kFrontEndURL, content::GetWebKitRevision().c_str()), | 466 base::StringPrintf(kFrontEndURL, content::GetWebKitRevision().c_str()), |
429 new DevToolsServerDelegate(auth_callback), | 467 new DevToolsServerDelegate(auth_callback), |
430 base::FilePath()); | 468 base::FilePath()); |
431 } | 469 } |
432 | 470 |
433 void DevToolsServer::Stop() { | 471 void DevToolsServer::Stop() { |
434 if (!protocol_handler_) | 472 if (!protocol_handler_) |
435 return; | 473 return; |
436 // Note that the call to Stop() below takes care of |protocol_handler_| | 474 // Note that the call to Stop() below takes care of |protocol_handler_| |
437 // deletion. | 475 // deletion. |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
470 jlong server, | 508 jlong server, |
471 jboolean enabled, | 509 jboolean enabled, |
472 jboolean allow_debug_permission) { | 510 jboolean allow_debug_permission) { |
473 DevToolsServer* devtools_server = reinterpret_cast<DevToolsServer*>(server); | 511 DevToolsServer* devtools_server = reinterpret_cast<DevToolsServer*>(server); |
474 if (enabled) { | 512 if (enabled) { |
475 devtools_server->Start(allow_debug_permission); | 513 devtools_server->Start(allow_debug_permission); |
476 } else { | 514 } else { |
477 devtools_server->Stop(); | 515 devtools_server->Stop(); |
478 } | 516 } |
479 } | 517 } |
OLD | NEW |