• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 org.chromium.base;
6 
7 import android.os.Parcel;
8 import android.os.Parcelable;
9 
10 import org.chromium.base.annotations.CalledByNative;
11 
12 /**
13  * This class mirrors unguessable_token.h .  Since tokens are passed by value,
14  * we don't bother to maintain a native token.  This implements Parcelable so
15  * that it may be sent via binder.
16  *
17  * To get one of these from native, one must start with a
18  * base::UnguessableToken, then create a Java object from it.  See
19  * jni_unguessable_token.h for information.
20  */
21 public class UnguessableToken implements Parcelable {
22     private final long mHigh;
23     private final long mLow;
24 
UnguessableToken(long high, long low)25     private UnguessableToken(long high, long low) {
26         mHigh = high;
27         mLow = low;
28     }
29 
30     @CalledByNative
create(long high, long low)31     private static UnguessableToken create(long high, long low) {
32         return new UnguessableToken(high, low);
33     }
34 
35     @CalledByNative
getHighForSerialization()36     public long getHighForSerialization() {
37         return mHigh;
38     }
39 
40     @CalledByNative
getLowForSerialization()41     public long getLowForSerialization() {
42         return mLow;
43     }
44 
45     @Override
describeContents()46     public int describeContents() {
47         return 0;
48     }
49 
50     @Override
writeToParcel(Parcel dest, int flags)51     public void writeToParcel(Parcel dest, int flags) {
52         dest.writeLong(mHigh);
53         dest.writeLong(mLow);
54     }
55 
56     public static final Parcelable.Creator<UnguessableToken> CREATOR =
57             new Parcelable.Creator<UnguessableToken>() {
58                 @Override
59                 public UnguessableToken createFromParcel(Parcel source) {
60                     long high = source.readLong();
61                     long low = source.readLong();
62                     if (high == 0 || low == 0) {
63                         // Refuse to create an empty UnguessableToken.
64                         return null;
65                     }
66                     return new UnguessableToken(high, low);
67                 }
68 
69                 @Override
70                 public UnguessableToken[] newArray(int size) {
71                     return new UnguessableToken[size];
72                 }
73             };
74 
75     // To avoid unwieldy calls in JNI for tests, parcel and unparcel.
76     // TODO(liberato): It would be nice if we could include this only with a
77     // java driver that's linked only with unit tests, but i don't see a way
78     // to do that.
79     @CalledByNative
parcelAndUnparcelForTesting()80     private UnguessableToken parcelAndUnparcelForTesting() {
81         Parcel parcel = Parcel.obtain();
82         writeToParcel(parcel, 0);
83 
84         // Rewind the parcel and un-parcel.
85         parcel.setDataPosition(0);
86         UnguessableToken token = CREATOR.createFromParcel(parcel);
87         parcel.recycle();
88 
89         return token;
90     }
91 };
92