OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.cookies; | 5 package org.chromium.chrome.browser.cookies; |
6 | 6 |
7 import java.io.DataInputStream; | 7 import java.io.DataInputStream; |
8 import java.io.DataOutputStream; | 8 import java.io.DataOutputStream; |
9 import java.io.IOException; | 9 import java.io.IOException; |
| 10 import java.util.ArrayList; |
| 11 import java.util.List; |
10 | 12 |
11 /** | 13 /** |
12 * Java representation of net/cookies/canonical_cookie.h. | 14 * Java representation of net/cookies/canonical_cookie.h. |
| 15 * |
| 16 * Also has static methods serialize Cookies. |
13 */ | 17 */ |
14 class CanonicalCookie { | 18 class CanonicalCookie { |
15 private final String mUrl; | 19 private final String mUrl; |
16 private final String mName; | 20 private final String mName; |
17 private final String mValue; | 21 private final String mValue; |
18 private final String mDomain; | 22 private final String mDomain; |
19 private final String mPath; | 23 private final String mPath; |
20 private final long mCreation; | 24 private final long mCreation; |
21 private final long mExpiration; | 25 private final long mExpiration; |
22 private final long mLastAccess; | 26 private final long mLastAccess; |
23 private final boolean mSecure; | 27 private final boolean mSecure; |
24 private final boolean mHttpOnly; | 28 private final boolean mHttpOnly; |
25 private final boolean mSameSite; | 29 private final int mSameSite; |
26 private final int mPriority; | 30 private final int mPriority; |
27 | 31 |
28 /** Constructs a CanonicalCookie */ | 32 /** Constructs a CanonicalCookie */ |
29 CanonicalCookie(String url, String name, String value, String domain, String
path, | 33 CanonicalCookie(String url, String name, String value, String domain, String
path, |
30 long creation, long expiration, long lastAccess, boolean secure, boo
lean httpOnly, | 34 long creation, long expiration, long lastAccess, boolean secure, boo
lean httpOnly, |
31 boolean sameSite, int priority) { | 35 int sameSite, int priority) { |
32 mUrl = url; | 36 mUrl = url; |
33 mName = name; | 37 mName = name; |
34 mValue = value; | 38 mValue = value; |
35 mDomain = domain; | 39 mDomain = domain; |
36 mPath = path; | 40 mPath = path; |
37 mCreation = creation; | 41 mCreation = creation; |
38 mExpiration = expiration; | 42 mExpiration = expiration; |
39 mLastAccess = lastAccess; | 43 mLastAccess = lastAccess; |
40 mSecure = secure; | 44 mSecure = secure; |
41 mHttpOnly = httpOnly; | 45 mHttpOnly = httpOnly; |
42 mSameSite = sameSite; | 46 mSameSite = sameSite; |
43 mPriority = priority; | 47 mPriority = priority; |
44 } | 48 } |
45 | 49 |
46 /** @return Priority of the cookie. */ | 50 /** @return Priority of the cookie. */ |
47 int getPriority() { | 51 int getPriority() { |
48 return mPriority; | 52 return mPriority; |
49 } | 53 } |
50 | 54 |
51 /** @return True if the cookie is HTTP only. */ | 55 /** @return True if the cookie is HTTP only. */ |
52 boolean isHttpOnly() { | 56 boolean isHttpOnly() { |
53 return mHttpOnly; | 57 return mHttpOnly; |
54 } | 58 } |
55 | 59 |
56 /** @return True if the cookie is Same-Site. */ | 60 /** @return SameSite enum */ |
57 boolean isSameSite() { | 61 int getSameSite() { |
58 return mSameSite; | 62 return mSameSite; |
59 } | 63 } |
60 | 64 |
61 /** @return True if the cookie is secure. */ | 65 /** @return True if the cookie is secure. */ |
62 boolean isSecure() { | 66 boolean isSecure() { |
63 return mSecure; | 67 return mSecure; |
64 } | 68 } |
65 | 69 |
66 /** @return Last accessed time. */ | 70 /** @return Last accessed time. */ |
67 long getLastAccessDate() { | 71 long getLastAccessDate() { |
(...skipping 28 matching lines...) Expand all Loading... |
96 /** @return Cookie domain. */ | 100 /** @return Cookie domain. */ |
97 String getDomain() { | 101 String getDomain() { |
98 return mDomain; | 102 return mDomain; |
99 } | 103 } |
100 | 104 |
101 /** @return Cookie value. */ | 105 /** @return Cookie value. */ |
102 String getValue() { | 106 String getValue() { |
103 return mValue; | 107 return mValue; |
104 } | 108 } |
105 | 109 |
106 /** | 110 // Note incognito state cannot persist across app installs since the encrypt
ion key is stored |
107 * Serializes for saving to disk. Does not close the stream. | 111 // in the activity state bundle. So the version here is more of a guard than
a real version |
108 * It is up to the caller to do so. | 112 // used for format migrations. |
109 * | 113 private static final int SERIALIZATION_VERSION = 20160426; |
110 * @param out Stream to write the cookie to. | 114 |
111 */ | 115 static void saveListToStream(DataOutputStream out, CanonicalCookie[] cookies
) |
112 void saveToStream(DataOutputStream out) throws IOException { | 116 throws IOException { |
| 117 if (out == null) { |
| 118 throw new IllegalArgumentException("out arg is null"); |
| 119 } |
| 120 if (cookies == null) { |
| 121 throw new IllegalArgumentException("cookies arg is null"); |
| 122 } |
| 123 for (CanonicalCookie cookie : cookies) { |
| 124 if (cookie == null) { |
| 125 throw new IllegalArgumentException("cookies arg contains null va
lue"); |
| 126 } |
| 127 } |
| 128 |
| 129 int length = cookies.length; |
| 130 out.writeInt(SERIALIZATION_VERSION); |
| 131 out.writeInt(length); |
| 132 for (int i = 0; i < length; ++i) { |
| 133 cookies[i].saveToStream(out); |
| 134 } |
| 135 } |
| 136 |
| 137 // Not private for tests. |
| 138 static class UnexpectedFormatException extends Exception { |
| 139 public UnexpectedFormatException(String message) { |
| 140 super(message); |
| 141 } |
| 142 } |
| 143 |
| 144 static List<CanonicalCookie> readListFromStream(DataInputStream in) |
| 145 throws IOException, UnexpectedFormatException { |
| 146 if (in == null) { |
| 147 throw new IllegalArgumentException("in arg is null"); |
| 148 } |
| 149 |
| 150 final int version = in.readInt(); |
| 151 if (version != SERIALIZATION_VERSION) { |
| 152 throw new UnexpectedFormatException("Unexpected version"); |
| 153 } |
| 154 final int length = in.readInt(); |
| 155 if (length < 0) { |
| 156 throw new UnexpectedFormatException("Negative length: " + length); |
| 157 } |
| 158 |
| 159 ArrayList<CanonicalCookie> cookies = new ArrayList<>(length); |
| 160 for (int i = 0; i < length; ++i) { |
| 161 cookies.add(createFromStream(in)); |
| 162 } |
| 163 return cookies; |
| 164 } |
| 165 |
| 166 private void saveToStream(DataOutputStream out) throws IOException { |
113 out.writeUTF(mUrl); | 167 out.writeUTF(mUrl); |
114 out.writeUTF(mName); | 168 out.writeUTF(mName); |
115 out.writeUTF(mValue); | 169 out.writeUTF(mValue); |
116 out.writeUTF(mDomain); | 170 out.writeUTF(mDomain); |
117 out.writeUTF(mPath); | 171 out.writeUTF(mPath); |
118 out.writeLong(mCreation); | 172 out.writeLong(mCreation); |
119 out.writeLong(mExpiration); | 173 out.writeLong(mExpiration); |
120 out.writeLong(mLastAccess); | 174 out.writeLong(mLastAccess); |
121 out.writeBoolean(mSecure); | 175 out.writeBoolean(mSecure); |
122 out.writeBoolean(mHttpOnly); | 176 out.writeBoolean(mHttpOnly); |
123 out.writeBoolean(mSameSite); | 177 out.writeInt(mSameSite); |
124 out.writeInt(mPriority); | 178 out.writeInt(mPriority); |
125 } | 179 } |
126 | 180 |
127 /** | 181 private static CanonicalCookie createFromStream(DataInputStream in) throws I
OException { |
128 * Constructs a cookie by deserializing a single entry from the | |
129 * input stream. | |
130 * | |
131 * @param in Stream to read a cookie entry from. | |
132 */ | |
133 static CanonicalCookie createFromStream(DataInputStream in) | |
134 throws IOException { | |
135 return new CanonicalCookie(in.readUTF(), in.readUTF(), in.readUTF(), in.
readUTF(), | 182 return new CanonicalCookie(in.readUTF(), in.readUTF(), in.readUTF(), in.
readUTF(), |
136 in.readUTF(), in.readLong(), in.readLong(), in.readLong(), in.re
adBoolean(), | 183 in.readUTF(), in.readLong(), in.readLong(), in.readLong(), in.re
adBoolean(), |
137 in.readBoolean(), in.readBoolean(), in.readInt()); | 184 in.readBoolean(), in.readInt(), in.readInt()); |
138 } | 185 } |
139 } | 186 } |
OLD | NEW |