| 1 | // Copyright (c) 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 | |
| 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 | */ |
| 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 | */ |
| 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 | */ |
| 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 | */ |
| 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 | */ |
| 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 | */ |
| 165 | public String getUrl() { |
| 166 | return mUrl; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Set load type of this load. Defaults to LOAD_TYPE_DEFAULT. |
| 171 | * @param loadType One of LOAD_TYPE static constants above. |
| 172 | */ |
| 173 | public void setLoadType(int loadType) { |
| 174 | mLoadUrlType = loadType; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Set transition type of this load. Defaults to PAGE_TRANSITION_LINK. |
| 179 | * @param transitionType One of PAGE_TRANSITION static constants in ContentView. |
| 180 | */ |
| 181 | public void setTransitionType(int transitionType) { |
| 182 | mTransitionType = transitionType; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Return the transition type. |
| 187 | */ |
| 188 | public int getTransitionType() { |
| 189 | return mTransitionType; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Set user agent override option of this load. Defaults to UA_OVERRIDE_INHERIT. |
| 194 | * @param uaOption One of UA_OVERRIDE static constants above. |
| 195 | */ |
| 196 | public void setOverrideUserAgent(int uaOption) { |
| 197 | mUaOverrideOption = uaOption; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Set extra headers for this load. |
| 202 | * @param extraHeaders Extra HTTP headers for this load. Note that these |
| 203 | * headers will never overwrite existing ones set by Chromium. |
| 204 | */ |
| 205 | public void setExtraHeaders(Map<String, String> extraHeaders) { |
| 206 | mExtraHeaders = extraHeaders; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Return the extra headers as a single String separated by "\n", or null if no extra header |
| 211 | * is set. This form is suitable for passing to native |
| 212 | * NavigationController::LoadUrlParams::extra_headers. |
| 213 | */ |
| 214 | String getExtraHeadersString() { |
| 215 | if (mExtraHeaders == null) return null; |
| 216 | |
| 217 | StringBuilder headerBuilder = new StringBuilder(); |
| 218 | for (Map.Entry<String, String> header : mExtraHeaders.entrySet()) { |
| 219 | if (headerBuilder.length() > 0) headerBuilder.append("\n"); |
| 220 | |
| 221 | // Header name should be lower case. |
| 222 | headerBuilder.append(header.getKey().toLowerCase()); |
| 223 | headerBuilder.append(":"); |
| 224 | headerBuilder.append(header.getValue()); |
| 225 | } |
| 226 | |
| 227 | return headerBuilder.toString(); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Set the post data of this load. This field is ignored unless load type is |
| 232 | * LOAD_TYPE_BROWSER_INITIATED_HTTP_POST. |
| 233 | * @param postData Post data for this http post load. |
| 234 | */ |
| 235 | public void setPostData(byte[] postData) { |
| 236 | mPostData = postData; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Set the base url for data load. It is used both to resolve relative URLs |
| 241 | * and when applying JavaScript's same origin policy. It is ignored unless |
| 242 | * load type is LOAD_TYPE_DATA. |
| 243 | * @param baseUrl The base url for this data load. |
| 244 | */ |
| 245 | public void setBaseUrlForDataUrl(String baseUrl) { |
| 246 | mBaseUrlForDataUrl = baseUrl; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Set the virtual url for data load. It is the url displayed to the user. |
| 251 | * It is ignored unless load type is LOAD_TYPE_DATA. |
| 252 | * @param virtualUrl The virtual url for this data load. |
| 253 | */ |
| 254 | public void setVirtualUrlForDataUrl(String virtualUrl) { |
| 255 | mVirtualUrlForDataUrl = virtualUrl; |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Set whether the load should be able to access local resources. This |
| 260 | * defaults to false. |
| 261 | */ |
| 262 | public void setCanLoadLocalResources(boolean canLoad) { |
| 263 | mCanLoadLocalResources = canLoad; |
| 264 | } |
| 265 | |
| 266 | public int getLoadUrlType() { |
| 267 | return mLoadUrlType; |
| 268 | } |
| 269 | |
| 270 | public boolean isBaseUrlDataScheme() { |
| 271 | // If there's no base url set, but this is a data load then |
| 272 | // treat the scheme as data:. |
| 273 | if (mBaseUrlForDataUrl == null && mLoadUrlType == LOAD_TYPE_DATA) { |
| 274 | return true; |
| 275 | } |
| 276 | return nativeIsDataScheme(mBaseUrlForDataUrl); |
| 277 | } |
| 278 | |
| 279 | @SuppressWarnings("unused") |
| 280 | @CalledByNative |
| 281 | private static void initializeConstants( |
| 282 | int load_type_default, |
| 283 | int load_type_browser_initiated_http_post, |
| 284 | int load_type_data, |
| 285 | int ua_override_inherit, |
| 286 | int ua_override_false, |
| 287 | int ua_override_true) { |
| 288 | LOAD_TYPE_DEFAULT = load_type_default; |
| 289 | LOAD_TYPE_BROWSER_INITIATED_HTTP_POST = load_type_browser_initiated_http_post; |
| 290 | LOAD_TYPE_DATA = load_type_data; |
| 291 | UA_OVERRIDE_INHERIT = ua_override_inherit; |
| 292 | UA_OVERRIDE_FALSE = ua_override_false; |
| 293 | UA_OVERRIDE_TRUE = ua_override_true; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Parses |url| as a GURL on the native side, and |
| 298 | * returns true if it's scheme is data:. |
| 299 | */ |
| 300 | private static native boolean nativeIsDataScheme(String url); |
| 301 | } |