1c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton/*
2c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton * Copyright (C) 2016 The Android Open Source Project
3c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton *
4c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton * Licensed under the Apache License, Version 2.0 (the "License");
5c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton * you may not use this file except in compliance with the License.
6c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton * You may obtain a copy of the License at
7c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton *
8c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton *      http://www.apache.org/licenses/LICENSE-2.0
9c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton *
10c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton * Unless required by applicable law or agreed to in writing, software
11c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton * distributed under the License is distributed on an "AS IS" BASIS,
12c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton * See the License for the specific language governing permissions and
14c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton * limitations under the License.
15c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton */
16c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Senntonpackage com.android.server.webkit;
17c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton
18c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Senntonimport android.os.RemoteException;
19c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Senntonimport android.os.ShellCommand;
20c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Senntonimport android.webkit.IWebViewUpdateService;
21c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton
22c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Senntonimport java.io.PrintWriter;
23c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton
24c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Senntonclass WebViewUpdateServiceShellCommand extends ShellCommand {
25c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton    final IWebViewUpdateService mInterface;
26c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton
27c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton    WebViewUpdateServiceShellCommand(IWebViewUpdateService service) {
28c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton        mInterface = service;
29c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton    }
30c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton
31c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton    @Override
32c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton    public int onCommand(String cmd) {
33c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton        if (cmd == null) {
34c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton            return handleDefaultCommands(cmd);
35c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton        }
36c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton
37c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton        final PrintWriter pw = getOutPrintWriter();
38c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton        try {
39c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton            switch(cmd) {
40c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton                case "enable-redundant-packages":
41c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton                    return enableFallbackLogic(false);
42c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton                case "disable-redundant-packages":
43c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton                    return enableFallbackLogic(true);
44ab3b6b12585c209b8c5287a6dcedfe648f857a9fGustav Sennton                case "set-webview-implementation":
45ab3b6b12585c209b8c5287a6dcedfe648f857a9fGustav Sennton                    return setWebViewImplementation();
46b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton                case "enable-multiprocess":
47b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton                    return enableMultiProcess(true);
48b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton                case "disable-multiprocess":
49b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton                    return enableMultiProcess(false);
50c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton                default:
51c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton                    return handleDefaultCommands(cmd);
52c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton            }
53c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton        } catch (RemoteException e) {
54c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton            pw.println("Remote exception: " + e);
55c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton        }
56c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton        return -1;
57c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton    }
58c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton
59c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton    private int enableFallbackLogic(boolean enable) throws RemoteException {
60c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton        final PrintWriter pw = getOutPrintWriter();
61c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton        mInterface.enableFallbackLogic(enable);
62c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton        pw.println("Success");
63c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton        return 0;
64c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton    }
65c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton
66ab3b6b12585c209b8c5287a6dcedfe648f857a9fGustav Sennton    private int setWebViewImplementation() throws RemoteException {
67ab3b6b12585c209b8c5287a6dcedfe648f857a9fGustav Sennton        final PrintWriter pw = getOutPrintWriter();
68ab3b6b12585c209b8c5287a6dcedfe648f857a9fGustav Sennton        String shellChosenPackage = getNextArg();
69ab3b6b12585c209b8c5287a6dcedfe648f857a9fGustav Sennton        String newPackage = mInterface.changeProviderAndSetting(shellChosenPackage);
70ab3b6b12585c209b8c5287a6dcedfe648f857a9fGustav Sennton        if (shellChosenPackage.equals(newPackage)) {
71ab3b6b12585c209b8c5287a6dcedfe648f857a9fGustav Sennton            pw.println("Success");
72ab3b6b12585c209b8c5287a6dcedfe648f857a9fGustav Sennton            return 0;
73ab3b6b12585c209b8c5287a6dcedfe648f857a9fGustav Sennton        } else {
74ab3b6b12585c209b8c5287a6dcedfe648f857a9fGustav Sennton            pw.println(String.format(
75ab3b6b12585c209b8c5287a6dcedfe648f857a9fGustav Sennton                        "Failed to switch to %s, the WebView implementation is now provided by %s.",
76ab3b6b12585c209b8c5287a6dcedfe648f857a9fGustav Sennton                        shellChosenPackage, newPackage));
77ab3b6b12585c209b8c5287a6dcedfe648f857a9fGustav Sennton            return 1;
78ab3b6b12585c209b8c5287a6dcedfe648f857a9fGustav Sennton        }
79ab3b6b12585c209b8c5287a6dcedfe648f857a9fGustav Sennton    }
80ab3b6b12585c209b8c5287a6dcedfe648f857a9fGustav Sennton
81b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton    private int enableMultiProcess(boolean enable) throws RemoteException {
82b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton        final PrintWriter pw = getOutPrintWriter();
83b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton        mInterface.enableMultiProcess(enable);
84b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton        pw.println("Success");
85b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton        return 0;
86b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton    }
87b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton
88c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton    @Override
89c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton    public void onHelp() {
90c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton        PrintWriter pw = getOutPrintWriter();
91c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton        pw.println("WebView updater commands:");
92c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton        pw.println("  help");
93c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton        pw.println("    Print this help text.");
94c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton        pw.println("");
95c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton        pw.println("  enable-redundant-packages");
96c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton        pw.println("    Allow a fallback package to be installed and enabled even when a");
97c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton        pw.println("    more-preferred package is available. This command is useful when testing");
98c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton        pw.println("    fallback packages.");
99c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton        pw.println("  disable-redundant-packages");
100c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton        pw.println("    Disallow installing and enabling fallback packages when a more-preferred");
101c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton        pw.println("    package is available.");
102ab3b6b12585c209b8c5287a6dcedfe648f857a9fGustav Sennton        pw.println("  set-webview-implementation PACKAGE");
103ab3b6b12585c209b8c5287a6dcedfe648f857a9fGustav Sennton        pw.println("    Set the WebView implementation to the specified package.");
104b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton        pw.println("  enable-multiprocess");
105b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton        pw.println("    Enable multi-process mode for WebView");
106b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton        pw.println("  disable-multiprocess");
107b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton        pw.println("    Disable multi-process mode for WebView");
108c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton        pw.println();
109c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton    }
110c83e3fa35a5736a1778d241abd18dffa5953f416Gustav Sennton}
111