// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium;

import org.apache.cordova.CordovaPlugin;

import android.net.Uri;

public class ChromeExtensionURLs extends CordovaPlugin {

    RequestModifyInterface i18nPlugin;

    static interface RequestModifyInterface
    {
        public Uri remapChromeUri(Uri uri);
    }

    @Override
    public Uri remapUri(Uri uri) {
        // Check the scheme to see if we need to handle.
        // Also ensure we haven't intercepted it before
        //  If this check wasn't present, the content-loaded section would go into an infinite loop of data retrieval attempts
        if (!uri.getScheme().equals("chrome-extension")) {
            return null;
        }
        String filePath = uri.getPath();

        if ("/chrome-content-loaded".equals(filePath)) {
            return Uri.parse("data:text/javascript,Object.defineProperty%28document%2C%20%27readyState%27%2C%20%7Bget%3A%20function%28%29%20%7B%20return%20%27loading%27%7D%2C%20configurable%3A%20true%20%7D%29%3B");
        }

        if (i18nPlugin != null) {
            uri = i18nPlugin.remapChromeUri(uri);
        }
        
        // i18n can return data URIs.
        if (uri.getScheme().equals("chrome-extension")) {
            uri = Uri.parse("file:///android_asset/www" + filePath);
            uri = webView.getResourceApi().remapUri(uri);
        }

        // We need the input stream below for the modifyResponseInputStream. So we load using a separate request.
        return uri;
    }
}

@@ -349,16 +349,44 @@
 nPlugin;
+%0A    private String baseUrl;
 %0A%0A    st
@@ -476,24 +476,947 @@
 ri);%0A    %7D%0A%0A
+    public Uri remapToRealLocation(Uri uri) %7B%0A        // Add this check just to make plugin work with older versions of CCA.%0A        String base = baseUrl == null ? %22file:///android_asset/www%22 : baseUrl;%0A        String filePath = uri.getPath();%0A        uri = Uri.parse(base + filePath);%0A        uri = webView.getResourceApi().remapUri(uri);%0A        return uri;%0A    %7D%0A%0A    // @Override%0A    public Boolean shouldAllowNavigation(String url) %7B%0A        if (baseUrl == null) %7B%0A            baseUrl = url.replaceAll(%22/plugins/.*?$%22, %22%22);%0A        %7D%0A        // Required for iframes.%0A        if (url.startsWith(%22chrome-extension:%22)) %7B%0A            return true;%0A        %7D%0A        return super.shouldAllowNavigation(url);%0A    %7D%0A%0A    // @Override%0A    public Boolean shouldAllowRequest(String url) %7B%0A        if (url.startsWith(%22chrome-extension:%22)) %7B%0A            return true;%0A        %7D%0A        return super.shouldAllowRequest(url);%0A    %7D%0A%0A
     @Overrid
@@ -1789,49 +1789,8 @@
    %7D
-%0A        String filePath = uri.getPath();
 %0A%0A  
@@ -1831,24 +1831,29 @@
 .equals(
-file
+uri.get
 Path
+()
 )) %7B%0A   
@@ -2296,110 +2296,27 @@
 i = 
-Uri.parse(%22file:///android_asset/www%22 + filePath);%0A            uri = webView.getResourceApi().remapUri
+remapToRealLocation
 (uri

Update to work with non-file:///android_asset start pages and fix chrome-extension: iframes