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.
5 package org.chromium.content_public.browser;
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;
12 import java.util.Locale;
16 * Holds parameters for NavigationController.LoadUrl. Parameters should match
17 * counterparts in NavigationController::LoadURLParams, including default
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
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;
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;
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
44 private Map<String, String> mExtraHeaders;
45 private String mVerbatimHeaders;
46 int mUaOverrideOption;
48 String mBaseUrlForDataUrl;
49 String mVirtualUrlForDataUrl;
50 boolean mCanLoadLocalResources;
51 boolean mIsRendererInitiated;
54 * Creates an instance with default page transition type.
55 * @param url the url to be loaded
57 public LoadUrlParams(String url) {
58 this(url, PageTransitionTypes.PAGE_TRANSITION_LINK);
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
66 public LoadUrlParams(String url, int transitionType) {
68 mTransitionType = transitionType;
70 // Initialize other fields to defaults matching defaults of the native
71 // NavigationController::LoadUrlParams.
72 mLoadUrlType = LOAD_TYPE_DEFAULT;
73 mUaOverrideOption = UA_OVERRIDE_INHERIT;
75 mBaseUrlForDataUrl = null;
76 mVirtualUrlForDataUrl = null;
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.
85 public static LoadUrlParams createLoadDataParams(
86 String data, String mimeType, boolean isBase64Encoded) {
87 return createLoadDataParams(data, mimeType, isBase64Encoded, null);
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.
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);
105 if (isBase64Encoded) {
106 dataUrl.append(";base64");
109 dataUrl.append(data);
111 LoadUrlParams params = new LoadUrlParams(dataUrl.toString());
112 params.setLoadType(LoadUrlParams.LOAD_TYPE_DATA);
113 params.setTransitionType(PageTransitionTypes.PAGE_TRANSITION_TYPED);
118 * Helper method to create a LoadUrlParams object for data url with base
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
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);
138 * Helper method to create a LoadUrlParams object for data url with base
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
149 * @param charset The character set for the data. Pass null if the mime type
150 * does not require a special 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");
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.
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);
185 public void setUrl(String url) {
192 public String getUrl() {
197 * Return the base url for a data url, otherwise null.
199 public String getBaseUrl() {
200 return mBaseUrlForDataUrl;
204 * Set load type of this load. Defaults to LOAD_TYPE_DEFAULT.
205 * @param loadType One of LOAD_TYPE static constants above.
207 public void setLoadType(int loadType) {
208 mLoadUrlType = loadType;
212 * Set transition type of this load. Defaults to PAGE_TRANSITION_LINK.
213 * @param transitionType One of PAGE_TRANSITION static constants in ContentView.
215 public void setTransitionType(int transitionType) {
216 mTransitionType = transitionType;
220 * Return the transition type.
222 public int getTransitionType() {
223 return mTransitionType;
227 * @return the referrer of this load
229 public void setReferrer(Referrer referrer) {
230 mReferrer = referrer;
234 * Sets the referrer of this load.
236 public Referrer getReferrer() {
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.
245 public void setExtraHeaders(Map<String, String> extraHeaders) {
246 mExtraHeaders = extraHeaders;
250 * Return the extra headers as a map.
252 public Map<String, String> getExtraHeaders() {
253 return mExtraHeaders;
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.
263 public String getExtraHeadersString() {
264 return getExtraHeadersString("\n", false);
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.
272 public String getExtraHttpRequestHeadersString() {
273 return getExtraHeadersString("\r\n", true);
276 private String getExtraHeadersString(String delimiter, boolean addTerminator) {
277 if (mExtraHeaders == null) return null;
279 StringBuilder headerBuilder = new StringBuilder();
280 for (Map.Entry<String, String> header : mExtraHeaders.entrySet()) {
281 if (headerBuilder.length() > 0) headerBuilder.append(delimiter);
283 // Header name should be lower case.
284 headerBuilder.append(header.getKey().toLowerCase(Locale.US));
285 headerBuilder.append(":");
286 headerBuilder.append(header.getValue());
289 headerBuilder.append(delimiter);
291 return headerBuilder.toString();
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.
298 public void setVerbatimHeaders(String headers) {
299 mVerbatimHeaders = headers;
303 * @return the verbatim extra headers string
305 public String getVerbatimHeaders() {
306 return mVerbatimHeaders;
310 * Set user agent override option of this load. Defaults to UA_OVERRIDE_INHERIT.
311 * @param uaOption One of UA_OVERRIDE static constants above.
313 public void setOverrideUserAgent(int uaOption) {
314 mUaOverrideOption = uaOption;
318 * Get user agent override option of this load. Defaults to UA_OVERRIDE_INHERIT.
319 * @param uaOption One of UA_OVERRIDE static constants above.
321 public int getUserAgentOverrideOption() {
322 return mUaOverrideOption;
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.
330 public void setPostData(byte[] postData) {
331 mPostData = postData;
335 * @return the data to be sent through POST
337 public byte[] getPostData() {
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.
347 public void setBaseUrlForDataUrl(String baseUrl) {
348 mBaseUrlForDataUrl = baseUrl;
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.
356 public String getVirtualUrlForDataUrl() {
357 return mVirtualUrlForDataUrl;
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.
365 public void setVirtualUrlForDataUrl(String virtualUrl) {
366 mVirtualUrlForDataUrl = virtualUrl;
370 * Set whether the load should be able to access local resources. This
373 public void setCanLoadLocalResources(boolean canLoad) {
374 mCanLoadLocalResources = canLoad;
378 * Get whether the load should be able to access local resources. This
381 public boolean getCanLoadLocalResources() {
382 return mCanLoadLocalResources;
385 public int getLoadUrlType() {
390 * @param rendererInitiated Whether or not this load was initiated from a renderer.
392 public void setIsRendererInitiated(boolean rendererInitiated) {
393 mIsRendererInitiated = rendererInitiated;
397 * @return Whether or not this load was initiated from a renderer or not.
399 public boolean getIsRendererInitiated() {
400 return mIsRendererInitiated;
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) {
409 return nativeIsDataScheme(mBaseUrlForDataUrl);
412 @SuppressWarnings("unused")
414 private static void initializeConstants(
416 int loadTypeBrowserInitiatedHttpPost,
418 int uaOverrideInherit,
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;
430 * Parses |url| as a GURL on the native side, and
431 * returns true if it's scheme is data:.
433 private static native boolean nativeIsDataScheme(String url);