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