Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: src/org/adblockplus/sbrowser/contentblocker/engine/EngineService.java

Issue 29341398: Issue 4041 - Missing engine startup synchronization causing 'blocker not available' message (Closed)
Patch Set: Created May 13, 2016, 10:42 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/org/adblockplus/sbrowser/contentblocker/engine/EngineService.java
diff --git a/src/org/adblockplus/sbrowser/contentblocker/engine/EngineService.java b/src/org/adblockplus/sbrowser/contentblocker/engine/EngineService.java
index df8740283a9213e63416e1dee584265e6b08f0c9..845474948fc1285a25f65e60d6d8ceb1e1bb0080 100644
--- a/src/org/adblockplus/sbrowser/contentblocker/engine/EngineService.java
+++ b/src/org/adblockplus/sbrowser/contentblocker/engine/EngineService.java
@@ -32,8 +32,8 @@ public final class EngineService extends Service
private volatile Engine engine = null;
private volatile boolean isInitialized = false;
private Throwable failureCause = null;
- private static final LinkedBlockingQueue<OnEngineCreatedCallback> ON_CREATED_CALLBACKS =
- new LinkedBlockingQueue<EngineService.OnEngineCreatedCallback>();
+ private static final LinkedBlockingQueue<EngineCreatedCallbackWrapper> ON_CREATED_CALLBACKS =
+ new LinkedBlockingQueue<EngineCreatedCallbackWrapper>();
@Override
public int onStartCommand(Intent intent, int flags, int startId)
@@ -49,8 +49,21 @@ public final class EngineService extends Service
*/
public static void startService(final Context context, final OnEngineCreatedCallback callback)
{
+ startService(context, callback, true);
+ }
+
+ /**
+ *
+ * @param context
+ * @param callback
+ * @param runOnUiThread
+ * {@code true} if the callback should be executed on the UI thread
+ */
+ public static void startService(final Context context, final OnEngineCreatedCallback callback,
+ final boolean runOnUiThread)
+ {
context.startService(new Intent(context, EngineService.class));
- ON_CREATED_CALLBACKS.offer(callback);
+ ON_CREATED_CALLBACKS.offer(new EngineCreatedCallbackWrapper(callback, runOnUiThread));
}
@Override
@@ -67,11 +80,6 @@ public final class EngineService extends Service
return null;
}
- public interface OnEngineCreatedCallback
- {
- public void onEngineCreated(Engine engine, boolean success);
- }
-
private static void startDaemonThread(final Runnable runnable)
{
final Thread t = new Thread(runnable);
@@ -129,19 +137,28 @@ public final class EngineService extends Service
for (;;)
{
- final OnEngineCreatedCallback callback = EngineService.ON_CREATED_CALLBACKS.take();
- if (callback != null)
+ final EngineCreatedCallbackWrapper wrapper = EngineService.ON_CREATED_CALLBACKS.take();
+ if (wrapper != null)
{
- Engine.runOnUiThread(new Runnable()
+ if (wrapper.runOnUiThread)
{
- private final EngineService service = CreationNotifier.this.service;
-
- @Override
- public void run()
+ Engine.runOnUiThread(new Runnable()
{
- callback.onEngineCreated(this.service.engine, this.service.failureCause == null);
- }
- });
+ private final EngineService service = CreationNotifier.this.service;
+
+ @Override
+ public void run()
+ {
+ wrapper.callback.onEngineCreated(this.service.engine,
+ this.service.failureCause == null);
+ }
+ });
+ }
+ else
+ {
+ wrapper.callback.onEngineCreated(this.service.engine,
+ this.service.failureCause == null);
+ }
}
}
}
@@ -151,4 +168,22 @@ public final class EngineService extends Service
}
}
}
+
+ public interface OnEngineCreatedCallback
+ {
+ public void onEngineCreated(Engine engine, boolean success);
+ }
+
+ private static class EngineCreatedCallbackWrapper
+ {
+ final OnEngineCreatedCallback callback;
+ final boolean runOnUiThread;
+
+ public EngineCreatedCallbackWrapper(final OnEngineCreatedCallback callback,
+ final boolean runOnUiThread)
+ {
+ this.callback = callback;
+ this.runOnUiThread = runOnUiThread;
+ }
+ }
}

Powered by Google App Engine
This is Rietveld