安卓4.0.1打破的WebView HTML 5本地存储?WebView、HTML

2023-09-12 21:45:57 作者:你最珍贵〃你别皱眉

我有一个简单的HTML5测试页面,它使用localStorage的显示/保存/重新显示一个数据。

I have a simple html5 test page which uses LocalStorage to display / save / redisplay a piece of data.

这code完全在Android的2.3.x但的日志的在4.0.1上是弗里斯特的HTML线18个例外 localStorage.getItem( )呼叫,在这一点上JS停止。

This code works perfectly in Android 2.3.x but logs an exception in 4.0.1 on line 18 of the html which is the frist localStorage.getItem() call and at this point the JS stops.

异常:未捕获的错误:SECURITY_ERR:DOM异常18处/data/data/my.app.name/app_htmlData:18 我也试着数据库路径设置为 getCacheDir()具有相同的结果。

Exception: Uncaught Error: SECURITY_ERR: DOM Exception 18 at /data/data/my.app.name/app_htmlData:18 I've also tried setting the database path to getCacheDir() with the same result.

String htmlContent = "HTML content listed below";    
File sharedDir = getActivity().getDir("htmlData", Context.MODE_PRIVATE);
WebView browser = (WebView)v.findViewById(R.id.wvBrowser);

browser.setWebChromeClient(new WebChromeClient(){
    public void onExceededDatabaseQuota(String url, String databaseIdentifier, long  currentQuota, long estimatedSize,   long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) { 
            quotaUpdater.updateQuota(estimatedSize * 2); 
        }
    });       
browser.setWebViewClient(new WebViewClient(){
    @Override
    public void onPageFinished(WebView view, String url){

        view.loadUrl("javascript:doTest()");

    });

browser.getSettings().setDatabaseEnabled(true);
browser.getSettings().setDatabasePath(sharedDir.getPath());
browser.getSettings().setDomStorageEnabled(true);
browser.loadDataWithBaseURL(mSharedDir.getPath(), 
            htmlContent, 
            "text/html", 
            "utf-8", 
            null);

该网页渲染HTML如下:

The HTML that the page is rendering is as follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Simple localStorage test</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script type="text/javascript">

        function doTest() {
            $('#stuff').append('<p>reading</p>');
            var item = read();

            $('#stuff').append('<p>writing</p>');
            localStorage['bar'] = new Date().toUTCString();

            $('#stuff').append('<p>&nbsp;</p><p>reading again</p>');
            read();
        }
        function read() {
            var item = localStorage.getItem('bar');
            if (item == null || (item == undefined)) {
                item = '';
            }
            $('#stuff').append('<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item: ' + item + '</p>');

         return item;
        }
    </script>
</head>
<body>
    <p>-Simple localStorage test-</p>
    <div id="stuff"></div>
</body>
</html>

这里来源提供

Source available here

推荐答案

通过与谷歌进行一些讨论工程师看来,他们已经做出的立案决定://方案是不安全的。

Via some discussion with a Google engineer it seems that they've made the decision that the file:// scheme is insecure.

有一个工作围绕这个是做以下

A work around for this is to do the following

browser.loadDataWithBaseURL("http://www.example.com", 
            htmlContent, 
            "text/html", 
            "utf-8", 
            null);