/core/java/android/webkit/ContentLoader.java

https://github.com/integralnd/android_frameworks_base · Java · 126 lines · 73 code · 14 blank · 39 comment · 7 complexity · 0cef60c9deb291882566ebc15c800e0a MD5 · raw file

  1. /*
  2. * Copyright (C) 2008 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package android.webkit;
  17. import android.content.Context;
  18. import android.net.http.EventHandler;
  19. import android.net.http.Headers;
  20. import android.net.Uri;
  21. import java.io.File;
  22. import java.io.FileInputStream;
  23. /**
  24. * This class is a concrete implementation of StreamLoader that loads
  25. * "content:" URIs
  26. */
  27. class ContentLoader extends StreamLoader {
  28. private String mUrl;
  29. private Context mContext;
  30. private String mContentType;
  31. /**
  32. * Construct a ContentLoader with the specified content URI
  33. *
  34. * @param rawUrl "content:" url pointing to content to be loaded. This url
  35. * is the same url passed in to the WebView.
  36. * @param loadListener LoadListener to pass the content to
  37. * @param context Context to use to access the asset.
  38. */
  39. ContentLoader(String rawUrl, LoadListener loadListener, Context context) {
  40. super(loadListener);
  41. mContext = context;
  42. /* strip off mimetype */
  43. int mimeIndex = rawUrl.lastIndexOf('?');
  44. if (mimeIndex != -1) {
  45. mUrl = rawUrl.substring(0, mimeIndex);
  46. mContentType = rawUrl.substring(mimeIndex + 1);
  47. } else {
  48. mUrl = rawUrl;
  49. }
  50. }
  51. @Override
  52. protected boolean setupStreamAndSendStatus() {
  53. Uri uri = Uri.parse(mUrl);
  54. if (uri == null) {
  55. mHandler.error(
  56. EventHandler.FILE_NOT_FOUND_ERROR,
  57. mContext.getString(
  58. com.android.internal.R.string.httpErrorBadUrl) +
  59. " " + mUrl);
  60. return false;
  61. }
  62. try {
  63. mDataStream = mContext.getContentResolver().openInputStream(uri);
  64. mHandler.status(1, 1, 0, "OK");
  65. } catch (java.io.FileNotFoundException ex) {
  66. mHandler.error(
  67. EventHandler.FILE_NOT_FOUND_ERROR,
  68. mContext.getString(
  69. com.android.internal.R.string.httpErrorFileNotFound) +
  70. " " + ex.getMessage());
  71. return false;
  72. } catch (java.io.IOException ex) {
  73. mHandler.error(
  74. EventHandler.FILE_ERROR,
  75. mContext.getString(
  76. com.android.internal.R.string.httpErrorFileNotFound) +
  77. " " + ex.getMessage());
  78. return false;
  79. } catch (RuntimeException ex) {
  80. // readExceptionWithFileNotFoundExceptionFromParcel in DatabaseUtils
  81. // can throw a serial of RuntimeException. Catch them all here.
  82. mHandler.error(
  83. EventHandler.FILE_ERROR,
  84. mContext.getString(
  85. com.android.internal.R.string.httpErrorFileNotFound) +
  86. " " + ex.getMessage());
  87. return false;
  88. }
  89. return true;
  90. }
  91. @Override
  92. protected void buildHeaders(Headers headers) {
  93. if (mContentType != null) {
  94. headers.setContentType("text/html");
  95. }
  96. // override the cache-control header set by StreamLoader as content can
  97. // change, we don't want WebKit to cache it
  98. headers.setCacheControl("no-store, no-cache");
  99. }
  100. /**
  101. * Construct a ContentLoader and instruct it to start loading.
  102. *
  103. * @param url "content:" url pointing to content to be loaded
  104. * @param loadListener LoadListener to pass the content to
  105. * @param context Context to use to access the asset.
  106. */
  107. public static void requestUrl(String url, LoadListener loadListener,
  108. Context context) {
  109. ContentLoader loader = new ContentLoader(url, loadListener, context);
  110. loader.load();
  111. }
  112. }