cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From na...@apache.org
Subject [06/50] git commit: Move plugin instantiation and instance storing logic PluginEntry->PluginManager
Date Thu, 28 Aug 2014 19:03:20 GMT
Move plugin instantiation and instance storing logic PluginEntry->PluginManager

Instantiation and storing of the instance should be owned privately by
PluginManager, not exposed via an unprotected public API. That said,
this refactoring does not make any breaking changes to the public API,
except for removing the createPlugin call in PluginEntry, which should
not be called by anyone other than PluginManager anyway.


Project: http://git-wip-us.apache.org/repos/asf/cordova-amazon-fireos/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-amazon-fireos/commit/38586b57
Tree: http://git-wip-us.apache.org/repos/asf/cordova-amazon-fireos/tree/38586b57
Diff: http://git-wip-us.apache.org/repos/asf/cordova-amazon-fireos/diff/38586b57

Branch: refs/heads/master
Commit: 38586b5730029c81ce1b9423e8d51f448724079f
Parents: 340d658
Author: Andrew Grieve <agrieve@chromium.org>
Authored: Mon Jul 14 14:08:27 2014 -0400
Committer: Archana Naik <naika@lab126.com>
Committed: Wed Jul 30 18:09:11 2014 -0700

----------------------------------------------------------------------
 .../src/org/apache/cordova/PluginEntry.java     |  85 +++----------
 .../src/org/apache/cordova/PluginManager.java   | 124 +++++++++++--------
 2 files changed, 90 insertions(+), 119 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-amazon-fireos/blob/38586b57/framework/src/org/apache/cordova/PluginEntry.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/PluginEntry.java b/framework/src/org/apache/cordova/PluginEntry.java
index 43bc610..e7d4fe5 100755
--- a/framework/src/org/apache/cordova/PluginEntry.java
+++ b/framework/src/org/apache/cordova/PluginEntry.java
@@ -20,11 +20,8 @@ package org.apache.cordova;
 
 import java.util.List;
 
-import org.apache.cordova.CordovaWebView;
-import org.apache.cordova.CordovaInterface;
 import org.apache.cordova.CordovaPlugin;
 
-
 /**
  * This class represents a service entry object.
  */
