blob: 97713fc2407aaa91e28b18fe177361db84b7bc1c [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
Gustav Sennton3098cf22015-11-10 03:33:09 +000057 // When a package is replaced we will receive two intents, one representing the
58 // removal of the old package and one representing the addition of the new
59 // package. We here ignore the intent representing the removed package to make
60 // sure we don't change WebView provider twice.
61 if (intent.getAction().equals(Intent.ACTION_PACKAGE_REMOVED)
62 && intent.getExtras().getBoolean(Intent.EXTRA_REPLACING)) {
63 return;
64 }
65
Gustav Sennton6ce92c92015-10-23 11:10:39 +010066 for (String packageName : WebViewFactory.getWebViewPackageNames()) {
67 String webviewPackage = "package:" + packageName;
68
69 if (webviewPackage.equals(intent.getDataString())) {
70 String usedPackageName =
71 WebViewFactory.findPreferredWebViewPackage().packageName;
72 // Only trigger update actions if the updated package is the one that
73 // will be used, or the one that was in use before the update.
74 if (packageName.equals(usedPackageName) ||
75 packageName.equals(oldWebViewPackageName)) {
76 onWebViewUpdateInstalled();
77 oldWebViewPackageName = usedPackageName;
78 }
79 return;
80 }
Ben Murdochdc00a842014-07-17 14:55:00 +010081 }
82 }
83 };
84 IntentFilter filter = new IntentFilter();
Gustav Sennton3098cf22015-11-10 03:33:09 +000085 filter.addAction(Intent.ACTION_PACKAGE_ADDED);
86 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
Ben Murdochdc00a842014-07-17 14:55:00 +010087 filter.addDataScheme("package");
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +010088 getContext().registerReceiver(mWebViewUpdatedReceiver, filter);
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +010089
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +010090 publishBinderService("webviewupdate", new BinderService());
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +010091 }
Ben Murdochdc00a842014-07-17 14:55:00 +010092
93 private void onWebViewUpdateInstalled() {
Primiano Tuccie76e81a2014-07-29 16:38:33 +010094 Slog.d(TAG, "WebView Package updated!");
Ben Murdochdc00a842014-07-17 14:55:00 +010095
96 synchronized (this) {
97 mRelroReady32Bit = false;
98 mRelroReady64Bit = false;
99 }
Ben Murdoch5ced5022014-07-28 15:57:00 +0100100 WebViewFactory.onWebViewUpdateInstalled();
Ben Murdochdc00a842014-07-17 14:55:00 +0100101 }
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +0100102
103 private class BinderService extends IWebViewUpdateService.Stub {
104
105 /**
106 * The shared relro process calls this to notify us that it's done trying to create a relro
107 * file. This method gets called even if the relro creation has failed or the process
108 * crashed.
109 */
110 @Override // Binder call
111 public void notifyRelroCreationCompleted(boolean is64Bit, boolean success) {
112 // Verify that the caller is either the shared relro process (nominal case) or the
113 // system server (only in the case the relro process crashes and we get here via the
114 // crashHandler).
115 if (Binder.getCallingUid() != Process.SHARED_RELRO_UID &&
116 Binder.getCallingUid() != Process.SYSTEM_UID) {
117 return;
118 }
119
120 synchronized (WebViewUpdateService.this) {
121 if (is64Bit) {
122 mRelroReady64Bit = true;
123 } else {
124 mRelroReady32Bit = true;
125 }
126 WebViewUpdateService.this.notifyAll();
127 }
128 }
129
130 /**
131 * WebViewFactory calls this to block WebView loading until the relro file is created.
132 */
133 @Override // Binder call
134 public void waitForRelroCreationCompleted(boolean is64Bit) {
Primiano Tuccie76e81a2014-07-29 16:38:33 +0100135 // The WebViewUpdateService depends on the prepareWebViewInSystemServer call, which
136 // happens later (during the PHASE_ACTIVITY_MANAGER_READY) in SystemServer.java. If
137 // another service there tries to bring up a WebView in the between, the wait below
138 // would deadlock without the check below.
139 if (Binder.getCallingPid() == Process.myPid()) {
140 throw new IllegalStateException("Cannot create a WebView from the SystemServer");
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +0100141 }
142
Primiano Tuccie76e81a2014-07-29 16:38:33 +0100143 final long NS_PER_MS = 1000000;
144 final long timeoutTimeMs = System.nanoTime() / NS_PER_MS + WAIT_TIMEOUT_MS;
Torne (Richard Coles)6a0d43f2014-08-01 18:40:20 +0100145 boolean relroReady = (is64Bit ? mRelroReady64Bit : mRelroReady32Bit);
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +0100146 synchronized (WebViewUpdateService.this) {
Primiano Tuccie76e81a2014-07-29 16:38:33 +0100147 while (!relroReady) {
148 final long timeNowMs = System.nanoTime() / NS_PER_MS;
149 if (timeNowMs >= timeoutTimeMs) break;
150 try {
151 WebViewUpdateService.this.wait(timeoutTimeMs - timeNowMs);
152 } catch (InterruptedException e) {}
153 relroReady = (is64Bit ? mRelroReady64Bit : mRelroReady32Bit);
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +0100154 }
155 }
Primiano Tuccie76e81a2014-07-29 16:38:33 +0100156 if (!relroReady) Slog.w(TAG, "creating relro file timed out");
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +0100157 }
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +0100158 }
159
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100160}