Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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.net; | 5 package org.chromium.net; |
| 6 | 6 |
| 7 import android.util.Log; | 7 import android.util.Log; |
| 8 import android.util.Pair; | 8 import android.util.Pair; |
| 9 | 9 |
| 10 import org.chromium.base.CalledByNative; | 10 import org.chromium.base.CalledByNative; |
| 11 import org.chromium.base.JNINamespace; | 11 import org.chromium.base.JNINamespace; |
| 12 import org.chromium.base.annotations.SuppressFBWarnings; | |
| 12 | 13 |
| 13 import java.nio.ByteBuffer; | 14 import java.nio.ByteBuffer; |
| 14 import java.util.ArrayList; | 15 import java.util.ArrayList; |
| 15 import java.util.Collections; | 16 import java.util.Collections; |
| 16 import java.util.HashMap; | 17 import java.util.Comparator; |
| 17 import java.util.List; | 18 import java.util.List; |
| 18 import java.util.Map; | 19 import java.util.Map; |
| 20 import java.util.TreeMap; | |
| 19 import java.util.concurrent.Executor; | 21 import java.util.concurrent.Executor; |
| 20 | 22 |
| 21 /** | 23 /** |
| 22 * UrlRequest using Chromium HTTP stack implementation. Could be accessed from | 24 * UrlRequest using Chromium HTTP stack implementation. Could be accessed from |
| 23 * any thread on Executor. Cancel can be done from any thread. | 25 * any thread on Executor. Cancel can be done from any thread. |
| 24 * All @CallByNative methods are called on native network thread | 26 * All @CallByNative methods are called on native network thread |
| 25 * and post tasks with listener calls onto Executor. Upon return from listener | 27 * and post tasks with listener calls onto Executor. Upon return from listener |
| 26 * callback native request adapter is called on executive thread and posts | 28 * callback native request adapter is called on executive thread and posts |
| 27 * native tasks to native network thread. Because Cancel could be called from | 29 * native tasks to native network thread. Because Cancel could be called from |
| 28 * any thread it is protected by mUrlRequestAdapterLock. | 30 * any thread it is protected by mUrlRequestAdapterLock. |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 57 private final HeadersList mRequestHeaders = new HeadersList(); | 59 private final HeadersList mRequestHeaders = new HeadersList(); |
| 58 | 60 |
| 59 private NativeResponseInfo mResponseInfo; | 61 private NativeResponseInfo mResponseInfo; |
| 60 | 62 |
| 61 /* | 63 /* |
| 62 * Listener callback is repeatedly called when data is received, so it is | 64 * Listener callback is repeatedly called when data is received, so it is |
| 63 * cached as member variable. | 65 * cached as member variable. |
| 64 */ | 66 */ |
| 65 private OnDataReceivedRunnable mOnDataReceivedTask; | 67 private OnDataReceivedRunnable mOnDataReceivedTask; |
| 66 | 68 |
| 69 // TODO(xunjieli): Share it with CronetHttpURLConnection. | |
| 70 private static final Comparator<String> FIELD_NAME_COMPARATOR = | |
| 71 new Comparator<String>() { | |
| 72 @Override | |
| 73 @SuppressFBWarnings("ES_STRING_EQUALS") | |
|
mmenke
2015/01/16 19:43:13
Rather than suppress the warning, maybe just:
if
xunjieli
2015/01/16 20:01:12
Done. That's solved my problem. Didn't see it earl
| |
| 74 public int compare(String a, String b) { | |
| 75 if (a == b) { | |
| 76 return 0; | |
| 77 } else if (a == null) { | |
|
mmenke
2015/01/16 19:43:13
Actually...Can we even have NULL response header s
xunjieli
2015/01/16 20:01:12
The default implementation has a null key which co
mmenke
2015/01/16 20:03:59
My opinion on this (And others may disagree, thoug
| |
| 78 return -1; | |
| 79 } else if (b == null) { | |
| 80 return 1; | |
| 81 } else { | |
| 82 return String.CASE_INSENSITIVE_ORDER.compare(a, b); | |
| 83 } | |
| 84 } | |
| 85 }; | |
| 86 | |
| 67 static final class HeadersList extends ArrayList<Pair<String, String>> { | 87 static final class HeadersList extends ArrayList<Pair<String, String>> { |
| 68 } | 88 } |
| 69 | 89 |
| 70 final class OnDataReceivedRunnable implements Runnable { | 90 final class OnDataReceivedRunnable implements Runnable { |
| 71 ByteBuffer mByteBuffer; | 91 ByteBuffer mByteBuffer; |
| 72 | 92 |
| 73 public void run() { | 93 public void run() { |
| 74 if (isCanceled()) { | 94 if (isCanceled()) { |
| 75 return; | 95 return; |
| 76 } | 96 } |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 110 } | 130 } |
| 111 } | 131 } |
| 112 | 132 |
| 113 static final class NativeResponseInfo implements ResponseInfo { | 133 static final class NativeResponseInfo implements ResponseInfo { |
| 114 private final String[] mResponseInfoUrlChain; | 134 private final String[] mResponseInfoUrlChain; |
| 115 private final int mHttpStatusCode; | 135 private final int mHttpStatusCode; |
| 116 private final String mHttpStatusText; | 136 private final String mHttpStatusText; |
| 117 private final HeadersList mAllHeaders = new HeadersList(); | 137 private final HeadersList mAllHeaders = new HeadersList(); |
| 118 private final boolean mWasCached; | 138 private final boolean mWasCached; |
| 119 private final String mNegotiatedProtocol; | 139 private final String mNegotiatedProtocol; |
| 140 private Map<String, List<String>> mResponseHeaders; | |
| 120 | 141 |
| 121 NativeResponseInfo(String[] urlChain, int httpStatusCode, | 142 NativeResponseInfo(String[] urlChain, int httpStatusCode, |
| 122 String httpStatusText, boolean wasCached, | 143 String httpStatusText, boolean wasCached, |
| 123 String negotiatedProtocol) { | 144 String negotiatedProtocol) { |
| 124 mResponseInfoUrlChain = urlChain; | 145 mResponseInfoUrlChain = urlChain; |
| 125 mHttpStatusCode = httpStatusCode; | 146 mHttpStatusCode = httpStatusCode; |
| 126 mHttpStatusText = httpStatusText; | 147 mHttpStatusText = httpStatusText; |
| 127 mWasCached = wasCached; | 148 mWasCached = wasCached; |
| 128 mNegotiatedProtocol = negotiatedProtocol; | 149 mNegotiatedProtocol = negotiatedProtocol; |
| 129 } | 150 } |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 148 return mHttpStatusText; | 169 return mHttpStatusText; |
| 149 } | 170 } |
| 150 | 171 |
| 151 @Override | 172 @Override |
| 152 public List<Pair<String, String>> getAllHeadersAsList() { | 173 public List<Pair<String, String>> getAllHeadersAsList() { |
| 153 return Collections.unmodifiableList(mAllHeaders); | 174 return Collections.unmodifiableList(mAllHeaders); |
| 154 } | 175 } |
| 155 | 176 |
| 156 @Override | 177 @Override |
| 157 public Map<String, List<String>> getAllHeaders() { | 178 public Map<String, List<String>> getAllHeaders() { |
| 158 Map<String, List<String>> map = new HashMap<String, List<String>>(); | 179 if (mResponseHeaders != null) { |
| 180 return mResponseHeaders; | |
| 181 } | |
| 182 Map<String, List<String>> map = | |
| 183 new TreeMap<String, List<String>>(FIELD_NAME_COMPARATOR); | |
|
mmenke
2015/01/16 19:43:13
Hrm...The comparison is not "consistent with equal
xunjieli
2015/01/16 20:01:12
Acknowledged.
| |
| 159 for (Pair<String, String> entry : mAllHeaders) { | 184 for (Pair<String, String> entry : mAllHeaders) { |
| 185 List<String> values = new ArrayList<String>(); | |
| 160 if (map.containsKey(entry.first)) { | 186 if (map.containsKey(entry.first)) { |
| 161 map.get(entry.first).add(entry.second); | 187 values.addAll(map.get(entry.first)); |
| 162 } else { | |
| 163 List<String> values = new ArrayList<String>(); | |
| 164 values.add(entry.second); | |
| 165 map.put(entry.first, values); | |
| 166 } | 188 } |
| 189 values.add(entry.second); | |
| 190 map.put(entry.first, Collections.unmodifiableList(values)); | |
| 167 } | 191 } |
| 168 return Collections.unmodifiableMap(map); | 192 mResponseHeaders = Collections.unmodifiableMap(map); |
| 193 return mResponseHeaders; | |
| 169 } | 194 } |
| 170 | 195 |
| 171 @Override | 196 @Override |
| 172 public boolean wasCached() { | 197 public boolean wasCached() { |
| 173 return mWasCached; | 198 return mWasCached; |
| 174 } | 199 } |
| 175 | 200 |
| 176 @Override | 201 @Override |
| 177 public String getNegotiatedProtocol() { | 202 public String getNegotiatedProtocol() { |
| 178 return mNegotiatedProtocol; | 203 return mNegotiatedProtocol; |
| (...skipping 425 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 604 HeadersList headers); | 629 HeadersList headers); |
| 605 | 630 |
| 606 private native String nativeGetNegotiatedProtocol(long urlRequestAdapter); | 631 private native String nativeGetNegotiatedProtocol(long urlRequestAdapter); |
| 607 | 632 |
| 608 private native String nativeGetHttpStatusText(long urlRequestAdapter); | 633 private native String nativeGetHttpStatusText(long urlRequestAdapter); |
| 609 | 634 |
| 610 private native boolean nativeGetWasCached(long urlRequestAdapter); | 635 private native boolean nativeGetWasCached(long urlRequestAdapter); |
| 611 | 636 |
| 612 private native long nativeGetTotalReceivedBytes(long urlRequestAdapter); | 637 private native long nativeGetTotalReceivedBytes(long urlRequestAdapter); |
| 613 } | 638 } |
| OLD | NEW |