9
9
import android .content .Context ;
10
10
import android .content .SharedPreferences ;
11
11
import android .graphics .Bitmap ;
12
- import android .graphics . SurfaceTexture ;
12
+ import android .os . Handler ;
13
13
import android .preference .PreferenceManager ;
14
14
import android .util .Log ;
15
15
import android .view .Surface ;
47
47
import java .net .URI ;
48
48
import java .net .URISyntaxException ;
49
49
import java .util .LinkedList ;
50
+ import java .util .Objects ;
50
51
import java .util .concurrent .atomic .AtomicBoolean ;
51
52
import java .util .concurrent .atomic .AtomicInteger ;
52
53
54
+ import static java .util .Objects .requireNonNull ;
53
55
import static org .mozilla .vrbrowser .utils .ServoUtils .createServoSession ;
54
56
import static org .mozilla .vrbrowser .utils .ServoUtils .isInstanceOfServoSession ;
55
57
import static org .mozilla .vrbrowser .utils .ServoUtils .isServoAvailable ;
@@ -72,6 +74,7 @@ public class Session implements ContentBlocking.Delegate, GeckoSession.Navigatio
72
74
private transient UserAgentOverride mUserAgentOverride ;
73
75
74
76
private SessionState mState ;
77
+ private LinkedList <Runnable > mQueuedCalls = new LinkedList <>();
75
78
private transient GeckoSession .PermissionDelegate mPermissionDelegate ;
76
79
private transient GeckoSession .PromptDelegate mPromptDelegate ;
77
80
private transient GeckoSession .HistoryDelegate mHistoryDelegate ;
@@ -82,6 +85,7 @@ public class Session implements ContentBlocking.Delegate, GeckoSession.Navigatio
82
85
private transient byte [] mPrivatePage ;
83
86
private boolean mIsActive ;
84
87
88
+
85
89
public interface BitmapChangedListener {
86
90
void onBitmapChanged (Session aSession , Bitmap aBitmap );
87
91
}
@@ -163,6 +167,7 @@ protected void shutdown() {
163
167
}
164
168
}
165
169
170
+ mQueuedCalls .clear ();
166
171
mNavigationListeners .clear ();
167
172
mProgressListeners .clear ();
168
173
mContentListeners .clear ();
@@ -221,6 +226,13 @@ private void dumpState(VideoAvailabilityListener aListener) {
221
226
aListener .onVideoAvailabilityChanged (mState .mMediaElements != null && mState .mMediaElements .size () > 0 );
222
227
}
223
228
229
+ private void flushQueuedEvents () {
230
+ for (Runnable call : mQueuedCalls ) {
231
+ call .run ();
232
+ }
233
+ mQueuedCalls .clear ();
234
+ }
235
+
224
236
public void setPermissionDelegate (GeckoSession .PermissionDelegate aDelegate ) {
225
237
mPermissionDelegate = aDelegate ;
226
238
}
@@ -567,9 +579,15 @@ public void goForward() {
567
579
}
568
580
569
581
public void setActive (boolean aActive ) {
582
+ // Flush the events queued while the session was inactive
583
+ if (mState .mSession != null && !mIsActive && aActive ) {
584
+ flushQueuedEvents ();
585
+ }
586
+
570
587
if (mState .mSession != null ) {
571
588
mState .mSession .setActive (aActive );
572
589
}
590
+
573
591
mIsActive = aActive ;
574
592
575
593
for (SessionChangeListener listener : mSessionChangeListeners ) {
@@ -1212,16 +1230,51 @@ public void onMediaRemove(@NonNull GeckoSession aSession, @NonNull MediaElement
1212
1230
1213
1231
@ Override
1214
1232
public void onHistoryStateChange (@ NonNull GeckoSession aSession , @ NonNull GeckoSession .HistoryDelegate .HistoryList historyList ) {
1215
- if (mState .mSession == aSession && mHistoryDelegate != null ) {
1216
- mHistoryDelegate .onHistoryStateChange (aSession , historyList );
1233
+ if (mState .mSession == aSession ) {
1234
+ if (mHistoryDelegate != null ) {
1235
+ mHistoryDelegate .onHistoryStateChange (aSession , historyList );
1236
+
1237
+ } else {
1238
+ mQueuedCalls .add (() -> {
1239
+ new Handler (mContext .getMainLooper ()).postDelayed (() -> {
1240
+ if (mHistoryDelegate != null ) {
1241
+ mHistoryDelegate .onHistoryStateChange (aSession , historyList );
1242
+ }
1243
+ }, 100 );
1244
+ });
1245
+ }
1217
1246
}
1218
1247
}
1219
1248
1220
1249
@ Nullable
1221
1250
@ Override
1222
1251
public GeckoResult <Boolean > onVisited (@ NonNull GeckoSession aSession , @ NonNull String url , @ Nullable String lastVisitedURL , int flags ) {
1223
- if (mState .mSession == aSession && mHistoryDelegate != null ) {
1224
- return mHistoryDelegate .onVisited (aSession , url , lastVisitedURL , flags );
1252
+ if (mState .mSession == aSession ) {
1253
+ if (mHistoryDelegate != null ) {
1254
+ return mHistoryDelegate .onVisited (aSession , url , lastVisitedURL , flags );
1255
+
1256
+ } else {
1257
+ final GeckoResult <Boolean > response = new GeckoResult <>();
1258
+ mQueuedCalls .add (() -> {
1259
+ if (mHistoryDelegate != null ) {
1260
+ try {
1261
+ requireNonNull (mHistoryDelegate .onVisited (aSession , url , lastVisitedURL , flags )).then (aBoolean -> {
1262
+ response .complete (aBoolean );
1263
+ return null ;
1264
+
1265
+ }).exceptionally (throwable -> {
1266
+ Log .d (LOGTAG , "Null GeckoResult from onVisited" );
1267
+ return null ;
1268
+ });
1269
+
1270
+ } catch (NullPointerException e ) {
1271
+ e .printStackTrace ();
1272
+ }
1273
+ }
1274
+ });
1275
+
1276
+ return response ;
1277
+ }
1225
1278
}
1226
1279
1227
1280
return GeckoResult .fromValue (false );
@@ -1230,8 +1283,31 @@ public GeckoResult<Boolean> onVisited(@NonNull GeckoSession aSession, @NonNull S
1230
1283
@ UiThread
1231
1284
@ Nullable
1232
1285
public GeckoResult <boolean []> getVisited (@ NonNull GeckoSession aSession , @ NonNull String [] urls ) {
1233
- if (mState .mSession == aSession && mHistoryDelegate != null ) {
1234
- return mHistoryDelegate .getVisited (aSession , urls );
1286
+ if (mState .mSession == aSession ) {
1287
+ if (mHistoryDelegate != null ) {
1288
+ return mHistoryDelegate .getVisited (aSession , urls );
1289
+
1290
+ } else {
1291
+ final GeckoResult <boolean []> response = new GeckoResult <>();
1292
+ mQueuedCalls .add (() -> {
1293
+ if (mHistoryDelegate != null ) {
1294
+ try {
1295
+ requireNonNull (mHistoryDelegate .getVisited (aSession , urls )).then (aBoolean -> {
1296
+ response .complete (aBoolean );
1297
+ return null ;
1298
+
1299
+ }).exceptionally (throwable -> {
1300
+ Log .d (LOGTAG , "Null GeckoResult from getVisited" );
1301
+ return null ;
1302
+ });
1303
+
1304
+ } catch (NullPointerException e ) {
1305
+ e .printStackTrace ();
1306
+ }
1307
+ }
1308
+ });
1309
+ return response ;
1310
+ }
1235
1311
}
1236
1312
1237
1313
return GeckoResult .fromValue (new boolean []{});
0 commit comments