Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5c33c625 authored by Grace Kloba's avatar Grace Kloba Committed by Android (Google) Code Review
Browse files

Merge "Enable StreamLoader to be loaded in a separate thread."

parents d0e18ffb ac75f566
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -43,7 +43,7 @@ class CacheLoader extends StreamLoader {
    protected boolean setupStreamAndSendStatus() {
        mDataStream = mCacheResult.inStream;
        mContentLength = mCacheResult.contentLength;
        mHandler.status(1, 1, mCacheResult.httpStatusCode, "OK");
        mLoadListener.status(1, 1, mCacheResult.httpStatusCode, "OK");
        return true;
    }

+4 −24
Original line number Diff line number Diff line
@@ -16,14 +16,10 @@

package android.webkit;

import android.content.Context;
import android.net.http.EventHandler;
import android.net.http.Headers;
import android.net.Uri;

import java.io.File;
import java.io.FileInputStream;

/**
 * This class is a concrete implementation of StreamLoader that loads
 * "content:" URIs
@@ -68,7 +64,7 @@ class ContentLoader extends StreamLoader {
    protected boolean setupStreamAndSendStatus() {
        Uri uri = Uri.parse(mUrl);
        if (uri == null) {
            mHandler.error(
            mLoadListener.error(
                    EventHandler.FILE_NOT_FOUND_ERROR,
                    mContext.getString(
                            com.android.internal.R.string.httpErrorBadUrl) +
@@ -78,18 +74,14 @@ class ContentLoader extends StreamLoader {

        try {
            mDataStream = mContext.getContentResolver().openInputStream(uri);
            mHandler.status(1, 1, 200, "OK");
            mLoadListener.status(1, 1, 200, "OK");
        } catch (java.io.FileNotFoundException ex) {
            mHandler.error(EventHandler.FILE_NOT_FOUND_ERROR, errString(ex));
            return false;

        } catch (java.io.IOException ex) {
            mHandler.error(EventHandler.FILE_ERROR, errString(ex));
            mLoadListener.error(EventHandler.FILE_NOT_FOUND_ERROR, errString(ex));
            return false;
        } catch (RuntimeException ex) {
            // readExceptionWithFileNotFoundExceptionFromParcel in DatabaseUtils
            // can throw a serial of RuntimeException. Catch them all here.
            mHandler.error(EventHandler.FILE_ERROR, errString(ex));
            mLoadListener.error(EventHandler.FILE_ERROR, errString(ex));
            return false;
        }
        return true;
@@ -103,16 +95,4 @@ class ContentLoader extends StreamLoader {
        // content can change, we don't want WebKit to cache it
        headers.setCacheControl("no-store, no-cache");
    }

    /**
     * Construct a ContentLoader and instruct it to start loading.
     *
     * @param url "content:" url pointing to content to be loaded
     * @param loadListener LoadListener to pass the content to
     */
    public static void requestUrl(String url, LoadListener loadListener) {
        ContentLoader loader = new ContentLoader(url, loadListener);
        loader.load();
    }

}
+2 −14
Original line number Diff line number Diff line
@@ -62,10 +62,10 @@ class DataLoader extends StreamLoader {
    @Override
    protected boolean setupStreamAndSendStatus() {
        if (mDataStream != null) {
            mHandler.status(1, 1, 200, "OK");
            mLoadListener.status(1, 1, 200, "OK");
            return true;
        } else {
            mHandler.error(EventHandler.ERROR,
            mLoadListener.error(EventHandler.ERROR,
                    mContext.getString(R.string.httpError));
            return false;
        }
@@ -74,16 +74,4 @@ class DataLoader extends StreamLoader {
    @Override
    protected void buildHeaders(android.net.http.Headers h) {
    }

    /**
     * Construct a DataLoader and instruct it to start loading.
     *
     * @param url data: URL string optionally containing a mimetype
     * @param loadListener LoadListener to pass the content to
     */
    public static void requestUrl(String url, LoadListener loadListener) {
        DataLoader loader = new DataLoader(url, loadListener);
        loader.load();
    }

}
+7 −27
Original line number Diff line number Diff line
@@ -18,11 +18,9 @@ package android.webkit;

import com.android.internal.R;

import android.content.Context;
import android.content.res.AssetManager;
import android.net.http.EventHandler;
import android.net.http.Headers;
import android.os.Environment;
import android.util.Log;
import android.util.TypedValue;

@@ -111,7 +109,7 @@ class FileLoader extends StreamLoader {
                // "<package>.R$drawable"
                if (mPath == null || mPath.length() == 0) {
                    Log.e(LOGTAG, "Need a path to resolve the res file");
                    mHandler.error(EventHandler.FILE_ERROR, mContext
                    mLoadListener.error(EventHandler.FILE_ERROR, mContext
                            .getString(R.string.httpErrorFileNotFound));
                    return false;

@@ -120,7 +118,7 @@ class FileLoader extends StreamLoader {
                int dot = mPath.indexOf('.', slash);
                if (slash == -1 || dot == -1) {
                    Log.e(LOGTAG, "Incorrect res path: " + mPath);
                    mHandler.error(EventHandler.FILE_ERROR, mContext
                    mLoadListener.error(EventHandler.FILE_ERROR, mContext
                            .getString(R.string.httpErrorFileNotFound));
                    return false;
                }
@@ -157,13 +155,13 @@ class FileLoader extends StreamLoader {
                    errorMsg = "Caught IllegalAccessException: " + e;
                }
                if (errorMsg != null) {
                    mHandler.error(EventHandler.FILE_ERROR, mContext
                    mLoadListener.error(EventHandler.FILE_ERROR, mContext
                            .getString(R.string.httpErrorFileNotFound));
                    return false;
                }
            } else {
                if (!mAllowFileAccess) {
                    mHandler.error(EventHandler.FILE_ERROR,
                    mLoadListener.error(EventHandler.FILE_ERROR,
                            mContext.getString(R.string.httpErrorFileNotFound));
                    return false;
                }
@@ -171,14 +169,14 @@ class FileLoader extends StreamLoader {
                mDataStream = new FileInputStream(mPath);
                mContentLength = (new File(mPath)).length();
            }
            mHandler.status(1, 1, 200, "OK");
            mLoadListener.status(1, 1, 200, "OK");

        } catch (java.io.FileNotFoundException ex) {
            mHandler.error(EventHandler.FILE_NOT_FOUND_ERROR, errString(ex));
            mLoadListener.error(EventHandler.FILE_NOT_FOUND_ERROR, errString(ex));
            return false;

        } catch (java.io.IOException ex) {
            mHandler.error(EventHandler.FILE_ERROR, errString(ex));
            mLoadListener.error(EventHandler.FILE_ERROR, errString(ex));
            return false;
        }
        return true;
@@ -188,22 +186,4 @@ class FileLoader extends StreamLoader {
    protected void buildHeaders(Headers headers) {
        // do nothing.
    }


    /**
     * Construct a FileLoader and instruct it to start loading.
     *
     * @param url Full file url pointing to content to be loaded
     * @param loadListener LoadListener to pass the content to
     * @param asset true if url points to an asset.
     * @param allowFileAccess true if this FileLoader can load files from the
     *                        file system.
     */
    public static void requestUrl(String url, LoadListener loadListener,
            int type, boolean allowFileAccess) {
        FileLoader loader = new FileLoader(url, loadListener, type,
                allowFileAccess);
        loader.load();
    }

}
+14 −9
Original line number Diff line number Diff line
@@ -141,24 +141,29 @@ class FrameLoader {
            return true;
        }
        if (URLUtil.isAssetUrl(url)) {
            FileLoader.requestUrl(url, loadListener, FileLoader.TYPE_ASSET,
                    true);
            // load asset in a separate thread as it involves IO
            new FileLoader(url, loadListener, FileLoader.TYPE_ASSET, true)
                    .enqueue();
            return true;
        } else if (URLUtil.isResourceUrl(url)) {
            FileLoader.requestUrl(url, loadListener, FileLoader.TYPE_RES,
                    true);
            // load resource in a separate thread as it involves IO
            new FileLoader(url, loadListener, FileLoader.TYPE_RES, true)
                    .enqueue();
            return true;
        } else if (URLUtil.isFileUrl(url)) {
            FileLoader.requestUrl(url, loadListener, FileLoader.TYPE_FILE,
                    settings.getAllowFileAccess());
            // load file in a separate thread as it involves IO
            new FileLoader(url, loadListener, FileLoader.TYPE_FILE, settings
                    .getAllowFileAccess()).enqueue();
            return true;
        } else if (URLUtil.isContentUrl(url)) {
            // Send the raw url to the ContentLoader because it will do a
            // permission check and the url has to match..
            ContentLoader.requestUrl(loadListener.url(), loadListener);
            // permission check and the url has to match.
            // load content in a separate thread as it involves IO
            new ContentLoader(loadListener.url(), loadListener).enqueue();
            return true;
        } else if (URLUtil.isDataUrl(url)) {
            DataLoader.requestUrl(url, loadListener);
            // load data in the current thread to reduce the latency
            new DataLoader(url, loadListener).load();
            return true;
        } else if (URLUtil.isAboutUrl(url)) {
            loadListener.data(mAboutBlank.getBytes(), mAboutBlank.length());
Loading