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 357 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
411 if (top_sites) | 413 if (top_sites) |
412 top_sites->SyncWithHistory(); | 414 top_sites->SyncWithHistory(); |
413 } | 415 } |
414 | 416 |
415 int last_tethering_socket_; | 417 int last_tethering_socket_; |
416 const net::UnixDomainServerSocket::AuthCallback auth_callback_; | 418 const net::UnixDomainServerSocket::AuthCallback auth_callback_; |
417 | 419 |
418 DISALLOW_COPY_AND_ASSIGN(DevToolsServerDelegate); | 420 DISALLOW_COPY_AND_ASSIGN(DevToolsServerDelegate); |
419 }; | 421 }; |
420 | 422 |
| 423 // Factory for UnixDomainServerSocket. It tries a fallback socket when |
| 424 // original socket doesn't work. |
| 425 class UnixDomainServerSocketFactory |
| 426 : public content::DevToolsHttpHandler::ServerSocketFactory { |
| 427 public: |
| 428 UnixDomainServerSocketFactory( |
| 429 const std::string& socket_name, |
| 430 const net::UnixDomainServerSocket::AuthCallback& auth_callback) |
| 431 : content::DevToolsHttpHandler::ServerSocketFactory(socket_name, 0, 1), |
| 432 auth_callback_(auth_callback) { |
| 433 } |
| 434 |
| 435 private: |
| 436 // content::DevToolsHttpHandler::ServerSocketFactory. |
| 437 virtual scoped_ptr<net::ServerSocket> Create() const OVERRIDE { |
| 438 return scoped_ptr<net::ServerSocket>( |
| 439 new net::UnixDomainServerSocket(auth_callback_, |
| 440 true /* use_abstract_namespace */)); |
| 441 } |
| 442 |
| 443 virtual scoped_ptr<net::ServerSocket> CreateAndListen() const OVERRIDE { |
| 444 scoped_ptr<net::ServerSocket> socket = Create(); |
| 445 if (!socket) |
| 446 return scoped_ptr<net::ServerSocket>(); |
| 447 |
| 448 if (socket->ListenWithAddressAndPort(address_, port_, backlog_) == net::OK) |
| 449 return socket.Pass(); |
| 450 |
| 451 // Try a fallback socket name. |
| 452 const std::string fallback_address( |
| 453 base::StringPrintf("%s_%d", address_.c_str(), getpid())); |
| 454 if (socket->ListenWithAddressAndPort(fallback_address, port_, backlog_) |
| 455 == net::OK) |
| 456 return socket.Pass(); |
| 457 |
| 458 return scoped_ptr<net::ServerSocket>(); |
| 459 } |
| 460 |
| 461 const net::UnixDomainServerSocket::AuthCallback auth_callback_; |
| 462 |
| 463 DISALLOW_COPY_AND_ASSIGN(UnixDomainServerSocketFactory); |
| 464 }; |
| 465 |
421 } // namespace | 466 } // namespace |
422 | 467 |
423 DevToolsServer::DevToolsServer(const std::string& socket_name_prefix) | 468 DevToolsServer::DevToolsServer(const std::string& socket_name_prefix) |
424 : socket_name_(base::StringPrintf(kDevToolsChannelNameFormat, | 469 : socket_name_(base::StringPrintf(kDevToolsChannelNameFormat, |
425 socket_name_prefix.c_str())), | 470 socket_name_prefix.c_str())), |
426 protocol_handler_(NULL) { | 471 protocol_handler_(NULL) { |
427 // Override the socket name if one is specified on the command line. | 472 // Override the socket name if one is specified on the command line. |
428 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 473 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
429 if (command_line.HasSwitch(switches::kRemoteDebuggingSocketName)) { | 474 if (command_line.HasSwitch(switches::kRemoteDebuggingSocketName)) { |
430 socket_name_ = command_line.GetSwitchValueASCII( | 475 socket_name_ = command_line.GetSwitchValueASCII( |
431 switches::kRemoteDebuggingSocketName); | 476 switches::kRemoteDebuggingSocketName); |
432 } | 477 } |
433 } | 478 } |
434 | 479 |
435 DevToolsServer::~DevToolsServer() { | 480 DevToolsServer::~DevToolsServer() { |
436 Stop(); | 481 Stop(); |
437 } | 482 } |
438 | 483 |
439 void DevToolsServer::Start(bool allow_debug_permission) { | 484 void DevToolsServer::Start(bool allow_debug_permission) { |
440 if (protocol_handler_) | 485 if (protocol_handler_) |
441 return; | 486 return; |
442 | 487 |
443 net::UnixDomainServerSocket::AuthCallback auth_callback = | 488 net::UnixDomainServerSocket::AuthCallback auth_callback = |
444 allow_debug_permission ? | 489 allow_debug_permission ? |
445 base::Bind(&AuthorizeSocketAccessWithDebugPermission) : | 490 base::Bind(&AuthorizeSocketAccessWithDebugPermission) : |
446 base::Bind(&content::CanUserConnectToDevTools); | 491 base::Bind(&content::CanUserConnectToDevTools); |
447 | 492 scoped_ptr<content::DevToolsHttpHandler::ServerSocketFactory> factory( |
| 493 new UnixDomainServerSocketFactory(socket_name_, auth_callback)); |
448 protocol_handler_ = content::DevToolsHttpHandler::Start( | 494 protocol_handler_ = content::DevToolsHttpHandler::Start( |
449 new net::deprecated::UnixDomainListenSocketWithAbstractNamespaceFactory( | 495 factory.Pass(), |
450 socket_name_, | |
451 base::StringPrintf("%s_%d", socket_name_.c_str(), getpid()), | |
452 auth_callback), | |
453 base::StringPrintf(kFrontEndURL, content::GetWebKitRevision().c_str()), | 496 base::StringPrintf(kFrontEndURL, content::GetWebKitRevision().c_str()), |
454 new DevToolsServerDelegate(auth_callback), | 497 new DevToolsServerDelegate(auth_callback), |
455 base::FilePath()); | 498 base::FilePath()); |
456 } | 499 } |
457 | 500 |
458 void DevToolsServer::Stop() { | 501 void DevToolsServer::Stop() { |
459 if (!protocol_handler_) | 502 if (!protocol_handler_) |
460 return; | 503 return; |
461 // Note that the call to Stop() below takes care of |protocol_handler_| | 504 // Note that the call to Stop() below takes care of |protocol_handler_| |
462 // deletion. | 505 // deletion. |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
495 jlong server, | 538 jlong server, |
496 jboolean enabled, | 539 jboolean enabled, |
497 jboolean allow_debug_permission) { | 540 jboolean allow_debug_permission) { |
498 DevToolsServer* devtools_server = reinterpret_cast<DevToolsServer*>(server); | 541 DevToolsServer* devtools_server = reinterpret_cast<DevToolsServer*>(server); |
499 if (enabled) { | 542 if (enabled) { |
500 devtools_server->Start(allow_debug_permission); | 543 devtools_server->Start(allow_debug_permission); |
501 } else { | 544 } else { |
502 devtools_server->Stop(); | 545 devtools_server->Stop(); |
503 } | 546 } |
504 } | 547 } |
OLD | NEW |