Upstream version 10.39.233.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / android / core_internal / src / org / xwalk / core / internal / XWalkDevToolsServer.java
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.
4
5 package org.xwalk.core.internal;
6
7 import org.chromium.base.JNINamespace;
8 import android.content.Context;
9 import android.content.pm.PackageManager;
10 import org.chromium.base.CalledByNative;
11
12 /**
13  * Controller for Remote Web Debugging (Developer Tools).
14  */
15 @JNINamespace("xwalk")
16 class XWalkDevToolsServer {
17     private static final String DEBUG_PERMISSION_SIFFIX = ".permission.DEBUG";
18
19     private long mNativeDevToolsServer = 0;
20     private String mSocketName = null;
21
22     // Defines what processes may access to the socket.
23     public enum Security {
24         // Use content::CanUserConnectToDevTools to authorize access to the socket.
25         DEFAULT,
26
27         // In addition to default authorization allows access to an app with android permission
28         // named chromeAppPackageName + DEBUG_PERMISSION_SUFFIX.
29         ALLOW_DEBUG_PERMISSION,
30     }
31
32     public XWalkDevToolsServer(String socketName) {
33         mNativeDevToolsServer = nativeInitRemoteDebugging(socketName);
34         mSocketName = socketName;
35     }
36
37     public void destroy() {
38         nativeDestroyRemoteDebugging(mNativeDevToolsServer);
39         mNativeDevToolsServer = 0;
40     }
41
42     public boolean isRemoteDebuggingEnabled() {
43         return nativeIsRemoteDebuggingEnabled(mNativeDevToolsServer);
44     }
45
46     public void setRemoteDebuggingEnabled(boolean enabled, Security security) {
47         boolean allowDebugPermission = security == Security.ALLOW_DEBUG_PERMISSION;
48         nativeSetRemoteDebuggingEnabled(mNativeDevToolsServer, enabled, allowDebugPermission);
49     }
50
51     public void setRemoteDebuggingEnabled(boolean enabled) {
52         setRemoteDebuggingEnabled(enabled, Security.DEFAULT);
53     }
54
55     public void allowConnectionFromUid(int uid) {
56         nativeAllowConnectionFromUid(mNativeDevToolsServer, uid);
57     }
58
59     public String getSocketName() {
60         return mSocketName;
61     }
62
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);
69
70     @CalledByNative
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;
75     }
76 }