• Home
  • History
  • Annotate
  • 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_public.browser;
6 
7 import org.chromium.base.CalledByNative;
8 import org.chromium.base.JNINamespace;
9 import org.chromium.content_public.common.Referrer;
10 import org.chromium.ui.base.PageTransitionTypes;
11 
12 import java.util.Locale;
13 import java.util.Map;
14 
15 /**
16  * Holds parameters for NavigationController.LoadUrl. Parameters should match
17  * counterparts in NavigationController::LoadURLParams, including default
18  * values.
19  */
20 @JNINamespace("content")
21 public class LoadUrlParams {
22     // Should match NavigationController::LoadUrlType exactly. See comments
23     // there for proper usage. initializeConstants() checks that the values
24     // are correct.
25     public static final int LOAD_TYPE_DEFAULT = 0;
26     public static final int LOAD_TYPE_BROWSER_INITIATED_HTTP_POST = 1;
27     public static final int LOAD_TYPE_DATA = 2;
28 
29     // Should match NavigationController::UserAgentOverrideOption exactly.
30     // See comments there for proper usage. initializeConstants() checks that
31     // the values are correct.
32     public static final int UA_OVERRIDE_INHERIT = 0;
33     public static final int UA_OVERRIDE_FALSE = 1;
34     public static final int UA_OVERRIDE_TRUE = 2;
35 
36     // Fields with counterparts in NavigationController::LoadURLParams.
37     // Package private so that ContentViewCore.loadUrl can pass them down to
38     // native code. Should not be accessed directly anywhere else outside of
39     // this class.
40     String mUrl;
41     int mLoadUrlType;
42     int mTransitionType;
43     Referrer mReferrer;
44     private Map<String, String> mExtraHeaders;
45     private String mVerbatimHeaders;
46     int mUaOverrideOption;
47     byte[] mPostData;
48     String mBaseUrlForDataUrl;
49     String mVirtualUrlForDataUrl;
50     boolean mCanLoadLocalResources;
51     boolean mIsRendererInitiated;
52 
53     /**
54      * Creates an instance with default page transition type.
55      * @param url the url to be loaded
56      */
LoadUrlParams(String url)57     public LoadUrlParams(String url) {
58         this(url, PageTransitionTypes.PAGE_TRANSITION_LINK);
59     }
60 
61     /**
62      * Creates an instance with the given page transition type.
63      * @param url the url to be loaded
64      * @param transitionType the PageTransitionType constant corresponding to the load
65      */
LoadUrlParams(String url, int transitionType)66     public LoadUrlParams(String url, int transitionType) {
67         mUrl = url;
68         mTransitionType = transitionType;
69 
70         // Initialize other fields to defaults matching defaults of the native
71         // NavigationController::LoadUrlParams.
72         mLoadUrlType = LOAD_TYPE_DEFAULT;
73         mUaOverrideOption = UA_OVERRIDE_INHERIT;
74         mPostData = null;
75         mBaseUrlForDataUrl = null;
76         mVirtualUrlForDataUrl = null;
77     }
78 
79     /**
80      * Helper method to create a LoadUrlParams object for data url.
81      * @param data Data to be loaded.
82      * @param mimeType Mime type of the data.
83      * @param isBase64Encoded True if the data is encoded in Base 64 format.
84      */
createLoadDataParams( String data, String mimeType, boolean isBase64Encoded)85     public static LoadUrlParams createLoadDataParams(
86         String data, String mimeType, boolean isBase64Encoded) {
87         return createLoadDataParams(data, mimeType, isBase64Encoded, null);
88     }
89 
90     /**
91      * Helper method to create a LoadUrlParams object for data url.
92      * @param data Data to be loaded.
93      * @param mimeType Mime type of the data.
94      * @param isBase64Encoded True if the data is encoded in Base 64 format.
95      * @param charset The character set for the data. Pass null if the mime type
96      *                does not require a special charset.
97      */
createLoadDataParams( String data, String mimeType, boolean isBase64Encoded, String charset)98     public static LoadUrlParams createLoadDataParams(
99             String data, String mimeType, boolean isBase64Encoded, String charset) {
100         StringBuilder dataUrl = new StringBuilder("data:");
101         dataUrl.append(mimeType);
102         if (charset != null && !charset.isEmpty()) {
103             dataUrl.append(";charset=" + charset);
104         }
105         if (isBase64Encoded) {
106             dataUrl.append(";base64");
107         }
108         dataUrl.append(",");
109         dataUrl.append(data);
110 
111         LoadUrlParams params = new LoadUrlParams(dataUrl.toString());
112         params.setLoadType(LoadUrlParams.LOAD_TYPE_DATA);
113         params.setTransitionType(PageTransitionTypes.PAGE_TRANSITION_TYPED);
114         return params;
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      */
createLoadDataParamsWithBaseUrl( String data, String mimeType, boolean isBase64Encoded, String baseUrl, String historyUrl)130     public static LoadUrlParams createLoadDataParamsWithBaseUrl(
131             String data, String mimeType, boolean isBase64Encoded,
132             String baseUrl, String historyUrl) {
133         return createLoadDataParamsWithBaseUrl(data, mimeType, isBase64Encoded,
134                 baseUrl, historyUrl, null);
135     }
136 
137     /**
138      * Helper method to create a LoadUrlParams object for data url with base
139      * and virtual url.
140      * @param data Data to be loaded.
141      * @param mimeType Mime type of the data.
142      * @param isBase64Encoded True if the data is encoded in Base 64 format.
143      * @param baseUrl Base url of this data load. Note that for WebView compatibility,
144      *                baseUrl and historyUrl are ignored if this is a data: url.
145      *                Defaults to about:blank if null.
146      * @param historyUrl History url for this data load. Note that for WebView compatibility,
147      *                   this is ignored if baseUrl is a data: url. Defaults to about:blank
148      *                   if null.
149      * @param charset The character set for the data. Pass null if the mime type
150      *                does not require a special charset.
151      */
createLoadDataParamsWithBaseUrl( String data, String mimeType, boolean isBase64Encoded, String baseUrl, String historyUrl, String charset)152     public static LoadUrlParams createLoadDataParamsWithBaseUrl(
153             String data, String mimeType, boolean isBase64Encoded,
154             String baseUrl, String historyUrl, String charset) {
155         LoadUrlParams params = createLoadDataParams(data, mimeType, isBase64Encoded, charset);
156         // For WebView compatibility, when the base URL has the 'data:'
157         // scheme, we treat it as a regular data URL load and skip setting
158         // baseUrl and historyUrl.
159         // TODO(joth): we should just append baseURL and historyURL here, and move the
160         // WebView specific transform up to a wrapper factory function in android_webview/.
161         if (baseUrl == null || !baseUrl.toLowerCase(Locale.US).startsWith("data:")) {
162             params.setBaseUrlForDataUrl(baseUrl != null ? baseUrl : "about:blank");
163             params.setVirtualUrlForDataUrl(historyUrl != null ? historyUrl : "about:blank");
164         }
165         return params;
166     }
167 
168     /**
169      * Helper method to create a LoadUrlParams object for an HTTP POST load.
170      * @param url URL of the load.
171      * @param postData Post data of the load. Can be null.
172      */
createLoadHttpPostParams( String url, byte[] postData)173     public static LoadUrlParams createLoadHttpPostParams(
174             String url, byte[] postData) {
175         LoadUrlParams params = new LoadUrlParams(url);
176         params.setLoadType(LOAD_TYPE_BROWSER_INITIATED_HTTP_POST);
177         params.setTransitionType(PageTransitionTypes.PAGE_TRANSITION_TYPED);
178         params.setPostData(postData);
179         return params;
180     }
181 
182     /**
183      * Sets the url.
184      */
setUrl(String url)185     public void setUrl(String url) {
186         mUrl = url;
187     }
188 
189     /**
190      * Return the url.
191      */
getUrl()192     public String getUrl() {
193         return mUrl;
194     }
195 
196     /**
197      * Return the base url for a data url, otherwise null.
198      */
getBaseUrl()199     public String getBaseUrl() {
200         return mBaseUrlForDataUrl;
201     }
202 
203     /**
204      * Set load type of this load. Defaults to LOAD_TYPE_DEFAULT.
205      * @param loadType One of LOAD_TYPE static constants above.
206      */
setLoadType(int loadType)207     public void setLoadType(int loadType) {
208         mLoadUrlType = loadType;
209     }
210 
211     /**
212      * Set transition type of this load. Defaults to PAGE_TRANSITION_LINK.
213      * @param transitionType One of PAGE_TRANSITION static constants in ContentView.
214      */
setTransitionType(int transitionType)215     public void setTransitionType(int transitionType) {
216         mTransitionType = transitionType;
217     }
218 
219     /**
220      * Return the transition type.
221      */
getTransitionType()222     public int getTransitionType() {
223         return mTransitionType;
224     }
225 
226     /**
227      * @return the referrer of this load
228      */
setReferrer(Referrer referrer)229     public void setReferrer(Referrer referrer) {
230         mReferrer = referrer;
231     }
232 
233     /**
234      * Sets the referrer of this load.
235      */
getReferrer()236     public Referrer getReferrer() {
237         return mReferrer;
238     }
239 
240     /**
241      * Set extra headers for this load.
242      * @param extraHeaders Extra HTTP headers for this load. Note that these
243      *                     headers will never overwrite existing ones set by Chromium.
244      */
setExtraHeaders(Map<String, String> extraHeaders)245     public void setExtraHeaders(Map<String, String> extraHeaders) {
246         mExtraHeaders = extraHeaders;
247     }
248 
249     /**
250      * Return the extra headers as a map.
251      */
getExtraHeaders()252     public Map<String, String> getExtraHeaders() {
253         return mExtraHeaders;
254     }
255 
256     /**
257      * Return the extra headers as a single String separated by "\n", or null if no extra header is
258      * set. This form is suitable for passing to native
259      * NavigationController::LoadUrlParams::extra_headers. This will return the headers set in an
260      * exploded form through setExtraHeaders(). Embedders that work with extra headers in opaque
261      * collapsed form can use the setVerbatimHeaders() / getVerbatimHeaders() instead.
262      */
getExtraHeadersString()263     public String getExtraHeadersString() {
264         return getExtraHeadersString("\n", false);
265     }
266 
267     /**
268      * Return the extra headers as a single String separated by "\r\n", or null if no extra header
269      * is set. This form is suitable for passing to native
270      * net::HttpRequestHeaders::AddHeadersFromString.
271      */
getExtraHttpRequestHeadersString()272     public String getExtraHttpRequestHeadersString() {
273         return getExtraHeadersString("\r\n", true);
274     }
275 
getExtraHeadersString(String delimiter, boolean addTerminator)276     private String getExtraHeadersString(String delimiter, boolean addTerminator) {
277         if (mExtraHeaders == null) return null;
278 
279         StringBuilder headerBuilder = new StringBuilder();
280         for (Map.Entry<String, String> header : mExtraHeaders.entrySet()) {
281             if (headerBuilder.length() > 0) headerBuilder.append(delimiter);
282 
283             // Header name should be lower case.
284             headerBuilder.append(header.getKey().toLowerCase(Locale.US));
285             headerBuilder.append(":");
286             headerBuilder.append(header.getValue());
287         }
288         if (addTerminator)
289             headerBuilder.append(delimiter);
290 
291         return headerBuilder.toString();
292     }
293 
294     /**
295      * Sets the verbatim extra headers string. This is an alternative to storing the headers in
296      * a map (setExtraHeaders()) for the embedders that use collapsed headers strings.
297      */
setVerbatimHeaders(String headers)298     public void setVerbatimHeaders(String headers) {
299         mVerbatimHeaders = headers;
300     }
301 
302     /**
303      * @return the verbatim extra headers string
304      */
getVerbatimHeaders()305     public String getVerbatimHeaders() {
306         return mVerbatimHeaders;
307     }
308 
309     /**
310      * Set user agent override option of this load. Defaults to UA_OVERRIDE_INHERIT.
311      * @param uaOption One of UA_OVERRIDE static constants above.
312      */
setOverrideUserAgent(int uaOption)313     public void setOverrideUserAgent(int uaOption) {
314         mUaOverrideOption = uaOption;
315     }
316 
317     /**
318      * Get user agent override option of this load. Defaults to UA_OVERRIDE_INHERIT.
319      * @param uaOption One of UA_OVERRIDE static constants above.
320      */
getUserAgentOverrideOption()321     public int getUserAgentOverrideOption() {
322         return mUaOverrideOption;
323     }
324 
325     /**
326      * Set the post data of this load. This field is ignored unless load type is
327      * LOAD_TYPE_BROWSER_INITIATED_HTTP_POST.
328      * @param postData Post data for this http post load.
329      */
setPostData(byte[] postData)330     public void setPostData(byte[] postData) {
331         mPostData = postData;
332     }
333 
334     /**
335      * @return the data to be sent through POST
336      */
getPostData()337     public byte[] getPostData() {
338         return mPostData;
339     }
340 
341     /**
342      * Set the base url for data load. It is used both to resolve relative URLs
343      * and when applying JavaScript's same origin policy. It is ignored unless
344      * load type is LOAD_TYPE_DATA.
345      * @param baseUrl The base url for this data load.
346      */
setBaseUrlForDataUrl(String baseUrl)347     public void setBaseUrlForDataUrl(String baseUrl) {
348         mBaseUrlForDataUrl = baseUrl;
349     }
350 
351     /**
352      * Get the virtual url for data load. It is the url displayed to the user.
353      * It is ignored unless load type is LOAD_TYPE_DATA.
354      * @return The virtual url for this data load.
355      */
getVirtualUrlForDataUrl()356     public String getVirtualUrlForDataUrl() {
357         return mVirtualUrlForDataUrl;
358     }
359 
360     /**
361      * Set the virtual url for data load. It is the url displayed to the user.
362      * It is ignored unless load type is LOAD_TYPE_DATA.
363      * @param virtualUrl The virtual url for this data load.
364      */
setVirtualUrlForDataUrl(String virtualUrl)365     public void setVirtualUrlForDataUrl(String virtualUrl) {
366         mVirtualUrlForDataUrl = virtualUrl;
367     }
368 
369     /**
370      * Set whether the load should be able to access local resources. This
371      * defaults to false.
372      */
setCanLoadLocalResources(boolean canLoad)373     public void setCanLoadLocalResources(boolean canLoad) {
374         mCanLoadLocalResources = canLoad;
375     }
376 
377     /**
378      * Get whether the load should be able to access local resources. This
379      * defaults to false.
380      */
getCanLoadLocalResources()381     public boolean getCanLoadLocalResources() {
382         return mCanLoadLocalResources;
383     }
384 
getLoadUrlType()385     public int getLoadUrlType() {
386         return mLoadUrlType;
387     }
388 
389     /**
390      * @param rendererInitiated Whether or not this load was initiated from a renderer.
391      */
setIsRendererInitiated(boolean rendererInitiated)392     public void setIsRendererInitiated(boolean rendererInitiated) {
393         mIsRendererInitiated = rendererInitiated;
394     }
395 
396     /**
397      * @return Whether or not this load was initiated from a renderer or not.
398      */
getIsRendererInitiated()399     public boolean getIsRendererInitiated() {
400         return mIsRendererInitiated;
401     }
402 
isBaseUrlDataScheme()403     public boolean isBaseUrlDataScheme() {
404         // If there's no base url set, but this is a data load then
405         // treat the scheme as data:.
406         if (mBaseUrlForDataUrl == null && mLoadUrlType == LOAD_TYPE_DATA) {
407             return true;
408         }
409         return nativeIsDataScheme(mBaseUrlForDataUrl);
410     }
411 
412     @SuppressWarnings("unused")
413     @CalledByNative
initializeConstants( int loadTypeDefault, int loadTypeBrowserInitiatedHttpPost, int loadTypeData, int uaOverrideInherit, int uaOverrideFalse, int uaOverrideTrue)414     private static void initializeConstants(
415             int loadTypeDefault,
416             int loadTypeBrowserInitiatedHttpPost,
417             int loadTypeData,
418             int uaOverrideInherit,
419             int uaOverrideFalse,
420             int uaOverrideTrue) {
421         assert LOAD_TYPE_DEFAULT == loadTypeDefault;
422         assert LOAD_TYPE_BROWSER_INITIATED_HTTP_POST == loadTypeBrowserInitiatedHttpPost;
423         assert LOAD_TYPE_DATA == loadTypeData;
424         assert UA_OVERRIDE_INHERIT == uaOverrideInherit;
425         assert UA_OVERRIDE_FALSE == uaOverrideFalse;
426         assert UA_OVERRIDE_TRUE == uaOverrideTrue;
427     }
428 
429     /**
430      * Parses |url| as a GURL on the native side, and
431      * returns true if it's scheme is data:.
432      */
nativeIsDataScheme(String url)433     private static native boolean nativeIsDataScheme(String url);
434 }
435