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 package org.xwalk.core.internal;
7 import org.chromium.base.JNINamespace;
8 import android.content.Context;
9 import android.content.pm.PackageManager;
10 import org.chromium.base.CalledByNative;
13 * Controller for Remote Web Debugging (Developer Tools).
15 @JNINamespace("xwalk")
16 class XWalkDevToolsServer {
17 private static final String DEBUG_PERMISSION_SIFFIX = ".permission.DEBUG";
19 private long mNativeDevToolsServer = 0;
20 private String mSocketName = null;
22 // Defines what processes may access to the socket.
23 public enum Security {
24 // Use content::CanUserConnectToDevTools to authorize access to the socket.
27 // In addition to default authorization allows access to an app with android permission
28 // named chromeAppPackageName + DEBUG_PERMISSION_SUFFIX.
29 ALLOW_DEBUG_PERMISSION,
32 public XWalkDevToolsServer(String socketName) {
33 mNativeDevToolsServer = nativeInitRemoteDebugging(socketName);
34 mSocketName = socketName;
37 public void destroy() {
38 nativeDestroyRemoteDebugging(mNativeDevToolsServer);
39 mNativeDevToolsServer = 0;
42 public boolean isRemoteDebuggingEnabled() {
43 return nativeIsRemoteDebuggingEnabled(mNativeDevToolsServer);
46 public void setRemoteDebuggingEnabled(boolean enabled, Security security) {
47 boolean allowDebugPermission = security == Security.ALLOW_DEBUG_PERMISSION;
48 nativeSetRemoteDebuggingEnabled(mNativeDevToolsServer, enabled, allowDebugPermission);
51 public void setRemoteDebuggingEnabled(boolean enabled) {
52 setRemoteDebuggingEnabled(enabled, Security.DEFAULT);
55 public void allowConnectionFromUid(int uid) {
56 nativeAllowConnectionFromUid(mNativeDevToolsServer, uid);
59 public String getSocketName() {
63 private native long nativeInitRemoteDebugging(String socketName);
64 private native void nativeDestroyRemoteDebugging(long devToolsServer);
65 private native boolean nativeIsRemoteDebuggingEnabled(long devToolsServer);
66 private native void nativeSetRemoteDebuggingEnabled(
67 long devToolsServer, boolean enabled, boolean allowDebugPermission);
68 private native void nativeAllowConnectionFromUid(long devToolsServer, int uid);
71 private static boolean checkDebugPermission(Context context, int pid, int uid) {
72 String debugPermissionName = context.getPackageName() + DEBUG_PERMISSION_SIFFIX;
73 return context.checkPermission(debugPermissionName, pid, uid)
74 == PackageManager.PERMISSION_GRANTED;