WebViewUpdateService.java revision dc00a84af15ff3594a6dfa512be21095bf9fee82
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.os.Binder;
24import android.os.Process;
25import android.util.Log;
26import android.webkit.IWebViewUpdateService;
27import android.webkit.WebViewFactory;
28
29/**
30 * Private service to wait for the updatable WebView to be ready for use.
31 * @hide
32 */
33public class WebViewUpdateService extends IWebViewUpdateService.Stub {
34
35    private static final String TAG = "WebViewUpdateService";
36
37    private boolean mRelroReady32Bit = false;
38    private boolean mRelroReady64Bit = false;
39
40    private BroadcastReceiver mWebViewUpdatedReceiver;
41
42    public WebViewUpdateService(Context context) {
43        mWebViewUpdatedReceiver = new BroadcastReceiver() {
44                @Override
45                public void onReceive(Context context, Intent intent) {
46                    String webviewPackage = "package:" + WebViewFactory.getWebViewPackageName();
47                    if (webviewPackage.equals(intent.getDataString())) {
48                        onWebViewUpdateInstalled();
49                    }
50                }
51        };
52        IntentFilter filter = new IntentFilter();
53        filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
54        filter.addDataScheme("package");
55        context.registerReceiver(mWebViewUpdatedReceiver, filter);
56    }
57
58    /**
59     * The shared relro process calls this to notify us that it's done trying to create a relro
60     * file.
61     */
62    public void notifyRelroCreationCompleted(boolean is64Bit, boolean success) {
63        // Verify that the caller is the shared relro process.
64        if (Binder.getCallingUid() != Process.SHARED_RELRO_UID) {
65            return;
66        }
67
68        synchronized (this) {
69            if (is64Bit) {
70                mRelroReady64Bit = true;
71            } else {
72                mRelroReady32Bit = true;
73            }
74            this.notifyAll();
75        }
76    }
77
78    /**
79     * WebViewFactory calls this to block WebView loading until the relro file is created.
80     */
81    public void waitForRelroCreationCompleted(boolean is64Bit) {
82        synchronized (this) {
83            if (is64Bit) {
84                while (!mRelroReady64Bit) {
85                    try {
86                        this.wait();
87                    } catch (InterruptedException e) {}
88                }
89            } else {
90                while (!mRelroReady32Bit) {
91                    try {
92                        this.wait();
93                    } catch (InterruptedException e) {}
94                }
95            }
96        }
97    }
98
99    private void onWebViewUpdateInstalled() {
100        Log.d(TAG, "WebView Package updated!");
101
102        synchronized (this) {
103            mRelroReady32Bit = false;
104            mRelroReady64Bit = false;
105        }
106        WebViewFactory.prepareWebViewInSystemServer();
107    }
108}
109