Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/public/browser/android/devtools_server.h" | |
| 6 | |
| 7 #include <pwd.h> | |
| 8 | |
| 9 #include <cstring> | |
| 10 #include <string> | |
|
jam
2012/07/11 15:19:22
nit: in header for this file, so not needed
Philippe
2012/07/11 15:48:05
Done.
| |
| 11 | |
| 12 #include "base/basictypes.h" | |
| 13 #include "base/bind.h" | |
| 14 #include "base/callback.h" | |
| 15 #include "base/compiler_specific.h" | |
| 16 #include "base/logging.h" | |
| 17 #include "base/memory/singleton.h" | |
| 18 #include "base/stringprintf.h" | |
| 19 #include "content/public/browser/devtools_http_handler.h" | |
| 20 #include "net/base/unix_domain_socket_posix.h" | |
| 21 #include "net/url_request/url_request_context_getter.h" | |
| 22 | |
| 23 namespace content { | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 const char kFrontEndURL[] = | |
| 28 "http://chrome-devtools-frontend.appspot.com/static/%s/devtools.html"; | |
| 29 const char kSocketName[] = "chrome_devtools_remote"; | |
| 30 | |
| 31 bool UserCanConnect(uid_t uid, gid_t gid) { | |
| 32 struct passwd* creds = getpwuid(uid); | |
| 33 if (!creds || !creds->pw_name) { | |
| 34 LOG(WARNING) << "DevToolsServer: can't obtain creds for uid " << uid; | |
| 35 return false; | |
| 36 } | |
| 37 if (gid == uid && | |
| 38 (strcmp("root", creds->pw_name) == 0 || | |
| 39 strcmp("shell", creds->pw_name) == 0)) { | |
| 40 return true; | |
| 41 } | |
| 42 LOG(WARNING) << "DevToolsServer: connection attempt from " << creds->pw_name; | |
| 43 return false; | |
| 44 } | |
| 45 | |
| 46 class DevToolsServerImpl : public DevToolsServer { | |
| 47 public: | |
| 48 static DevToolsServerImpl* GetInstance() { | |
| 49 return Singleton<DevToolsServerImpl>::get(); | |
| 50 } | |
| 51 | |
| 52 // DevToolsServer: | |
| 53 virtual void Init(const std::string& frontend_version, | |
| 54 net::URLRequestContextGetter* url_request_context, | |
| 55 const DelegateCreator& delegate_creator) OVERRIDE { | |
| 56 frontend_url_ = StringPrintf(kFrontEndURL, frontend_version.c_str()); | |
| 57 url_request_context_ = url_request_context; | |
| 58 delegate_creator_ = delegate_creator; | |
| 59 } | |
| 60 | |
| 61 virtual void Start() OVERRIDE { | |
| 62 Stop(); | |
| 63 DCHECK(is_initialized()); | |
| 64 protocol_handler_ = content::DevToolsHttpHandler::Start( | |
|
jam
2012/07/11 15:19:22
nit: here and below, don't need "content::"
Philippe
2012/07/11 15:48:05
Done.
| |
| 65 new net::UnixDomainSocketWithAbstractNamespaceFactory( | |
| 66 kSocketName, base::Bind(&UserCanConnect)), | |
| 67 frontend_url_, | |
| 68 url_request_context_, | |
| 69 delegate_creator_.Run()); | |
| 70 } | |
| 71 | |
| 72 virtual void Stop() OVERRIDE { | |
| 73 if (protocol_handler_) { | |
| 74 // Note that the call to Stop() below takes care of |protocol_handler_| | |
| 75 // deletion. | |
| 76 protocol_handler_->Stop(); | |
| 77 protocol_handler_ = NULL; | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 virtual bool is_initialized() const OVERRIDE { | |
| 82 return !frontend_url_.empty() && url_request_context_; | |
| 83 } | |
| 84 | |
| 85 virtual bool is_started() const OVERRIDE { | |
| 86 return protocol_handler_; | |
| 87 } | |
| 88 | |
| 89 private: | |
| 90 friend struct DefaultSingletonTraits<DevToolsServerImpl>; | |
| 91 | |
| 92 DevToolsServerImpl() : protocol_handler_(NULL), url_request_context_(NULL) {} | |
| 93 | |
| 94 virtual ~DevToolsServerImpl() { | |
| 95 Stop(); | |
| 96 } | |
| 97 | |
| 98 content::DevToolsHttpHandler* protocol_handler_; | |
| 99 std::string frontend_url_; | |
| 100 net::URLRequestContextGetter* url_request_context_; | |
| 101 DelegateCreator delegate_creator_; | |
| 102 | |
| 103 DISALLOW_COPY_AND_ASSIGN(DevToolsServerImpl); | |
| 104 }; | |
| 105 | |
| 106 } // namespace | |
| 107 | |
| 108 // static | |
| 109 DevToolsServer* DevToolsServer::GetInstance() { | |
| 110 return DevToolsServerImpl::GetInstance(); | |
| 111 } | |
| 112 | |
| 113 } // namespace content | |
| OLD | NEW |