OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 com.android.webview.chromium; |
| 6 |
| 7 import android.net.Uri; |
| 8 import android.webkit.TokenBindingService; |
| 9 import android.webkit.TokenBindingService.TokenBindingKey; |
| 10 import android.webkit.ValueCallback; |
| 11 |
| 12 import org.chromium.android_webview.AwTokenBindingManager; |
| 13 |
| 14 import java.security.KeyPair; |
| 15 |
| 16 /** |
| 17 * Chromium implementation of TokenBindingManager. The API requires |
| 18 * all access to TokenBindingManager to be on UI thread, so we start the |
| 19 * chromium engines with this assumption. |
| 20 */ |
| 21 public class TokenBindingManagerAdapter extends TokenBindingService { |
| 22 |
| 23 private AwTokenBindingManager mTokenBindingManager = new AwTokenBindingManag
er(); |
| 24 private WebViewChromiumFactoryProvider mProvider; |
| 25 private boolean mEnabled; |
| 26 |
| 27 TokenBindingManagerAdapter(WebViewChromiumFactoryProvider provider) { |
| 28 mProvider = provider; |
| 29 } |
| 30 |
| 31 @Override |
| 32 public void enableTokenBinding() { |
| 33 // We cannot start the chromium engine yet, since doing so would |
| 34 // initialize the UrlRequestContextGetter and then it would be too |
| 35 // late to enable token binding. |
| 36 if (mProvider.hasStarted()) { |
| 37 throw new IllegalStateException( |
| 38 "Token binding cannot be enabled after webview creation"); |
| 39 } |
| 40 mEnabled = true; |
| 41 mTokenBindingManager.enableTokenBinding(); |
| 42 } |
| 43 |
| 44 @Override |
| 45 public void getKey(Uri origin, |
| 46 String[] algorithm, |
| 47 final ValueCallback<TokenBindingKey> callback) { |
| 48 startChromiumEngine(); |
| 49 if (algorithm != null && algorithm.length == 0) { |
| 50 throw new IllegalArgumentException("algorithms cannot be empty"); |
| 51 } |
| 52 if (algorithm != null) { |
| 53 boolean found = false; |
| 54 for (String alg:algorithm) { |
| 55 if (alg.equals(TokenBindingService.KEY_ALGORITHM_ECDSAP256)) { |
| 56 found = true; break; |
| 57 } |
| 58 } |
| 59 if (!found) { |
| 60 throw new IllegalArgumentException("no supported algorithm found
"); |
| 61 } |
| 62 } |
| 63 // Only return the KeyPair for now. We retrieve the key from Channel Id |
| 64 // store which does not provide a way to set/retrieve the Token |
| 65 // Binding algorithms yet. |
| 66 ValueCallback<KeyPair> newCallback = new ValueCallback<KeyPair>() { |
| 67 @Override |
| 68 public void onReceiveValue(final KeyPair value) { |
| 69 TokenBindingKey key = new TokenBindingKey() { |
| 70 @Override |
| 71 public KeyPair getKeyPair() { |
| 72 return value; |
| 73 } |
| 74 @Override |
| 75 public String getAlgorithm() { |
| 76 return TokenBindingService.KEY_ALGORITHM_ECDSAP256; |
| 77 } |
| 78 }; |
| 79 callback.onReceiveValue(key); |
| 80 } |
| 81 }; |
| 82 mTokenBindingManager.getKey(origin, null, newCallback); |
| 83 } |
| 84 |
| 85 @Override |
| 86 public void deleteKey(Uri origin, |
| 87 ValueCallback<Boolean> callback) { |
| 88 startChromiumEngine(); |
| 89 mTokenBindingManager.deleteKey(origin, callback); |
| 90 } |
| 91 |
| 92 @Override |
| 93 public void deleteAllKeys(ValueCallback<Boolean> callback) { |
| 94 startChromiumEngine(); |
| 95 mTokenBindingManager.deleteAllKeys(callback); |
| 96 } |
| 97 |
| 98 private void startChromiumEngine() { |
| 99 if (!mEnabled) { |
| 100 throw new IllegalStateException("Token binding is not enabled"); |
| 101 } |
| 102 // Make sure chromium engine is running. |
| 103 mProvider.startYourEngines(false); |
| 104 } |
| 105 } |
OLD | NEW |