Search in sources :

Example 6 with PluginManager

use of org.apache.cordova.PluginManager in project cordova-android by apache.

the class MessageChannelMultipageTest method testThatCachedCallbackIdIsValid.

//test that after a page load the cached callback id and the live callback id match
//this is to prevent a regression
//the issue was that CordovaWebViewImpl's cached instance of CoreAndroid would become stale on page load
//this is because the cached instance was not being cleared when the pluginManager was reset on page load
//the plugin manager would get a new instance which would be updated with a new callback id
//the cached instance's message channel callback id would become stale
//effectively this caused message channel events to not be delivered
@Test
public void testThatCachedCallbackIdIsValid() throws Throwable {
    final CordovaWebView cordovaWebView = testActivity.getWebInterface();
    Class cordovaWebViewImpl = CordovaWebViewImpl.class;
    //send a test event - this initializes cordovaWebViewImpl.appPlugin (the cached instance of CoreAndroid)
    Method method = cordovaWebViewImpl.getDeclaredMethod("sendJavascriptEvent", String.class);
    method.setAccessible(true);
    method.invoke(cordovaWebView, "testEvent");
    sleep(1000);
    //load a page - this resets the plugin manager and nulls cordovaWebViewImpl.appPlugin
    //(previously this resets plugin manager but did not null cordovaWebViewImpl.appPlugin, leading to the issue)
    mActivityRule.runOnUiThread(new Runnable() {

        public void run() {
            cordovaWebView.loadUrl(START_URL);
        }
    });
    assertEquals(START_URL, testActivity.onPageFinishedUrl.take());
    //send a test event - this initializes cordovaWebViewImpl.appPlugin (the cached instance of CoreAndroid)
    method.invoke(cordovaWebView, "testEvent");
    sleep(1000);
    //get reference to package protected class CoreAndroid
    Class coreAndroid = Class.forName("org.apache.cordova.CoreAndroid");
    //get cached CoreAndroid
    Field appPluginField = cordovaWebViewImpl.getDeclaredField("appPlugin");
    appPluginField.setAccessible(true);
    Object cachedAppPlugin = appPluginField.get(cordovaWebView);
    //get cached CallbackContext
    Field messageChannelField = coreAndroid.getDeclaredField("messageChannel");
    messageChannelField.setAccessible(true);
    CallbackContext cachedCallbackContext = (CallbackContext) messageChannelField.get(cachedAppPlugin);
    //get live CoreAndroid
    PluginManager pluginManager = cordovaWebView.getPluginManager();
    Field coreAndroidPluginNameField = coreAndroid.getField("PLUGIN_NAME");
    String coreAndroidPluginName = (String) coreAndroidPluginNameField.get(null);
    Object liveAppPlugin = pluginManager.getPlugin(coreAndroidPluginName);
    //get live CallbackContext
    CallbackContext liveCallbackContext = (CallbackContext) messageChannelField.get(liveAppPlugin);
    //get callback id from live callbackcontext
    String liveCallbackId = (liveCallbackContext != null) ? liveCallbackContext.getCallbackId() : null;
    //get callback id from cached callbackcontext
    String cachedCallbackId = (cachedCallbackContext != null) ? cachedCallbackContext.getCallbackId() : null;
    //verify that the live message channel has been initialized
    assertNotNull(liveCallbackId);
    //verify that the cached message channel and the live message channel have the same id
    assertEquals(liveCallbackId, cachedCallbackId);
}
Also used : PluginManager(org.apache.cordova.PluginManager) Field(java.lang.reflect.Field) CallbackContext(org.apache.cordova.CallbackContext) CordovaWebView(org.apache.cordova.CordovaWebView) Method(java.lang.reflect.Method) CordovaWebViewImpl(org.apache.cordova.CordovaWebViewImpl) Test(org.junit.Test)

Example 7 with PluginManager

use of org.apache.cordova.PluginManager in project jpHolo by teusink.

the class FileTransfer method download.

/**
 * Downloads a file form a given URL and saves it to the specified directory.
 *
 * @param source        URL of the server to receive the file
 * @param target            Full path of the file on the file system
 */
