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

Delta Between Two Patch Sets: adblockplussbrowser/src/main/java/org/adblockplus/sbrowser/contentblocker/engine/EngineManager.java

Issue 29716681: Issue 6454 - IllegalStateException crash (Closed)
Left Patch Set: Created March 7, 2018, 8:33 p.m.
Right Patch Set: Adjusting engine check Created March 9, 2018, 11:42 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 package org.adblockplus.sbrowser.contentblocker.engine; 18 package org.adblockplus.sbrowser.contentblocker.engine;
19 19
20 import android.content.Context; 20 import android.content.Context;
21 import android.os.AsyncTask; 21 import android.os.AsyncTask;
22 22
23 import java.io.IOException; 23 import java.io.IOException;
24 import java.lang.ref.WeakReference; 24 import java.lang.ref.WeakReference;
25 import java.util.ArrayList; 25 import java.util.ArrayList;
26 import java.util.Iterator; 26 import java.util.Iterator;
27 import java.util.List; 27 import java.util.List;
28 28
jens 2018/03/08 09:18:40 I didn't want to leave a lot of single comments, b
diegocarloslima 2018/03/08 11:54:34 Nope, the reason is just that I don't usually mark
jens 2018/03/08 12:33:54 Acknowledged.
29 public class EngineManager 29 public class EngineManager
30 { 30 {
31 private static final EngineManager INSTANCE = new EngineManager(); 31 private static final EngineManager INSTANCE = new EngineManager();
32 32
33 private Engine engine; 33 private Engine engine;
34 private CreateEngineAsyncTask createEngineTask; 34 private CreateEngineAsyncTask createEngineTask;
35 private final List<WeakReference<OnEngineCreatedCallback>> engineCreatedCallba cks = new ArrayList<>(); 35 private final List<WeakReference<OnEngineCreatedCallback>> engineCreatedCallba cks = new ArrayList<>();
36 36
37 private EngineManager() 37 private EngineManager()
38 { 38 {
39 } 39 }
40 40
41 public static EngineManager getInstance() 41 public static EngineManager getInstance()
42 { 42 {
43 return INSTANCE; 43 return INSTANCE;
44 } 44 }
45 45
46 public void retrieveEngine(Context context, OnEngineCreatedCallback callback) 46 public void retrieveEngine(final Context context, final OnEngineCreatedCallbac k callback)
47 { 47 {
48 synchronized (engineCreatedCallbacks) 48 synchronized (engineCreatedCallbacks)
49 { 49 {
50 if (callback != null) 50 if (callback != null)
51 { 51 {
52 engineCreatedCallbacks.add(new WeakReference<>(callback)); 52 engineCreatedCallbacks.add(new WeakReference<>(callback));
53 } 53 }
54 } 54 }
55 if (engine != null) 55 if (engine != null)
56 { 56 {
57 notifyEngineCreated(); 57 notifyEngineCreated();
58 } 58 }
59 else if (createEngineTask == null || createEngineTask.isCancelled()) 59 else if (createEngineTask == null || createEngineTask.isCancelled())
60 { 60 {
61 this.createEngineTask = new CreateEngineAsyncTask(); 61 this.createEngineTask = new CreateEngineAsyncTask();
62 this.createEngineTask.execute(context); 62 this.createEngineTask.execute(context);
63 } 63 }
64 } 64 }
65 65
66 public void removeEngineCreatedCallback(OnEngineCreatedCallback callback) 66 public void removeOnEngineCreatedCallback(final OnEngineCreatedCallback callba ck)
67 { 67 {
68 if (callback != null) 68 if (callback != null)
69 { 69 {
70 synchronized (engineCreatedCallbacks) 70 synchronized (engineCreatedCallbacks)
71 { 71 {
72 final Iterator<WeakReference<OnEngineCreatedCallback>> iterator = engine CreatedCallbacks.iterator(); 72 final Iterator<WeakReference<OnEngineCreatedCallback>> iterator = engine CreatedCallbacks.iterator();
73 while (iterator.hasNext()) 73 while (iterator.hasNext())
74 { 74 {
75 final OnEngineCreatedCallback cb = iterator.next().get(); 75 final OnEngineCreatedCallback cb = iterator.next().get();
76 if (callback.equals(cb)) 76 if (callback.equals(cb))
77 { 77 {
78 iterator.remove(); 78 iterator.remove();
79 } 79 }
80 } 80 }
81 } 81 }
82 } 82 }
83 } 83 }
84 84
85 private void setEngineAndNotify(Engine engine) 85 private void setEngineAndNotify(final Engine engine)
86 { 86 {
87 this.createEngineTask = null; 87 this.createEngineTask = null;
88 this.engine = engine; 88 this.engine = engine;
89 notifyEngineCreated(); 89 notifyEngineCreated();
90 } 90 }
91 91
92 private void notifyEngineCreated() 92 private void notifyEngineCreated()
93 { 93 {
94 synchronized (engineCreatedCallbacks) 94 synchronized (engineCreatedCallbacks)
95 { 95 {
(...skipping 18 matching lines...) Expand all
114 } 114 }
115 115
116 public interface OnEngineCreatedCallback 116 public interface OnEngineCreatedCallback
117 { 117 {
118 void onEngineCreated(Engine engine); 118 void onEngineCreated(Engine engine);
119 } 119 }
120 120
121 private static final class CreateEngineAsyncTask extends AsyncTask<Context, Vo id, Engine> 121 private static final class CreateEngineAsyncTask extends AsyncTask<Context, Vo id, Engine>
122 { 122 {
123 @Override 123 @Override
124 protected Engine doInBackground(Context... context) 124 protected Engine doInBackground(final Context... context)
125 { 125 {
126 try 126 try
127 { 127 {
128 return Engine.create(context[0].getApplicationContext()); 128 return Engine.create(context[0].getApplicationContext());
129 } 129 }
130 catch (IOException e) 130 catch (IOException e)
131 { 131 {
132 } 132 }
133 return null; 133 return null;
134 } 134 }
135 135
136 @Override 136 @Override
137 protected void onPostExecute(Engine engine) 137 protected void onPostExecute(final Engine engine)
138 { 138 {
139 EngineManager.getInstance().setEngineAndNotify(engine); 139 EngineManager.getInstance().setEngineAndNotify(engine);
140 } 140 }
141 } 141 }
142 } 142 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld