• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 The Chromium Authors. All rights reserved.
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.content.browser;
6 
7 import org.chromium.base.CalledByNative;
8 import org.chromium.base.JNINamespace;
9 
10 import java.util.Map;
11 
12 /**
13  * Holds parameters for ContentViewCore.LoadUrl. Parameters should match
14  * counterparts in NavigationController::LoadURLParams, including default
15  * values.
16  */
17 @JNINamespace("content")
18 public class LoadUrlParams {
19     // Should match NavigationController::LoadUrlType exactly. See comments
20     // there for proper usage. Values are initialized in initializeConstants.
21     public static int LOAD_TYPE_DEFAULT;
22     public static int LOAD_TYPE_BROWSER_INITIATED_HTTP_POST;
23     public static int LOAD_TYPE_DATA;
24 
25     // Should match NavigationController::UserAgentOverrideOption exactly.
26     // See comments there for proper usage. Values are initialized in
27     // initializeConstants.
28     public static int UA_OVERRIDE_INHERIT;
29     public static int UA_OVERRIDE_FALSE;
30     public static int UA_OVERRIDE_TRUE;
31 
32     // Fields with counterparts in NavigationController::LoadURLParams.
33     // Package private so that ContentViewCore.loadUrl can pass them down to
34     // native code. Should not be accessed directly anywhere else outside of
35     // this class.
36     final String mUrl;
37     int mLoadUrlType;
38     int mTransitionType;
39     int mUaOverrideOption;
40     private Map<String, String> mExtraHeaders;
41     byte[] mPostData;
42     String mBaseUrlForDataUrl;
43     String mVirtualUrlForDataUrl;
44     boolean mCanLoadLocalResources;
45 
LoadUrlParams(String url)46     public LoadUrlParams(String url) {
47         // Check initializeConstants was called.
48         assert LOAD_TYPE_DEFAULT != LOAD_TYPE_BROWSER_INITIATED_HTTP_POST;
49 
50         mUrl = url;
51         mLoadUrlType = LOAD_TYPE_DEFAULT;
52         mTransitionType = PageTransitionTypes.PAGE_TRANSITION_LINK;
53         mUaOverrideOption = UA_OVERRIDE_INHERIT;
54         mPostData = null;
55         mBaseUrlForDataUrl = null;
56         mVirtualUrlForDataUrl = null;
57     }
58 
59     /**
60      * Helper method to create a LoadUrlParams object for data url.
61      * @param data Data to be loaded.
62      * @param mimeType Mime type of the data.
63      * @param isBase64Encoded True if the data is encoded in Base 64 format.
64      */
createLoadDataParams( String data, String mimeType, boolean isBase64Encoded)65     public static LoadUrlParams createLoadDataParams(
66         String data, String mimeType, boolean isBase64Encoded) {
67         return createLoadDataParams(data, mimeType, isBase64Encoded, null);
68     }
69 
70     /**
71      * Helper method to create a LoadUrlParams object for data url.
72      * @param data Data to be loaded.
73      * @param mimeType Mime type of the data.
74      * @param isBase64Encoded True if the data is encoded in Base 64 format.
75      * @param charset The character set for the data. Pass null if the mime type
76      *                does not require a special charset.
77      */
createLoadDataParams( String data, String mimeType, boolean isBase64Encoded, String charset)78     public static LoadUrlParams createLoadDataParams(
79             String data, String mimeType, boolean isBase64Encoded, String charset) {
80         StringBuilder dataUrl = new StringBuilder("data:");
81         dataUrl.append(mimeType);
82         if (charset != null && !charset.isEmpty()) {
83             dataUrl.append(";charset=" + charset);
84         }
85         if (isBase64Encoded) {
86             dataUrl.append(";base64");
87         }
88         dataUrl.append(",");
89         dataUrl.append(data);
90 
91         LoadUrlParams params = new LoadUrlParams(dataUrl.toString());
92         params.setLoadType(LoadUrlParams.LOAD_TYPE_DATA);
93         params.setTransitionType(PageTransitionTypes.PAGE_TRANSITION_TYPED);
94         return params;
95     }
96 
97     /**
98      * Helper method to create a LoadUrlParams object for data url with base
99      * and virtual url.
100      * @param data Data to be loaded.
101      * @param mimeType Mime type of the data.
102      * @param isBase64Encoded True if the data is encoded in Base 64 format.
103      * @param baseUrl Base url of this data load. Note that for WebView compatibility,
104      *                baseUrl and historyUrl are ignored if this is a data: url.
105      *                Defaults to about:blank if null.
106      * @param historyUrl History url for this data load. Note that for WebView compatibility,
107      *                   this is ignored if baseUrl is a data: url. Defaults to about:blank
108      *                   if null.
109      */
createLoadDataParamsWithBaseUrl( String data, String mimeType, boolean isBase64Encoded, String baseUrl, String historyUrl)110     public static LoadUrlParams createLoadDataParamsWithBaseUrl(
111             String data, String mimeType, boolean isBase64Encoded,
112             String baseUrl, String historyUrl) {
113         return createLoadDataParamsWithBaseUrl(data, mimeType, isBase64Encoded,
114                 baseUrl, historyUrl, null);
115     }
116 
117     /**
118      * Helper method to create a LoadUrlParams object for data url with base
119      * and virtual url.
120      * @param data Data to be loaded.
121      * @param mimeType Mime type of the data.
122      * @param isBase64Encoded True if the data is encoded in Base 64 format.
123      * @param baseUrl Base url of this data load. Note that for WebView compatibility,
124      *                baseUrl and historyUrl are ignored if this is a data: url.
125      *                Defaults to about:blank if null.
126      * @param historyUrl History url for this data load. Note that for WebView compatibility,
127      *                   this is ignored if baseUrl is a data: url. Defaults to about:blank
128      *                   if null.
129      * @param charset The character set for the data. Pass null if the mime type
130      *                does not require a special charset.
131      */
createLoadDataParamsWithBaseUrl( String data, String mimeType, boolean isBase64Encoded, String baseUrl, String historyUrl, String charset)132     public static LoadUrlParams createLoadDataParamsWithBaseUrl(
133             String data, String mimeType, boolean isBase64Encoded,
134             String baseUrl, String historyUrl, String charset) {
135         LoadUrlParams params = createLoadDataParams(data, mimeType, isBase64Encoded, charset);
136         // For WebView compatibility, when the base URL has the 'data:'
137         // scheme, we treat it as a regular data URL load and skip setting
138         // baseUrl and historyUrl.
139         // TODO(joth): we should just append baseURL and historyURL here, and move the
140         // WebView specific transform up to a wrapper factory function in android_webview/.
141         if (baseUrl == null || !baseUrl.toLowerCase().startsWith("data:")) {
142             params.setBaseUrlForDataUrl(baseUrl != null ? baseUrl : "about:blank");
143             params.setVirtualUrlForDataUrl(historyUrl != null ? historyUrl : "about:blank");
144         }
145         return params;
146     }
147 
148     /**
149      * Helper method to create a LoadUrlParams object for an HTTP POST load.
150      * @param url URL of the load.
151      * @param postData Post data of the load. Can be null.
152      */
createLoadHttpPostParams( String url, byte[] postData)153     public static LoadUrlParams createLoadHttpPostParams(
154             String url, byte[] postData) {
155         LoadUrlParams params = new LoadUrlParams(url);
156         params.setLoadType(LOAD_TYPE_BROWSER_INITIATED_HTTP_POST);
157         params.setTransitionType(PageTransitionTypes.PAGE_TRANSITION_TYPED);
158         params.setPostData(postData);
159         return params;
160     }
161 
162     /**
163      * Return the url.
164      */
getUrl()165     public String getUrl() {
166         return mUrl;
167     }
168 
169     /**
170      * Return the base url for a data url, otherwise null.
171      */
getBaseUrl()172     public String getBaseUrl() {
173         return mBaseUrlForDataUrl;
174     }
175 
176     /**
177      * Set load type of this load. Defaults to LOAD_TYPE_DEFAULT.
178      * @param loadType One of LOAD_TYPE static constants above.
179      */
setLoadType(int loadType)180     public void setLoadType(int loadType) {
181         mLoadUrlType = loadType;
182     }
183 
184     /**
185      * Set transition type of this load. Defaults to PAGE_TRANSITION_LINK.
186      * @param transitionType One of PAGE_TRANSITION static constants in ContentView.
187      */
setTransitionType(int transitionType)188     public void setTransitionType(int transitionType) {
189         mTransitionType = transitionType;
190     }
191 
192     /**
193      * Return the transition type.
194      */
getTransitionType()195     public int getTransitionType() {
196         return mTransitionType;
197     }
198 
199     /**
200      * Set user agent override option of this load. Defaults to UA_OVERRIDE_INHERIT.
201      * @param uaOption One of UA_OVERRIDE static constants above.
202      */
setOverrideUserAgent(int uaOption)203     public void setOverrideUserAgent(int uaOption) {
204         mUaOverrideOption = uaOption;
205     }
206 
207     /**
208      * Set extra headers for this load.
209      * @param extraHeaders Extra HTTP headers for this load. Note that these
210      *                     headers will never overwrite existing ones set by Chromium.
211      */
setExtraHeaders(Map<String, String> extraHeaders)212     public void setExtraHeaders(Map<String, String> extraHeaders) {
213         mExtraHeaders = extraHeaders;
214     }
215 
216     /**
217      * Return the extra headers as a single String separated by "\n", or null if no extra header
218      * is set. This form is suitable for passing to native
219      * NavigationController::LoadUrlParams::extra_headers.
220      */
getExtraHeadersString()221     String getExtraHeadersString() {
222         return getExtraHeadersString("\n", false);
223     }
224 
225     /**
226      * Return the extra headers as a single String separated by "\r\n", or null if no extra header
227      * is set. This form is suitable for passing to native
228      * net::HttpRequestHeaders::AddHeadersFromString.
229      */
getExtraHttpRequestHeadersString()230     public String getExtraHttpRequestHeadersString() {
231         return getExtraHeadersString("\r\n", true);
232     }
233 
getExtraHeadersString(String delimiter, boolean addTerminator)234     private String getExtraHeadersString(String delimiter, boolean addTerminator) {
235         if (mExtraHeaders == null) return null;
236 
237         StringBuilder headerBuilder = new StringBuilder();
238         for (Map.Entry<String, String> header : mExtraHeaders.entrySet()) {
239             if (headerBuilder.length() > 0) headerBuilder.append(delimiter);
240 
241             // Header name should be lower case.
242             headerBuilder.append(header.getKey().toLowerCase());
243             headerBuilder.append(":");
244             headerBuilder.append(header.getValue());
245         }
246         if (addTerminator)
247             headerBuilder.append(delimiter);
248 
249         return headerBuilder.toString();
250     }
251 
252     /**
253      * Set the post data of this load. This field is ignored unless load type is
254      * LOAD_TYPE_BROWSER_INITIATED_HTTP_POST.
255      * @param postData Post data for this http post load.
256      */
setPostData(byte[] postData)257     public void setPostData(byte[] postData) {
258         mPostData = postData;
259     }
260 
261     /**
262      * Set the base url for data load. It is used both to resolve relative URLs
263      * and when applying JavaScript's same origin policy. It is ignored unless
264      * load type is LOAD_TYPE_DATA.
265      * @param baseUrl The base url for this data load.
266      */
setBaseUrlForDataUrl(String baseUrl)267     public void setBaseUrlForDataUrl(String baseUrl) {
268         mBaseUrlForDataUrl = baseUrl;
269     }
270 
271     /**
272      * Set the virtual url for data load. It is the url displayed to the user.
273      * It is ignored unless load type is LOAD_TYPE_DATA.
274      * @param virtualUrl The virtual url for this data load.
275      */
setVirtualUrlForDataUrl(String virtualUrl)276     public void setVirtualUrlForDataUrl(String virtualUrl) {
277         mVirtualUrlForDataUrl = virtualUrl;
278     }
279 
280     /**
281      * Set whether the load should be able to access local resources. This
282      * defaults to false.
283      */
setCanLoadLocalResources(boolean canLoad)284     public void setCanLoadLocalResources(boolean canLoad) {
285         mCanLoadLocalResources = canLoad;
286     }
287 
getLoadUrlType()288     public int getLoadUrlType() {
289         return mLoadUrlType;
290     }
291 
isBaseUrlDataScheme()292     public boolean isBaseUrlDataScheme() {
293         // If there's no base url set, but this is a data load then
294         // treat the scheme as data:.
295         if (mBaseUrlForDataUrl == null && mLoadUrlType == LOAD_TYPE_DATA) {
296             return true;
297         }
298         return nativeIsDataScheme(mBaseUrlForDataUrl);
299     }
300 
301     @SuppressWarnings("unused")
302     @CalledByNative
initializeConstants( int load_type_default, int load_type_browser_initiated_http_post, int load_type_data, int ua_override_inherit, int ua_override_false, int ua_override_true)303     private static void initializeConstants(
304             int load_type_default,
305             int load_type_browser_initiated_http_post,
306             int load_type_data,
307             int ua_override_inherit,
308             int ua_override_false,
309             int ua_override_true) {
310         LOAD_TYPE_DEFAULT = load_type_default;
311         LOAD_TYPE_BROWSER_INITIATED_HTTP_POST = load_type_browser_initiated_http_post;
312         LOAD_TYPE_DATA = load_type_data;
313         UA_OVERRIDE_INHERIT = ua_override_inherit;
314         UA_OVERRIDE_FALSE = ua_override_false;
315         UA_OVERRIDE_TRUE = ua_override_true;
316     }
317 
318     /**
319      * Parses |url| as a GURL on the native side, and
320      * returns true if it's scheme is data:.
321      */
nativeIsDataScheme(String url)322     private static native boolean nativeIsDataScheme(String url);
323 }
324