OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.webapk.lib.runtime_library; | |
6 | |
7 import android.app.Notification; | |
8 import android.app.NotificationManager; | |
9 import android.app.PendingIntent; | |
10 import android.content.Context; | |
11 import android.content.Intent; | |
12 import android.content.pm.PackageManager; | |
13 import android.os.Binder; | |
14 import android.os.Bundle; | |
15 import android.os.Parcel; | |
16 import android.os.RemoteException; | |
17 import android.util.Log; | |
18 | |
19 import java.util.Arrays; | |
20 import java.util.HashSet; | |
21 import java.util.Set; | |
22 | |
23 /** | |
24 * Implements services offered by the minted APK to chrome. | |
25 */ | |
26 public class WebApkServiceImpl extends IWebApkApi.Stub { | |
27 public static final String KEY_APP_ICON_ID = "app_icon_id"; | |
28 public static final String KEY_EXPECTED_HOST_BROWSER = "expected_host_browse
r"; | |
29 private static final String TAG = "WebApkServiceImpl"; | |
30 | |
31 private final Context mContext; | |
32 // TODO(hanxi): Removes when org.chromium.webapk.R.java is in the web_apk.de
x. | |
33 private final int mAppIcon; | |
34 private final String mExpectedHostPackage; | |
35 private final Set<Integer> mAuthorizedUids = new HashSet<>(); | |
36 | |
37 public WebApkServiceImpl(Context context, Bundle bundle) { | |
38 mContext = context; | |
39 mAppIcon = bundle.getInt(KEY_APP_ICON_ID); | |
40 mExpectedHostPackage = bundle.getString(KEY_EXPECTED_HOST_BROWSER); | |
41 } | |
42 | |
43 boolean checkHasAccess(int uid, PackageManager packageManager) { | |
44 String[] callingPackageNames = packageManager.getPackagesForUid(uid); | |
45 Log.d(TAG, "Verifying bind request from: " + Arrays.toString(callingPack
ageNames)); | |
46 if (callingPackageNames == null) { | |
47 return false; | |
48 } | |
49 | |
50 for (String packageName : callingPackageNames) { | |
51 if (packageName.equals(mExpectedHostPackage)) { | |
52 return true; | |
53 } | |
54 } | |
55 | |
56 Log.e(TAG, "Unauthorized request from uid: " + uid); | |
57 return false; | |
58 } | |
59 | |
60 @Override | |
61 public boolean onTransact(int arg0, Parcel arg1, Parcel arg2, int arg3) thro
ws RemoteException { | |
62 int callingUid = Binder.getCallingUid(); | |
63 if (mExpectedHostPackage != null && !mAuthorizedUids.contains(Binder.get
CallingUid())) { | |
64 if (checkHasAccess(callingUid, mContext.getPackageManager())) { | |
65 mAuthorizedUids.add(callingUid); | |
66 } else { | |
67 throw new RemoteException("Unauthorized caller " + callingUid | |
68 + " does not match expected host=" + mExpectedHostPackag
e); | |
69 } | |
70 } | |
71 return super.onTransact(arg0, arg1, arg2, arg3); | |
72 } | |
73 | |
74 @Override | |
75 public PendingIntent getBroadcastPendingIntent(int requestCode, Intent inten
t, int flags) { | |
76 return PendingIntent.getBroadcast( | |
77 mContext.getApplicationContext(), requestCode, intent, flags); | |
78 } | |
79 | |
80 @Override | |
81 public int getSmallIconId() { | |
82 return mAppIcon; | |
83 } | |
84 | |
85 @Override | |
86 public void displayNotification(String platformTag, int platformID, Notifica
tion notification) { | |
87 getNotificationManager().notify(platformTag, platformID, notification); | |
88 } | |
89 | |
90 @Override | |
91 public void closeNotification(String platformTag, int platformID) { | |
92 getNotificationManager().cancel(platformTag, platformID); | |
93 } | |
94 | |
95 private NotificationManager getNotificationManager() { | |
96 return (NotificationManager) mContext.getSystemService(Context.NOTIFICAT
ION_SERVICE); | |
97 } | |
98 } | |
OLD | NEW |