| 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/devtools_server.h" |
| 6 | 6 |
| 7 #include <pwd.h> | 7 #include <pwd.h> |
| 8 #include <cstring> | 8 #include <cstring> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/android/context_utils.h" | 11 #include "base/android/context_utils.h" |
| 12 #include "base/android/jni_string.h" | 12 #include "base/android/jni_string.h" |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/callback.h" | 14 #include "base/callback.h" |
| 15 #include "base/command_line.h" | 15 #include "base/command_line.h" |
| 16 #include "base/compiler_specific.h" | 16 #include "base/compiler_specific.h" |
| 17 #include "base/files/file_path.h" | 17 #include "base/files/file_path.h" |
| 18 #include "base/logging.h" | 18 #include "base/logging.h" |
| 19 #include "base/macros.h" | 19 #include "base/macros.h" |
| 20 #include "base/strings/string_number_conversions.h" | 20 #include "base/strings/string_number_conversions.h" |
| 21 #include "base/strings/stringprintf.h" | 21 #include "base/strings/stringprintf.h" |
| 22 #include "base/strings/utf_string_conversions.h" | 22 #include "base/strings/utf_string_conversions.h" |
| 23 #include "chrome/browser/android/tab_android.h" | 23 #include "chrome/browser/android/tab_android.h" |
| 24 #include "chrome/browser/browser_process.h" | 24 #include "chrome/browser/browser_process.h" |
| 25 #include "chrome/browser/profiles/profile_manager.h" | 25 #include "chrome/browser/profiles/profile_manager.h" |
| 26 #include "chrome/browser/ui/android/tab_model/tab_model.h" | 26 #include "chrome/browser/ui/android/tab_model/tab_model.h" |
| 27 #include "chrome/browser/ui/android/tab_model/tab_model_list.h" | 27 #include "chrome/browser/ui/android/tab_model/tab_model_list.h" |
| 28 #include "chrome/common/chrome_content_client.h" | 28 #include "chrome/common/chrome_content_client.h" |
| 29 #include "chrome/grit/browser_resources.h" | 29 #include "chrome/grit/browser_resources.h" |
| 30 #include "components/devtools_http_handler/devtools_http_handler.h" | |
| 31 #include "components/devtools_http_handler/devtools_http_handler_delegate.h" | |
| 32 #include "components/version_info/version_info.h" | 30 #include "components/version_info/version_info.h" |
| 33 #include "content/public/browser/android/devtools_auth.h" | 31 #include "content/public/browser/android/devtools_auth.h" |
| 34 #include "content/public/browser/browser_thread.h" | 32 #include "content/public/browser/browser_thread.h" |
| 35 #include "content/public/browser/devtools_agent_host.h" | 33 #include "content/public/browser/devtools_agent_host.h" |
| 36 #include "content/public/browser/devtools_socket_factory.h" | 34 #include "content/public/browser/devtools_socket_factory.h" |
| 37 #include "content/public/browser/favicon_status.h" | 35 #include "content/public/browser/favicon_status.h" |
| 38 #include "content/public/browser/navigation_entry.h" | 36 #include "content/public/browser/navigation_entry.h" |
| 39 #include "content/public/browser/render_view_host.h" | 37 #include "content/public/browser/render_view_host.h" |
| 40 #include "content/public/browser/web_contents.h" | 38 #include "content/public/browser/web_contents.h" |
| 41 #include "content/public/browser/web_contents_delegate.h" | 39 #include "content/public/browser/web_contents_delegate.h" |
| 42 #include "content/public/common/content_switches.h" | 40 #include "content/public/common/content_switches.h" |
| 43 #include "content/public/common/url_constants.h" | 41 #include "content/public/common/url_constants.h" |
| 44 #include "content/public/common/user_agent.h" | 42 #include "content/public/common/user_agent.h" |
| 45 #include "jni/DevToolsServer_jni.h" | 43 #include "jni/DevToolsServer_jni.h" |
| 46 #include "net/base/net_errors.h" | 44 #include "net/base/net_errors.h" |
| 47 #include "net/socket/unix_domain_server_socket_posix.h" | 45 #include "net/socket/unix_domain_server_socket_posix.h" |
| 48 #include "net/url_request/url_request_context_getter.h" | 46 #include "net/url_request/url_request_context_getter.h" |
| 49 #include "ui/base/resource/resource_bundle.h" | |
| 50 | 47 |
| 51 using base::android::JavaParamRef; | 48 using base::android::JavaParamRef; |
| 52 using content::DevToolsAgentHost; | 49 using content::DevToolsAgentHost; |
| 53 using content::RenderViewHost; | 50 using content::RenderViewHost; |
| 54 using content::WebContents; | 51 using content::WebContents; |
| 55 using devtools_http_handler::DevToolsHttpHandler; | |
| 56 | 52 |
| 57 namespace { | 53 namespace { |
| 58 | 54 |
| 59 // TL;DR: Do not change this string. | 55 // TL;DR: Do not change this string. |
| 60 // | 56 // |
| 61 // Desktop Chrome relies on this format to identify debuggable apps on Android | 57 // Desktop Chrome relies on this format to identify debuggable apps on Android |
| 62 // (see the code under chrome/browser/devtools/device). | 58 // (see the code under chrome/browser/devtools/device). |
| 63 // If this string ever changes it would not be sufficient to change the | 59 // If this string ever changes it would not be sufficient to change the |
| 64 // corresponding string on the client side. Since debugging an older version of | 60 // corresponding string on the client side. Since debugging an older version of |
| 65 // Chrome for Android from a newer version of desktop Chrome is a very common | 61 // Chrome for Android from a newer version of desktop Chrome is a very common |
| 66 // scenario, the client code will have to be modified to recognize both the old | 62 // scenario, the client code will have to be modified to recognize both the old |
| 67 // and the new format. | 63 // and the new format. |
| 68 const char kDevToolsChannelNameFormat[] = "%s_devtools_remote"; | 64 const char kDevToolsChannelNameFormat[] = "%s_devtools_remote"; |
| 69 | 65 |
| 70 const char kFrontEndURL[] = | 66 const char kFrontEndURL[] = |
| 71 "http://chrome-devtools-frontend.appspot.com/serve_rev/%s/inspector.html"; | 67 "http://chrome-devtools-frontend.appspot.com/serve_rev/%s/inspector.html"; |
| 72 const char kTetheringSocketName[] = "chrome_devtools_tethering_%d_%d"; | 68 const char kTetheringSocketName[] = "chrome_devtools_tethering_%d_%d"; |
| 73 | 69 |
| 74 const int kBackLog = 10; | 70 const int kBackLog = 10; |
| 75 | 71 |
| 76 bool AuthorizeSocketAccessWithDebugPermission( | 72 bool AuthorizeSocketAccessWithDebugPermission( |
| 77 const net::UnixDomainServerSocket::Credentials& credentials) { | 73 const net::UnixDomainServerSocket::Credentials& credentials) { |
| 78 JNIEnv* env = base::android::AttachCurrentThread(); | 74 JNIEnv* env = base::android::AttachCurrentThread(); |
| 79 return Java_DevToolsServer_checkDebugPermission( | 75 return Java_DevToolsServer_checkDebugPermission( |
| 80 env, base::android::GetApplicationContext(), | 76 env, base::android::GetApplicationContext(), |
| 81 credentials.process_id, credentials.user_id) || | 77 credentials.process_id, credentials.user_id) || |
| 82 content::CanUserConnectToDevTools(credentials); | 78 content::CanUserConnectToDevTools(credentials); |
| 83 } | 79 } |
| 84 | 80 |
| 85 // Delegate implementation for the devtools http handler on android. A new | |
| 86 // instance of this gets created each time devtools is enabled. | |
| 87 class DevToolsServerDelegate : | |
| 88 public devtools_http_handler::DevToolsHttpHandlerDelegate { | |
| 89 public: | |
| 90 DevToolsServerDelegate() { | |
| 91 } | |
| 92 | |
| 93 std::string GetDiscoveryPageHTML() override { | |
| 94 return ResourceBundle::GetSharedInstance().GetRawDataResource( | |
| 95 IDR_DEVTOOLS_DISCOVERY_PAGE_HTML).as_string(); | |
| 96 } | |
| 97 | |
| 98 std::string GetFrontendResource(const std::string& path) override { | |
| 99 return std::string(); | |
| 100 } | |
| 101 | |
| 102 private: | |
| 103 | |
| 104 DISALLOW_COPY_AND_ASSIGN(DevToolsServerDelegate); | |
| 105 }; | |
| 106 | |
| 107 // Factory for UnixDomainServerSocket. It tries a fallback socket when | 81 // Factory for UnixDomainServerSocket. It tries a fallback socket when |
| 108 // original socket doesn't work. | 82 // original socket doesn't work. |
| 109 class UnixDomainServerSocketFactory : public content::DevToolsSocketFactory { | 83 class UnixDomainServerSocketFactory : public content::DevToolsSocketFactory { |
| 110 public: | 84 public: |
| 111 UnixDomainServerSocketFactory( | 85 UnixDomainServerSocketFactory( |
| 112 const std::string& socket_name, | 86 const std::string& socket_name, |
| 113 const net::UnixDomainServerSocket::AuthCallback& auth_callback) | 87 const net::UnixDomainServerSocket::AuthCallback& auth_callback) |
| 114 : socket_name_(socket_name), | 88 : socket_name_(socket_name), |
| 115 last_tethering_socket_(0), | 89 last_tethering_socket_(0), |
| 116 auth_callback_(auth_callback) { | 90 auth_callback_(auth_callback) { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 int last_tethering_socket_; | 124 int last_tethering_socket_; |
| 151 net::UnixDomainServerSocket::AuthCallback auth_callback_; | 125 net::UnixDomainServerSocket::AuthCallback auth_callback_; |
| 152 | 126 |
| 153 DISALLOW_COPY_AND_ASSIGN(UnixDomainServerSocketFactory); | 127 DISALLOW_COPY_AND_ASSIGN(UnixDomainServerSocketFactory); |
| 154 }; | 128 }; |
| 155 | 129 |
| 156 } // namespace | 130 } // namespace |
| 157 | 131 |
| 158 DevToolsServer::DevToolsServer(const std::string& socket_name_prefix) | 132 DevToolsServer::DevToolsServer(const std::string& socket_name_prefix) |
| 159 : socket_name_(base::StringPrintf(kDevToolsChannelNameFormat, | 133 : socket_name_(base::StringPrintf(kDevToolsChannelNameFormat, |
| 160 socket_name_prefix.c_str())) { | 134 socket_name_prefix.c_str())), |
| 135 is_started_(false) { |
| 161 // Override the socket name if one is specified on the command line. | 136 // Override the socket name if one is specified on the command line. |
| 162 const base::CommandLine& command_line = | 137 const base::CommandLine& command_line = |
| 163 *base::CommandLine::ForCurrentProcess(); | 138 *base::CommandLine::ForCurrentProcess(); |
| 164 if (command_line.HasSwitch(switches::kRemoteDebuggingSocketName)) { | 139 if (command_line.HasSwitch(switches::kRemoteDebuggingSocketName)) { |
| 165 socket_name_ = command_line.GetSwitchValueASCII( | 140 socket_name_ = command_line.GetSwitchValueASCII( |
| 166 switches::kRemoteDebuggingSocketName); | 141 switches::kRemoteDebuggingSocketName); |
| 167 } | 142 } |
| 168 } | 143 } |
| 169 | 144 |
| 170 DevToolsServer::~DevToolsServer() { | 145 DevToolsServer::~DevToolsServer() { |
| 171 Stop(); | 146 Stop(); |
| 172 } | 147 } |
| 173 | 148 |
| 174 void DevToolsServer::Start(bool allow_debug_permission) { | 149 void DevToolsServer::Start(bool allow_debug_permission) { |
| 175 if (devtools_http_handler_) | 150 if (is_started_) |
| 176 return; | 151 return; |
| 177 | 152 |
| 178 net::UnixDomainServerSocket::AuthCallback auth_callback = | 153 net::UnixDomainServerSocket::AuthCallback auth_callback = |
| 179 allow_debug_permission ? | 154 allow_debug_permission ? |
| 180 base::Bind(&AuthorizeSocketAccessWithDebugPermission) : | 155 base::Bind(&AuthorizeSocketAccessWithDebugPermission) : |
| 181 base::Bind(&content::CanUserConnectToDevTools); | 156 base::Bind(&content::CanUserConnectToDevTools); |
| 182 std::unique_ptr<content::DevToolsSocketFactory> factory( | 157 std::unique_ptr<content::DevToolsSocketFactory> factory( |
| 183 new UnixDomainServerSocketFactory(socket_name_, auth_callback)); | 158 new UnixDomainServerSocketFactory(socket_name_, auth_callback)); |
| 184 devtools_http_handler_.reset(new DevToolsHttpHandler( | 159 DevToolsAgentHost::StartRemoteDebuggingServer( |
| 185 std::move(factory), | 160 std::move(factory), |
| 186 base::StringPrintf(kFrontEndURL, content::GetWebKitRevision().c_str()), | 161 base::StringPrintf(kFrontEndURL, content::GetWebKitRevision().c_str()), |
| 187 new DevToolsServerDelegate(), base::FilePath(), base::FilePath(), | 162 base::FilePath(), base::FilePath(), |
| 188 version_info::GetProductNameAndVersionForUserAgent(), ::GetUserAgent())); | 163 version_info::GetProductNameAndVersionForUserAgent(), ::GetUserAgent()); |
| 164 is_started_ = true; |
| 189 } | 165 } |
| 190 | 166 |
| 191 void DevToolsServer::Stop() { | 167 void DevToolsServer::Stop() { |
| 192 devtools_http_handler_.reset(); | 168 is_started_ = false; |
| 169 DevToolsAgentHost::StopRemoteDebuggingServer(); |
| 193 } | 170 } |
| 194 | 171 |
| 195 bool DevToolsServer::IsStarted() const { | 172 bool DevToolsServer::IsStarted() const { |
| 196 return !!devtools_http_handler_; | 173 return is_started_; |
| 197 } | 174 } |
| 198 | 175 |
| 199 bool RegisterDevToolsServer(JNIEnv* env) { | 176 bool RegisterDevToolsServer(JNIEnv* env) { |
| 200 return RegisterNativesImpl(env); | 177 return RegisterNativesImpl(env); |
| 201 } | 178 } |
| 202 | 179 |
| 203 static jlong InitRemoteDebugging( | 180 static jlong InitRemoteDebugging( |
| 204 JNIEnv* env, | 181 JNIEnv* env, |
| 205 const JavaParamRef<jobject>& obj, | 182 const JavaParamRef<jobject>& obj, |
| 206 const JavaParamRef<jstring>& socket_name_prefix) { | 183 const JavaParamRef<jstring>& socket_name_prefix) { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 226 jlong server, | 203 jlong server, |
| 227 jboolean enabled, | 204 jboolean enabled, |
| 228 jboolean allow_debug_permission) { | 205 jboolean allow_debug_permission) { |
| 229 DevToolsServer* devtools_server = reinterpret_cast<DevToolsServer*>(server); | 206 DevToolsServer* devtools_server = reinterpret_cast<DevToolsServer*>(server); |
| 230 if (enabled) { | 207 if (enabled) { |
| 231 devtools_server->Start(allow_debug_permission); | 208 devtools_server->Start(allow_debug_permission); |
| 232 } else { | 209 } else { |
| 233 devtools_server->Stop(); | 210 devtools_server->Stop(); |
| 234 } | 211 } |
| 235 } | 212 } |
| OLD | NEW |