1 // Copyright 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 package org.chromium.chrome.browser;
7 import android.content.Context;
8 import android.content.pm.PackageManager;
10 import org.chromium.base.CalledByNative;
13 * Controller for Remote Web Debugging (Developer Tools).
15 public class DevToolsServer {
16 private static final String DEBUG_PERMISSION_SIFFIX = ".permission.DEBUG";
18 private long mNativeDevToolsServer = 0;
20 // Defines what processes may access to the socket.
21 public enum Security {
22 // Use content::CanUserConnectToDevTools to authorize access to the socket.
25 // In addition to default authorization allows access to an app with android permission
26 // named chromeAppPackageName + DEBUG_PERMISSION_SIFFIX.
27 ALLOW_DEBUG_PERMISSION,
30 public DevToolsServer(String socketNamePrefix) {
31 mNativeDevToolsServer = nativeInitRemoteDebugging(socketNamePrefix);
34 public void destroy() {
35 nativeDestroyRemoteDebugging(mNativeDevToolsServer);
36 mNativeDevToolsServer = 0;
39 public boolean isRemoteDebuggingEnabled() {
40 return nativeIsRemoteDebuggingEnabled(mNativeDevToolsServer);
43 public void setRemoteDebuggingEnabled(boolean enabled, Security security) {
44 boolean allowDebugPermission = security == Security.ALLOW_DEBUG_PERMISSION;
45 nativeSetRemoteDebuggingEnabled(mNativeDevToolsServer, enabled, allowDebugPermission);
48 public void setRemoteDebuggingEnabled(boolean enabled) {
49 setRemoteDebuggingEnabled(enabled, Security.DEFAULT);
52 private native long nativeInitRemoteDebugging(String socketNamePrefix);
53 private native void nativeDestroyRemoteDebugging(long devToolsServer);
54 private native boolean nativeIsRemoteDebuggingEnabled(long devToolsServer);
55 private native void nativeSetRemoteDebuggingEnabled(
56 long devToolsServer, boolean enabled, boolean allowDebugPermission);
59 private static boolean checkDebugPermission(Context context, int pid, int uid) {
60 String debugPermissionName = context.getPackageName() + DEBUG_PERMISSION_SIFFIX;
61 return context.checkPermission(debugPermissionName, pid, uid)
62 == PackageManager.PERMISSION_GRANTED;