• 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 package org.chromium.net.impl;
5 
6 import androidx.annotation.IntDef;
7 
8 import org.chromium.net.ExperimentalUrlRequest;
9 import org.chromium.net.UploadDataProvider;
10 import org.chromium.net.UrlRequest;
11 import org.chromium.net.UrlRequest.Status;
12 
13 import java.lang.annotation.Retention;
14 import java.lang.annotation.RetentionPolicy;
15 import java.util.concurrent.Executor;
16 
17 /**
18  * Base class for classes that implement {@link UrlRequest} including experimental
19  * features. {@link CronetUrlRequest} and {@link JavaUrlRequest} extends this class.
20  */
21 public abstract class UrlRequestBase extends ExperimentalUrlRequest {
22     /**
23      * Sets the HTTP method verb to use for this request. Must be done before
24      * request has started.
25      *
26      * <p>The default when this method is not called is "GET" if the request has
27      * no body or "POST" if it does.
28      *
29      * @param method "GET", "HEAD", "DELETE", "POST" or "PUT".
30      */
setHttpMethod(String method)31     protected abstract void setHttpMethod(String method);
32 
33     /**
34      * Adds a request header. Must be done before request has started.
35      *
36      * @param header header name.
37      * @param value header value.
38      */
addHeader(String header, String value)39     protected abstract void addHeader(String header, String value);
40 
41     /**
42      * Sets upload data provider. Must be done before request has started. May only be
43      * invoked once per request. Switches method to "POST" if not explicitly
44      * set. Starting the request will throw an exception if a Content-Type
45      * header is not set.
46      *
47      * @param uploadDataProvider responsible for providing the upload data.
48      * @param executor All {@code uploadDataProvider} methods will be invoked
49      *     using this {@code Executor}. May optionally be the same
50      *     {@code Executor} the request itself is using.
51      */
setUploadDataProvider( UploadDataProvider uploadDataProvider, Executor executor)52     protected abstract void setUploadDataProvider(
53             UploadDataProvider uploadDataProvider, Executor executor);
54 
55     /** Possible URL Request statuses. */
56     @IntDef({
57         Status.INVALID,
58         Status.IDLE,
59         Status.WAITING_FOR_STALLED_SOCKET_POOL,
60         Status.WAITING_FOR_AVAILABLE_SOCKET,
61         Status.WAITING_FOR_DELEGATE,
62         Status.WAITING_FOR_CACHE,
63         Status.DOWNLOADING_PAC_FILE,
64         Status.RESOLVING_PROXY_FOR_URL,
65         Status.RESOLVING_HOST_IN_PAC_FILE,
66         Status.ESTABLISHING_PROXY_TUNNEL,
67         Status.RESOLVING_HOST,
68         Status.CONNECTING,
69         Status.SSL_HANDSHAKE,
70         Status.SENDING_REQUEST,
71         Status.WAITING_FOR_RESPONSE,
72         Status.READING_RESPONSE
73     })
74     @Retention(RetentionPolicy.SOURCE)
75     public @interface StatusValues {}
76 
77     /**
78      * Convert a LoadState int to one of values listed above.
79      * @param loadState a LoadState to convert.
80      * @return static int Status.
81      */
82     @StatusValues
convertLoadState(int loadState)83     public static int convertLoadState(int loadState) {
84         assert loadState >= LoadState.IDLE && loadState <= LoadState.READING_RESPONSE;
85         switch (loadState) {
86             case (LoadState.IDLE):
87                 return Status.IDLE;
88 
89             case (LoadState.WAITING_FOR_STALLED_SOCKET_POOL):
90                 return Status.WAITING_FOR_STALLED_SOCKET_POOL;
91 
92             case (LoadState.WAITING_FOR_AVAILABLE_SOCKET):
93                 return Status.WAITING_FOR_AVAILABLE_SOCKET;
94 
95             case (LoadState.WAITING_FOR_DELEGATE):
96                 return Status.WAITING_FOR_DELEGATE;
97 
98             case (LoadState.WAITING_FOR_CACHE):
99                 return Status.WAITING_FOR_CACHE;
100 
101             case (LoadState.DOWNLOADING_PAC_FILE):
102                 return Status.DOWNLOADING_PAC_FILE;
103 
104             case (LoadState.RESOLVING_PROXY_FOR_URL):
105                 return Status.RESOLVING_PROXY_FOR_URL;
106 
107             case (LoadState.RESOLVING_HOST_IN_PAC_FILE):
108                 return Status.RESOLVING_HOST_IN_PAC_FILE;
109 
110             case (LoadState.ESTABLISHING_PROXY_TUNNEL):
111                 return Status.ESTABLISHING_PROXY_TUNNEL;
112 
113             case (LoadState.RESOLVING_HOST):
114                 return Status.RESOLVING_HOST;
115 
116             case (LoadState.CONNECTING):
117                 return Status.CONNECTING;
118 
119             case (LoadState.SSL_HANDSHAKE):
120                 return Status.SSL_HANDSHAKE;
121 
122             case (LoadState.SENDING_REQUEST):
123                 return Status.SENDING_REQUEST;
124 
125             case (LoadState.WAITING_FOR_RESPONSE):
126                 return Status.WAITING_FOR_RESPONSE;
127 
128             case (LoadState.READING_RESPONSE):
129                 return Status.READING_RESPONSE;
130 
131             default:
132                 // A load state is retrieved but there is no corresponding
133                 // request status. This most likely means that the mapping is
134                 // incorrect.
135                 throw new IllegalArgumentException("No request status found.");
136         }
137     }
138 }
139