@@ -33,24 +30,22 @@ public class PluginEntry implements Comparable<PluginEntry> {
     /**
      * The name of the service that this plugin implements
      */
-    public String service = "";
+    public String service;
 
     /**
      * The plugin class name that implements the service.
      */
-    public String pluginClass = "";
+    public String pluginClass;
 
     /**
-     * The plugin object.
-     * Plugin objects are only created when they are called from JavaScript.  (see PluginManager.exec)
-     * The exception is if the onload flag is set, then they are created when PluginManager
is initialized.
+     * The pre-instantiated plugin to use for this entry.
      */
-    public CordovaPlugin plugin = null;
+    public CordovaPlugin plugin;
 
     /**
      * Flag that indicates the plugin object should be created when PluginManager is initialized.
      */
-    public boolean onload = false;
+    public boolean onload;
 
 	/**
 	 * The numerical priority used to determine which plugin takes precendence
@@ -62,13 +57,12 @@ public class PluginEntry implements Comparable<PluginEntry> {
 	public float priority = 0;
     private List<String> urlFilters;
 
+
     /**
-     * @param service               The name of the service
-     * @param plugin                The plugin associated with this entry
+     * Constructs with a CordovaPlugin already instantiated.
      */
     public PluginEntry(String service, CordovaPlugin plugin) {
-        this(service, plugin.getClass().getName(), true, null);
-        this.plugin = plugin;
+        this(service, plugin.getClass().getName(), true, plugin, null);
     }
 
     /**
@@ -82,69 +76,30 @@ public class PluginEntry implements Comparable<PluginEntry> {
     }
     
     public PluginEntry(String service, String pluginClass, boolean onload) {
-        this(service, pluginClass, onload, null);
+        this(service, pluginClass, onload, null, null);
     }
     
-
+    @Deprecated // urlFilters are going away
     public PluginEntry(String service, String pluginClass, boolean onload, List<String>
urlFilters) {
         this.service = service;
         this.pluginClass = pluginClass;
         this.onload = onload;
         this.urlFilters = urlFilters;
         this.priority = 0;
+        
+        plugin = null;
     }
 
-    public List<String> getUrlFilters() {
-        return urlFilters;
-    }
-
-    /**
-     * Create plugin object.
-     * If plugin is already created, then just return it.
-     *
-     * @return                      The plugin object
-     */
-    public CordovaPlugin createPlugin(CordovaWebView webView, CordovaInterface ctx) {
-        if (this.plugin != null) {
-            return this.plugin;
-        }
-        try {
-            Class<?> c = getClassByName(this.pluginClass);
-            if (isCordovaPlugin(c)) {
-                this.plugin = (CordovaPlugin) c.newInstance();
-                this.plugin.privateInitialize(ctx, webView, webView.getPreferences());
-                return plugin;
-            }
-        } catch (Exception e) {
-            e.printStackTrace();
-            System.out.println("Error adding plugin " + this.pluginClass + ".");
-        }
-        return null;
-    }
-
-    /**
-     * Get the class.
-     *
-     * @param clazz
-     * @return a reference to the named class
-     * @throws ClassNotFoundException
-     */
-    private Class<?> getClassByName(final String clazz) throws ClassNotFoundException
{
-        Class<?> c = null;
-        if ((clazz != null) && !("".equals(clazz))) {
-            c = Class.forName(clazz);
-        }
-        return c;
+    private PluginEntry(String service, String pluginClass, boolean onload, CordovaPlugin
plugin, List<String> urlFilters) {
+        this.service = service;
+        this.pluginClass = pluginClass;
+        this.onload = onload;
+        this.urlFilters = urlFilters;
+        this.plugin = plugin;
     }
 
-    /**
-     * Returns whether the given class extends CordovaPlugin.
-     */
-    private boolean isCordovaPlugin(Class<?> c) {
-        if (c != null) {
-            return CordovaPlugin.class.isAssignableFrom(c);
-        }
-        return false;
+    public List<String> getUrlFilters() {
+        return urlFilters;
     }
 
 	/**

http://git-wip-us.apache.org/repos/asf/cordova-amazon-fireos/blob/38586b57/framework/src/org/apache/cordova/PluginManager.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/PluginManager.java b/framework/src/org/apache/cordova/PluginManager.java
index f54e075..04ac200 100755
--- a/framework/src/org/apache/cordova/PluginManager.java
+++ b/framework/src/org/apache/cordova/PluginManager.java
@@ -46,8 +46,9 @@ public class PluginManager {
     private static String TAG = "PluginManager";
     private static final int SLOW_EXEC_WARNING_THRESHOLD = Debug.isDebuggerConnected() ?
60 : 16;
 
-    // List of service entries, sorted by priority
-    private HashMap<String, PluginEntry> entries = new LinkedHashMap<String, PluginEntry>();
+    // List of service entries
+    private final HashMap<String, CordovaPlugin> pluginMap = new HashMap<String,
CordovaPlugin>();
+    private final HashMap<String, PluginEntry> entryMap = new LinkedHashMap<String,
PluginEntry>();
 
     private final CordovaInterface ctx;
     private final CordovaWebView app;
@@ -75,8 +76,7 @@ public class PluginManager {
     public void setPluginEntries(List<PluginEntry> pluginEntries) {
         this.onPause(false);
         this.onDestroy();
-        this.clearPluginObjects();
-        entries.clear();
+        pluginMap.clear();
         urlMap.clear();
         for (PluginEntry entry : pluginEntries) {
             addService(entry);
@@ -90,7 +90,7 @@ public class PluginManager {
         LOG.d(TAG, "init()");
         this.onPause(false);
         this.onDestroy();
-        this.clearPluginObjects();
+        pluginMap.clear();
         this.startupPlugins();
     }
 
@@ -102,18 +102,16 @@ public class PluginManager {
      * Delete all plugin objects.
      */
     public void clearPluginObjects() {
-        for (PluginEntry entry : this.entries.values()) {
-            entry.plugin = null;
-        }
+        pluginMap.clear();
     }
 
     /**
      * Create plugins objects that have onload set.
      */
     public void startupPlugins() {
-        for (PluginEntry entry : this.entries.values()) {
+        for (PluginEntry entry : entryMap.values()) {
             if (entry.onload) {
-                entry.createPlugin(this.app, this.ctx);
+                getPlugin(entry.service);
             }
         }
     }
@@ -179,15 +177,21 @@ public class PluginManager {
      * @return              CordovaPlugin or null
      */
     public CordovaPlugin getPlugin(String service) {
-        PluginEntry entry = this.entries.get(service);
-        if (entry == null) {
-            return null;
-        }
-        CordovaPlugin plugin = entry.plugin;
-        if (plugin == null) {
-            plugin = entry.createPlugin(this.app, this.ctx);
+        CordovaPlugin ret = pluginMap.get(service);
+        if (ret == null) {
+            PluginEntry pe = entryMap.get(service);
+            if (pe == null) {
+                return null;
+            }
+            if (pe.plugin != null) {
+                ret = pe.plugin;
+            } else {
+                ret = instantiatePlugin(pe.pluginClass);
+            }
+            ret.privateInitialize(ctx, app, app.getPreferences());
+            pluginMap.put(service, ret);
         }
-        return plugin;
+        return ret;
     }
 
     /**
@@ -253,11 +257,16 @@ public class PluginManager {
     }
     
     public void addService(PluginEntry entry) {
-        this.entries.put(entry.service, entry);
+        this.entryMap.put(entry.service, entry);
         List<String> urlFilters = entry.getUrlFilters();
         if (urlFilters != null) {
             urlMap.put(entry.service, urlFilters);
         }
+        if (entry.plugin != null) {
+            entry.plugin.privateInitialize(ctx, app, app.getPreferences());
+            pluginMap.put(entry.service, entry.plugin);
+        }
+
     }
 
     /**
@@ -266,10 +275,8 @@ public class PluginManager {
      * @param multitasking      Flag indicating if multitasking is turned on for app
      */
     public void onPause(boolean multitasking) {
-        for (PluginEntry entry : this.entries.values()) {
-            if (entry.plugin != null) {
-                entry.plugin.onPause(multitasking);
-            }
+        for (CordovaPlugin plugin : this.pluginMap.values()) {
+            plugin.onPause(multitasking);
         }
     }
 
@@ -279,10 +286,8 @@ public class PluginManager {
      * @param multitasking      Flag indicating if multitasking is turned on for app
      */
     public void onResume(boolean multitasking) {
-        for (PluginEntry entry : this.entries.values()) {
-            if (entry.plugin != null) {
-                entry.plugin.onResume(multitasking);
-            }
+        for (CordovaPlugin plugin : this.pluginMap.values()) {
+            plugin.onResume(multitasking);
         }
     }
 
@@ -290,10 +295,8 @@ public class PluginManager {
      * The final call you receive before your activity is destroyed.
      */
     public void onDestroy() {
-        for (PluginEntry entry : this.entries.values()) {
-            if (entry.plugin != null) {
-                entry.plugin.onDestroy();
-            }
+        for (CordovaPlugin plugin : this.pluginMap.values()) {
+            plugin.onDestroy();
         }
     }
 
@@ -309,12 +312,10 @@ public class PluginManager {
         if (obj != null) {
             return obj;
         }
-        for (PluginEntry entry : this.entries.values()) {
-            if (entry.plugin != null) {
-                obj = entry.plugin.onMessage(id, data);
-                if (obj != null) {
-                    return obj;
-                }
+        for (CordovaPlugin plugin : this.pluginMap.values()) {
+            obj = plugin.onMessage(id, data);
+            if (obj != null) {
+                return obj;
             }
         }
         return null;
@@ -324,10 +325,8 @@ public class PluginManager {
      * Called when the activity receives a new intent.
      */
     public void onNewIntent(Intent intent) {
-        for (PluginEntry entry : this.entries.values()) {
-            if (entry.plugin != null) {
-                entry.plugin.onNewIntent(intent);
-            }
+        for (CordovaPlugin plugin : this.pluginMap.values()) {
+            plugin.onNewIntent(intent);
         }
     }
 
@@ -342,7 +341,7 @@ public class PluginManager {
         // Instead, plugins should not include <url-filter> and instead ensure
         // that they are loaded before this function is called (either by setting
         // the onload <param> or by making an exec() call to them)
-        for (PluginEntry entry : this.entries.values()) {
+        for (PluginEntry entry : this.entryMap.values()) {
             List<String> urlFilters = urlMap.get(entry.service);
             if (urlFilters != null) {
                 for (String s : urlFilters) {
@@ -350,8 +349,9 @@ public class PluginManager {
                         return getPlugin(entry.service).onOverrideUrlLoading(url);
                     }
                 }
-            } else if (entry.plugin != null) {
-                if (entry.plugin.onOverrideUrlLoading(url)) {
+            } else {
+                CordovaPlugin plugin = pluginMap.get(entry.service);
+                if (plugin != null && plugin.onOverrideUrlLoading(url)) {
                     return true;
                 }
             }
@@ -363,22 +363,38 @@ public class PluginManager {
      * Called when the app navigates or refreshes.
      */
     public void onReset() {
-        for (PluginEntry entry : this.entries.values()) {
-            if (entry.plugin != null) {
-                entry.plugin.onReset();
-            }
+        for (CordovaPlugin plugin : this.pluginMap.values()) {
+            plugin.onReset();
         }
     }
 
     Uri remapUri(Uri uri) {
-        for (PluginEntry entry : this.entries.values()) {
-            if (entry.plugin != null) {
-                Uri ret = entry.plugin.remapUri(uri);
-                if (ret != null) {
-                    return ret;
-                }
+        for (CordovaPlugin plugin : this.pluginMap.values()) {
+            Uri ret = plugin.remapUri(uri);
+            if (ret != null) {
+                return ret;
             }
         }
         return null;
     }
+
+    /**
+     * Create a plugin based on class name.
+     */
+    private CordovaPlugin instantiatePlugin(String className) {
+        CordovaPlugin ret = null;
+        try {
+            Class<?> c = null;
+            if ((className != null) && !("".equals(className))) {
+                c = Class.forName(className);
+            }
+            if (c != null & CordovaPlugin.class.isAssignableFrom(c)) {
+                ret = (CordovaPlugin) c.newInstance();
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+            System.out.println("Error adding plugin " + className + ".");
+        }
+        return ret;
+    }
 }


Mime
View raw message