blob: 06dc362b545f7eb0e1d86cf2a940da296dd5b37b [file] [log] [blame]
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +01001/*
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
Ben Murdochdc00a842014-07-17 14:55:00 +010019import android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22import android.content.IntentFilter;
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +010023import android.os.Binder;
24import android.os.Process;
25import android.util.Log;
Primiano Tucci810c0522014-07-25 18:03:16 +010026import android.util.Slog;
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +010027import android.webkit.IWebViewUpdateService;
Ben Murdochdc00a842014-07-17 14:55:00 +010028import android.webkit.WebViewFactory;
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +010029
30/**
31 * Private service to wait for the updatable WebView to be ready for use.
32 * @hide
33 */
Primiano Tucci810c0522014-07-25 18:03:16 +010034// TODO This should be implemented using the new pattern for system services.
35// See comments in CL 509840.
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +010036public class WebViewUpdateService extends IWebViewUpdateService.Stub {
37
38 private static final String TAG = "WebViewUpdateService";
39
40 private boolean mRelroReady32Bit = false;
41 private boolean mRelroReady64Bit = false;
42
Ben Murdochdc00a842014-07-17 14:55:00 +010043 private BroadcastReceiver mWebViewUpdatedReceiver;
44
45 public WebViewUpdateService(Context context) {
46 mWebViewUpdatedReceiver = new BroadcastReceiver() {
47 @Override
48 public void onReceive(Context context, Intent intent) {
49 String webviewPackage = "package:" + WebViewFactory.getWebViewPackageName();
50 if (webviewPackage.equals(intent.getDataString())) {
51 onWebViewUpdateInstalled();
52 }
53 }
54 };
55 IntentFilter filter = new IntentFilter();
56 filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
57 filter.addDataScheme("package");
58 context.registerReceiver(mWebViewUpdatedReceiver, filter);
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +010059 }
60
61 /**
62 * The shared relro process calls this to notify us that it's done trying to create a relro
Primiano Tucci810c0522014-07-25 18:03:16 +010063 * file. This method gets called even if the relro creation has failed or the process crashed.
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +010064 */
Primiano Tucci810c0522014-07-25 18:03:16 +010065 @Override // Binder call
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +010066 public void notifyRelroCreationCompleted(boolean is64Bit, boolean success) {
Primiano Tucci810c0522014-07-25 18:03:16 +010067 // Verify that the caller is either the shared relro process (nominal case) or the system
68 // server (only in the case the relro process crashes and we get here via the crashHandler).
69 if (Binder.getCallingUid() != Process.SHARED_RELRO_UID &&
70 Binder.getCallingUid() != Process.SYSTEM_UID) {
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +010071 return;
72 }
73
74 synchronized (this) {
75 if (is64Bit) {
76 mRelroReady64Bit = true;
77 } else {
78 mRelroReady32Bit = true;
79 }
80 this.notifyAll();
81 }
82 }
83
84 /**
85 * WebViewFactory calls this to block WebView loading until the relro file is created.
86 */
Primiano Tucci810c0522014-07-25 18:03:16 +010087 @Override // Binder call
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +010088 public void waitForRelroCreationCompleted(boolean is64Bit) {
Primiano Tucci810c0522014-07-25 18:03:16 +010089 if (Binder.getCallingUid() == Process.SYSTEM_UID) {
90 Slog.wtf(TAG, "Trying to load WebView from the SystemServer",
91 new IllegalStateException());
92 }
93
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +010094 synchronized (this) {
95 if (is64Bit) {
96 while (!mRelroReady64Bit) {
97 try {
98 this.wait();
99 } catch (InterruptedException e) {}
100 }
101 } else {
102 while (!mRelroReady32Bit) {
103 try {
104 this.wait();
105 } catch (InterruptedException e) {}
106 }
107 }
108 }
109 }
Ben Murdochdc00a842014-07-17 14:55:00 +0100110
111 private void onWebViewUpdateInstalled() {
112 Log.d(TAG, "WebView Package updated!");
113
114 synchronized (this) {
115 mRelroReady32Bit = false;
116 mRelroReady64Bit = false;
117 }
118 WebViewFactory.prepareWebViewInSystemServer();
119 }
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100120}