Index: libadblockplus-android/src/org/adblockplus/libadblockplus/android/SingleInstanceEngineProvider.java |
diff --git a/libadblockplus-android/src/org/adblockplus/libadblockplus/android/SingletonEngineProvider.java b/libadblockplus-android/src/org/adblockplus/libadblockplus/android/SingleInstanceEngineProvider.java |
similarity index 80% |
rename from libadblockplus-android/src/org/adblockplus/libadblockplus/android/SingletonEngineProvider.java |
rename to libadblockplus-android/src/org/adblockplus/libadblockplus/android/SingleInstanceEngineProvider.java |
index a0d0a62e811f7ec06ad2002ac19ac82a305e3a2e..d73e6215ea81eedabe7e474df2ad120e17e36666 100644 |
--- a/libadblockplus-android/src/org/adblockplus/libadblockplus/android/SingletonEngineProvider.java |
+++ b/libadblockplus-android/src/org/adblockplus/libadblockplus/android/SingleInstanceEngineProvider.java |
@@ -28,11 +28,11 @@ import java.util.concurrent.CountDownLatch; |
import java.util.concurrent.atomic.AtomicInteger; |
/** |
- * Provides single instance of AdblockEngine |
+ * Provides single instance of AdblockEngine shared between registered clients |
*/ |
-public class SingletonEngineProvider implements AdblockEngineProvider |
+public class SingleInstanceEngineProvider implements AdblockEngineProvider |
{ |
- private static final String TAG = Utils.getTag(SingletonEngineProvider.class); |
+ private static final String TAG = Utils.getTag(SingleInstanceEngineProvider.class); |
private Context context; |
private String basePath; |
@@ -63,45 +63,39 @@ public class SingletonEngineProvider implements AdblockEngineProvider |
* recommended because it can be cleared by the system. |
* @param developmentBuild debug or release? |
*/ |
- public SingletonEngineProvider(Context context, String basePath, boolean developmentBuild) |
+ public SingleInstanceEngineProvider(Context context, String basePath, boolean developmentBuild) |
{ |
this.context = context.getApplicationContext(); |
this.basePath = basePath; |
this.developmentBuild = developmentBuild; |
} |
- @Override |
- public AdblockEngine getAdblockEngine() |
- { |
- return engine; |
- } |
- |
/** |
* Use preloaded subscriptions |
* @param preferenceName Shared Preferences name to store intercepted requests stats |
* @param urlToResourceIdMap |
*/ |
- public SingletonEngineProvider preloadSubscriptions(String preferenceName, |
- Map<String, Integer> urlToResourceIdMap) |
+ public SingleInstanceEngineProvider preloadSubscriptions(String preferenceName, |
+ Map<String, Integer> urlToResourceIdMap) |
{ |
this.preloadedPreferenceName = preferenceName; |
this.urlToResourceIdMap = urlToResourceIdMap; |
return this; |
} |
- public SingletonEngineProvider useV8IsolateProvider(long ptr) |
+ public SingleInstanceEngineProvider useV8IsolateProvider(long ptr) |
{ |
this.v8IsolateProviderPtr = ptr; |
return this; |
} |
- public SingletonEngineProvider setEngineCreatedCallback(Runnable callback) |
+ public SingleInstanceEngineProvider setEngineCreatedCallback(Runnable callback) |
{ |
this.engineCreatedCallback = callback; |
return this; |
} |
- public SingletonEngineProvider setEngineDisposedCallback(Runnable callback) |
+ public SingleInstanceEngineProvider setEngineDisposedCallback(Runnable callback) |
{ |
this.engineDisposedCallback = callback; |
return this; |
@@ -151,60 +145,7 @@ public class SingletonEngineProvider implements AdblockEngineProvider |
} |
} |
- /** |
- * Wait until everything is ready (used for `retain(true)`) |
- * Warning: locks current thread |
- */ |
- public void waitForReady() |
- { |
- if (engineCreated == null) |
- { |
- throw new RuntimeException("AdblockHelper Plus usage exception: call retain(true) first"); |
- } |
- |
- try |
- { |
- Log.d(TAG, "Waiting for ready ..."); |
- engineCreated.await(); |
- Log.d(TAG, "Ready"); |
- } |
- catch (InterruptedException e) |
- { |
- Log.w(TAG, "Interrupted", e); |
- } |
- } |
- |
- private void disposeAdblock() |
- { |
- Log.w(TAG, "Disposing adblock engine"); |
- |
- engine.dispose(); |
- engine = null; |
- |
- // sometimes we need to deinit something after AdblockEngine instance disposed |
- // eg. release user settings |
- if (engineDisposedCallback != null) |
- { |
- engineDisposedCallback.run(); |
- } |
- } |
- |
- /** |
- * Get registered clients count |
- * @return registered clients count |
- */ |
- public int getCounter() |
- { |
- return referenceCounter.get(); |
- } |
- |
- /** |
- * Register AdblockHelper engine client |
- * @param asynchronous If `true` engines will be created in background thread without locking of |
- * current thread. Use waitForReady() before getAdblockEngine() later. |
- * If `false` locks current thread. |
- * @return if a new instance is allocated |
- */ |
+ @Override |
public synchronized boolean retain(boolean asynchronous) |
{ |
boolean firstInstance = false; |
@@ -238,10 +179,33 @@ public class SingletonEngineProvider implements AdblockEngineProvider |
return firstInstance; |
} |
- /** |
- * Unregister AdblockHelper engine client |
- * @return `true` if the last instance is destroyed |
- */ |
+ @Override |
+ public void waitForReady() |
+ { |
+ if (engineCreated == null) |
+ { |
+ throw new IllegalStateException("Usage exception: call retain(true) first"); |
+ } |
+ |
+ try |
+ { |
+ Log.d(TAG, "Waiting for ready ..."); |
+ engineCreated.await(); |
+ Log.d(TAG, "Ready"); |
+ } |
+ catch (InterruptedException e) |
+ { |
+ Log.w(TAG, "Interrupted", e); |
+ } |
+ } |
+ |
+ @Override |
+ public AdblockEngine getEngine() |
+ { |
+ return engine; |
+ } |
+ |
+ @Override |
public synchronized boolean release() |
{ |
boolean lastInstance = false; |
@@ -267,4 +231,25 @@ public class SingletonEngineProvider implements AdblockEngineProvider |
} |
return lastInstance; |
} |
+ |
+ private void disposeAdblock() |
+ { |
+ Log.w(TAG, "Disposing adblock engine"); |
+ |
+ engine.dispose(); |
+ engine = null; |
+ |
+ // sometimes we need to deinit something after AdblockEngine instance disposed |
+ // eg. release user settings |
+ if (engineDisposedCallback != null) |
+ { |
+ engineDisposedCallback.run(); |
+ } |
+ } |
+ |
+ @Override |
+ public int getCounter() |
+ { |
+ return referenceCounter.get(); |
+ } |
} |