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