blob: 06fa1a76b7a726978b097c9f4753efaf28d5f881 [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
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +010030import com.android.server.SystemService;
31
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +010032/**
33 * Private service to wait for the updatable WebView to be ready for use.
34 * @hide
35 */
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +010036public class WebViewUpdateService extends SystemService {
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +010037
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) {
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +010046 super(context);
47 }
48
49 @Override
50 public void onStart() {
Ben Murdochdc00a842014-07-17 14:55:00 +010051 mWebViewUpdatedReceiver = new BroadcastReceiver() {
52 @Override
53 public void onReceive(Context context, Intent intent) {
54 String webviewPackage = "package:" + WebViewFactory.getWebViewPackageName();
55 if (webviewPackage.equals(intent.getDataString())) {
56 onWebViewUpdateInstalled();
57 }
58 }
59 };
60 IntentFilter filter = new IntentFilter();
61 filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
62 filter.addDataScheme("package");
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +010063 getContext().registerReceiver(mWebViewUpdatedReceiver, filter);
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +010064
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +010065 publishBinderService("webviewupdate", new BinderService());
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +010066 }
Ben Murdochdc00a842014-07-17 14:55:00 +010067
68 private void onWebViewUpdateInstalled() {
69 Log.d(TAG, "WebView Package updated!");
70
71 synchronized (this) {
72 mRelroReady32Bit = false;
73 mRelroReady64Bit = false;
74 }
75 WebViewFactory.prepareWebViewInSystemServer();
76 }
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +010077
78 private class BinderService extends IWebViewUpdateService.Stub {
79
80 /**
81 * The shared relro process calls this to notify us that it's done trying to create a relro
82 * file. This method gets called even if the relro creation has failed or the process
83 * crashed.
84 */
85 @Override // Binder call
86 public void notifyRelroCreationCompleted(boolean is64Bit, boolean success) {
87 // Verify that the caller is either the shared relro process (nominal case) or the
88 // system server (only in the case the relro process crashes and we get here via the
89 // crashHandler).
90 if (Binder.getCallingUid() != Process.SHARED_RELRO_UID &&
91 Binder.getCallingUid() != Process.SYSTEM_UID) {
92 return;
93 }
94
95 synchronized (WebViewUpdateService.this) {
96 if (is64Bit) {
97 mRelroReady64Bit = true;
98 } else {
99 mRelroReady32Bit = true;
100 }
101 WebViewUpdateService.this.notifyAll();
102 }
103 }
104
105 /**
106 * WebViewFactory calls this to block WebView loading until the relro file is created.
107 */
108 @Override // Binder call
109 public void waitForRelroCreationCompleted(boolean is64Bit) {
110 if (Binder.getCallingUid() == Process.SYSTEM_UID) {
111 Slog.wtf(TAG, "Trying to load WebView from the SystemServer",
112 new IllegalStateException());
113 }
114
115 synchronized (WebViewUpdateService.this) {
116 if (is64Bit) {
117 while (!mRelroReady64Bit) {
118 try {
119 WebViewUpdateService.this.wait();
120 } catch (InterruptedException e) {}
121 }
122 } else {
123 while (!mRelroReady32Bit) {
124 try {
125 WebViewUpdateService.this.wait();
126 } catch (InterruptedException e) {}
127 }
128 }
129 }
130 }
131
132 }
133
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100134}