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;
21 // Defines what processes may access to the socket.
22 public enum Security {
23 // Use content::CanUserConnectToDevTools to authorize access to the socket.
26 // In addition to default authorization allows access to an app with android permission
27 // named chromeAppPackageName + DEBUG_PERMISSION_SUFFIX.
28 ALLOW_DEBUG_PERMISSION,
31 public XWalkDevToolsServer(String socketName) {
32 mNativeDevToolsServer = nativeInitRemoteDebugging(socketName);
35 public void destroy() {
36 nativeDestroyRemoteDebugging(mNativeDevToolsServer);
37 mNativeDevToolsServer = 0;
40 public boolean isRemoteDebuggingEnabled() {
41 return nativeIsRemoteDebuggingEnabled(mNativeDevToolsServer);
44 public void setRemoteDebuggingEnabled(boolean enabled, Security security) {
45 boolean allowDebugPermission = security == Security.ALLOW_DEBUG_PERMISSION;
46 nativeSetRemoteDebuggingEnabled(mNativeDevToolsServer, enabled, allowDebugPermission);
49 public void setRemoteDebuggingEnabled(boolean enabled) {
50 setRemoteDebuggingEnabled(enabled, Security.DEFAULT);
53 public void allowConnectionFromUid(int uid) {
54 nativeAllowConnectionFromUid(mNativeDevToolsServer, uid);
57 private native long nativeInitRemoteDebugging(String socketName);
58 private native void nativeDestroyRemoteDebugging(long devToolsServer);
59 private native boolean nativeIsRemoteDebuggingEnabled(long devToolsServer);
60 private native void nativeSetRemoteDebuggingEnabled(
61 long devToolsServer, boolean enabled, boolean allowDebugPermission);
62 private native void nativeAllowConnectionFromUid(long devToolsServer, int uid);
65 private static boolean checkDebugPermission(Context context, int pid, int uid) {
66 String debugPermissionName = context.getPackageName() + DEBUG_PERMISSION_SIFFIX;
67 return context.checkPermission(debugPermissionName, pid, uid)
68 == PackageManager.PERMISSION_GRANTED;