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.
5 #include "chrome/browser/android/dev_tools_server.h"
10 #include "base/android/jni_string.h"
11 #include "base/basictypes.h"
12 #include "base/bind.h"
13 #include "base/callback.h"
14 #include "base/command_line.h"
15 #include "base/compiler_specific.h"
16 #include "base/files/file_path.h"
17 #include "base/logging.h"
18 #include "base/strings/string_number_conversions.h"
19 #include "base/strings/stringprintf.h"
20 #include "base/strings/utf_string_conversions.h"
21 #include "chrome/browser/android/tab_android.h"
22 #include "chrome/browser/browser_process.h"
23 #include "chrome/browser/history/top_sites_factory.h"
24 #include "chrome/browser/profiles/profile_manager.h"
25 #include "chrome/browser/ui/android/tab_model/tab_model.h"
26 #include "chrome/browser/ui/android/tab_model/tab_model_list.h"
27 #include "chrome/common/chrome_content_client.h"
28 #include "chrome/common/chrome_version_info.h"
29 #include "components/devtools_http_handler/devtools_http_handler.h"
30 #include "components/devtools_http_handler/devtools_http_handler_delegate.h"
31 #include "components/history/core/browser/top_sites.h"
32 #include "content/public/browser/android/devtools_auth.h"
33 #include "content/public/browser/browser_thread.h"
34 #include "content/public/browser/devtools_agent_host.h"
35 #include "content/public/browser/favicon_status.h"
36 #include "content/public/browser/navigation_entry.h"
37 #include "content/public/browser/render_view_host.h"
38 #include "content/public/browser/web_contents.h"
39 #include "content/public/browser/web_contents_delegate.h"
40 #include "content/public/common/content_switches.h"
41 #include "content/public/common/url_constants.h"
42 #include "content/public/common/user_agent.h"
43 #include "grit/browser_resources.h"
44 #include "jni/DevToolsServer_jni.h"
45 #include "net/base/net_errors.h"
46 #include "net/socket/unix_domain_listen_socket_posix.h"
47 #include "net/socket/unix_domain_server_socket_posix.h"
48 #include "net/url_request/url_request_context_getter.h"
49 #include "ui/base/resource/resource_bundle.h"
51 using content::DevToolsAgentHost
;
52 using content::RenderViewHost
;
53 using content::WebContents
;
54 using devtools_http_handler::DevToolsHttpHandler
;
58 // TL;DR: Do not change this string.
60 // Desktop Chrome relies on this format to identify debuggable apps on Android
61 // (see the code under chrome/browser/devtools/device).
62 // If this string ever changes it would not be sufficient to change the
63 // corresponding string on the client side. Since debugging an older version of
64 // Chrome for Android from a newer version of desktop Chrome is a very common
65 // scenario, the client code will have to be modified to recognize both the old
66 // and the new format.
67 const char kDevToolsChannelNameFormat
[] = "%s_devtools_remote";
69 const char kFrontEndURL
[] =
70 "http://chrome-devtools-frontend.appspot.com/serve_rev/%s/inspector.html";
71 const char kTetheringSocketName
[] = "chrome_devtools_tethering_%d_%d";
73 const int kBackLog
= 10;
75 bool AuthorizeSocketAccessWithDebugPermission(
76 const net::UnixDomainServerSocket::Credentials
& credentials
) {
77 JNIEnv
* env
= base::android::AttachCurrentThread();
78 return Java_DevToolsServer_checkDebugPermission(
79 env
, base::android::GetApplicationContext(),
80 credentials
.process_id
, credentials
.user_id
) ||
81 content::CanUserConnectToDevTools(credentials
);
84 // Delegate implementation for the devtools http handler on android. A new
85 // instance of this gets created each time devtools is enabled.
86 class DevToolsServerDelegate
:
87 public devtools_http_handler::DevToolsHttpHandlerDelegate
{
89 DevToolsServerDelegate() {
92 std::string
GetDiscoveryPageHTML() override
{
93 // TopSites updates itself after a delay. Ask TopSites to update itself
94 // when we're about to show the remote debugging landing page.
95 content::BrowserThread::PostTask(
96 content::BrowserThread::UI
,
98 base::Bind(&DevToolsServerDelegate::PopulatePageThumbnails
));
99 return ResourceBundle::GetSharedInstance().GetRawDataResource(
100 IDR_DEVTOOLS_DISCOVERY_PAGE_HTML
).as_string();
103 std::string
GetFrontendResource(const std::string
& path
) override
{
104 return std::string();
107 std::string
GetPageThumbnailData(const GURL
& url
) override
{
109 ProfileManager::GetLastUsedProfile()->GetOriginalProfile();
110 scoped_refptr
<history::TopSites
> top_sites
=
111 TopSitesFactory::GetForProfile(profile
);
113 scoped_refptr
<base::RefCountedMemory
> data
;
114 if (top_sites
->GetPageThumbnail(url
, false, &data
))
115 return std::string(data
->front_as
<char>(), data
->size());
117 return std::string();
121 static void PopulatePageThumbnails() {
123 ProfileManager::GetLastUsedProfile()->GetOriginalProfile();
124 scoped_refptr
<history::TopSites
> top_sites
=
125 TopSitesFactory::GetForProfile(profile
);
127 top_sites
->SyncWithHistory();
130 DISALLOW_COPY_AND_ASSIGN(DevToolsServerDelegate
);
133 // Factory for UnixDomainServerSocket. It tries a fallback socket when
134 // original socket doesn't work.
135 class UnixDomainServerSocketFactory
136 : public DevToolsHttpHandler::ServerSocketFactory
{
138 UnixDomainServerSocketFactory(
139 const std::string
& socket_name
,
140 const net::UnixDomainServerSocket::AuthCallback
& auth_callback
)
141 : socket_name_(socket_name
),
142 last_tethering_socket_(0),
143 auth_callback_(auth_callback
) {
147 scoped_ptr
<net::ServerSocket
> CreateForHttpServer() override
{
148 scoped_ptr
<net::ServerSocket
> socket(
149 new net::UnixDomainServerSocket(auth_callback_
,
150 true /* use_abstract_namespace */));
152 if (socket
->ListenWithAddressAndPort(socket_name_
, 0, kBackLog
) == net::OK
)
153 return socket
.Pass();
155 // Try a fallback socket name.
156 const std::string
fallback_address(
157 base::StringPrintf("%s_%d", socket_name_
.c_str(), getpid()));
158 if (socket
->ListenWithAddressAndPort(fallback_address
, 0, kBackLog
)
160 return socket
.Pass();
162 return scoped_ptr
<net::ServerSocket
>();
165 scoped_ptr
<net::ServerSocket
> CreateForTethering(std::string
* name
) override
{
166 *name
= base::StringPrintf(
167 kTetheringSocketName
, getpid(), ++last_tethering_socket_
);
168 scoped_ptr
<net::UnixDomainServerSocket
> socket(
169 new net::UnixDomainServerSocket(auth_callback_
, true));
170 if (socket
->ListenWithAddressAndPort(*name
, 0, kBackLog
) != net::OK
)
171 return scoped_ptr
<net::ServerSocket
>();
173 return socket
.Pass();
176 std::string socket_name_
;
177 int last_tethering_socket_
;
178 net::UnixDomainServerSocket::AuthCallback auth_callback_
;
180 DISALLOW_COPY_AND_ASSIGN(UnixDomainServerSocketFactory
);
185 DevToolsServer::DevToolsServer(const std::string
& socket_name_prefix
)
186 : socket_name_(base::StringPrintf(kDevToolsChannelNameFormat
,
187 socket_name_prefix
.c_str())) {
188 // Override the socket name if one is specified on the command line.
189 const base::CommandLine
& command_line
=
190 *base::CommandLine::ForCurrentProcess();
191 if (command_line
.HasSwitch(switches::kRemoteDebuggingSocketName
)) {
192 socket_name_
= command_line
.GetSwitchValueASCII(
193 switches::kRemoteDebuggingSocketName
);
197 DevToolsServer::~DevToolsServer() {
201 void DevToolsServer::Start(bool allow_debug_permission
) {
202 if (devtools_http_handler_
)
205 net::UnixDomainServerSocket::AuthCallback auth_callback
=
206 allow_debug_permission
?
207 base::Bind(&AuthorizeSocketAccessWithDebugPermission
) :
208 base::Bind(&content::CanUserConnectToDevTools
);
209 chrome::VersionInfo version_info
;
211 scoped_ptr
<DevToolsHttpHandler::ServerSocketFactory
> factory(
212 new UnixDomainServerSocketFactory(socket_name_
, auth_callback
));
213 devtools_http_handler_
.reset(new DevToolsHttpHandler(
215 base::StringPrintf(kFrontEndURL
, content::GetWebKitRevision().c_str()),
216 new DevToolsServerDelegate(),
219 version_info
.ProductNameAndVersionForUserAgent(),
223 void DevToolsServer::Stop() {
224 devtools_http_handler_
.reset();
227 bool DevToolsServer::IsStarted() const {
228 return devtools_http_handler_
;
231 bool RegisterDevToolsServer(JNIEnv
* env
) {
232 return RegisterNativesImpl(env
);
235 static jlong
InitRemoteDebugging(JNIEnv
* env
,
237 jstring socket_name_prefix
) {
238 DevToolsServer
* server
= new DevToolsServer(
239 base::android::ConvertJavaStringToUTF8(env
, socket_name_prefix
));
240 return reinterpret_cast<intptr_t>(server
);
243 static void DestroyRemoteDebugging(JNIEnv
* env
, jobject obj
, jlong server
) {
244 delete reinterpret_cast<DevToolsServer
*>(server
);
247 static jboolean
IsRemoteDebuggingEnabled(JNIEnv
* env
,
250 return reinterpret_cast<DevToolsServer
*>(server
)->IsStarted();
253 static void SetRemoteDebuggingEnabled(JNIEnv
* env
,
257 jboolean allow_debug_permission
) {
258 DevToolsServer
* devtools_server
= reinterpret_cast<DevToolsServer
*>(server
);
260 devtools_server
->Start(allow_debug_permission
);
262 devtools_server
->Stop();