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" | 23 #include "components/devtools_remote_frontend/devtools_auth_android.h" |
24 #include "content/public/browser/android/devtools_auth.h" | 24 #include "components/devtools_remote_frontend/devtools_remote_frontend_util.h" |
25 #include "content/public/browser/browser_thread.h" | 25 #include "content/public/browser/browser_thread.h" |
26 #include "content/public/browser/devtools_http_handler.h" | 26 #include "content/public/browser/devtools_http_handler.h" |
27 #include "content/public/browser/devtools_http_handler_delegate.h" | 27 #include "content/public/browser/devtools_http_handler_delegate.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 29 matching lines...) Expand all Loading... | |
105 | 102 |
106 virtual scoped_refptr<net::StreamListenSocket> CreateSocketForTethering( | 103 virtual scoped_refptr<net::StreamListenSocket> CreateSocketForTethering( |
107 net::StreamListenSocket::Delegate* delegate, | 104 net::StreamListenSocket::Delegate* delegate, |
108 std::string* name) OVERRIDE { | 105 std::string* name) OVERRIDE { |
109 *name = base::StringPrintf( | 106 *name = base::StringPrintf( |
110 kTetheringSocketName, getpid(), ++last_tethering_socket_); | 107 kTetheringSocketName, getpid(), ++last_tethering_socket_); |
111 return net::UnixDomainSocket::CreateAndListenWithAbstractNamespace( | 108 return net::UnixDomainSocket::CreateAndListenWithAbstractNamespace( |
112 *name, | 109 *name, |
113 "", | 110 "", |
114 delegate, | 111 delegate, |
115 base::Bind(&content::CanUserConnectToDevTools)); | 112 base::Bind(&devtools_remote_frontend::CanUserConnectToDevTools)); |
116 } | 113 } |
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_(devtools_remote_frontend::GetDevToolsServerSocketName( |
pfeldman
2013/06/20 12:37:11
Simply hardcode values here.
mnaganov (inactive)
2013/06/20 16:48:08
I'm now reusing it from devtools_adb_bridge, since
| |
137 socket_name_(kDefaultSocketName), | 133 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_(devtools_remote_frontend::GetDevToolsServerSocketName( |
149 : use_bundled_frontend_resources_(use_bundled_frontend_resources), | 145 socket_name_prefix)), |
150 socket_name_(socket_name), | |
151 protocol_handler_(NULL) { | 146 protocol_handler_(NULL) { |
152 } | 147 } |
153 | 148 |
154 DevToolsServer::~DevToolsServer() { | 149 DevToolsServer::~DevToolsServer() { |
155 Stop(); | 150 Stop(); |
156 } | 151 } |
157 | 152 |
158 void DevToolsServer::Start() { | 153 void DevToolsServer::Start() { |
159 if (protocol_handler_) | 154 if (protocol_handler_) |
160 return; | 155 return; |
161 | 156 |
162 chrome::VersionInfo version_info; | |
163 | |
164 protocol_handler_ = content::DevToolsHttpHandler::Start( | 157 protocol_handler_ = content::DevToolsHttpHandler::Start( |
165 new net::UnixDomainSocketWithAbstractNamespaceFactory( | 158 new net::UnixDomainSocketWithAbstractNamespaceFactory( |
166 socket_name_, | 159 socket_name_, |
167 base::StringPrintf("%s_%d", socket_name_.c_str(), getpid()), | 160 base::StringPrintf("%s_%d", socket_name_.c_str(), getpid()), |
168 base::Bind(&content::CanUserConnectToDevTools)), | 161 base::Bind(&devtools_remote_frontend::CanUserConnectToDevTools)), |
169 use_bundled_frontend_resources_ ? | 162 devtools_remote_frontend::GetDevToolsFrontendMainResourceURL(), |
170 "" : | 163 new DevToolsServerDelegate()); |
171 base::StringPrintf(kFrontEndURL, version_info.Version().c_str()), | |
172 new DevToolsServerDelegate(use_bundled_frontend_resources_)); | |
173 } | 164 } |
174 | 165 |
175 void DevToolsServer::Stop() { | 166 void DevToolsServer::Stop() { |
176 if (!protocol_handler_) | 167 if (!protocol_handler_) |
177 return; | 168 return; |
178 // Note that the call to Stop() below takes care of |protocol_handler_| | 169 // Note that the call to Stop() below takes care of |protocol_handler_| |
179 // deletion. | 170 // deletion. |
180 protocol_handler_->Stop(); | 171 protocol_handler_->Stop(); |
181 protocol_handler_ = NULL; | 172 protocol_handler_ = NULL; |
182 } | 173 } |
183 | 174 |
184 bool DevToolsServer::IsStarted() const { | 175 bool DevToolsServer::IsStarted() const { |
185 return protocol_handler_; | 176 return protocol_handler_; |
186 } | 177 } |
187 | 178 |
188 bool RegisterDevToolsServer(JNIEnv* env) { | 179 bool RegisterDevToolsServer(JNIEnv* env) { |
189 return RegisterNativesImpl(env); | 180 return RegisterNativesImpl(env); |
190 } | 181 } |
191 | 182 |
192 static jint InitRemoteDebugging(JNIEnv* env, | 183 static jint InitRemoteDebugging(JNIEnv* env, |
193 jobject obj, | 184 jobject obj, |
194 jboolean use_bundled_frontend_resources, | 185 jstring socketNamePrefix) { |
195 jstring socketName) { | |
196 DevToolsServer* server = new DevToolsServer( | 186 DevToolsServer* server = new DevToolsServer( |
197 use_bundled_frontend_resources, | 187 base::android::ConvertJavaStringToUTF8(env, socketNamePrefix)); |
198 base::android::ConvertJavaStringToUTF8(env, socketName)); | |
199 return reinterpret_cast<jint>(server); | 188 return reinterpret_cast<jint>(server); |
200 } | 189 } |
201 | 190 |
202 static void DestroyRemoteDebugging(JNIEnv* env, jobject obj, jint server) { | 191 static void DestroyRemoteDebugging(JNIEnv* env, jobject obj, jint server) { |
203 delete reinterpret_cast<DevToolsServer*>(server); | 192 delete reinterpret_cast<DevToolsServer*>(server); |
204 } | 193 } |
205 | 194 |
206 static jboolean IsRemoteDebuggingEnabled(JNIEnv* env, | 195 static jboolean IsRemoteDebuggingEnabled(JNIEnv* env, |
207 jobject obj, | 196 jobject obj, |
208 jint server) { | 197 jint server) { |
209 return reinterpret_cast<DevToolsServer*>(server)->IsStarted(); | 198 return reinterpret_cast<DevToolsServer*>(server)->IsStarted(); |
210 } | 199 } |
211 | 200 |
212 static void SetRemoteDebuggingEnabled(JNIEnv* env, | 201 static void SetRemoteDebuggingEnabled(JNIEnv* env, |
213 jobject obj, | 202 jobject obj, |
214 jint server, | 203 jint server, |
215 jboolean enabled) { | 204 jboolean enabled) { |
216 DevToolsServer* devtools_server = reinterpret_cast<DevToolsServer*>(server); | 205 DevToolsServer* devtools_server = reinterpret_cast<DevToolsServer*>(server); |
217 if (enabled) { | 206 if (enabled) { |
218 devtools_server->Start(); | 207 devtools_server->Start(); |
219 } else { | 208 } else { |
220 devtools_server->Stop(); | 209 devtools_server->Stop(); |
221 } | 210 } |
222 } | 211 } |
OLD | NEW |