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