@@ -91,7 +91,6 @@ public class Session implements ContentBlocking.Delegate, GeckoSession.Navigatio
9191 private transient byte [] mPrivatePage ;
9292 private transient boolean mFirstContentfulPaint ;
9393 private transient long mKeepAlive ;
94- private transient boolean mSessionRemoved ;
9594
9695 public interface BitmapChangedListener {
9796 void onBitmapChanged (Session aSession , Bitmap aBitmap );
@@ -110,19 +109,39 @@ public interface DrmStateChangedListener {
110109 }
111110
112111 @ IntDef (value = { SESSION_OPEN , SESSION_DO_NOT_OPEN })
113- public @interface SessionOpenModeFlags {}
114- public static final int SESSION_OPEN = 0 ;
115- public static final int SESSION_DO_NOT_OPEN = 1 ;
112+ @interface SessionOpenModeFlags {}
113+ static final int SESSION_OPEN = 0 ;
114+ static final int SESSION_DO_NOT_OPEN = 1 ;
116115
117- protected Session (Context aContext , GeckoRuntime aRuntime ,
118- @ NonNull SessionSettings aSettings ) {
116+ @ NonNull
117+ static Session createSession (Context aContext , GeckoRuntime aRuntime , @ NonNull SessionSettings aSettings , @ Session .SessionOpenModeFlags int aOpenMode , @ NonNull SessionChangeListener listener ) {
118+ Session session = new Session (aContext , aRuntime , aSettings );
119+ session .addSessionChangeListener (listener );
120+ listener .onSessionAdded (session );
121+ if (aOpenMode == Session .SESSION_OPEN ) {
122+ session .openSession ();
123+ session .setActive (true );
124+ }
125+
126+ return session ;
127+ }
128+
129+ @ NonNull
130+ static Session createSuspendedSession (Context aContext , GeckoRuntime aRuntime , @ NonNull SessionState aRestoreState , @ NonNull SessionChangeListener listener ) {
131+ Session session = new Session (aContext , aRuntime , aRestoreState );
132+ session .addSessionChangeListener (listener );
133+
134+ return session ;
135+ }
136+
137+ private Session (Context aContext , GeckoRuntime aRuntime , @ NonNull SessionSettings aSettings ) {
119138 mContext = aContext ;
120139 mRuntime = aRuntime ;
121140 initialize ();
122- mState = createSession (aSettings );
141+ mState = createSessionState (aSettings );
123142 }
124143
125- protected Session (Context aContext , GeckoRuntime aRuntime , @ NonNull SessionState aRestoreState ) {
144+ private Session (Context aContext , GeckoRuntime aRuntime , @ NonNull SessionState aRestoreState ) {
126145 mContext = aContext ;
127146 mRuntime = aRuntime ;
128147 initialize ();
@@ -159,8 +178,8 @@ private void initialize() {
159178
160179 protected void shutdown () {
161180 if (mState .mSession != null ) {
162- closeSession ( mState );
163- mState . mSession = null ;
181+ setActive ( false );
182+ suspend () ;
164183 }
165184
166185 if (mState .mParentId != null ) {
@@ -170,13 +189,6 @@ protected void shutdown() {
170189 }
171190 }
172191
173- if (!mSessionRemoved ) {
174- mSessionChangeListeners .forEach (listener -> {
175- listener .onSessionRemoved (mState .mId );
176- });
177- mSessionRemoved = true ;
178- }
179-
180192 mQueuedCalls .clear ();
181193 mNavigationListeners .clear ();
182194 mProgressListeners .clear ();
@@ -430,6 +442,8 @@ public void suspend() {
430442 return ;
431443 }
432444
445+ mSessionChangeListeners .forEach (listener -> listener .onSessionRemoved (mState .mId ));
446+
433447 Log .d (LOGTAG , "Suspending Session: " + mState .mId );
434448 closeSession (mState );
435449 mState .mSession = null ;
@@ -470,12 +484,7 @@ private void restore() {
470484
471485 mState .mSession = createGeckoSession (settings );
472486
473- // We call restore when a session is first activated and when it's recreated.
474- // We only need to notify of the session creation if it's not a recreation.
475- if (mSessionRemoved ) {
476- mSessionChangeListeners .forEach (listener -> listener .onSessionAdded (this ));
477- mSessionRemoved = false ;
478- }
487+ mSessionChangeListeners .forEach (listener -> listener .onSessionAdded (this ));
479488
480489 openSession ();
481490
@@ -498,7 +507,7 @@ private void restore() {
498507 }
499508
500509
501- private SessionState createSession (@ NonNull SessionSettings aSettings ) {
510+ private SessionState createSessionState (@ NonNull SessionSettings aSettings ) {
502511 SessionState state = new SessionState ();
503512 state .mSettings = aSettings ;
504513 state .mSession = createGeckoSession (aSettings );
@@ -541,6 +550,8 @@ void recreateSession() {
541550
542551 mState = mState .recreate ();
543552
553+ mSessionChangeListeners .forEach (listener -> listener .onSessionRemoved (mState .mId ));
554+
544555 restore ();
545556
546557 for (SessionChangeListener listener : mSessionChangeListeners ) {
@@ -563,9 +574,7 @@ void openSession() {
563574 mState .mSession .open (mRuntime );
564575 }
565576
566- mSessionChangeListeners .forEach (listener -> {
567- listener .onSessionOpened (this );
568- });
577+ mSessionChangeListeners .forEach (listener -> listener .onSessionOpened (this ));
569578 }
570579
571580 private void closeSession (@ NonNull SessionState aState ) {
@@ -804,6 +813,7 @@ public void setActive(boolean aActive) {
804813
805814 } else if (aActive ) {
806815 restore ();
816+
807817 } else {
808818 Log .e (LOGTAG , "ERROR: Setting null GeckoView to inactive!" );
809819 }
@@ -867,7 +877,7 @@ public void toggleServo() {
867877 .withServo (!isInstanceOfServoSession (mState .mSession ))
868878 .build ();
869879
870- mState = createSession (settings );
880+ mState = createSessionState (settings );
871881 openSession ();
872882 closeSession (previous );
873883
0 commit comments