Upstream version 9.38.198.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
21     // Defines what processes may access to the socket.
22     public enum Security {
23         // Use content::CanUserConnectToDevTools to authorize access to the socket.
24         DEFAULT,
25
26         // In addition to default authorization allows access to an app with android permission
27         // named chromeAppPackageName + DEBUG_PERMISSION_SUFFIX.
28         ALLOW_DEBUG_PERMISSION,
29     }
30
31     public XWalkDevToolsServer(String socketName) {
32         mNativeDevToolsServer = nativeInitRemoteDebugging(socketName);
33     }
34
35     public void destroy() {
36         nativeDestroyRemoteDebugging(mNativeDevToolsServer);
37         mNativeDevToolsServer = 0;
38     }
39
40     public boolean isRemoteDebuggingEnabled() {
41         return nativeIsRemoteDebuggingEnabled(mNativeDevToolsServer);
42     }
43
44     public void setRemoteDebuggingEnabled(boolean enabled, Security security) {
45         boolean allowDebugPermission = security == Security.ALLOW_DEBUG_PERMISSION;
46         nativeSetRemoteDebuggingEnabled(mNativeDevToolsServer, enabled, allowDebugPermission);
47     }
48
49     public void setRemoteDebuggingEnabled(boolean enabled) {
50         setRemoteDebuggingEnabled(enabled, Security.DEFAULT);
51     }
52
53     public void allowConnectionFromUid(int uid) {
54         nativeAllowConnectionFromUid(mNativeDevToolsServer, uid);
55     }
56
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);
63
64     @CalledByNative
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;
69     }
70 }