1/*
2 * Copyright (C) 2007 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
17package android.webkit;
18
19import com.android.internal.R;
20
21import android.content.Context;
22import android.content.res.AssetManager;
23import android.net.http.EventHandler;
24import android.net.http.Headers;
25import android.os.Environment;
26
27import java.io.File;
28import java.io.FileInputStream;
29
30/**
31 * This class is a concrete implementation of StreamLoader that uses a
32 * file or asset as the source for the stream.
33 *
34 */
35class FileLoader extends StreamLoader {
36
37    private String mPath;  // Full path to the file to load
38    private Context mContext;  // Application context, used for asset loads
39    private boolean mIsAsset;  // Indicates if the load is an asset or not
40    private boolean mAllowFileAccess; // Allow/block file system access
41
42    /**
43     * Construct a FileLoader with the file URL specified as the content
44     * source.
45     *
46     * @param url Full file url pointing to content to be loaded
47     * @param loadListener LoadListener to pass the content to
48     * @param context Context to use to access the asset.
49     * @param asset true if url points to an asset.
50     * @param allowFileAccess true if this WebView is allowed to access files
51     *                        on the file system.
52     */
53    FileLoader(String url, LoadListener loadListener, Context context,
54            boolean asset, boolean allowFileAccess) {
55        super(loadListener);
56        mIsAsset = asset;
57        mContext = context;
58        mAllowFileAccess = allowFileAccess;
59
60        // clean the Url
61        int index = url.indexOf('?');
62        if (mIsAsset) {
63            mPath = index > 0 ? URLUtil.stripAnchor(
64                    url.substring(URLUtil.ASSET_BASE.length(), index)) :
65                    URLUtil.stripAnchor(url.substring(
66                            URLUtil.ASSET_BASE.length()));
67        } else {
68            mPath = index > 0 ? URLUtil.stripAnchor(
69                    url.substring(URLUtil.FILE_BASE.length(), index)) :
70                    URLUtil.stripAnchor(url.substring(
71                            URLUtil.FILE_BASE.length()));
72        }
73    }
74
75    private String errString(Exception ex) {
76        String exMessage = ex.getMessage();
77        String errString = mContext.getString(R.string.httpErrorFileNotFound);
78        if (exMessage != null) {
79            errString += " " + exMessage;
80        }
81        return errString;
82    }
83
84    @Override
85    protected boolean setupStreamAndSendStatus() {
86        try {
87            if (mIsAsset) {
88                try {
89                    mDataStream = mContext.getAssets().open(mPath);
90                } catch (java.io.FileNotFoundException ex) {
91                    // try the rest files included in the package
92                    mDataStream = mContext.getAssets().openNonAsset(mPath);
93                }
94            } else {
95                if (!mAllowFileAccess) {
96                    mHandler.error(EventHandler.FILE_ERROR,
97                            mContext.getString(R.string.httpErrorFileNotFound));
98                    return false;
99                }
100
101                mDataStream = new FileInputStream(mPath);
102                mContentLength = (new File(mPath)).length();
103            }
104            mHandler.status(1, 1, 0, "OK");
105
106        } catch (java.io.FileNotFoundException ex) {
107            mHandler.error(EventHandler.FILE_NOT_FOUND_ERROR, errString(ex));
108            return false;
109
110        } catch (java.io.IOException ex) {
111            mHandler.error(EventHandler.FILE_ERROR, errString(ex));
112            return false;
113        }
114        return true;
115    }
116
117    @Override
118    protected void buildHeaders(Headers headers) {
119        // do nothing.
120    }
121
122
123    /**
124     * Construct a FileLoader and instruct it to start loading.
125     *
126     * @param url Full file url pointing to content to be loaded
127     * @param loadListener LoadListener to pass the content to
128     * @param context Context to use to access the asset.
129     * @param asset true if url points to an asset.
130     * @param allowFileAccess true if this FileLoader can load files from the
131     *                        file system.
132     */
133    public static void requestUrl(String url, LoadListener loadListener,
134            Context context, boolean asset, boolean allowFileAccess) {
135        FileLoader loader = new FileLoader(url, loadListener, context, asset,
136                allowFileAccess);
137        loader.load();
138    }
139
140}
141