Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / android / dev_tools_server.cc
blob5ada22166d08864030a9fc2f8ae36c9d3aaebd64
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 "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_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;
53 using devtools_http_handler::DevToolsHttpHandler;
55 namespace {
57 // TL;DR: Do not change this string.
59 // Desktop Chrome relies on this format to identify debuggable apps on Android
60 // (see the code under chrome/browser/devtools/device).
61 // If this string ever changes it would not be sufficient to change the
62 // corresponding string on the client side. Since debugging an older version of
63 // Chrome for Android from a newer version of desktop Chrome is a very common
64 // scenario, the client code will have to be modified to recognize both the old
65 // and the new format.
66 const char kDevToolsChannelNameFormat[] = "%s_devtools_remote";
68 const char kFrontEndURL[] =
69 "http://chrome-devtools-frontend.appspot.com/serve_rev/%s/inspector.html";
70 const char kTetheringSocketName[] = "chrome_devtools_tethering_%d_%d";
72 const int kBackLog = 10;
74 bool AuthorizeSocketAccessWithDebugPermission(
75 const net::UnixDomainServerSocket::Credentials& credentials) {
76 JNIEnv* env = base::android::AttachCurrentThread();
77 return Java_DevToolsServer_checkDebugPermission(
78 env, base::android::GetApplicationContext(),
79 credentials.process_id, credentials.user_id) ||
80 content::CanUserConnectToDevTools(credentials);
83 // Delegate implementation for the devtools http handler on android. A new
84 // instance of this gets created each time devtools is enabled.
85 class DevToolsServerDelegate :
86 public devtools_http_handler::DevToolsHttpHandlerDelegate {
87 public:
88 DevToolsServerDelegate() {
91 std::string GetDiscoveryPageHTML() override {
92 // TopSites updates itself after a delay. Ask TopSites to update itself
93 // when we're about to show the remote debugging landing page.
94 content::BrowserThread::PostTask(
95 content::BrowserThread::UI,
96 FROM_HERE,
97 base::Bind(&DevToolsServerDelegate::PopulatePageThumbnails));
98 return ResourceBundle::GetSharedInstance().GetRawDataResource(
99 IDR_DEVTOOLS_DISCOVERY_PAGE_HTML).as_string();
102 std::string GetFrontendResource(const std::string& path) override {
103 return std::string();
106 std::string GetPageThumbnailData(const GURL& url) override {
107 Profile* profile =
108 ProfileManager::GetLastUsedProfile()->GetOriginalProfile();
109 scoped_refptr<history::TopSites> top_sites =
110 TopSitesFactory::GetForProfile(profile);
111 if (top_sites) {
112 scoped_refptr<base::RefCountedMemory> data;
113 if (top_sites->GetPageThumbnail(url, false, &data))
114 return std::string(data->front_as<char>(), data->size());
116 return std::string();
119 private:
120 static void PopulatePageThumbnails() {
121 Profile* profile =
122 ProfileManager::GetLastUsedProfile()->GetOriginalProfile();
123 scoped_refptr<history::TopSites> top_sites =
124 TopSitesFactory::GetForProfile(profile);
125 if (top_sites)
126 top_sites->SyncWithHistory();
129 DISALLOW_COPY_AND_ASSIGN(DevToolsServerDelegate);
132 // Factory for UnixDomainServerSocket. It tries a fallback socket when
133 // original socket doesn't work.
134 class UnixDomainServerSocketFactory
135 : public DevToolsHttpHandler::ServerSocketFactory {
136 public:
137 UnixDomainServerSocketFactory(
138 const std::string& socket_name,
139 const net::UnixDomainServerSocket::AuthCallback& auth_callback)
140 : socket_name_(socket_name),
141 last_tethering_socket_(0),
142 auth_callback_(auth_callback) {
145 private:
146 scoped_ptr<net::ServerSocket> CreateForHttpServer() override {
147 scoped_ptr<net::ServerSocket> socket(
148 new net::UnixDomainServerSocket(auth_callback_,
149 true /* use_abstract_namespace */));
151 if (socket->ListenWithAddressAndPort(socket_name_, 0, kBackLog) == net::OK)
152 return socket.Pass();
154 // Try a fallback socket name.
155 const std::string fallback_address(
156 base::StringPrintf("%s_%d", socket_name_.c_str(), getpid()));
157 if (socket->ListenWithAddressAndPort(fallback_address, 0, kBackLog)
158 == net::OK)
159 return socket.Pass();
161 return scoped_ptr<net::ServerSocket>();
164 scoped_ptr<net::ServerSocket> CreateForTethering(std::string* name) override {
165 *name = base::StringPrintf(
166 kTetheringSocketName, getpid(), ++last_tethering_socket_);
167 scoped_ptr<net::UnixDomainServerSocket> socket(
168 new net::UnixDomainServerSocket(auth_callback_, true));
169 if (socket->ListenWithAddressAndPort(*name, 0, kBackLog) != net::OK)
170 return scoped_ptr<net::ServerSocket>();
172 return socket.Pass();
175 std::string socket_name_;
176 int last_tethering_socket_;
177 net::UnixDomainServerSocket::AuthCallback auth_callback_;
179 DISALLOW_COPY_AND_ASSIGN(UnixDomainServerSocketFactory);
182 } // namespace
184 DevToolsServer::DevToolsServer(const std::string& socket_name_prefix)
185 : socket_name_(base::StringPrintf(kDevToolsChannelNameFormat,
186 socket_name_prefix.c_str())) {
187 // Override the socket name if one is specified on the command line.
188 const base::CommandLine& command_line =
189 *base::CommandLine::ForCurrentProcess();
190 if (command_line.HasSwitch(switches::kRemoteDebuggingSocketName)) {
191 socket_name_ = command_line.GetSwitchValueASCII(
192 switches::kRemoteDebuggingSocketName);
196 DevToolsServer::~DevToolsServer() {
197 Stop();
200 void DevToolsServer::Start(bool allow_debug_permission) {
201 if (devtools_http_handler_)
202 return;
204 net::UnixDomainServerSocket::AuthCallback auth_callback =
205 allow_debug_permission ?
206 base::Bind(&AuthorizeSocketAccessWithDebugPermission) :
207 base::Bind(&content::CanUserConnectToDevTools);
208 chrome::VersionInfo version_info;
210 scoped_ptr<DevToolsHttpHandler::ServerSocketFactory> factory(
211 new UnixDomainServerSocketFactory(socket_name_, auth_callback));
212 devtools_http_handler_.reset(new DevToolsHttpHandler(
213 factory.Pass(),
214 base::StringPrintf(kFrontEndURL, content::GetWebKitRevision().c_str()),
215 new DevToolsServerDelegate(),
216 base::FilePath(),
217 base::FilePath(),
218 version_info.ProductNameAndVersionForUserAgent(),
219 ::GetUserAgent()));
222 void DevToolsServer::Stop() {
223 devtools_http_handler_.reset();
226 bool DevToolsServer::IsStarted() const {
227 return devtools_http_handler_;
230 bool RegisterDevToolsServer(JNIEnv* env) {
231 return RegisterNativesImpl(env);
234 static jlong InitRemoteDebugging(JNIEnv* env,
235 jobject obj,
236 jstring socket_name_prefix) {
237 DevToolsServer* server = new DevToolsServer(
238 base::android::ConvertJavaStringToUTF8(env, socket_name_prefix));
239 return reinterpret_cast<intptr_t>(server);
242 static void DestroyRemoteDebugging(JNIEnv* env, jobject obj, jlong server) {
243 delete reinterpret_cast<DevToolsServer*>(server);
246 static jboolean IsRemoteDebuggingEnabled(JNIEnv* env,
247 jobject obj,
248 jlong server) {
249 return reinterpret_cast<DevToolsServer*>(server)->IsStarted();
252 static void SetRemoteDebuggingEnabled(JNIEnv* env,
253 jobject obj,
254 jlong server,
255 jboolean enabled,
256 jboolean allow_debug_permission) {
257 DevToolsServer* devtools_server = reinterpret_cast<DevToolsServer*>(server);
258 if (enabled) {
259 devtools_server->Start(allow_debug_permission);
260 } else {
261 devtools_server->Stop();