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 | 12 |
| 13 import java.nio.ByteBuffer; | 13 import java.nio.ByteBuffer; |
| 14 import java.util.ArrayList; | 14 import java.util.ArrayList; |
| 15 import java.util.Collections; | 15 import java.util.Collections; |
| 16 import java.util.HashMap; | |
| 17 import java.util.List; | 16 import java.util.List; |
| 18 import java.util.Map; | 17 import java.util.Map; |
| 18 import java.util.TreeMap; | |
| 19 import java.util.concurrent.Executor; | 19 import java.util.concurrent.Executor; |
| 20 | 20 |
| 21 /** | 21 /** |
| 22 * UrlRequest using Chromium HTTP stack implementation. Could be accessed from | 22 * UrlRequest using Chromium HTTP stack implementation. Could be accessed from |
| 23 * any thread on Executor. Cancel can be done from any thread. | 23 * any thread on Executor. Cancel can be done from any thread. |
| 24 * All @CallByNative methods are called on native network thread | 24 * All @CallByNative methods are called on native network thread |
| 25 * and post tasks with listener calls onto Executor. Upon return from listener | 25 * and post tasks with listener calls onto Executor. Upon return from listener |
| 26 * callback native request adapter is called on executive thread and posts | 26 * callback native request adapter is called on executive thread and posts |
| 27 * native tasks to native network thread. Because Cancel could be called from | 27 * native tasks to native network thread. Because Cancel could be called from |
| 28 * any thread it is protected by mUrlRequestAdapterLock. | 28 * any thread it is protected by mUrlRequestAdapterLock. |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 110 } | 110 } |
| 111 } | 111 } |
| 112 | 112 |
| 113 static final class NativeResponseInfo implements ResponseInfo { | 113 static final class NativeResponseInfo implements ResponseInfo { |
| 114 private final String[] mResponseInfoUrlChain; | 114 private final String[] mResponseInfoUrlChain; |
| 115 private final int mHttpStatusCode; | 115 private final int mHttpStatusCode; |
| 116 private final String mHttpStatusText; | 116 private final String mHttpStatusText; |
| 117 private final HeadersList mAllHeaders = new HeadersList(); | 117 private final HeadersList mAllHeaders = new HeadersList(); |
| 118 private final boolean mWasCached; | 118 private final boolean mWasCached; |
| 119 private final String mNegotiatedProtocol; | 119 private final String mNegotiatedProtocol; |
| 120 private Map<String, List<String>> mResponseHeaders; | |
| 121 private List<Pair<String, String>> mUnmodifiableAllHeaders; | |
| 120 | 122 |
| 121 NativeResponseInfo(String[] urlChain, int httpStatusCode, | 123 NativeResponseInfo(String[] urlChain, int httpStatusCode, |
| 122 String httpStatusText, boolean wasCached, | 124 String httpStatusText, boolean wasCached, |
| 123 String negotiatedProtocol) { | 125 String negotiatedProtocol) { |
| 124 mResponseInfoUrlChain = urlChain; | 126 mResponseInfoUrlChain = urlChain; |
| 125 mHttpStatusCode = httpStatusCode; | 127 mHttpStatusCode = httpStatusCode; |
| 126 mHttpStatusText = httpStatusText; | 128 mHttpStatusText = httpStatusText; |
| 127 mWasCached = wasCached; | 129 mWasCached = wasCached; |
| 128 mNegotiatedProtocol = negotiatedProtocol; | 130 mNegotiatedProtocol = negotiatedProtocol; |
| 129 } | 131 } |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 143 return mHttpStatusCode; | 145 return mHttpStatusCode; |
| 144 } | 146 } |
| 145 | 147 |
| 146 @Override | 148 @Override |
| 147 public String getHttpStatusText() { | 149 public String getHttpStatusText() { |
| 148 return mHttpStatusText; | 150 return mHttpStatusText; |
| 149 } | 151 } |
| 150 | 152 |
| 151 @Override | 153 @Override |
| 152 public List<Pair<String, String>> getAllHeadersAsList() { | 154 public List<Pair<String, String>> getAllHeadersAsList() { |
| 153 return Collections.unmodifiableList(mAllHeaders); | 155 if (mUnmodifiableAllHeaders == null) { |
| 156 mUnmodifiableAllHeaders = | |
| 157 Collections.unmodifiableList(mAllHeaders); | |
| 158 } | |
| 159 return mUnmodifiableAllHeaders; | |
|
mef
2015/01/30 16:22:51
Do I understand correctly that the reason to retur
xunjieli
2015/01/30 18:22:58
Yes, this seems to be an requirement for HttpURLCo
| |
| 154 } | 160 } |
| 155 | 161 |
| 156 @Override | 162 @Override |
| 157 public Map<String, List<String>> getAllHeaders() { | 163 public Map<String, List<String>> getAllHeaders() { |
| 158 Map<String, List<String>> map = new HashMap<String, List<String>>(); | 164 if (mResponseHeaders != null) { |
| 165 return mResponseHeaders; | |
| 166 } | |
| 167 Map<String, List<String>> map = new TreeMap<String, List<String>>( | |
| 168 String.CASE_INSENSITIVE_ORDER); | |
| 159 for (Pair<String, String> entry : mAllHeaders) { | 169 for (Pair<String, String> entry : mAllHeaders) { |
| 170 List<String> values = new ArrayList<String>(); | |
| 160 if (map.containsKey(entry.first)) { | 171 if (map.containsKey(entry.first)) { |
| 161 map.get(entry.first).add(entry.second); | 172 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 } | 173 } |
| 174 values.add(entry.second); | |
| 175 map.put(entry.first, Collections.unmodifiableList(values)); | |
| 167 } | 176 } |
| 168 return Collections.unmodifiableMap(map); | 177 mResponseHeaders = Collections.unmodifiableMap(map); |
| 178 return mResponseHeaders; | |
| 169 } | 179 } |
| 170 | 180 |
| 171 @Override | 181 @Override |
| 172 public boolean wasCached() { | 182 public boolean wasCached() { |
| 173 return mWasCached; | 183 return mWasCached; |
| 174 } | 184 } |
| 175 | 185 |
| 176 @Override | 186 @Override |
| 177 public String getNegotiatedProtocol() { | 187 public String getNegotiatedProtocol() { |
| 178 return mNegotiatedProtocol; | 188 return mNegotiatedProtocol; |
| (...skipping 425 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 604 HeadersList headers); | 614 HeadersList headers); |
| 605 | 615 |
| 606 private native String nativeGetNegotiatedProtocol(long urlRequestAdapter); | 616 private native String nativeGetNegotiatedProtocol(long urlRequestAdapter); |
| 607 | 617 |
| 608 private native String nativeGetHttpStatusText(long urlRequestAdapter); | 618 private native String nativeGetHttpStatusText(long urlRequestAdapter); |
| 609 | 619 |
| 610 private native boolean nativeGetWasCached(long urlRequestAdapter); | 620 private native boolean nativeGetWasCached(long urlRequestAdapter); |
| 611 | 621 |
| 612 private native long nativeGetTotalReceivedBytes(long urlRequestAdapter); | 622 private native long nativeGetTotalReceivedBytes(long urlRequestAdapter); |
| 613 } | 623 } |
| OLD | NEW |