• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The Chromium Authors
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.net.impl;
6 
7 import android.net.http.HeaderBlock;
8 import android.net.http.UrlRequest;
9 import android.net.http.UrlResponseInfo;
10 
11 import java.util.Collections;
12 import java.util.List;
13 import java.util.Locale;
14 import java.util.Map;
15 import java.util.concurrent.atomic.AtomicLong;
16 
17 /**
18  * Implements the container for basic information about a response. Included in
19  * {@link UrlRequest.Callback} callbacks. Each
20  * {@link UrlRequest.Callback#onRedirectReceived onRedirectReceived()}
21  * callback gets a different copy of {@code UrlResponseInfo} describing a particular
22  * redirect response.
23  */
24 public final class UrlResponseInfoImpl extends UrlResponseInfo {
25     private final List<String> mResponseInfoUrlChain;
26     private final int mHttpStatusCode;
27     private final String mHttpStatusText;
28     private final boolean mWasCached;
29     private final String mNegotiatedProtocol;
30     private final String mProxyServer;
31     private final AtomicLong mReceivedByteCount;
32     private final HeaderBlockImpl mHeaders;
33 
34     /**
35      * Creates an implementation of {@link UrlResponseInfo}.
36      *
37      * @param urlChain the URL chain. The first entry is the originally requested URL;
38      *         the following entries are redirects followed.
39      * @param httpStatusCode the HTTP status code.
40      * @param httpStatusText the HTTP status text of the status line.
41      * @param allHeadersList list of response header field and value pairs.
42      * @param wasCached {@code true} if the response came from the cache, {@code false}
43      *         otherwise.
44      * @param negotiatedProtocol the protocol negotiated with the server.
45      * @param proxyServer the proxy server that was used for the request.
46      * @param receivedByteCount minimum count of bytes received from the network to process this
47      *         request.
48      */
UrlResponseInfoImpl(List<String> urlChain, int httpStatusCode, String httpStatusText, List<Map.Entry<String, String>> allHeadersList, boolean wasCached, String negotiatedProtocol, String proxyServer, long receivedByteCount)49     public UrlResponseInfoImpl(List<String> urlChain, int httpStatusCode, String httpStatusText,
50             List<Map.Entry<String, String>> allHeadersList, boolean wasCached,
51             String negotiatedProtocol, String proxyServer, long receivedByteCount) {
52         mResponseInfoUrlChain = Collections.unmodifiableList(urlChain);
53         mHttpStatusCode = httpStatusCode;
54         mHttpStatusText = httpStatusText;
55         mHeaders = new HeaderBlockImpl(Collections.unmodifiableList(allHeadersList));
56         mWasCached = wasCached;
57         mNegotiatedProtocol = negotiatedProtocol;
58         mProxyServer = proxyServer;
59         mReceivedByteCount = new AtomicLong(receivedByteCount);
60     }
61 
62     /**
63      * Constructor for backwards compatibility.  See main constructor above for more info.
64      */
65     @Deprecated
UrlResponseInfoImpl(List<String> urlChain, int httpStatusCode, String httpStatusText, List<Map.Entry<String, String>> allHeadersList, boolean wasCached, String negotiatedProtocol, String proxyServer)66     public UrlResponseInfoImpl(List<String> urlChain, int httpStatusCode, String httpStatusText,
67             List<Map.Entry<String, String>> allHeadersList, boolean wasCached,
68             String negotiatedProtocol, String proxyServer) {
69         this(urlChain, httpStatusCode, httpStatusText, allHeadersList, wasCached,
70                 negotiatedProtocol, proxyServer, 0);
71     }
72 
73     @Override
getUrl()74     public String getUrl() {
75         return mResponseInfoUrlChain.get(mResponseInfoUrlChain.size() - 1);
76     }
77 
78     @Override
getUrlChain()79     public List<String> getUrlChain() {
80         return mResponseInfoUrlChain;
81     }
82 
83     @Override
getHttpStatusCode()84     public int getHttpStatusCode() {
85         return mHttpStatusCode;
86     }
87 
88     @Override
getHttpStatusText()89     public String getHttpStatusText() {
90         return mHttpStatusText;
91     }
92 
93     @Override
getHeaders()94     public HeaderBlock getHeaders() {
95         return mHeaders;
96     }
97 
98     @Override
wasCached()99     public boolean wasCached() {
100         return mWasCached;
101     }
102 
103     @Override
getNegotiatedProtocol()104     public String getNegotiatedProtocol() {
105         return mNegotiatedProtocol;
106     }
107 
108     @Override
getProxyServer()109     public String getProxyServer() {
110         return mProxyServer;
111     }
112 
113     @Override
getReceivedByteCount()114     public long getReceivedByteCount() {
115         return mReceivedByteCount.get();
116     }
117 
118     @Override
toString()119     public String toString() {
120         return String.format(Locale.ROOT, "UrlResponseInfo@[%s][%s]: urlChain = %s, "
121                         + "httpStatus = %d %s, headers = %s, wasCached = %b, "
122                         + "negotiatedProtocol = %s, proxyServer= %s, receivedByteCount = %d",
123                 // Prevent asserting on the contents of this string
124                 Integer.toHexString(System.identityHashCode(this)), getUrl(),
125                 getUrlChain().toString(), getHttpStatusCode(), getHttpStatusText(),
126                 getHeaders().getAsList().toString(), wasCached(), getNegotiatedProtocol(),
127                 getProxyServer(), getReceivedByteCount());
128     }
129 
130     /**
131      * Sets mReceivedByteCount. Must not be called after request completion or cancellation.
132      */
setReceivedByteCount(long currentReceivedByteCount)133     public void setReceivedByteCount(long currentReceivedByteCount) {
134         mReceivedByteCount.set(currentReceivedByteCount);
135     }
136 }
137