-
Notifications
You must be signed in to change notification settings - Fork 215
/
SessionStore.java
1445 lines (1238 loc) · 49.8 KB
/
SessionStore.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.vrbrowser.browser;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.inputmethod.CursorAnchorInfo;
import android.view.inputmethod.ExtractedText;
import android.view.inputmethod.ExtractedTextRequest;
import org.mozilla.gecko.GeckoProfile;
import org.mozilla.geckoview.AllowOrDeny;
import org.mozilla.geckoview.ContentBlocking;
import org.mozilla.geckoview.MediaElement;
import org.mozilla.geckoview.GeckoResult;
import org.mozilla.geckoview.GeckoRuntime;
import org.mozilla.geckoview.GeckoRuntimeSettings;
import org.mozilla.geckoview.GeckoSession;
import org.mozilla.geckoview.GeckoSessionSettings;
import org.mozilla.geckoview.WebRequestError;
import org.mozilla.vrbrowser.BuildConfig;
import org.mozilla.vrbrowser.R;
import org.mozilla.vrbrowser.crashreporting.CrashReporterService;
import org.mozilla.vrbrowser.geolocation.GeolocationData;
import org.mozilla.vrbrowser.telemetry.TelemetryWrapper;
import org.mozilla.vrbrowser.utils.InternalPages;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import static org.mozilla.vrbrowser.utils.ServoUtils.createServoSession;
import static org.mozilla.vrbrowser.utils.ServoUtils.isInstanceOfServoSession;
import static org.mozilla.vrbrowser.utils.ServoUtils.isServoAvailable;
public class SessionStore implements ContentBlocking.Delegate, GeckoSession.NavigationDelegate,
GeckoSession.ProgressDelegate, GeckoSession.ContentDelegate, GeckoSession.TextInputDelegate,
GeckoSession.PromptDelegate, GeckoSession.MediaDelegate, SharedPreferences.OnSharedPreferenceChangeListener {
private static SessionStore mInstance;
private static final String LOGTAG = "VRB";
public static SessionStore get() {
if (mInstance == null) {
mInstance = new SessionStore();
}
return mInstance;
}
// You can test a local file using: "resource://android/assets/webvr/index.html"
public static final String PRIVATE_BROWSING_URI = "about:privatebrowsing";
public static final int NO_SESSION_ID = -1;
private LinkedList<GeckoSession.NavigationDelegate> mNavigationListeners;
private LinkedList<GeckoSession.ProgressDelegate> mProgressListeners;
private LinkedList<GeckoSession.ContentDelegate> mContentListeners;
private LinkedList<SessionChangeListener> mSessionChangeListeners;
private LinkedList<GeckoSession.TextInputDelegate> mTextInputListeners;
private LinkedList<GeckoSession.PromptDelegate> mPromptListeners;
private UserAgentOverride mUserAgentOverride;
public interface SessionChangeListener {
void onNewSession(GeckoSession aSession, int aId);
void onRemoveSession(GeckoSession aSession, int aId);
void onCurrentSessionChange(GeckoSession aSession, int aId);
}
class SessionSettings {
boolean multiprocess = SettingsStore.getInstance(mContext).isMultiprocessEnabled();
boolean privateMode = false;
boolean trackingProtection = SettingsStore.getInstance(mContext).isTrackingProtectionEnabled();
boolean suspendMediaWhenInactive = true;
int userAgentMode = SettingsStore.getInstance(mContext).getUaMode();
boolean servo = false;
}
class State {
boolean mCanGoBack;
boolean mCanGoForward;
boolean mIsLoading;
boolean mIsInputActive;
GeckoSession.ProgressDelegate.SecurityInformation mSecurityInformation;
String mUri;
String mPreviousUri;
String mTitle;
boolean mFullScreen;
GeckoSession mSession;
SessionSettings mSettings;
ArrayList<Media> mMediaElements = new ArrayList<>();
}
private GeckoRuntime mRuntime;
private GeckoSession mCurrentSession;
private HashMap<Integer, State> mSessions;
private Deque<Integer> mSessionsStack;
private Deque<Integer> mPrivateSessionsStack;
private GeckoSession.PermissionDelegate mPermissionDelegate;
private int mPreviousSessionId = SessionStore.NO_SESSION_ID;
private int mPreviousGeckoSessionId = SessionStore.NO_SESSION_ID;
private String mRegion;
private Context mContext;
private SharedPreferences mPrefs;
private SessionStore() {
mSessions = new LinkedHashMap<>();
mSessionsStack = new ArrayDeque<>();
mPrivateSessionsStack = new ArrayDeque<>();
}
public void registerListeners() {
mNavigationListeners = new LinkedList<>();
mProgressListeners = new LinkedList<>();
mContentListeners = new LinkedList<>();
mSessionChangeListeners = new LinkedList<>();
mTextInputListeners = new LinkedList<>();
mPromptListeners = new LinkedList<>();
if (mPrefs != null) {
mPrefs.registerOnSharedPreferenceChangeListener(this);
}
}
public void unregisterListeners() {
mNavigationListeners.clear();
mProgressListeners.clear();
mContentListeners.clear();
mSessionChangeListeners.clear();
mTextInputListeners.clear();
if (mPrefs != null) {
mPrefs.unregisterOnSharedPreferenceChangeListener(this);
}
}
public void setContext(Context aContext, Bundle aExtras) {
if (mRuntime == null) {
// FIXME: Once GeckoView has a prefs API
vrPrefsWorkAround(aContext, aExtras);
GeckoRuntimeSettings.Builder runtimeSettingsBuilder = new GeckoRuntimeSettings.Builder();
runtimeSettingsBuilder.crashHandler(CrashReporterService.class);
runtimeSettingsBuilder.contentBlocking((new ContentBlocking.Settings.Builder())
.categories(ContentBlocking.AT_AD | ContentBlocking.AT_SOCIAL | ContentBlocking.AT_ANALYTIC)
.build());
runtimeSettingsBuilder.consoleOutput(SettingsStore.getInstance(aContext).isConsoleLogsEnabled());
runtimeSettingsBuilder.displayDensityOverride(SettingsStore.getInstance(aContext).getDisplayDensity());
runtimeSettingsBuilder.remoteDebuggingEnabled(SettingsStore.getInstance(aContext).isRemoteDebuggingEnabled());
runtimeSettingsBuilder.displayDpiOverride(SettingsStore.getInstance(aContext).getDisplayDpi());
runtimeSettingsBuilder.screenSizeOverride(SettingsStore.getInstance(aContext).getMaxWindowWidth(),
SettingsStore.getInstance(aContext).getMaxWindowHeight());
if (SettingsStore.getInstance(aContext).getTransparentBorderWidth() > 0) {
runtimeSettingsBuilder.useMaxScreenDepth(true);
}
if (BuildConfig.DEBUG) {
runtimeSettingsBuilder.arguments(new String[] { "-purgecaches" });
}
mRuntime = GeckoRuntime.create(aContext, runtimeSettingsBuilder.build());
} else {
mRuntime.attachTo(aContext);
}
mContext = aContext;
mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
if (mUserAgentOverride == null) {
mUserAgentOverride = new UserAgentOverride();
mUserAgentOverride.loadOverridesFromAssets((Activity)aContext, aContext.getString(R.string.user_agent_override_file));
}
}
public void dumpAllState(Integer sessionId) {
dumpAllState(getSession(sessionId));
}
private boolean isLocalizedContent(@Nullable String url) {
return url != null && (url.startsWith("about:") || url.startsWith("data:"));
}
private void dumpAllState(GeckoSession aSession) {
for (GeckoSession.NavigationDelegate listener: mNavigationListeners) {
dumpState(aSession, listener);
}
for (GeckoSession.ProgressDelegate listener: mProgressListeners) {
dumpState(aSession, listener);
}
for (GeckoSession.ContentDelegate listener: mContentListeners) {
dumpState(aSession, listener);
}
}
private void dumpState(GeckoSession aSession, GeckoSession.NavigationDelegate aListener) {
boolean canGoForward = false;
boolean canGoBack = false;
String uri = "";
if (aSession != null) {
State state = mSessions.get(aSession.hashCode());
if (state != null) {
canGoBack = state.mCanGoBack;
canGoForward = state.mCanGoForward;
uri = state.mUri;
}
}
aListener.onCanGoBack(aSession, canGoBack);
aListener.onCanGoForward(aSession, canGoForward);
aListener.onLocationChange(aSession, uri);
}
private void dumpState(GeckoSession aSession, GeckoSession.ProgressDelegate aListener) {
boolean isLoading = false;
GeckoSession.ProgressDelegate.SecurityInformation securityInfo = null;
String uri = "";
if (aSession != null) {
State state = mSessions.get(aSession.hashCode());
if (state != null) {
isLoading = state.mIsLoading;
securityInfo = state.mSecurityInformation;
uri = state.mUri;
}
}
if (isLoading) {
aListener.onPageStart(aSession, uri);
} else {
aListener.onPageStop(aSession, true);
}
if (securityInfo != null) {
aListener.onSecurityChange(aSession, securityInfo);
}
}
public void dumpState(GeckoSession aSession, GeckoSession.ContentDelegate aListener) {
String title = "";
if (aSession != null) {
State state = mSessions.get(aSession.hashCode());
if (state != null) {
title = state.mTitle;
}
}
aListener.onTitleChange(aSession, title);
}
public void addNavigationListener(GeckoSession.NavigationDelegate aListener) {
mNavigationListeners.add(aListener);
dumpState(mCurrentSession, aListener);
}
public void removeNavigationListener(GeckoSession.NavigationDelegate aListener) {
mNavigationListeners.remove(aListener);
}
public void addProgressListener(GeckoSession.ProgressDelegate aListener) {
mProgressListeners.add(aListener);
dumpState(mCurrentSession, aListener);
}
public void removeProgressListener(GeckoSession.ProgressDelegate aListener) {
mProgressListeners.remove(aListener);
}
public void addContentListener(GeckoSession.ContentDelegate aListener) {
mContentListeners.add(aListener);
dumpState(mCurrentSession, aListener);
}
public void removeContentListener(GeckoSession.ContentDelegate aListener) {
mContentListeners.remove(aListener);
}
public void addSessionChangeListener(SessionChangeListener aListener) {
mSessionChangeListeners.add(aListener);
}
public void removeSessionChangeListener(SessionChangeListener aListener) {
mSessionChangeListeners.remove(aListener);
}
public void addTextInputListener(GeckoSession.TextInputDelegate aListener) {
mTextInputListeners.add(aListener);
}
public void removeTextInputListener(GeckoSession.TextInputDelegate aListener) {
mTextInputListeners.remove(aListener);
}
public void addPromptListener(GeckoSession.PromptDelegate aListener) {
mPromptListeners.add(aListener);
}
public void removePromptListener(GeckoSession.PromptDelegate aListener) {
mPromptListeners.remove(aListener);
}
public int createSession() {
return createSession(false);
}
public int createSession(boolean isPrivate) {
SessionStore.SessionSettings settings = new SessionStore.SessionSettings();
if (isPrivate) {
settings.privateMode = true;
}
return createSession(settings);
}
int createSession(SessionSettings aSettings) {
State state = new State();
state.mSettings = aSettings;
GeckoSessionSettings geckoSettings = new GeckoSessionSettings.Builder()
.useMultiprocess(aSettings.multiprocess)
.usePrivateMode(aSettings.privateMode)
.useTrackingProtection(aSettings.trackingProtection)
.build();
if (aSettings.servo) {
if (isServoAvailable()) {
state.mSession = createServoSession(mContext);
} else {
Log.e(LOGTAG, "Attempt to create a ServoSession. Servo hasn't been enable at build time. Using a GeckoSession instead.");
state.mSession = new GeckoSession(geckoSettings);
}
} else {
state.mSession = new GeckoSession(geckoSettings);
}
int result = state.mSession.hashCode();
mSessions.put(result, state);
state.mSession.getSettings().setSuspendMediaWhenInactive(aSettings.suspendMediaWhenInactive);
state.mSession.getSettings().setUserAgentMode(aSettings.userAgentMode);
state.mSession.setNavigationDelegate(this);
state.mSession.setProgressDelegate(this);
state.mSession.setPromptDelegate(this);
state.mSession.setContentDelegate(this);
state.mSession.getTextInput().setDelegate(this);
state.mSession.setPermissionDelegate(mPermissionDelegate);
state.mSession.setContentBlockingDelegate(this);
state.mSession.setMediaDelegate(this);
for (SessionChangeListener listener: mSessionChangeListeners) {
listener.onNewSession(state.mSession, result);
}
return result;
}
public void removeSession(int aSessionId) {
GeckoSession session = getSession(aSessionId);
if (session != null) {
session.setContentDelegate(null);
session.setNavigationDelegate(null);
session.setProgressDelegate(null);
session.getTextInput().setDelegate(null);
session.setPromptDelegate(null);
session.setPermissionDelegate(null);
session.setContentBlockingDelegate(null);
session.setMediaDelegate(null);
mSessions.remove(aSessionId);
for (SessionChangeListener listener: mSessionChangeListeners) {
listener.onRemoveSession(session, aSessionId);
}
session.setActive(false);
session.stop();
session.close();
}
}
private void pushSession(int aSessionId) {
boolean isPrivateMode = mCurrentSession.getSettings().getUsePrivateMode();
if (isPrivateMode)
mPrivateSessionsStack.push(aSessionId);
else
mSessionsStack.push(aSessionId);
}
private Integer popSession() {
boolean isPrivateMode = mCurrentSession.getSettings().getUsePrivateMode();
if (isPrivateMode)
return mPrivateSessionsStack.pop();
else
return mSessionsStack.pop();
}
private Integer peekSession() {
boolean isPrivateMode = mCurrentSession.getSettings().getUsePrivateMode();
if (isPrivateMode)
return mPrivateSessionsStack.peek();
else
return mSessionsStack.peek();
}
public GeckoSession getSession(int aId) {
State result = mSessions.get(aId);
if (result == null) {
return null;
}
return result.mSession;
}
public Integer getSessionId(GeckoSession aSession) {
for (Map.Entry<Integer, State> entry : mSessions.entrySet()) {
if (entry.getValue().mSession == aSession) {
return entry.getKey();
}
}
return null;
}
public String getUriFromSession(GeckoSession aSession) {
Integer sessionId = getSessionId(aSession);
if (sessionId == null) {
return "";
}
State state = mSessions.get(sessionId);
if (state != null) {
return state.mUri;
}
return "";
}
public List<Integer> getSessions() {
return new ArrayList<>(mSessions.keySet());
}
public List<Integer> getSessionsByPrivateMode(boolean aUsingPrivateMode) {
ArrayList<Integer> result = new ArrayList<>();
for (Integer sessionId : mSessions.keySet()) {
GeckoSession session = getSession(sessionId);
if (session != null && session.getSettings().getUsePrivateMode() == aUsingPrivateMode) {
result.add(sessionId);
}
}
return result;
}
public void setCurrentSession(int aId) {
if (mRuntime == null) {
Log.e(LOGTAG, "SessionStore failed to set current session, GeckoRuntime is null");
return;
}
Log.d(LOGTAG, "Creating session: " + aId);
if (mCurrentSession != null) {
mCurrentSession.setActive(false);
}
mCurrentSession = null;
State state = mSessions.get(aId);
if (state != null) {
mCurrentSession = state.mSession;
if (!mCurrentSession.isOpen()) {
mCurrentSession.open(mRuntime);
}
for (SessionChangeListener listener: mSessionChangeListeners) {
listener.onCurrentSessionChange(mCurrentSession, aId);
}
}
dumpAllState(mCurrentSession);
if (mCurrentSession != null)
mCurrentSession.setActive(true);
}
public void setRegion(String aRegion) {
Log.d(LOGTAG, "SessionStore setRegion: " + aRegion);
mRegion = aRegion != null ? aRegion.toLowerCase() : "worldwide";
// There is a region initialize and the home is already loaded
if (mCurrentSession != null && isHomeUri(getCurrentUri())) {
mCurrentSession.loadUri("javascript:window.location.replace('" + getHomeUri() + "');");
}
}
public String getHomeUri() {
String homepage = SettingsStore.getInstance(mContext).getHomepage();
if (homepage.equals(mContext.getString(R.string.homepage_url)) && mRegion != null) {
homepage = homepage + "?region=" + mRegion;
}
return homepage;
}
public Boolean isHomeUri(String aUri) {
return aUri != null && aUri.toLowerCase().startsWith(
SettingsStore.getInstance(mContext).getHomepage()
);
}
public String getCurrentUri() {
String result = "";
if (mCurrentSession != null) {
State state = mSessions.get(mCurrentSession.hashCode());
if (state == null) {
return result;
}
result = state.mUri;
}
return result;
}
public String getPreviousUri() {
String result = "";
if (mCurrentSession != null) {
State state = mSessions.get(mCurrentSession.hashCode());
if (state == null) {
return result;
}
result = state.mPreviousUri;
}
return result;
}
public String getCurrentTitle() {
String result = "";
if (mCurrentSession != null) {
State state = mSessions.get(mCurrentSession.hashCode());
if (state == null) {
return result;
}
result = state.mTitle;
}
return result;
}
public Media getFullScreenVideo() {
if (mCurrentSession != null) {
State state = mSessions.get(mCurrentSession.hashCode());
if (state == null) {
return null;
}
for (Media media: state.mMediaElements) {
if (media.isFullscreen()) {
return media;
}
}
if (state.mMediaElements.size() > 0) {
return state.mMediaElements.get(state.mMediaElements.size() - 1);
}
}
return null;
}
public boolean isInputActive(int aSessionId) {
SessionStore.State state = mSessions.get(aSessionId);
if (state != null) {
return state.mIsInputActive;
}
return false;
}
public boolean canGoBack() {
if (mCurrentSession == null) {
return false;
}
State state = mSessions.get(mCurrentSession.hashCode());
if (state != null) {
return state.mCanGoBack;
}
return false;
}
public void goBack() {
if (mCurrentSession == null) {
return;
}
if (isInFullScreen()) {
exitFullScreen();
} else {
mCurrentSession.goBack();
}
}
public void goForward() {
if (mCurrentSession == null) {
return;
}
mCurrentSession.goForward();
}
public void setActive(boolean aActive) {
if (mCurrentSession == null) {
return;
}
mCurrentSession.setActive(aActive);
}
public void reload() {
if (mCurrentSession == null) {
return;
}
mCurrentSession.reload();
}
public void stop() {
if (mCurrentSession == null) {
return;
}
mCurrentSession.stop();
}
public void loadUri(String aUri) {
if (mCurrentSession == null) {
return;
}
if (aUri == null) {
aUri = getHomeUri();
}
Log.d(LOGTAG, "Loading URI: " + aUri);
mCurrentSession.loadUri(aUri);
}
public void toggleServo() {
if (mCurrentSession == null) {
return;
}
Log.v("servo", "toggleServo");
if (!isInstanceOfServoSession(mCurrentSession)) {
if (mPreviousGeckoSessionId == SessionStore.NO_SESSION_ID) {
mPreviousGeckoSessionId = getCurrentSessionId();
String uri = getCurrentUri();
SessionStore.SessionSettings settings = new SessionStore.SessionSettings();
settings.servo = true;
int id = createSession(settings);
setCurrentSession(id);
loadUri(uri);
} else {
Log.e(LOGTAG, "Multiple Servo sessions not supported yet.");
}
} else {
removeSession(getCurrentSessionId());
setCurrentSession(mPreviousGeckoSessionId);
mPreviousGeckoSessionId = SessionStore.NO_SESSION_ID;
}
}
public boolean isInFullScreen() {
if (mCurrentSession == null) {
return false;
}
State state = mSessions.get(mCurrentSession.hashCode());
if (state != null) {
return state.mFullScreen;
}
return false;
}
public boolean isInFullScreen(GeckoSession aSession) {
Integer sessionId = getSessionId(aSession);
if (sessionId == null) {
return false;
}
State state = mSessions.get(sessionId);
if (state != null) {
return state.mFullScreen;
}
return false;
}
public void exitFullScreen() {
if (mCurrentSession == null) {
return;
}
mCurrentSession.exitFullScreen();
}
public GeckoSession getCurrentSession() {
return mCurrentSession;
}
public int getCurrentSessionId() {
if (mCurrentSession == null) {
return NO_SESSION_ID;
}
return mCurrentSession.hashCode();
}
public void setPermissionDelegate(GeckoSession.PermissionDelegate aDelegate) {
mPermissionDelegate = aDelegate;
for (HashMap.Entry<Integer, State> entry : mSessions.entrySet()) {
entry.getValue().mSession.setPermissionDelegate(aDelegate);
}
}
private void vrPrefsWorkAround(Context aContext, Bundle aExtras) {
File path = GeckoProfile.initFromArgs(aContext, null).getDir();
String prefFileName = path.getAbsolutePath() + File.separator + "user.js";
Log.i(LOGTAG, "Creating file: " + prefFileName);
try (FileOutputStream out = new FileOutputStream(prefFileName)) {
out.write("pref(\"dom.vr.enabled\", true);\n".getBytes());
out.write("pref(\"dom.vr.external.enabled\", true);\n".getBytes());
out.write("pref(\"webgl.enable-surface-texture\", true);\n".getBytes());
out.write("pref(\"apz.allow_double_tap_zooming\", false);\n".getBytes());
out.write("pref(\"dom.webcomponents.customelements.enabled\", true);\n".getBytes());
out.write("pref(\"javascript.options.ion\", true);\n".getBytes());
// Prevent autozoom when giving a form field focus.
out.write("pref(\"formhelper.autozoom\", false);\n".getBytes());
// Uncomment this to enable WebRender. WARNING NOT READY FOR USAGE.
// out.write("pref(\"gfx.webrender.all\", true);\n".getBytes());
int msaa = SettingsStore.getInstance(aContext).getMSAALevel();
if (msaa > 0) {
int msaaLevel = msaa == 2 ? 4 : 2;
out.write(("pref(\"gl.msaa-level\"," + msaaLevel + ");\n").getBytes());
}
addOptionalPref(out, "dom.vr.require-gesture", aExtras);
addOptionalPref(out, "privacy.reduceTimerPrecision", aExtras);
} catch (FileNotFoundException e) {
Log.e(LOGTAG, "Unable to create file: '" + prefFileName + "' got exception: " + e.toString());
} catch (IOException e) {
Log.e(LOGTAG, "Unable to write file: '" + prefFileName + "' got exception: " + e.toString());
}
}
private void addOptionalPref(FileOutputStream out, String aKey, Bundle aExtras) throws IOException {
if (aExtras != null && aExtras.containsKey(aKey)) {
boolean value = aExtras.getBoolean(aKey);
out.write(String.format("pref(\"%s\", %s);\n", aKey, value ? "true" : "false").getBytes());
}
}
public void switchPrivateMode() {
if (mCurrentSession == null)
return;
boolean isPrivateMode = mCurrentSession.getSettings().getUsePrivateMode();
if (!isPrivateMode) {
if (mPreviousSessionId == SessionStore.NO_SESSION_ID) {
mPreviousSessionId = getCurrentSessionId();
SessionStore.SessionSettings settings = new SessionStore.SessionSettings();
settings.privateMode = true;
int id = createSession(settings);
setCurrentSession(id);
InternalPages.PageResources pageResources = InternalPages.PageResources.create(R.raw.private_mode, R.raw.private_style);
getCurrentSession().loadData(InternalPages.createAboutPage(mContext, pageResources), "text/html");
} else {
int sessionId = getCurrentSessionId();
setCurrentSession(mPreviousSessionId);
mPreviousSessionId = sessionId;
}
} else {
int sessionId = getCurrentSessionId();
setCurrentSession(mPreviousSessionId);
mPreviousSessionId = sessionId;
}
}
public void setUaMode(int mode) {
if (mCurrentSession != null) {
mCurrentSession.getSettings().setUserAgentMode(mode);
mCurrentSession.reload();
}
}
public void exitPrivateMode() {
if (mCurrentSession == null)
return;
boolean isPrivateMode = mCurrentSession.getSettings().getUsePrivateMode();
if (isPrivateMode) {
int privateSessionId = getCurrentSessionId();
setCurrentSession(mPreviousSessionId);
mPreviousSessionId = SessionStore.NO_SESSION_ID;
// Remove current private_mode session
removeSession(privateSessionId);
// Remove all the stacked private_mode sessions
for (Iterator<Integer> it = mPrivateSessionsStack.iterator(); it.hasNext();) {
int sessionId = it.next();
removeSession(sessionId);
}
mPrivateSessionsStack.clear();
}
}
public boolean isCurrentSessionPrivate() {
if (mCurrentSession != null)
return mCurrentSession.getSettings().getUsePrivateMode();
return false;
}
public boolean canUnstackSession() {
Integer prevSessionId = peekSession();
return prevSessionId != null;
}
public void stackSession(int sessionId) {
pushSession(getCurrentSessionId());
setCurrentSession(sessionId);
mCurrentSession = null;
State state = mSessions.get(sessionId);
if (state != null) {
mCurrentSession = state.mSession;
for (SessionChangeListener listener : mSessionChangeListeners) {
listener.onCurrentSessionChange(mCurrentSession, sessionId);
}
}
dumpAllState(mCurrentSession);
}
public void unstackSession() {
Integer prevSessionId = popSession();
if (prevSessionId != null) {
int currentSession = getCurrentSessionId();
setCurrentSession(prevSessionId);
removeSession(currentSession);
}
}
public void setConsoleOutputEnabled(boolean enabled) {
if (mRuntime != null) {
mRuntime.getSettings().setConsoleOutputEnabled(enabled);
}
}
public void setMaxWindowSize(int width, int height) {
SharedPreferences.Editor editor = mPrefs.edit();
editor.putInt(
mContext.getString(R.string.settings_key_max_window_height),
height);
editor.putInt(
mContext.getString(R.string.settings_key_max_window_width),
width);
editor.commit();
}
public void setServo(final boolean enabled) {
if (!enabled && mCurrentSession != null && isInstanceOfServoSession(mCurrentSession)) {
String uri = getCurrentUri();
int id = createSession();
setCurrentSession(id);
loadUri(uri);
}
}
private void recreateSession(SessionStore.SessionSettings aSettings) {
if (mCurrentSession != null) {
final GeckoResult<GeckoSession.SessionState> state = mCurrentSession.saveState();
state.then(new GeckoResult.OnValueListener<GeckoSession.SessionState, Object>() {
@Nullable
@Override
public GeckoResult<Object> onValue(@Nullable GeckoSession.SessionState value) throws Throwable {
if (value != null) {
mCurrentSession.stop();
mCurrentSession.close();
int oldSessionId = getCurrentSessionId();
int sessionId = createSession(aSettings);
GeckoSession session = getSession(sessionId);
session.restoreState(value);
setCurrentSession(sessionId);
removeSession(oldSessionId);
}
return null;
}
}, new GeckoResult.OnExceptionListener<Object>() {
@Nullable
@Override
public GeckoResult<Object> onException(@NonNull Throwable exception) throws Throwable {
Log.e(LOGTAG, "State saving exception while setting multiprocess mode: " + exception.getLocalizedMessage());
return null;
}
});
}
}
private State getCurrentState() {
if (mCurrentSession != null) {
return mSessions.get(mCurrentSession.hashCode());
}
return null;
}
public void setMultiprocess(final boolean aEnabled) {
State state = getCurrentState();
if (state != null && state.mSettings.multiprocess != aEnabled) {
state.mSettings.multiprocess = aEnabled;
recreateSession(state.mSettings);
}
}
public void setTrackingProtection(final boolean aEnabled) {
State state = getCurrentState();
if (state != null && state.mSettings.trackingProtection != aEnabled) {
state.mSettings.trackingProtection = aEnabled;
recreateSession(state.mSettings);
}
}
public void setRemoteDebugging(final boolean enabled) {
if (mRuntime != null) {
mRuntime.getSettings().setRemoteDebuggingEnabled(enabled);
}
}
// NavigationDelegate
@Override
public void onLocationChange(GeckoSession aSession, String aUri) {
Log.d(LOGTAG, "SessionStore onLocationChange: " + aUri);
State state = mSessions.get(aSession.hashCode());
if (state == null) {
Log.e(LOGTAG, "Unknown session!");
return;
}
state.mPreviousUri = state.mUri;
state.mUri = aUri;
if (mCurrentSession == aSession) {
for (GeckoSession.NavigationDelegate listener : mNavigationListeners) {
listener.onLocationChange(aSession, aUri);
}
}
// The homepage finishes loading after the region has been updated
if (mRegion != null && aUri.equalsIgnoreCase(SettingsStore.getInstance(mContext).getHomepage())) {
aSession.loadUri("javascript:window.location.replace('" + getHomeUri() + "');");
}
}
@Override
public void onCanGoBack(GeckoSession aSession, boolean aCanGoBack) {
Log.d(LOGTAG, "SessionStore onCanGoBack: " + (aCanGoBack ? "true" : "false"));
State state = mSessions.get(aSession.hashCode());
if (state == null) {
return;
}
state.mCanGoBack = aCanGoBack;
if (mCurrentSession == aSession) {
for (GeckoSession.NavigationDelegate listener : mNavigationListeners) {
listener.onCanGoBack(aSession, aCanGoBack);
}
}
}
@Override
public void onCanGoForward(GeckoSession aSession, boolean aCanGoForward) {
Log.d(LOGTAG, "SessionStore onCanGoForward: " + (aCanGoForward ? "true" : "false"));
State state = mSessions.get(aSession.hashCode());
if (state == null) {
return;
}
state.mCanGoForward = aCanGoForward;
if (mCurrentSession == aSession) {
for (GeckoSession.NavigationDelegate listener : mNavigationListeners) {
listener.onCanGoForward(aSession, aCanGoForward);
}
}
}
public boolean mFirstOnLoadRequest = true;
@Override
public @Nullable GeckoResult<AllowOrDeny> onLoadRequest(@NonNull GeckoSession aSession, @NonNull LoadRequest aRequest) {
final GeckoResult<AllowOrDeny> result = new GeckoResult<>();
Log.d(LOGTAG, "onLoadRequest: " + aRequest.uri);