-
Notifications
You must be signed in to change notification settings - Fork 215
/
SessionStore.java
330 lines (276 loc) · 9.99 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
package org.mozilla.vrbrowser.browser.engine;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import org.mozilla.gecko.util.ThreadUtils;
import org.mozilla.geckoview.GeckoRuntime;
import org.mozilla.geckoview.GeckoSession;
import org.mozilla.vrbrowser.VRBrowserApplication;
import org.mozilla.vrbrowser.browser.BookmarksStore;
import org.mozilla.vrbrowser.browser.HistoryStore;
import org.mozilla.vrbrowser.browser.PermissionDelegate;
import org.mozilla.vrbrowser.browser.Services;
import org.mozilla.vrbrowser.utils.SystemUtils;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class SessionStore implements GeckoSession.PermissionDelegate {
private static final String LOGTAG = SystemUtils.createLogtag(SessionStore.class);
private static final int MAX_GECKO_SESSIONS = 5;
private static SessionStore mInstance;
public static SessionStore get() {
if (mInstance == null) {
mInstance = new SessionStore();
}
return mInstance;
}
private Context mContext;
private GeckoRuntime mRuntime;
private ArrayList<Session> mSessions;
private Session mActiveSession;
private PermissionDelegate mPermissionDelegate;
private BookmarksStore mBookmarksStore;
private HistoryStore mHistoryStore;
private Services mServices;
private boolean mSuspendPending;
private SessionStore() {
mSessions = new ArrayList<>();
}
public void setContext(Context context, Bundle aExtras) {
mContext = context;
// FIXME: Once GeckoView has a prefs API
SessionUtils.vrPrefsWorkAround(context, aExtras);
mRuntime = EngineProvider.INSTANCE.getOrCreateRuntime(context);
}
public void initializeServices() {
mServices = ((VRBrowserApplication)mContext.getApplicationContext()).getServices();
}
public void initializeStores(Context context) {
mBookmarksStore = new BookmarksStore(context);
mHistoryStore = new HistoryStore(context);
}
@NonNull
private Session addSession(@NonNull Session aSession) {
aSession.setPermissionDelegate(this);
aSession.addNavigationListener(mServices);
mSessions.add(aSession);
sessionActiveStateChanged();
return aSession;
}
@NonNull
public Session createSession(boolean aPrivateMode) {
SessionSettings settings = new SessionSettings(new SessionSettings.Builder().withDefaultSettings(mContext).withPrivateBrowsing(aPrivateMode));
return createSession(settings, Session.SESSION_OPEN);
}
@NonNull
/* package */ Session createSession(@NonNull SessionSettings aSettings, @Session.SessionOpenModeFlags int aOpenMode) {
return addSession(new Session(mContext, mRuntime, aSettings, aOpenMode));
}
@NonNull
public Session createSuspendedSession(SessionState aRestoreState) {
return addSession(new Session(mContext, mRuntime, aRestoreState));
}
@NonNull
public Session createSuspendedSession(final String aUri, final boolean aPrivateMode) {
SessionState state = new SessionState();
state.mUri = aUri;
state.mSettings = new SessionSettings(new SessionSettings.Builder().withDefaultSettings(mContext).withPrivateBrowsing(aPrivateMode));
Session session = new Session(mContext, mRuntime, state);
return addSession(session);
}
private void shutdownSession(@NonNull Session aSession) {
aSession.setPermissionDelegate(null);
aSession.removeNavigationListener(mServices);
aSession.shutdown();
}
public void destroySession(Session aSession) {
mSessions.remove(aSession);
if (aSession != null) {
shutdownSession(aSession);
}
}
public void destroyPrivateSessions() {
mSessions.removeIf(session -> {
if (!session.isPrivateMode()) {
return false;
}
shutdownSession(session);
return true;
});
}
public void suspendAllInactiveSessions() {
for (Session session: mSessions) {
if (!session.isActive()) {
session.suspend();
}
}
}
public @Nullable Session getSession(String aId) {
return mSessions.stream().filter(session -> session.getId().equals(aId)).findFirst().orElse(null);
}
public void setActiveSession(Session aSession) {
if (aSession != null) {
aSession.setActive(true);
}
mActiveSession = aSession;
}
private void limitInactiveSessions() {
Log.d(LOGTAG, "Limiting Inactive Sessions");
suspendAllInactiveSessions();
mSuspendPending = false;
}
void sessionActiveStateChanged() {
if (mSuspendPending) {
return;
}
int count = 0;
int activeCount = 0;
int inactiveCount = 0;
int suspendedCount = 0;
for(Session session: mSessions) {
if (session.getGeckoSession() != null) {
count++;
if (session.isActive()) {
activeCount++;
} else {
inactiveCount++;
}
} else {
suspendedCount++;
}
}
if (count > MAX_GECKO_SESSIONS) {
Log.d(LOGTAG, "Too many GeckoSessions. Active: " + activeCount + " Inactive: " + inactiveCount + " Suspended: " + suspendedCount);
mSuspendPending = true;
ThreadUtils.postToUiThread(this::limitInactiveSessions);
}
}
public Session getActiveSession() {
return mActiveSession;
}
public ArrayList<Session> getSortedSessions(boolean aPrivateMode) {
ArrayList<Session> result = new ArrayList<>(mSessions);
result.removeIf(session -> session.isPrivateMode() != aPrivateMode);
result.sort((o1, o2) -> {
if (o2.getLastUse() < o1.getLastUse()) {
return -1;
}
return o2.getLastUse() == o1.getLastUse() ? 0 : 1;
});
return result;
}
public void setPermissionDelegate(PermissionDelegate delegate) {
mPermissionDelegate = delegate;
}
public BookmarksStore getBookmarkStore() {
return mBookmarksStore;
}
public HistoryStore getHistoryStore() {
return mHistoryStore;
}
public void purgeSessionHistory() {
for (Session session: mSessions) {
session.purgeHistory();
}
}
public void onPause() {
for (Session session: mSessions) {
session.setActive(false);
}
}
public void onResume() {
for (Session session: mSessions) {
session.setActive(true);
}
}
public void onDestroy() {
for (int i = mSessions.size() - 1; i >= 0; --i) {
destroySession(mSessions.get(i));
}
if (mBookmarksStore != null) {
mBookmarksStore.removeAllListeners();
}
if (mHistoryStore != null) {
mHistoryStore.removeAllListeners();
}
}
public void onConfigurationChanged(Configuration newConfig) {
if (mRuntime != null) {
mRuntime.configurationChanged(newConfig);
}
}
// Session Settings
public void setServo(final boolean enabled) {
for (Session session: mSessions) {
session.setServo(enabled);
}
}
public void setUaMode(final int mode) {
for (Session session: mSessions) {
session.setUaMode(mode);
}
}
public void resetMultiprocess() {
for (Session session: mSessions) {
session.resetMultiprocess();
}
}
public void setTrackingProtection(final boolean aEnabled) {
for (Session session: mSessions) {
session.setTrackingProtection(aEnabled);
}
}
// Runtime Settings
public void setConsoleOutputEnabled(boolean enabled) {
if (mRuntime != null) {
mRuntime.getSettings().setConsoleOutputEnabled(enabled);
}
}
public void setRemoteDebugging(final boolean enabled) {
if (mRuntime != null) {
mRuntime.getSettings().setRemoteDebuggingEnabled(enabled);
}
}
public void setLocales(List<String> locales) {
if (mRuntime != null) {
mRuntime.getSettings().setLocales(locales.stream().toArray(String[]::new));
}
}
public void clearCache(long clearFlags) {
LinkedList<Session> activeSession = new LinkedList<>();
for (Session session: mSessions) {
if (session.getGeckoSession() != null) {
session.suspend();
activeSession.add(session);
}
}
mRuntime.getStorageController().clearData(clearFlags).then(aVoid -> {
for (Session session: activeSession) {
session.recreateSession();
}
return null;
});
}
// Permission Delegate
@Override
public void onAndroidPermissionsRequest(@NonNull GeckoSession session, @Nullable String[] permissions, @NonNull Callback callback) {
if (mPermissionDelegate != null) {
mPermissionDelegate.onAndroidPermissionsRequest(session, permissions, callback);
}
}
@Override
public void onContentPermissionRequest(@NonNull GeckoSession session, @Nullable String uri, int type, @NonNull Callback callback) {
if (mPermissionDelegate != null) {
mPermissionDelegate.onContentPermissionRequest(session, uri, type, callback);
}
}
@Override
public void onMediaPermissionRequest(@NonNull GeckoSession session, @NonNull String uri, @Nullable MediaSource[] video, @Nullable MediaSource[] audio, @NonNull MediaCallback callback) {
if (mPermissionDelegate != null) {
mPermissionDelegate.onMediaPermissionRequest(session, uri, video, audio, callback);
}
}
}