private void download(final String source, final String target, JSONArray args, CallbackContext callbackContext) throws JSONException {
    Log.d(LOG_TAG, "download " + source + " to " + target);
    final CordovaResourceApi resourceApi = webView.getResourceApi();
    final boolean trustEveryone = args.optBoolean(2);
    final String objectId = args.getString(3);
    final JSONObject headers = args.optJSONObject(4);
    final Uri sourceUri = resourceApi.remapUri(Uri.parse(source));
    // Accept a path or a URI for the source.
    Uri tmpTarget = Uri.parse(target);
    final Uri targetUri = resourceApi.remapUri(tmpTarget.getScheme() != null ? tmpTarget : Uri.fromFile(new File(target)));
    int uriType = CordovaResourceApi.getUriType(sourceUri);
    final boolean useHttps = uriType == CordovaResourceApi.URI_TYPE_HTTPS;
    final boolean isLocalTransfer = !useHttps && uriType != CordovaResourceApi.URI_TYPE_HTTP;
    if (uriType == CordovaResourceApi.URI_TYPE_UNKNOWN) {
        JSONObject error = createFileTransferError(INVALID_URL_ERR, source, target, null, 0, null);
        Log.e(LOG_TAG, "Unsupported URI: " + targetUri);
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, error));
        return;
    }
    // TODO: refactor to also allow resources & content:
    if (!isLocalTransfer && !Config.isUrlWhiteListed(source)) {
        Log.w(LOG_TAG, "Source URL is not in white list: '" + source + "'");
        JSONObject error = createFileTransferError(CONNECTION_ERR, source, target, null, 401, null);
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, error));
        return;
    }
    final RequestContext context = new RequestContext(source, target, callbackContext);
    synchronized (activeRequests) {
        activeRequests.put(objectId, context);
    }
    cordova.getThreadPool().execute(new Runnable() {

        public void run() {
            if (context.aborted) {
                return;
            }
            HttpURLConnection connection = null;
            HostnameVerifier oldHostnameVerifier = null;
            SSLSocketFactory oldSocketFactory = null;
            File file = null;
            PluginResult result = null;
            TrackingInputStream inputStream = null;
            boolean cached = false;
            OutputStream outputStream = null;
            try {
                OpenForReadResult readResult = null;
                file = resourceApi.mapUriToFile(targetUri);
                context.targetFile = file;
                Log.d(LOG_TAG, "Download file:" + sourceUri);
                FileProgressResult progress = new FileProgressResult();
                if (isLocalTransfer) {
                    readResult = resourceApi.openForRead(sourceUri);
                    if (readResult.length != -1) {
                        progress.setLengthComputable(true);
                        progress.setTotal(readResult.length);
                    }
                    inputStream = new SimpleTrackingInputStream(readResult.inputStream);
                } else {
                    // connect to server
                    // Open a HTTP connection to the URL based on protocol
                    connection = resourceApi.createHttpConnection(sourceUri);
                    if (useHttps && trustEveryone) {
                        // Setup the HTTPS connection class to trust everyone
                        HttpsURLConnection https = (HttpsURLConnection) connection;
                        oldSocketFactory = trustAllHosts(https);
                        // Save the current hostnameVerifier
                        oldHostnameVerifier = https.getHostnameVerifier();
                        // Setup the connection not to verify hostnames
                        https.setHostnameVerifier(DO_NOT_VERIFY);
                    }
                    connection.setRequestMethod("GET");
                    // TODO: Make OkHttp use this CookieManager by default.
                    String cookie = CookieManager.getInstance().getCookie(sourceUri.toString());
                    if (cookie != null) {
                        connection.setRequestProperty("cookie", cookie);
                    }
                    // This must be explicitly set for gzip progress tracking to work.
                    connection.setRequestProperty("Accept-Encoding", "gzip");
                    // Handle the other headers
                    if (headers != null) {
                        addHeadersToRequest(connection, headers);
                    }
                    connection.connect();
                    if (connection.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) {
                        cached = true;
                        connection.disconnect();
                        Log.d(LOG_TAG, "Resource not modified: " + source);
                        JSONObject error = createFileTransferError(NOT_MODIFIED_ERR, source, target, connection, null);
                        result = new PluginResult(PluginResult.Status.ERROR, error);
                    } else {
                        if (connection.getContentEncoding() == null || connection.getContentEncoding().equalsIgnoreCase("gzip")) {
                            // the encoding -- identity or gzip
                            if (connection.getContentLength() != -1) {
                                progress.setLengthComputable(true);
                                progress.setTotal(connection.getContentLength());
                            }
                        }
                        inputStream = getInputStream(connection);
                    }
                }
                if (!cached) {
                    try {
                        synchronized (context) {
                            if (context.aborted) {
                                return;
                            }
                            context.connection = connection;
                        }
                        // write bytes to file
                        byte[] buffer = new byte[MAX_BUFFER_SIZE];
                        int bytesRead = 0;
                        outputStream = resourceApi.openOutputStream(targetUri);
                        while ((bytesRead = inputStream.read(buffer)) > 0) {
                            outputStream.write(buffer, 0, bytesRead);
                            // Send a progress event.
                            progress.setLoaded(inputStream.getTotalRawBytesRead());
                            PluginResult progressResult = new PluginResult(PluginResult.Status.OK, progress.toJSONObject());
                            progressResult.setKeepCallback(true);
                            context.sendPluginResult(progressResult);
                        }
                    } finally {
                        synchronized (context) {
                            context.connection = null;
                        }
                        safeClose(inputStream);
                        safeClose(outputStream);
                    }
                    Log.d(LOG_TAG, "Saved file: " + target);
                    // create FileEntry object
                    Class webViewClass = webView.getClass();
                    PluginManager pm = null;
                    try {
                        Method gpm = webViewClass.getMethod("getPluginManager");
                        pm = (PluginManager) gpm.invoke(webView);
                    } catch (NoSuchMethodException e) {
                    } catch (IllegalAccessException e) {
                    } catch (InvocationTargetException e) {
                    }
                    if (pm == null) {
                        try {
                            Field pmf = webViewClass.getField("pluginManager");
                            pm = (PluginManager) pmf.get(webView);
                        } catch (NoSuchFieldException e) {
                        } catch (IllegalAccessException e) {
                        }
                    }
                    file = resourceApi.mapUriToFile(targetUri);
                    context.targetFile = file;
                    FileUtils filePlugin = (FileUtils) pm.getPlugin("File");
                    if (filePlugin != null) {
                        JSONObject fileEntry = filePlugin.getEntryForFile(file);
                        if (fileEntry != null) {
                            result = new PluginResult(PluginResult.Status.OK, fileEntry);
                        } else {
                            JSONObject error = createFileTransferError(CONNECTION_ERR, source, target, connection, null);
                            Log.e(LOG_TAG, "File plugin cannot represent download path");
                            result = new PluginResult(PluginResult.Status.IO_EXCEPTION, error);
                        }
                    } else {
                        Log.e(LOG_TAG, "File plugin not found; cannot save downloaded file");
                        result = new PluginResult(PluginResult.Status.ERROR, "File plugin not found; cannot save downloaded file");
                    }
                }
            } catch (FileNotFoundException e) {
                JSONObject error = createFileTransferError(FILE_NOT_FOUND_ERR, source, target, connection, e);
                Log.e(LOG_TAG, error.toString(), e);
                result = new PluginResult(PluginResult.Status.IO_EXCEPTION, error);
            } catch (IOException e) {
                JSONObject error = createFileTransferError(CONNECTION_ERR, source, target, connection, e);
                Log.e(LOG_TAG, error.toString(), e);
                result = new PluginResult(PluginResult.Status.IO_EXCEPTION, error);
            } catch (JSONException e) {
                Log.e(LOG_TAG, e.getMessage(), e);
                result = new PluginResult(PluginResult.Status.JSON_EXCEPTION);
            } catch (Throwable e) {
                JSONObject error = createFileTransferError(CONNECTION_ERR, source, target, connection, e);
                Log.e(LOG_TAG, error.toString(), e);
                result = new PluginResult(PluginResult.Status.IO_EXCEPTION, error);
            } finally {
                synchronized (activeRequests) {
                    activeRequests.remove(objectId);
                }
                if (connection != null) {
                    // Revert back to the proper verifier and socket factories
                    if (trustEveryone && useHttps) {
                        HttpsURLConnection https = (HttpsURLConnection) connection;
                        https.setHostnameVerifier(oldHostnameVerifier);
                        https.setSSLSocketFactory(oldSocketFactory);
                    }
                }
                if (result == null) {
                    result = new PluginResult(PluginResult.Status.ERROR, createFileTransferError(CONNECTION_ERR, source, target, connection, null));
                }
                // Remove incomplete download.
                if (!cached && result.getStatus() != PluginResult.Status.OK.ordinal() && file != null) {
                    file.delete();
                }
                context.sendPluginResult(result);
            }
        }
    });
}
Also used : PluginResult(org.apache.cordova.PluginResult) FileUtils(org.apache.cordova.file.FileUtils) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileNotFoundException(java.io.FileNotFoundException) Uri(android.net.Uri) PluginManager(org.apache.cordova.PluginManager) Field(java.lang.reflect.Field) HttpURLConnection(java.net.HttpURLConnection) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) JSONException(org.json.JSONException) Method(java.lang.reflect.Method) IOException(java.io.IOException) CordovaResourceApi(org.apache.cordova.CordovaResourceApi) InvocationTargetException(java.lang.reflect.InvocationTargetException) HostnameVerifier(javax.net.ssl.HostnameVerifier) JSONObject(org.json.JSONObject) OpenForReadResult(org.apache.cordova.CordovaResourceApi.OpenForReadResult) File(java.io.File) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Aggregations

PluginManager (org.apache.cordova.PluginManager)7 Method (java.lang.reflect.Method)4 Field (java.lang.reflect.Field)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 File (java.io.File)2 FileUtils (org.apache.cordova.file.FileUtils)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 SuppressLint (android.annotation.SuppressLint)1 TargetApi (android.annotation.TargetApi)1 BroadcastReceiver (android.content.BroadcastReceiver)1 Context (android.content.Context)1 Intent (android.content.Intent)1 IntentFilter (android.content.IntentFilter)1 ApplicationInfo (android.content.pm.ApplicationInfo)1 PackageManager (android.content.pm.PackageManager)1 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)1 Uri (android.net.Uri)1 WebSettings (android.webkit.WebSettings)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1