MDM Developer Tools Restriction

When restriction is in effect, SWE sessions in the work profile
will not appear in desktop Chrome's 'chrome://inspect/#devices'
page.

Change-Id: I2f0be4ea0c1d8b62f071b000c4b5c827f93f431b
diff --git a/src/com/android/browser/EngineInitializer.java b/src/com/android/browser/EngineInitializer.java
index 037e658..b64f493 100644
--- a/src/com/android/browser/EngineInitializer.java
+++ b/src/com/android/browser/EngineInitializer.java
@@ -39,6 +39,8 @@
 import android.util.Log;
 import android.view.ViewTreeObserver;
 
+import com.android.browser.mdm.DevToolsRestriction;
+
 import org.codeaurora.swe.BrowserCommandLine;
 import org.codeaurora.swe.Engine;
 
@@ -376,8 +378,8 @@
                         .build());
             }
 
-            //Enable remote debugging by default
-            Engine.setWebContentsDebuggingEnabled(true);
+            //Enable remote debugging by default as long as MDM restriction is not enabled
+            Engine.setWebContentsDebuggingEnabled(!DevToolsRestriction.getInstance().isEnabled());
             mInitializationCompleted = true;
             mInitializationStarted = true;
             BrowserSettings.getInstance().onEngineInitializationComplete();
diff --git a/src/com/android/browser/mdm/DevToolsRestriction.java b/src/com/android/browser/mdm/DevToolsRestriction.java
new file mode 100644
index 0000000..36acce8
--- /dev/null
+++ b/src/com/android/browser/mdm/DevToolsRestriction.java
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2015, The Linux Foundation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ * * Neither the name of The Linux Foundation nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+package com.android.browser.mdm;
+
+import android.os.Bundle;
+import android.util.Log;
+
+import org.codeaurora.swe.Engine;
+
+public class DevToolsRestriction extends Restriction {
+
+    private final static String TAG = "DevToolsRestriction";
+
+    public static final String DEV_TOOLS_RESTRICTION = "DevToolsEnabled";
+
+    private static DevToolsRestriction sInstance;
+
+    private DevToolsRestriction() {
+        super(TAG);
+    }
+
+    public static DevToolsRestriction getInstance() {
+        synchronized (DevToolsRestriction.class) {
+            if (sInstance == null) {
+                sInstance = new DevToolsRestriction();
+            }
+        }
+        return sInstance;
+    }
+
+    @Override
+    protected void doCustomInit() {
+    }
+
+    /*
+     *   Note reversed logic:
+     *       [x] 'Restrict' true  = DevToolsEnabled : false   => disable DevTools in swe
+     *       [ ] 'Restrict' false = DevToolsEnabled : true    => enable DevTools in swe
+     */
+    @Override
+    public void enforce(Bundle restrictions) {
+
+        boolean bEnable = false;
+        if (restrictions.containsKey(DEV_TOOLS_RESTRICTION)) {
+            bEnable = ! restrictions.getBoolean(DEV_TOOLS_RESTRICTION);
+        }
+        Log.d(TAG, "Enforce [" + bEnable + "]");
+        enable(bEnable);
+
+        Engine.setWebContentsDebuggingEnabled(!isEnabled());
+    }
+}
diff --git a/src/com/android/browser/mdm/tests/DevToolsRestrictionsTest.java b/src/com/android/browser/mdm/tests/DevToolsRestrictionsTest.java
new file mode 100644
index 0000000..06a4117
--- /dev/null
+++ b/src/com/android/browser/mdm/tests/DevToolsRestrictionsTest.java
@@ -0,0 +1,109 @@
+/*
+ * Copyright (c) 2015, The Linux Foundation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ * * Neither the name of The Linux Foundation nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+package com.android.browser.mdm.tests;
+
+import android.app.Instrumentation;
+import android.os.Bundle;
+import android.test.ActivityInstrumentationTestCase2;
+import android.util.Log;
+
+import com.android.browser.BrowserActivity;
+import com.android.browser.mdm.DevToolsRestriction;
+import com.android.browser.mdm.ManagedProfileManager;
+
+public class DevToolsRestrictionsTest extends ActivityInstrumentationTestCase2<BrowserActivity> {
+
+    private final static String TAG = "+++DevToolsRestTest";
+
+    private Instrumentation mInstrumentation;
+    private BrowserActivity mActivity;
+    private DevToolsRestriction devToolsRestriction;
+
+    public DevToolsRestrictionsTest() {
+        super(BrowserActivity.class);
+    }
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        mInstrumentation = getInstrumentation();
+        mActivity = getActivity();
+        devToolsRestriction = DevToolsRestriction.getInstance();
+    }
+
+    public void test_EditBookmarksRestriction() throws Throwable {
+        Log.i(TAG, "*** Starting DevTools Test ***");
+        clearDevToolsRestrictions();
+        assertFalse(devToolsRestriction.isEnabled());
+
+        setDevToolsRestrictions(true);
+        assertTrue(devToolsRestriction.isEnabled());
+
+        setDevToolsRestrictions(false);
+        assertFalse(devToolsRestriction.isEnabled());
+    }
+
+    /**
+    * Activate DevTools restriction
+     * @param clear    if true, sends an empty bundle (which is interpreted as "allow editing"
+     * @param enabled  Required. true (disallow editing: restriction enforced)
+     *                        or false (allow editing: restriction lifted)
+    *
+    */
+    private void setDevToolsRestrictions(boolean clear, boolean enabled) {
+        final Bundle restrictions = new Bundle();
+
+        if (!clear) {
+            // note reversed logic. This is setting 'DevToolsEnabled'
+            //    if enabled is true, we want it set to false and vice cersa
+            restrictions.putBoolean(DevToolsRestriction.DEV_TOOLS_RESTRICTION, ! enabled);
+        }
+
+        // Deliver restriction on UI thread
+        mActivity.runOnUiThread(new Runnable() {
+            @Override
+            public void run() {
+                ManagedProfileManager.getInstance().setMdmRestrictions(restrictions);
+            }
+        });
+
+        // Wait to ensure restriction is set
+        mInstrumentation.waitForIdleSync();
+    }
+
+    private void clearDevToolsRestrictions() {
+        setDevToolsRestrictions(true, false);
+    }
+
+    private void setDevToolsRestrictions(boolean enabled) {
+        setDevToolsRestrictions(false, enabled);
+    }
+}