WebViewUpdateService.java revision 79fea48aeca5c7d4c16c70c996c42a18d6cd8731
1/*
2 * Copyright (C) 2012 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 */
16
17package com.android.server.webkit;
18
19import android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22import android.content.IntentFilter;
23import android.content.pm.PackageManager;
24import android.os.Binder;
25import android.os.PatternMatcher;
26import android.os.Process;
27import android.os.ResultReceiver;
28import android.os.UserHandle;
29import android.util.Slog;
30import android.webkit.IWebViewUpdateService;
31import android.webkit.WebViewFactory;
32import android.webkit.WebViewProviderInfo;
33import android.webkit.WebViewProviderResponse;
34
35import com.android.server.SystemService;
36
37import java.io.FileDescriptor;
38import java.util.Arrays;
39
40/**
41 * Private service to wait for the updatable WebView to be ready for use.
42 * @hide
43 */
44public class WebViewUpdateService extends SystemService {
45
46    private static final String TAG = "WebViewUpdateService";
47
48    private BroadcastReceiver mWebViewUpdatedReceiver;
49    private WebViewUpdateServiceImpl mImpl;
50
51    static final int PACKAGE_CHANGED = 0;
52    static final int PACKAGE_ADDED = 1;
53    static final int PACKAGE_ADDED_REPLACED = 2;
54    static final int PACKAGE_REMOVED = 3;
55
56    public WebViewUpdateService(Context context) {
57        super(context);
58        mImpl = new WebViewUpdateServiceImpl(context, new SystemImpl());
59    }
60
61    @Override
62    public void onStart() {
63        mWebViewUpdatedReceiver = new BroadcastReceiver() {
64                @Override
65                public void onReceive(Context context, Intent intent) {
66                    switch (intent.getAction()) {
67                        case Intent.ACTION_PACKAGE_REMOVED:
68                            // When a package is replaced we will receive two intents, one
69                            // representing the removal of the old package and one representing the
70                            // addition of the new package.
71                            // In the case where we receive an intent to remove the old version of
72                            // the package that is being replaced we early-out here so that we don't
73                            // run the update-logic twice.
74                            if (intent.getExtras().getBoolean(Intent.EXTRA_REPLACING)) return;
75                            mImpl.packageStateChanged(packageNameFromIntent(intent),
76                                    PACKAGE_REMOVED);
77                            break;
78                        case Intent.ACTION_PACKAGE_CHANGED:
79                            // Ensure that we only heed PACKAGE_CHANGED intents if they change an
80                            // entire package, not just a component
81                            if (entirePackageChanged(intent)) {
82                                mImpl.packageStateChanged(packageNameFromIntent(intent),
83                                        PACKAGE_CHANGED);
84                            }
85                            break;
86                        case Intent.ACTION_PACKAGE_ADDED:
87                            mImpl.packageStateChanged(packageNameFromIntent(intent),
88                                    (intent.getExtras().getBoolean(Intent.EXTRA_REPLACING)
89                                     ? PACKAGE_ADDED_REPLACED : PACKAGE_ADDED));
90                            break;
91                        case Intent.ACTION_USER_ADDED:
92                            int userId =
93                                intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL);
94                            mImpl.handleNewUser(userId);
95                            break;
96                    }
97                }
98        };
99        IntentFilter filter = new IntentFilter();
100        filter.addAction(Intent.ACTION_PACKAGE_ADDED);
101        filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
102        filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
103        filter.addDataScheme("package");
104        // Make sure we only receive intents for WebView packages from our config file.
105        for (WebViewProviderInfo provider : mImpl.getWebViewPackages()) {
106            filter.addDataSchemeSpecificPart(provider.packageName, PatternMatcher.PATTERN_LITERAL);
107        }
108        getContext().registerReceiver(mWebViewUpdatedReceiver, filter);
109
110        IntentFilter userAddedFilter = new IntentFilter();
111        userAddedFilter.addAction(Intent.ACTION_USER_ADDED);
112        getContext().registerReceiver(mWebViewUpdatedReceiver, userAddedFilter);
113
114        publishBinderService("webviewupdate", new BinderService(), true /*allowIsolated*/);
115    }
116
117    public void prepareWebViewInSystemServer() {
118        mImpl.prepareWebViewInSystemServer();
119    }
120
121    private static String packageNameFromIntent(Intent intent) {
122        return intent.getDataString().substring("package:".length());
123    }
124
125    /**
126     * Returns whether the entire package from an ACTION_PACKAGE_CHANGED intent was changed (rather
127     * than just one of its components).
128     * @hide
129     */
130    public static boolean entirePackageChanged(Intent intent) {
131        String[] componentList =
132            intent.getStringArrayExtra(Intent.EXTRA_CHANGED_COMPONENT_NAME_LIST);
133        return Arrays.asList(componentList).contains(
134                intent.getDataString().substring("package:".length()));
135    }
136
137    private class BinderService extends IWebViewUpdateService.Stub {
138
139        @Override
140        public void onShellCommand(FileDescriptor in, FileDescriptor out,
141                FileDescriptor err, String[] args, ResultReceiver resultReceiver) {
142            (new WebViewUpdateServiceShellCommand(this)).exec(
143                    this, in, out, err, args, resultReceiver);
144        }
145
146
147        /**
148         * The shared relro process calls this to notify us that it's done trying to create a relro
149         * file. This method gets called even if the relro creation has failed or the process
150         * crashed.
151         */
152        @Override // Binder call
153        public void notifyRelroCreationCompleted() {
154            // Verify that the caller is either the shared relro process (nominal case) or the
155            // system server (only in the case the relro process crashes and we get here via the
156            // crashHandler).
157            if (Binder.getCallingUid() != Process.SHARED_RELRO_UID &&
158                    Binder.getCallingUid() != Process.SYSTEM_UID) {
159                return;
160            }
161
162            long callingId = Binder.clearCallingIdentity();
163            try {
164                WebViewUpdateService.this.mImpl.notifyRelroCreationCompleted();
165            } finally {
166                Binder.restoreCallingIdentity(callingId);
167            }
168        }
169
170        /**
171         * WebViewFactory calls this to block WebView loading until the relro file is created.
172         * Returns the WebView provider for which we create relro files.
173         */
174        @Override // Binder call
175        public WebViewProviderResponse waitForAndGetProvider() {
176            // The WebViewUpdateService depends on the prepareWebViewInSystemServer call, which
177            // happens later (during the PHASE_ACTIVITY_MANAGER_READY) in SystemServer.java. If
178            // another service there tries to bring up a WebView in the between, the wait below
179            // would deadlock without the check below.
180            if (Binder.getCallingPid() == Process.myPid()) {
181                throw new IllegalStateException("Cannot create a WebView from the SystemServer");
182            }
183
184            return WebViewUpdateService.this.mImpl.waitForAndGetProvider();
185        }
186
187        /**
188         * This is called from DeveloperSettings when the user changes WebView provider.
189         */
190        @Override // Binder call
191        public String changeProviderAndSetting(String newProvider) {
192            if (getContext().checkCallingPermission(
193                        android.Manifest.permission.WRITE_SECURE_SETTINGS)
194                    != PackageManager.PERMISSION_GRANTED) {
195                String msg = "Permission Denial: changeProviderAndSetting() from pid="
196                        + Binder.getCallingPid()
197                        + ", uid=" + Binder.getCallingUid()
198                        + " requires " + android.Manifest.permission.WRITE_SECURE_SETTINGS;
199                Slog.w(TAG, msg);
200                throw new SecurityException(msg);
201            }
202
203            long callingId = Binder.clearCallingIdentity();
204            try {
205                return WebViewUpdateService.this.mImpl.changeProviderAndSetting(
206                        newProvider);
207            } finally {
208                Binder.restoreCallingIdentity(callingId);
209            }
210        }
211
212        @Override // Binder call
213        public WebViewProviderInfo[] getValidWebViewPackages() {
214            return WebViewUpdateService.this.mImpl.getValidWebViewPackages();
215        }
216
217        @Override // Binder call
218        public WebViewProviderInfo[] getAllWebViewPackages() {
219            return WebViewUpdateService.this.mImpl.getWebViewPackages();
220        }
221
222        @Override // Binder call
223        public String getCurrentWebViewPackageName() {
224            return WebViewUpdateService.this.mImpl.getCurrentWebViewPackageName();
225        }
226
227        @Override // Binder call
228        public boolean isFallbackPackage(String packageName) {
229            return WebViewUpdateService.this.mImpl.isFallbackPackage(packageName);
230        }
231
232        @Override // Binder call
233        public void enableFallbackLogic(boolean enable) {
234            if (getContext().checkCallingPermission(
235                        android.Manifest.permission.WRITE_SECURE_SETTINGS)
236                    != PackageManager.PERMISSION_GRANTED) {
237                String msg = "Permission Denial: enableFallbackLogic() from pid="
238                        + Binder.getCallingPid()
239                        + ", uid=" + Binder.getCallingUid()
240                        + " requires " + android.Manifest.permission.WRITE_SECURE_SETTINGS;
241                Slog.w(TAG, msg);
242                throw new SecurityException(msg);
243            }
244
245            WebViewUpdateService.this.mImpl.enableFallbackLogic(enable);
246        }
247    }
248}
249