| 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" |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/callback.h" | 13 #include "base/callback.h" |
| 14 #include "base/command_line.h" | 14 #include "base/command_line.h" |
| 15 #include "base/compiler_specific.h" | 15 #include "base/compiler_specific.h" |
| 16 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
| 18 #include "chrome/browser/browser_process.h" | 18 #include "chrome/browser/browser_process.h" |
| 19 #include "chrome/browser/history/top_sites.h" | 19 #include "chrome/browser/history/top_sites.h" |
| 20 #include "chrome/browser/profiles/profile_manager.h" | 20 #include "chrome/browser/profiles/profile_manager.h" |
| 21 #include "chrome/browser/ui/android/tab_model/tab_model.h" | 21 #include "chrome/browser/ui/android/tab_model/tab_model.h" |
| 22 #include "chrome/browser/ui/android/tab_model/tab_model_list.h" | 22 #include "chrome/browser/ui/android/tab_model/tab_model_list.h" |
| 23 #include "chrome/common/chrome_version_info.h" | |
| 24 #include "content/public/browser/android/devtools_auth.h" | 23 #include "content/public/browser/android/devtools_auth.h" |
| 25 #include "content/public/browser/browser_thread.h" | 24 #include "content/public/browser/browser_thread.h" |
| 26 #include "content/public/browser/devtools_http_handler.h" | 25 #include "content/public/browser/devtools_http_handler.h" |
| 27 #include "content/public/browser/devtools_http_handler_delegate.h" | 26 #include "content/public/browser/devtools_http_handler_delegate.h" |
| 27 #include "content/public/browser/devtools_remote_frontend_util.h" |
| 28 #include "content/public/browser/web_contents.h" | 28 #include "content/public/browser/web_contents.h" |
| 29 #include "content/public/common/content_switches.h" | 29 #include "content/public/common/content_switches.h" |
| 30 #include "content/public/common/url_constants.h" | 30 #include "content/public/common/url_constants.h" |
| 31 #include "grit/devtools_discovery_page_resources.h" | 31 #include "grit/devtools_discovery_page_resources.h" |
| 32 #include "jni/DevToolsServer_jni.h" | 32 #include "jni/DevToolsServer_jni.h" |
| 33 #include "net/socket/unix_domain_socket_posix.h" | 33 #include "net/socket/unix_domain_socket_posix.h" |
| 34 #include "net/url_request/url_request_context_getter.h" | 34 #include "net/url_request/url_request_context_getter.h" |
| 35 #include "ui/base/resource/resource_bundle.h" | 35 #include "ui/base/resource/resource_bundle.h" |
| 36 | 36 |
| 37 namespace { | 37 namespace { |
| 38 | 38 |
| 39 const char kFrontEndURL[] = | 39 const char kDefaultSocketNamePrefix[] = "chrome"; |
| 40 "http://chrome-devtools-frontend.appspot.com/static/%s/devtools.html"; | |
| 41 const char kDefaultSocketName[] = "chrome_devtools_remote"; | |
| 42 const char kTetheringSocketName[] = "chrome_devtools_tethering_%d_%d"; | 40 const char kTetheringSocketName[] = "chrome_devtools_tethering_%d_%d"; |
| 43 | 41 |
| 44 // Delegate implementation for the devtools http handler on android. A new | 42 // Delegate implementation for the devtools http handler on android. A new |
| 45 // instance of this gets created each time devtools is enabled. | 43 // instance of this gets created each time devtools is enabled. |
| 46 class DevToolsServerDelegate : public content::DevToolsHttpHandlerDelegate { | 44 class DevToolsServerDelegate : public content::DevToolsHttpHandlerDelegate { |
| 47 public: | 45 public: |
| 48 explicit DevToolsServerDelegate(bool use_bundled_frontend_resources) | 46 DevToolsServerDelegate() |
| 49 : use_bundled_frontend_resources_(use_bundled_frontend_resources), | 47 : last_tethering_socket_(0) { |
| 50 last_tethering_socket_(0) { | |
| 51 } | 48 } |
| 52 | 49 |
| 53 virtual std::string GetDiscoveryPageHTML() OVERRIDE { | 50 virtual std::string GetDiscoveryPageHTML() OVERRIDE { |
| 54 // TopSites updates itself after a delay. Ask TopSites to update itself | 51 // TopSites updates itself after a delay. Ask TopSites to update itself |
| 55 // when we're about to show the remote debugging landing page. | 52 // when we're about to show the remote debugging landing page. |
| 56 content::BrowserThread::PostTask( | 53 content::BrowserThread::PostTask( |
| 57 content::BrowserThread::UI, | 54 content::BrowserThread::UI, |
| 58 FROM_HERE, | 55 FROM_HERE, |
| 59 base::Bind(&DevToolsServerDelegate::PopulatePageThumbnails)); | 56 base::Bind(&DevToolsServerDelegate::PopulatePageThumbnails)); |
| 60 return ResourceBundle::GetSharedInstance().GetRawDataResource( | 57 return ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 61 IDR_DEVTOOLS_DISCOVERY_PAGE_HTML).as_string(); | 58 IDR_DEVTOOLS_DISCOVERY_PAGE_HTML).as_string(); |
| 62 } | 59 } |
| 63 | 60 |
| 64 virtual bool BundlesFrontendResources() OVERRIDE { | 61 virtual bool BundlesFrontendResources() OVERRIDE { |
| 65 return use_bundled_frontend_resources_; | 62 return false; |
| 66 } | 63 } |
| 67 | 64 |
| 68 virtual base::FilePath GetDebugFrontendDir() OVERRIDE { | 65 virtual base::FilePath GetDebugFrontendDir() OVERRIDE { |
| 69 return base::FilePath(); | 66 return base::FilePath(); |
| 70 } | 67 } |
| 71 | 68 |
| 72 virtual std::string GetPageThumbnailData(const GURL& url) OVERRIDE { | 69 virtual std::string GetPageThumbnailData(const GURL& url) OVERRIDE { |
| 73 Profile* profile = | 70 Profile* profile = |
| 74 ProfileManager::GetLastUsedProfile()->GetOriginalProfile(); | 71 ProfileManager::GetLastUsedProfile()->GetOriginalProfile(); |
| 75 history::TopSites* top_sites = profile->GetTopSites(); | 72 history::TopSites* top_sites = profile->GetTopSites(); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 | 114 |
| 118 private: | 115 private: |
| 119 static void PopulatePageThumbnails() { | 116 static void PopulatePageThumbnails() { |
| 120 Profile* profile = | 117 Profile* profile = |
| 121 ProfileManager::GetLastUsedProfile()->GetOriginalProfile(); | 118 ProfileManager::GetLastUsedProfile()->GetOriginalProfile(); |
| 122 history::TopSites* top_sites = profile->GetTopSites(); | 119 history::TopSites* top_sites = profile->GetTopSites(); |
| 123 if (top_sites) | 120 if (top_sites) |
| 124 top_sites->SyncWithHistory(); | 121 top_sites->SyncWithHistory(); |
| 125 } | 122 } |
| 126 | 123 |
| 127 bool use_bundled_frontend_resources_; | |
| 128 int last_tethering_socket_; | 124 int last_tethering_socket_; |
| 129 | 125 |
| 130 DISALLOW_COPY_AND_ASSIGN(DevToolsServerDelegate); | 126 DISALLOW_COPY_AND_ASSIGN(DevToolsServerDelegate); |
| 131 }; | 127 }; |
| 132 | 128 |
| 133 } // namespace | 129 } // namespace |
| 134 | 130 |
| 135 DevToolsServer::DevToolsServer() | 131 DevToolsServer::DevToolsServer() |
| 136 : use_bundled_frontend_resources_(false), | 132 : socket_name_( |
| 137 socket_name_(kDefaultSocketName), | 133 content::GetDevToolsServerSocketName(kDefaultSocketNamePrefix)), |
| 138 protocol_handler_(NULL) { | 134 protocol_handler_(NULL) { |
| 139 // Override the default socket name if one is specified on the command line. | 135 // Override the default socket name if one is specified on the command line. |
| 140 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 136 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| 141 if (command_line.HasSwitch(switches::kRemoteDebuggingSocketName)) { | 137 if (command_line.HasSwitch(switches::kRemoteDebuggingSocketName)) { |
| 142 socket_name_ = command_line.GetSwitchValueASCII( | 138 socket_name_ = command_line.GetSwitchValueASCII( |
| 143 switches::kRemoteDebuggingSocketName); | 139 switches::kRemoteDebuggingSocketName); |
| 144 } | 140 } |
| 145 } | 141 } |
| 146 | 142 |
| 147 DevToolsServer::DevToolsServer(bool use_bundled_frontend_resources, | 143 DevToolsServer::DevToolsServer(const std::string& socket_name_prefix) |
| 148 const std::string& socket_name) | 144 : socket_name_(content::GetDevToolsServerSocketName(socket_name_prefix)), |
| 149 : use_bundled_frontend_resources_(use_bundled_frontend_resources), | |
| 150 socket_name_(socket_name), | |
| 151 protocol_handler_(NULL) { | 145 protocol_handler_(NULL) { |
| 152 } | 146 } |
| 153 | 147 |
| 154 DevToolsServer::~DevToolsServer() { | 148 DevToolsServer::~DevToolsServer() { |
| 155 Stop(); | 149 Stop(); |
| 156 } | 150 } |
| 157 | 151 |
| 158 void DevToolsServer::Start() { | 152 void DevToolsServer::Start() { |
| 159 if (protocol_handler_) | 153 if (protocol_handler_) |
| 160 return; | 154 return; |
| 161 | 155 |
| 162 chrome::VersionInfo version_info; | |
| 163 | |
| 164 protocol_handler_ = content::DevToolsHttpHandler::Start( | 156 protocol_handler_ = content::DevToolsHttpHandler::Start( |
| 165 new net::UnixDomainSocketWithAbstractNamespaceFactory( | 157 new net::UnixDomainSocketWithAbstractNamespaceFactory( |
| 166 socket_name_, | 158 socket_name_, |
| 167 base::StringPrintf("%s_%d", socket_name_.c_str(), getpid()), | 159 base::StringPrintf("%s_%d", socket_name_.c_str(), getpid()), |
| 168 base::Bind(&content::CanUserConnectToDevTools)), | 160 base::Bind(&content::CanUserConnectToDevTools)), |
| 169 use_bundled_frontend_resources_ ? | 161 content::GetDevToolsFrontendMainResourceURL(), |
| 170 "" : | 162 new DevToolsServerDelegate()); |
| 171 base::StringPrintf(kFrontEndURL, version_info.Version().c_str()), | |
| 172 new DevToolsServerDelegate(use_bundled_frontend_resources_)); | |
| 173 } | 163 } |
| 174 | 164 |
| 175 void DevToolsServer::Stop() { | 165 void DevToolsServer::Stop() { |
| 176 if (!protocol_handler_) | 166 if (!protocol_handler_) |
| 177 return; | 167 return; |
| 178 // Note that the call to Stop() below takes care of |protocol_handler_| | 168 // Note that the call to Stop() below takes care of |protocol_handler_| |
| 179 // deletion. | 169 // deletion. |
| 180 protocol_handler_->Stop(); | 170 protocol_handler_->Stop(); |
| 181 protocol_handler_ = NULL; | 171 protocol_handler_ = NULL; |
| 182 } | 172 } |
| 183 | 173 |
| 184 bool DevToolsServer::IsStarted() const { | 174 bool DevToolsServer::IsStarted() const { |
| 185 return protocol_handler_; | 175 return protocol_handler_; |
| 186 } | 176 } |
| 187 | 177 |
| 188 bool RegisterDevToolsServer(JNIEnv* env) { | 178 bool RegisterDevToolsServer(JNIEnv* env) { |
| 189 return RegisterNativesImpl(env); | 179 return RegisterNativesImpl(env); |
| 190 } | 180 } |
| 191 | 181 |
| 192 static jint InitRemoteDebugging(JNIEnv* env, | 182 static jint InitRemoteDebugging(JNIEnv* env, |
| 193 jobject obj, | 183 jobject obj, |
| 194 jboolean use_bundled_frontend_resources, | 184 jstring socketNamePrefix) { |
| 195 jstring socketName) { | |
| 196 DevToolsServer* server = new DevToolsServer( | 185 DevToolsServer* server = new DevToolsServer( |
| 197 use_bundled_frontend_resources, | 186 base::android::ConvertJavaStringToUTF8(env, socketNamePrefix)); |
| 198 base::android::ConvertJavaStringToUTF8(env, socketName)); | |
| 199 return reinterpret_cast<jint>(server); | 187 return reinterpret_cast<jint>(server); |
| 200 } | 188 } |
| 201 | 189 |
| 202 static void DestroyRemoteDebugging(JNIEnv* env, jobject obj, jint server) { | 190 static void DestroyRemoteDebugging(JNIEnv* env, jobject obj, jint server) { |
| 203 delete reinterpret_cast<DevToolsServer*>(server); | 191 delete reinterpret_cast<DevToolsServer*>(server); |
| 204 } | 192 } |
| 205 | 193 |
| 206 static jboolean IsRemoteDebuggingEnabled(JNIEnv* env, | 194 static jboolean IsRemoteDebuggingEnabled(JNIEnv* env, |
| 207 jobject obj, | 195 jobject obj, |
| 208 jint server) { | 196 jint server) { |
| 209 return reinterpret_cast<DevToolsServer*>(server)->IsStarted(); | 197 return reinterpret_cast<DevToolsServer*>(server)->IsStarted(); |
| 210 } | 198 } |
| 211 | 199 |
| 212 static void SetRemoteDebuggingEnabled(JNIEnv* env, | 200 static void SetRemoteDebuggingEnabled(JNIEnv* env, |
| 213 jobject obj, | 201 jobject obj, |
| 214 jint server, | 202 jint server, |
| 215 jboolean enabled) { | 203 jboolean enabled) { |
| 216 DevToolsServer* devtools_server = reinterpret_cast<DevToolsServer*>(server); | 204 DevToolsServer* devtools_server = reinterpret_cast<DevToolsServer*>(server); |
| 217 if (enabled) { | 205 if (enabled) { |
| 218 devtools_server->Start(); | 206 devtools_server->Start(); |
| 219 } else { | 207 } else { |
| 220 devtools_server->Stop(); | 208 devtools_server->Stop(); |
| 221 } | 209 } |
| 222 } | 210 } |
| OLD | NEW |