Revert "DataRequest code cleaned up."

This reverts commit a001d8cfb7.
Reverting all DataResource changes for the 2.8.0 release.

Conflicts:

	framework/src/org/apache/cordova/IceCreamCordovaWebViewClient.java
This commit is contained in:
Andrew Grieve 2013-05-27 22:22:14 -04:00 committed by Joe Bowser
parent 2f9c512b59
commit e3989bcc2b
8 changed files with 55 additions and 45 deletions

View File

@ -342,7 +342,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
// Add compressed version of captured image to returned media store Uri // Add compressed version of captured image to returned media store Uri
DataResource dataResource = DataResource.initiateNewDataRequestForUri(uri, webView.pluginManager, cordova, "CameraLauncher.CameraExitIntent"); DataResource dataResource = DataResource.initiateNewDataRequestForUri(uri, webView.pluginManager, cordova, "CameraLauncher.CameraExitIntent");
OutputStream os = dataResource.getOutputStream(); OutputStream os = dataResource.getOs();
bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os); bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os);
os.close(); os.close();
@ -540,9 +540,9 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
private void writeUncompressedImage(Uri uri) throws FileNotFoundException, private void writeUncompressedImage(Uri uri) throws FileNotFoundException,
IOException { IOException {
DataResource inputDataResource = DataResource.initiateNewDataRequestForUri(imageUri, webView.pluginManager, cordova, "CameraLauncher.writeUncompressedImage"); DataResource inputDataResource = DataResource.initiateNewDataRequestForUri(imageUri, webView.pluginManager, cordova, "CameraLauncher.writeUncompressedImage");
InputStream fis = inputDataResource.getInputStream(); InputStream fis = inputDataResource.getIs();
DataResource outDataResource = DataResource.initiateNewDataRequestForUri(uri, webView.pluginManager, cordova, "CameraLauncher.writeUncompressedImage"); DataResource outDataResource = DataResource.initiateNewDataRequestForUri(uri, webView.pluginManager, cordova, "CameraLauncher.writeUncompressedImage");
OutputStream os = outDataResource.getOutputStream(); OutputStream os = outDataResource.getOs();
if(fis == null) { if(fis == null) {
throw new FileNotFoundException("Could not get the input file"); throw new FileNotFoundException("Could not get the input file");
} else if(os == null) { } else if(os == null) {
@ -592,13 +592,13 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
// If no new width or height were specified return the original bitmap // If no new width or height were specified return the original bitmap
DataResource dataResource = DataResource.initiateNewDataRequestForUri(imageUrl, webView.pluginManager, cordova, "CameraLauncher.getScaledBitmap"); DataResource dataResource = DataResource.initiateNewDataRequestForUri(imageUrl, webView.pluginManager, cordova, "CameraLauncher.getScaledBitmap");
if (this.targetWidth <= 0 && this.targetHeight <= 0) { if (this.targetWidth <= 0 && this.targetHeight <= 0) {
return BitmapFactory.decodeStream(dataResource.getInputStream()); return BitmapFactory.decodeStream(dataResource.getIs());
} }
// figure out the original width and height of the image // figure out the original width and height of the image
BitmapFactory.Options options = new BitmapFactory.Options(); BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(dataResource.getInputStream(), null, options); BitmapFactory.decodeStream(dataResource.getIs(), null, options);
//CB-2292: WTF? Why is the width null? //CB-2292: WTF? Why is the width null?
if(options.outWidth == 0 || options.outHeight == 0) if(options.outWidth == 0 || options.outHeight == 0)
@ -612,7 +612,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
// Load in the smallest bitmap possible that is closest to the size we want // Load in the smallest bitmap possible that is closest to the size we want
options.inJustDecodeBounds = false; options.inJustDecodeBounds = false;
options.inSampleSize = calculateSampleSize(options.outWidth, options.outHeight, this.targetWidth, this.targetHeight); options.inSampleSize = calculateSampleSize(options.outWidth, options.outHeight, this.targetWidth, this.targetHeight);
Bitmap unscaledBitmap = BitmapFactory.decodeStream(dataResource.getInputStream(), null, options); Bitmap unscaledBitmap = BitmapFactory.decodeStream(dataResource.getIs(), null, options);
if (unscaledBitmap == null) { if (unscaledBitmap == null) {
return null; return null;
} }

View File

@ -130,27 +130,33 @@ public class FileHelper {
*/ */
public static boolean isUriWritable(String uriString) { public static boolean isUriWritable(String uriString) {
String scheme = uriString.split(":")[0]; String scheme = uriString.split(":")[0];
String writableSchemes[] = new String[]{ "content" };
if(scheme.equals("file")){ if(scheme.equals("file")){
// special case file // special case file
return !uriString.startsWith("file:///android_asset/"); if(uriString.startsWith("file:///android_asset/")){
return false;
} else {
return true;
}
} }
return "content".equals(scheme); for(int i = writableSchemes.length - 1; i >= 0 ; i--){
if(writableSchemes[i].equals(scheme)){
return true;
}
}
return false;
} }
/** /**
* Ensures the "file://" prefix exists for the given string * Ensures the "file://" prefix exists for the given string
* If the given URI string already has a scheme, it is returned unchanged * If the given URI string has a "file://" prefix, it is returned unchanged
* *
* @param path - the path string to operate on * @param path - the path string to operate on
* @return a String with the "file://" scheme set * @return a String with the "file://" scheme set
*/ */
public static String insertFileProtocol(String path) { public static String insertFileProtocol(String path) {
if(!path.matches("^[a-z0-9+.-]+:.*")){ if(!path.matches("^[a-z0-9+.-]+:.*")){
//Ensure it is not a relative path
if(!path.startsWith("/")){
throw new IllegalArgumentException("Relative paths" + path + "are not allowed.");
}
path = "file://" + path; path = "file://" + path;
} }
return path; return path;

View File

@ -897,7 +897,7 @@ public class FileUtils extends CordovaPlugin {
public void run() { public void run() {
try { try {
DataResource dataResource = DataResource.initiateNewDataRequestForUri(filename, webView.pluginManager, cordova, "FileUtils.readFileAs"); DataResource dataResource = DataResource.initiateNewDataRequestForUri(filename, webView.pluginManager, cordova, "FileUtils.readFileAs");
byte[] bytes = readAsBinaryHelper(dataResource.getInputStream(), start, end); byte[] bytes = readAsBinaryHelper(dataResource.getIs(), start, end);
PluginResult result; PluginResult result;
switch (resultType) { switch (resultType) {

View File

@ -46,13 +46,16 @@ public class IceCreamCordovaWebViewClient extends CordovaWebViewClient {
public WebResourceResponse shouldInterceptRequest(WebView view, String url) { public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
// We need to support the new DataResource intercepts without breaking the shouldInterceptRequest mechanism. // We need to support the new DataResource intercepts without breaking the shouldInterceptRequest mechanism.
DataResource dataResource = DataResource.initiateNewDataRequestForUri(url, this.appView.pluginManager, cordova, DataResource dataResource = DataResource.initiateNewDataRequestForUri(url, this.appView.pluginManager, cordova,
"WebViewClient.shouldInterceptRequest"); new DataResourceContext("WebViewClient.shouldInterceptRequest", true /* this is from a browser request*/));
url = dataResource.getUri().toString(); url = dataResource.getUri().toString();
// This mechanism is no longer needed due to the dataResource mechanism. It would be awesome to just get rid of it. // This mechanism is no longer needed due to the dataResource mechanism. It would be awesome to just get rid of it.
//Check if plugins intercept the request //Check if plugins intercept the request
WebResourceResponse ret = super.shouldInterceptRequest(view, url); WebResourceResponse ret = super.shouldInterceptRequest(view, url);
// The below bugfix is taken care of by the dataResource mechanism
// if(ret == null && (url.contains("?") || url.contains("#") || needsIceCreamSpaceInAssetUrlFix(url))){
// ret = generateWebResourceResponse(url);
// }
if(ret == null) { if(ret == null) {
try { try {
ret = new WebResourceResponse(dataResource.getMimeType(), "UTF-8", dataResource.getInputStream()); ret = new WebResourceResponse(dataResource.getMimeType(), "UTF-8", dataResource.getInputStream());

View File

@ -182,10 +182,10 @@ public class CordovaPlugin {
* *
* @param dataResource The resource to be loaded. * @param dataResource The resource to be loaded.
* @param dataResourceContext Context associated with the resource load * @param dataResourceContext Context associated with the resource load
* @return Return a new DataResource if the plugin wants to assist in loading the request or null if it doesn't. * @return Return a new DataResource if the plugin wants o assist in loading the request or null if it doesn't.
*/ */
@TargetApi(Build.VERSION_CODES.HONEYCOMB) @TargetApi(Build.VERSION_CODES.HONEYCOMB)
public DataResource handleDataResourceRequest(DataResource dataResource, DataResourceContext dataResourceContext) { public DataResource shouldInterceptDataResourceRequest(DataResource dataResource, DataResourceContext dataResourceContext) {
return null; return null;
} }

View File

@ -42,13 +42,10 @@ public class DataResource {
private String mimeType; private String mimeType;
private Boolean writable; private Boolean writable;
private File realFile; private File realFile;
private boolean retryIsLoad = true; private boolean retryLoad = true;
private boolean retryOsLoad = true;
private boolean retryMimeTypeLoad = true;
private boolean retryWritableLoad = true;
private boolean retryRealFileLoad = true;
public DataResource(CordovaInterface cordova, Uri uri) { public DataResource(CordovaInterface cordova, Uri uri) {
super();
this.cordova = cordova; this.cordova = cordova;
this.uri = uri; this.uri = uri;
} }
@ -64,61 +61,61 @@ public class DataResource {
// Uri is always provided // Uri is always provided
return uri; return uri;
} }
public InputStream getInputStream() throws IOException { public InputStream getIs() throws IOException {
if(is == null && retryIsLoad) { if(is == null && retryLoad) {
try { try {
is = FileHelper.getInputStreamFromUriString(uri.toString(), cordova); is = FileHelper.getInputStreamFromUriString(uri.toString(), cordova);
} finally { } finally {
// We failed loading once, don't try loading anymore // We failed loading once, don't try loading anymore
if(is == null) { if(is == null) {
retryIsLoad = false; retryLoad = false;
} }
} }
} }
return is; return is;
} }
public OutputStream getOutputStream() throws FileNotFoundException { public OutputStream getOs() throws FileNotFoundException {
if(os == null && retryOsLoad) { if(os == null && retryLoad) {
try { try {
os = FileHelper.getOutputStreamFromUriString(uri.toString(), cordova); os = FileHelper.getOutputStreamFromUriString(uri.toString(), cordova);
} finally { } finally {
// We failed loading once, don't try loading anymore // We failed loading once, don't try loading anymore
if(os == null) { if(os == null) {
retryOsLoad = false; retryLoad = false;
} }
} }
} }
return os; return os;
} }
public String getMimeType() { public String getMimeType() {
if(mimeType == null && retryMimeTypeLoad) { if(mimeType == null && retryLoad) {
try { try {
mimeType = FileHelper.getMimeType(uri.toString(), cordova); mimeType = FileHelper.getMimeType(uri.toString(), cordova);
} finally { } finally {
// We failed loading once, don't try loading anymore // We failed loading once, don't try loading anymore
if(mimeType == null) { if(mimeType == null) {
retryMimeTypeLoad = false; retryLoad = false;
} }
} }
} }
return mimeType; return mimeType;
} }
public boolean isWritable() { public boolean isWritable() {
if(writable == null && retryWritableLoad) { if(writable == null && retryLoad) {
try { try {
writable = FileHelper.isUriWritable(uri.toString()); writable = FileHelper.isUriWritable(uri.toString());
} finally { } finally {
// We failed loading once, don't try loading anymore // We failed loading once, don't try loading anymore
if(writable == null) { if(writable == null) {
retryWritableLoad = false; retryLoad = false;
} }
} }
} }
// default to false // default to false
return writable != null && writable.booleanValue(); return writable != null? writable.booleanValue() : false;
} }
public File getRealFile() { public File getRealFile() {
if(realFile == null && retryRealFileLoad) { if(realFile == null && retryLoad) {
try { try {
String realPath = FileHelper.getRealPath(uri, cordova); String realPath = FileHelper.getRealPath(uri, cordova);
if(realPath != null) { if(realPath != null) {
@ -127,7 +124,7 @@ public class DataResource {
} finally { } finally {
// We failed loading once, don't try loading anymore // We failed loading once, don't try loading anymore
if(realFile == null) { if(realFile == null) {
retryRealFileLoad = false; retryLoad = false;
} }
} }
} }
@ -141,7 +138,7 @@ public class DataResource {
return initiateNewDataRequestForUri(Uri.parse(uriString), pluginManager, cordova, requestSourceTag); return initiateNewDataRequestForUri(Uri.parse(uriString), pluginManager, cordova, requestSourceTag);
} }
public static DataResource initiateNewDataRequestForUri(Uri uri, PluginManager pluginManager, CordovaInterface cordova, String requestSourceTag){ public static DataResource initiateNewDataRequestForUri(Uri uri, PluginManager pluginManager, CordovaInterface cordova, String requestSourceTag){
return initiateNewDataRequestForUri(uri, pluginManager, cordova, new DataResourceContext(requestSourceTag)); return initiateNewDataRequestForUri(uri, pluginManager, cordova, new DataResourceContext(requestSourceTag, false /* Assume, not a browser request by default */ ));
} }
public static DataResource initiateNewDataRequestForUri(String uriString, PluginManager pluginManager, CordovaInterface cordova, DataResourceContext dataResourceContext){ public static DataResource initiateNewDataRequestForUri(String uriString, PluginManager pluginManager, CordovaInterface cordova, DataResourceContext dataResourceContext){
// if no protocol is specified, assume its file: // if no protocol is specified, assume its file:
@ -152,7 +149,7 @@ public class DataResource {
DataResource dataResource = new DataResource(cordova, uri); DataResource dataResource = new DataResource(cordova, uri);
if (pluginManager != null) { if (pluginManager != null) {
// get the resource as returned by plugins // get the resource as returned by plugins
dataResource = pluginManager.handleDataResourceRequestWithPlugins(dataResource, dataResourceContext); dataResource = pluginManager.shouldInterceptDataResourceRequest(dataResource, dataResourceContext);
} }
return dataResource; return dataResource;
} }

View File

@ -30,11 +30,15 @@ public class DataResourceContext {
// A tag associated with the source of this dataResourceContext // A tag associated with the source of this dataResourceContext
private String source; private String source;
// If needed, any data associated with core plugins can be a part of the context object // If needed, any data associated with core plugins can be a part of the context object
// This field indicates whether the request came from a browser network request
private boolean isFromBrowser;
// If needed, any data associated with non core plugins should store data in a Map so as to not clutter the context object // If needed, any data associated with non core plugins should store data in a Map so as to not clutter the context object
private Map<String, Object> dataMap; private Map<String, Object> dataMap;
public DataResourceContext(String source) { public DataResourceContext(String source, boolean isFromBrowser) {
super();
this.requestId = new Random().nextInt(); this.requestId = new Random().nextInt();
this.source = source; this.source = source;
this.isFromBrowser = isFromBrowser;
this.dataMap = new HashMap<String, Object>(); this.dataMap = new HashMap<String, Object>();
} }
public int getRequestId() { public int getRequestId() {
@ -43,6 +47,9 @@ public class DataResourceContext {
public String getSource() { public String getSource() {
return source; return source;
} }
public boolean isFromBrowser() {
return isFromBrowser;
}
public Map<String, Object> getDataMap() { public Map<String, Object> getDataMap() {
return dataMap; return dataMap;
} }

View File

@ -54,7 +54,6 @@ public class PluginManager {
// Map URL schemes like foo: to plugins that want to handle those schemes // Map URL schemes like foo: to plugins that want to handle those schemes
// This would allow how all URLs are handled to be offloaded to a plugin // This would allow how all URLs are handled to be offloaded to a plugin
protected HashMap<String, String> urlMap = new HashMap<String, String>(); protected HashMap<String, String> urlMap = new HashMap<String, String>();
private int MAX_REPITIONS = 1000;
/** /**
* Constructor. * Constructor.
@ -410,15 +409,13 @@ public class PluginManager {
* @param dataResourceContext The context of the dataResource request * @param dataResourceContext The context of the dataResource request
* @return Return the resource request that will be loaded. The returned request may be modified or unchanged. * @return Return the resource request that will be loaded. The returned request may be modified or unchanged.
*/ */
public DataResource handleDataResourceRequestWithPlugins(DataResource dataResource, DataResourceContext dataResourceContext){ public DataResource shouldInterceptDataResourceRequest(DataResource dataResource, DataResourceContext dataResourceContext){
int repetitions = 0;
boolean requestModified = true; boolean requestModified = true;
while(requestModified && repetitions < MAX_REPITIONS) { while(requestModified) {
requestModified = false; requestModified = false;
repetitions ++;
for (PluginEntry entry : this.entries.values()) { for (PluginEntry entry : this.entries.values()) {
if (entry.plugin != null) { if (entry.plugin != null) {
DataResource ret = entry.plugin.handleDataResourceRequest(dataResource, dataResourceContext); DataResource ret = entry.plugin.shouldInterceptDataResourceRequest(dataResource, dataResourceContext);
if(ret != null) { if(ret != null) {
dataResource = ret; dataResource = ret;
requestModified = true; requestModified = true;