blob: ac79b36f70053d3a1f010290f5d165f712bed98b [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;
Primiano Tucci810c0522014-07-25 18:03:16 +010025import android.util.Slog;
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +010026import android.webkit.IWebViewUpdateService;
Ben Murdochdc00a842014-07-17 14:55:00 +010027import android.webkit.WebViewFactory;
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +010028
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +010029import com.android.server.SystemService;
30
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +010031/**
32 * Private service to wait for the updatable WebView to be ready for use.
33 * @hide
34 */
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +010035public class WebViewUpdateService extends SystemService {
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +010036
37 private static final String TAG = "WebViewUpdateService";
Primiano Tuccie76e81a2014-07-29 16:38:33 +010038 private static final int WAIT_TIMEOUT_MS = 5000; // Same as KEY_DISPATCHING_TIMEOUT.
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +010039
40 private boolean mRelroReady32Bit = false;
41 private boolean mRelroReady64Bit = false;
42
Gustav Sennton6ce92c92015-10-23 11:10:39 +010043 private String oldWebViewPackageName = null;
44
Ben Murdochdc00a842014-07-17 14:55:00 +010045 private BroadcastReceiver mWebViewUpdatedReceiver;
46
47 public WebViewUpdateService(Context context) {
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +010048 super(context);
49 }
50
51 @Override
52 public void onStart() {
Ben Murdochdc00a842014-07-17 14:55:00 +010053 mWebViewUpdatedReceiver = new BroadcastReceiver() {
54 @Override
55 public void onReceive(Context context, Intent intent) {
Gustav Sennton6ce92c92015-10-23 11:10:39 +010056
57 for (String packageName : WebViewFactory.getWebViewPackageNames()) {
58 String webviewPackage = "package:" + packageName;
59
60 if (webviewPackage.equals(intent.getDataString())) {
61 String usedPackageName =
62 WebViewFactory.findPreferredWebViewPackage().packageName;
63 // Only trigger update actions if the updated package is the one that
64 // will be used, or the one that was in use before the update.
65 if (packageName.equals(usedPackageName) ||
66 packageName.equals(oldWebViewPackageName)) {
67 onWebViewUpdateInstalled();
68 oldWebViewPackageName = usedPackageName;
69 }
70 return;
71 }
Ben Murdochdc00a842014-07-17 14:55:00 +010072 }
73 }
74 };
75 IntentFilter filter = new IntentFilter();
76 filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
77 filter.addDataScheme("package");
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +010078 getContext().registerReceiver(mWebViewUpdatedReceiver, filter);
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +010079
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +010080 publishBinderService("webviewupdate", new BinderService());
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +010081 }
Ben Murdochdc00a842014-07-17 14:55:00 +010082
83 private void onWebViewUpdateInstalled() {
Primiano Tuccie76e81a2014-07-29 16:38:33 +010084 Slog.d(TAG, "WebView Package updated!");
Ben Murdochdc00a842014-07-17 14:55:00 +010085
86 synchronized (this) {
87 mRelroReady32Bit = false;
88 mRelroReady64Bit = false;
89 }
Ben Murdoch5ced5022014-07-28 15:57:00 +010090 WebViewFactory.onWebViewUpdateInstalled();
Ben Murdochdc00a842014-07-17 14:55:00 +010091 }
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +010092
93 private class BinderService extends IWebViewUpdateService.Stub {
94
95 /**
96 * The shared relro process calls this to notify us that it's done trying to create a relro
97 * file. This method gets called even if the relro creation has failed or the process
98 * crashed.
99 */
100 @Override // Binder call
101 public void notifyRelroCreationCompleted(boolean is64Bit, boolean success) {
102 // Verify that the caller is either the shared relro process (nominal case) or the
103 // system server (only in the case the relro process crashes and we get here via the
104 // crashHandler).
105 if (Binder.getCallingUid() != Process.SHARED_RELRO_UID &&
106 Binder.getCallingUid() != Process.SYSTEM_UID) {
107 return;
108 }
109
110 synchronized (WebViewUpdateService.this) {
111 if (is64Bit) {
112 mRelroReady64Bit = true;
113 } else {
114 mRelroReady32Bit = true;
115 }
116 WebViewUpdateService.this.notifyAll();
117 }
118 }
119
120 /**
121 * WebViewFactory calls this to block WebView loading until the relro file is created.
122 */
123 @Override // Binder call
124 public void waitForRelroCreationCompleted(boolean is64Bit) {
Primiano Tuccie76e81a2014-07-29 16:38:33 +0100125 // The WebViewUpdateService depends on the prepareWebViewInSystemServer call, which
126 // happens later (during the PHASE_ACTIVITY_MANAGER_READY) in SystemServer.java. If
127 // another service there tries to bring up a WebView in the between, the wait below
128 // would deadlock without the check below.
129 if (Binder.getCallingPid() == Process.myPid()) {
130 throw new IllegalStateException("Cannot create a WebView from the SystemServer");
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +0100131 }
132
Primiano Tuccie76e81a2014-07-29 16:38:33 +0100133 final long NS_PER_MS = 1000000;
134 final long timeoutTimeMs = System.nanoTime() / NS_PER_MS + WAIT_TIMEOUT_MS;
Torne (Richard Coles)6a0d43f2014-08-01 18:40:20 +0100135 boolean relroReady = (is64Bit ? mRelroReady64Bit : mRelroReady32Bit);
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +0100136 synchronized (WebViewUpdateService.this) {
Primiano Tuccie76e81a2014-07-29 16:38:33 +0100137 while (!relroReady) {
138 final long timeNowMs = System.nanoTime() / NS_PER_MS;
139 if (timeNowMs >= timeoutTimeMs) break;
140 try {
141 WebViewUpdateService.this.wait(timeoutTimeMs - timeNowMs);
142 } catch (InterruptedException e) {}
143 relroReady = (is64Bit ? mRelroReady64Bit : mRelroReady32Bit);
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +0100144 }
145 }
Primiano Tuccie76e81a2014-07-29 16:38:33 +0100146 if (!relroReady) Slog.w(TAG, "creating relro file timed out");
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +0100147 }
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +0100148 }
149
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100150}