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

Unified Diff: libadblockplus-android/src/org/adblockplus/libadblockplus/android/SingletonEngineProvider.java

Issue 29671734: Issue 6265 - Create shared AdblockEngine instance in AdblockWebView in background (Closed)
Patch Set: addressed Diego's comments Created Jan. 19, 2018, 12:23 p.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: libadblockplus-android/src/org/adblockplus/libadblockplus/android/SingletonEngineProvider.java
diff --git a/libadblockplus-android/src/org/adblockplus/libadblockplus/android/SingletonEngineProvider.java b/libadblockplus-android/src/org/adblockplus/libadblockplus/android/SingletonEngineProvider.java
deleted file mode 100644
index a0d0a62e811f7ec06ad2002ac19ac82a305e3a2e..0000000000000000000000000000000000000000
--- a/libadblockplus-android/src/org/adblockplus/libadblockplus/android/SingletonEngineProvider.java
+++ /dev/null
@@ -1,270 +0,0 @@
-/*
- * This file is part of Adblock Plus <https://adblockplus.org/>,
- * Copyright (C) 2006-present eyeo GmbH
- *
- * Adblock Plus is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 3 as
- * published by the Free Software Foundation.
- *
- * Adblock Plus is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
- */
-package org.adblockplus.libadblockplus.android;
-
-import org.adblockplus.libadblockplus.IsAllowedConnectionCallback;
-
-import android.content.Context;
-import android.content.SharedPreferences;
-import android.net.ConnectivityManager;
-import android.util.Log;
-
-import java.util.Map;
-import java.util.concurrent.CountDownLatch;
-import java.util.concurrent.atomic.AtomicInteger;
-
-/**
- * Provides single instance of AdblockEngine
- */
-public class SingletonEngineProvider implements AdblockEngineProvider
-{
- private static final String TAG = Utils.getTag(SingletonEngineProvider.class);
-
- private Context context;
- private String basePath;
- private boolean developmentBuild;
- private String preloadedPreferenceName;
- private Map<String, Integer> urlToResourceIdMap;
- private AdblockEngine engine;
- private CountDownLatch engineCreated;
- private Long v8IsolateProviderPtr;
- private Runnable engineCreatedCallback;
- private Runnable engineDisposedCallback;
-
- /*
- Simple ARC management for AdblockEngine
- Use `retain` and `release`
- */
-
- private AtomicInteger referenceCounter = new AtomicInteger(0);
-
- /**
- * Init with context
- * @param context application context
- * @param basePath file system root to store files
- *
- * Adblock Plus library will download subscription files and store them on
- * the path passed. The path should exist and the directory content should not be
- * cleared out occasionally. Using `context.getCacheDir().getAbsolutePath()` is not
- * recommended because it can be cleared by the system.
- * @param developmentBuild debug or release?
- */
- public SingletonEngineProvider(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)
- {
- this.preloadedPreferenceName = preferenceName;
- this.urlToResourceIdMap = urlToResourceIdMap;
- return this;
- }
-
- public SingletonEngineProvider useV8IsolateProvider(long ptr)
- {
- this.v8IsolateProviderPtr = ptr;
- return this;
- }
-
- public SingletonEngineProvider setEngineCreatedCallback(Runnable callback)
- {
- this.engineCreatedCallback = callback;
- return this;
- }
-
- public SingletonEngineProvider setEngineDisposedCallback(Runnable callback)
- {
- this.engineDisposedCallback = callback;
- return this;
- }
-
- private void createAdblock()
- {
- ConnectivityManager connectivityManager =
- (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
- IsAllowedConnectionCallback isAllowedConnectionCallback =
- new IsAllowedConnectionCallbackImpl(connectivityManager);
-
- Log.d(TAG, "Creating adblock engine ...");
-
- AdblockEngine.Builder builder = AdblockEngine
- .builder(
- AdblockEngine.generateAppInfo(context, developmentBuild),
- basePath)
- .setIsAllowedConnectionCallback(isAllowedConnectionCallback)
- .enableElementHiding(true);
-
- if (v8IsolateProviderPtr != null)
- {
- builder.useV8IsolateProvider(v8IsolateProviderPtr);
- }
-
- // if preloaded subscriptions provided
- if (preloadedPreferenceName != null)
- {
- SharedPreferences preloadedSubscriptionsPrefs = context.getSharedPreferences(
- preloadedPreferenceName,
- Context.MODE_PRIVATE);
- builder.preloadSubscriptions(
- context,
- urlToResourceIdMap,
- new AndroidWebRequestResourceWrapper.SharedPrefsStorage(preloadedSubscriptionsPrefs));
- }
-
- engine = builder.build();
-
- Log.d(TAG, "AdblockHelper engine created");
-
- // sometimes we need to init AdblockEngine instance, eg. set user settings
- if (engineCreatedCallback != null)
- {
- engineCreatedCallback.run();
- }
- }
-
- /**
- * 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
- */
- public synchronized boolean retain(boolean asynchronous)
- {
- boolean firstInstance = false;
-
- if (referenceCounter.getAndIncrement() == 0)
- {
- firstInstance = true;
-
- if (!asynchronous)
- {
- createAdblock();
- }
- else
- {
- // latch is required for async (see `waitForReady()`)
- engineCreated = new CountDownLatch(1);
-
- new Thread(new Runnable()
- {
- @Override
- public void run()
- {
- createAdblock();
-
- // unlock waiting client thread
- engineCreated.countDown();
- }
- }).start();
- }
- }
- return firstInstance;
- }
-
- /**
- * Unregister AdblockHelper engine client
- * @return `true` if the last instance is destroyed
- */
- public synchronized boolean release()
- {
- boolean lastInstance = false;
-
- if (referenceCounter.decrementAndGet() == 0)
- {
- lastInstance = true;
-
- if (engineCreated != null)
- {
- // retained asynchronously
- waitForReady();
- disposeAdblock();
-
- // to unlock waiting client in waitForReady()
- engineCreated.countDown();
- engineCreated = null;
- }
- else
- {
- disposeAdblock();
- }
- }
- return lastInstance;
- }
-}

Powered by Google App Engine
This is Rietveld