Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / android / dev_tools_server.cc
blobe6d15aadd93fd29d35c9c979456269bdc1e4bc18
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"
7 #include <pwd.h>
8 #include <cstring>
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 "components/history/core/browser/top_sites.h"
28 #include "content/public/browser/android/devtools_auth.h"
29 #include "content/public/browser/browser_thread.h"
30 #include "content/public/browser/devtools_agent_host.h"
31 #include "content/public/browser/devtools_http_handler.h"
32 #include "content/public/browser/devtools_http_handler_delegate.h"
33 #include "content/public/browser/devtools_target.h"
34 #include "content/public/browser/favicon_status.h"
35 #include "content/public/browser/navigation_entry.h"
36 #include "content/public/browser/render_view_host.h"
37 #include "content/public/browser/web_contents.h"
38 #include "content/public/browser/web_contents_delegate.h"
39 #include "content/public/common/content_switches.h"
40 #include "content/public/common/url_constants.h"
41 #include "content/public/common/user_agent.h"
42 #include "grit/browser_resources.h"
43 #include "jni/DevToolsServer_jni.h"
44 #include "net/base/net_errors.h"
45 #include "net/socket/unix_domain_listen_socket_posix.h"
46 #include "net/socket/unix_domain_server_socket_posix.h"
47 #include "net/url_request/url_request_context_getter.h"
48 #include "ui/base/resource/resource_bundle.h"
50 using content::DevToolsAgentHost;
51 using content::RenderViewHost;
52 using content::WebContents;
54 namespace {
56 // TL;DR: Do not change this string.
58 // Desktop Chrome relies on this format to identify debuggable apps on Android
59 // (see the code under chrome/browser/devtools/device).
60 // If this string ever changes it would not be sufficient to change the
61 // corresponding string on the client side. Since debugging an older version of
62 // Chrome for Android from a newer version of desktop Chrome is a very common
63 // scenario, the client code will have to be modified to recognize both the old
64 // and the new format.
65 const char kDevToolsChannelNameFormat[] = "%s_devtools_remote";
67 const char kFrontEndURL[] =
68 "http://chrome-devtools-frontend.appspot.com/serve_rev/%s/inspector.html";
69 const char kTetheringSocketName[] = "chrome_devtools_tethering_%d_%d";
71 const int kBackLog = 10;
73 bool AuthorizeSocketAccessWithDebugPermission(
74 const net::UnixDomainServerSocket::Credentials& credentials) {
75 JNIEnv* env = base::android::AttachCurrentThread();
76 return Java_DevToolsServer_checkDebugPermission(
77 env, base::android::GetApplicationContext(),
78 credentials.process_id, credentials.user_id) ||
79 content::CanUserConnectToDevTools(credentials);
82 // Delegate implementation for the devtools http handler on android. A new
83 // instance of this gets created each time devtools is enabled.
84 class DevToolsServerDelegate : public content::DevToolsHttpHandlerDelegate {
85 public:
86 DevToolsServerDelegate() {
89 std::string GetDiscoveryPageHTML() override {
90 // TopSites updates itself after a delay. Ask TopSites to update itself
91 // when we're about to show the remote debugging landing page.
92 content::BrowserThread::PostTask(
93 content::BrowserThread::UI,
94 FROM_HERE,
95 base::Bind(&DevToolsServerDelegate::PopulatePageThumbnails));
96 return ResourceBundle::GetSharedInstance().GetRawDataResource(
97 IDR_DEVTOOLS_DISCOVERY_PAGE_HTML).as_string();
100 bool BundlesFrontendResources() override {
101 return false;
104 base::FilePath GetDebugFrontendDir() override {
105 return base::FilePath();
108 private:
109 static void PopulatePageThumbnails() {
110 Profile* profile =
111 ProfileManager::GetLastUsedProfile()->GetOriginalProfile();
112 scoped_refptr<history::TopSites> top_sites =
113 TopSitesFactory::GetForProfile(profile);
114 if (top_sites)
115 top_sites->SyncWithHistory();
118 DISALLOW_COPY_AND_ASSIGN(DevToolsServerDelegate);
121 // Factory for UnixDomainServerSocket. It tries a fallback socket when
122 // original socket doesn't work.
123 class UnixDomainServerSocketFactory
124 : public content::DevToolsHttpHandler::ServerSocketFactory {
125 public:
126 UnixDomainServerSocketFactory(
127 const std::string& socket_name,
128 const net::UnixDomainServerSocket::AuthCallback& auth_callback)
129 : socket_name_(socket_name),
130 last_tethering_socket_(0),
131 auth_callback_(auth_callback) {
134 private:
135 scoped_ptr<net::ServerSocket> CreateForHttpServer() override {
136 scoped_ptr<net::ServerSocket> socket(
137 new net::UnixDomainServerSocket(auth_callback_,
138 true /* use_abstract_namespace */));
140 if (socket->ListenWithAddressAndPort(socket_name_, 0, kBackLog) == net::OK)
141 return socket.Pass();
143 // Try a fallback socket name.
144 const std::string fallback_address(
145 base::StringPrintf("%s_%d", socket_name_.c_str(), getpid()));
146 if (socket->ListenWithAddressAndPort(fallback_address, 0, kBackLog)
147 == net::OK)
148 return socket.Pass();
150 return scoped_ptr<net::ServerSocket>();
153 scoped_ptr<net::ServerSocket> CreateForTethering(std::string* name) override {
154 *name = base::StringPrintf(
155 kTetheringSocketName, getpid(), ++last_tethering_socket_);
156 scoped_ptr<net::UnixDomainServerSocket> socket(
157 new net::UnixDomainServerSocket(auth_callback_, true));
158 if (socket->ListenWithAddressAndPort(*name, 0, kBackLog) != net::OK)
159 return scoped_ptr<net::ServerSocket>();
161 return socket.Pass();
164 std::string socket_name_;
165 int last_tethering_socket_;
166 net::UnixDomainServerSocket::AuthCallback auth_callback_;
168 DISALLOW_COPY_AND_ASSIGN(UnixDomainServerSocketFactory);
171 } // namespace
173 DevToolsServer::DevToolsServer(const std::string& socket_name_prefix)
174 : socket_name_(base::StringPrintf(kDevToolsChannelNameFormat,
175 socket_name_prefix.c_str())) {
176 // Override the socket name if one is specified on the command line.
177 const base::CommandLine& command_line =
178 *base::CommandLine::ForCurrentProcess();
179 if (command_line.HasSwitch(switches::kRemoteDebuggingSocketName)) {
180 socket_name_ = command_line.GetSwitchValueASCII(
181 switches::kRemoteDebuggingSocketName);
185 DevToolsServer::~DevToolsServer() {
186 Stop();
189 void DevToolsServer::Start(bool allow_debug_permission) {
190 if (protocol_handler_)
191 return;
193 net::UnixDomainServerSocket::AuthCallback auth_callback =
194 allow_debug_permission ?
195 base::Bind(&AuthorizeSocketAccessWithDebugPermission) :
196 base::Bind(&content::CanUserConnectToDevTools);
197 scoped_ptr<content::DevToolsHttpHandler::ServerSocketFactory> factory(
198 new UnixDomainServerSocketFactory(socket_name_, auth_callback));
199 protocol_handler_.reset(content::DevToolsHttpHandler::Start(
200 factory.Pass(),
201 base::StringPrintf(kFrontEndURL, content::GetWebKitRevision().c_str()),
202 new DevToolsServerDelegate(),
203 base::FilePath()));
206 void DevToolsServer::Stop() {
207 protocol_handler_.reset();
210 bool DevToolsServer::IsStarted() const {
211 return protocol_handler_;
214 bool RegisterDevToolsServer(JNIEnv* env) {
215 return RegisterNativesImpl(env);
218 static jlong InitRemoteDebugging(JNIEnv* env,
219 jobject obj,
220 jstring socket_name_prefix) {
221 DevToolsServer* server = new DevToolsServer(
222 base::android::ConvertJavaStringToUTF8(env, socket_name_prefix));
223 return reinterpret_cast<intptr_t>(server);
226 static void DestroyRemoteDebugging(JNIEnv* env, jobject obj, jlong server) {
227 delete reinterpret_cast<DevToolsServer*>(server);
230 static jboolean IsRemoteDebuggingEnabled(JNIEnv* env,
231 jobject obj,
232 jlong server) {
233 return reinterpret_cast<DevToolsServer*>(server)->IsStarted();
236 static void SetRemoteDebuggingEnabled(JNIEnv* env,
237 jobject obj,
238 jlong server,
239 jboolean enabled,
240 jboolean allow_debug_permission) {
241 DevToolsServer* devtools_server = reinterpret_cast<DevToolsServer*>(server);
242 if (enabled) {
243 devtools_server->Start(allow_debug_permission);
244 } else {
245 devtools_server->Stop();