Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / chrome / browser / android / dev_tools_server.cc
blob904bcb76e943e4ca6e37367676a5898ff99a24a8
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 "components/devtools_http_handler/devtools_http_handler.h"
29 #include "components/devtools_http_handler/devtools_http_handler_delegate.h"
30 #include "components/history/core/browser/top_sites.h"
31 #include "components/version_info/version_info.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 content::DevToolsExternalAgentProxyDelegate*
120 HandleWebSocketConnection(const std::string& path) override {
121 return nullptr;
124 private:
125 static void PopulatePageThumbnails() {
126 Profile* profile =
127 ProfileManager::GetLastUsedProfile()->GetOriginalProfile();
128 scoped_refptr<history::TopSites> top_sites =
129 TopSitesFactory::GetForProfile(profile);
130 if (top_sites)
131 top_sites->SyncWithHistory();
134 DISALLOW_COPY_AND_ASSIGN(DevToolsServerDelegate);
137 // Factory for UnixDomainServerSocket. It tries a fallback socket when
138 // original socket doesn't work.
139 class UnixDomainServerSocketFactory
140 : public DevToolsHttpHandler::ServerSocketFactory {
141 public:
142 UnixDomainServerSocketFactory(
143 const std::string& socket_name,
144 const net::UnixDomainServerSocket::AuthCallback& auth_callback)
145 : socket_name_(socket_name),
146 last_tethering_socket_(0),
147 auth_callback_(auth_callback) {
150 private:
151 scoped_ptr<net::ServerSocket> CreateForHttpServer() override {
152 scoped_ptr<net::ServerSocket> socket(
153 new net::UnixDomainServerSocket(auth_callback_,
154 true /* use_abstract_namespace */));
156 if (socket->ListenWithAddressAndPort(socket_name_, 0, kBackLog) == net::OK)
157 return socket.Pass();
159 // Try a fallback socket name.
160 const std::string fallback_address(
161 base::StringPrintf("%s_%d", socket_name_.c_str(), getpid()));
162 if (socket->ListenWithAddressAndPort(fallback_address, 0, kBackLog)
163 == net::OK)
164 return socket.Pass();
166 return scoped_ptr<net::ServerSocket>();
169 scoped_ptr<net::ServerSocket> CreateForTethering(std::string* name) override {
170 *name = base::StringPrintf(
171 kTetheringSocketName, getpid(), ++last_tethering_socket_);
172 scoped_ptr<net::UnixDomainServerSocket> socket(
173 new net::UnixDomainServerSocket(auth_callback_, true));
174 if (socket->ListenWithAddressAndPort(*name, 0, kBackLog) != net::OK)
175 return scoped_ptr<net::ServerSocket>();
177 return socket.Pass();
180 std::string socket_name_;
181 int last_tethering_socket_;
182 net::UnixDomainServerSocket::AuthCallback auth_callback_;
184 DISALLOW_COPY_AND_ASSIGN(UnixDomainServerSocketFactory);
187 } // namespace
189 DevToolsServer::DevToolsServer(const std::string& socket_name_prefix)
190 : socket_name_(base::StringPrintf(kDevToolsChannelNameFormat,
191 socket_name_prefix.c_str())) {
192 // Override the socket name if one is specified on the command line.
193 const base::CommandLine& command_line =
194 *base::CommandLine::ForCurrentProcess();
195 if (command_line.HasSwitch(switches::kRemoteDebuggingSocketName)) {
196 socket_name_ = command_line.GetSwitchValueASCII(
197 switches::kRemoteDebuggingSocketName);
201 DevToolsServer::~DevToolsServer() {
202 Stop();
205 void DevToolsServer::Start(bool allow_debug_permission) {
206 if (devtools_http_handler_)
207 return;
209 net::UnixDomainServerSocket::AuthCallback auth_callback =
210 allow_debug_permission ?
211 base::Bind(&AuthorizeSocketAccessWithDebugPermission) :
212 base::Bind(&content::CanUserConnectToDevTools);
213 scoped_ptr<DevToolsHttpHandler::ServerSocketFactory> factory(
214 new UnixDomainServerSocketFactory(socket_name_, auth_callback));
215 devtools_http_handler_.reset(new DevToolsHttpHandler(
216 factory.Pass(),
217 base::StringPrintf(kFrontEndURL, content::GetWebKitRevision().c_str()),
218 new DevToolsServerDelegate(),
219 base::FilePath(),
220 base::FilePath(),
221 version_info::GetProductNameAndVersionForUserAgent(),
222 ::GetUserAgent()));
225 void DevToolsServer::Stop() {
226 devtools_http_handler_.reset();
229 bool DevToolsServer::IsStarted() const {
230 return devtools_http_handler_;
233 bool RegisterDevToolsServer(JNIEnv* env) {
234 return RegisterNativesImpl(env);
237 static jlong InitRemoteDebugging(
238 JNIEnv* env,
239 const JavaParamRef<jobject>& obj,
240 const JavaParamRef<jstring>& socket_name_prefix) {
241 DevToolsServer* server = new DevToolsServer(
242 base::android::ConvertJavaStringToUTF8(env, socket_name_prefix));
243 return reinterpret_cast<intptr_t>(server);
246 static void DestroyRemoteDebugging(JNIEnv* env,
247 const JavaParamRef<jobject>& obj,
248 jlong server) {
249 delete reinterpret_cast<DevToolsServer*>(server);
252 static jboolean IsRemoteDebuggingEnabled(JNIEnv* env,
253 const JavaParamRef<jobject>& obj,
254 jlong server) {
255 return reinterpret_cast<DevToolsServer*>(server)->IsStarted();
258 static void SetRemoteDebuggingEnabled(JNIEnv* env,
259 const JavaParamRef<jobject>& obj,
260 jlong server,
261 jboolean enabled,
262 jboolean allow_debug_permission) {
263 DevToolsServer* devtools_server = reinterpret_cast<DevToolsServer*>(server);
264 if (enabled) {
265 devtools_server->Start(allow_debug_permission);
266 } else {
267 devtools_server->Stop();