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.chrome.browser.externalnav; | |
6 | |
7 import android.content.Intent; | |
8 import android.net.Uri; | |
9 | |
10 import java.security.SecureRandom; | |
11 import java.util.Arrays; | |
12 | |
13 /** | |
14 * This class hold a token for the most recent launched external intent that has | |
15 * user gesture. If the external intent resolves to chrome itself, the user | |
16 * gesture will be applied to the new url request. Since there could be at most | |
17 * one intent chooser at a time, this class only stores the most recent intent | |
18 * that has user gesture. A previously launched inten with user gesture will be | |
Maria
2015/08/05 01:41:44
inten->intent and remove "be"
qinmin
2015/08/05 20:22:26
Done.
| |
19 * have its token invalidated if a new one comes. | |
20 */ | |
21 public class IntentWithGesturesHandler { | |
22 /** | |
23 * Extra to record whether the intent is launched by user gesture. | |
24 */ | |
25 public static final String EXTRA_USER_GESTURE_TOKEN = | |
26 "org.chromium.chrome.browser.user_gesture_token"; | |
27 | |
28 private static final Object INSTANCE_LOCK = new Object(); | |
29 private static IntentWithGesturesHandler sIntentWithGesturesHandler; | |
30 private SecureRandom mSecureRandom; | |
31 private byte[] mIntentToken; | |
32 private Uri mUri; | |
33 | |
34 /** | |
35 * Get the singleton instance of this object. | |
36 */ | |
37 public static IntentWithGesturesHandler getInstance() { | |
38 synchronized (INSTANCE_LOCK) { | |
39 if (sIntentWithGesturesHandler == null) { | |
40 sIntentWithGesturesHandler = new IntentWithGesturesHandler(); | |
41 } | |
42 } | |
43 return sIntentWithGesturesHandler; | |
44 } | |
45 | |
46 private IntentWithGesturesHandler() { | |
47 mSecureRandom = new SecureRandom(); | |
48 } | |
49 | |
50 /** | |
51 * Generate a new token for the intent that has user gesture. This will | |
52 * invalidate the token on the previously launched intent with user gesture. | |
53 * | |
54 * @param intent Intent with user gesture. | |
55 */ | |
56 public void onNewIntentWithGesture(Intent intent) { | |
57 mIntentToken = new byte[32]; | |
58 mSecureRandom.nextBytes(mIntentToken); | |
59 intent.putExtra(EXTRA_USER_GESTURE_TOKEN, mIntentToken); | |
60 mUri = intent.getData(); | |
Maria
2015/08/05 01:41:44
I would use IntentHandler.getUrlFromIntent() here
qinmin
2015/08/05 20:22:26
Done.
| |
61 } | |
62 | |
63 /** | |
64 * Get the user gesture from the intent and clear the stored token. | |
65 * | |
66 * @param intent Intent that is used to launch chrome. | |
67 * @return true if the intent has user gesture, or false otherwise. | |
68 */ | |
69 public boolean getUserGestureAndClear(Intent intent) { | |
70 if (mIntentToken == null || mUri == null) return false; | |
71 byte[] bytes = intent.getByteArrayExtra(EXTRA_USER_GESTURE_TOKEN); | |
Maria
2015/08/05 01:41:44
Add an accessor to IntentUtils.safeGet... collecti
qinmin
2015/08/05 20:22:26
Done.
| |
72 boolean ret = (bytes != null) && Arrays.equals(bytes, mIntentToken) | |
73 && mUri.equals(intent.getData()); | |
74 clear(); | |
75 return ret; | |
76 } | |
77 | |
78 /** | |
79 * Clear the gesture token. | |
80 */ | |
81 public void clear() { | |
82 mIntentToken = null; | |
83 mUri = null; | |
84 } | |
85 } | |
OLD | NEW |