Commit a7a5243d authored by Mark Cogan's avatar Mark Cogan Committed by Commit Bot

[iOS] Move session saving after navigation out of BVC.

Prior to this CL, one of the BVC's numerous responsibilities was to
save the session when each web state navigation completes.

Because this has nothing to do with the UI layer, this CL moves that
responsibility to the SesionRestorationBrowserAgent, which already
handles session updates when a new web state becomes active.

Change-Id: I0f17a39e3efc244513200c0f5225ce59b99fae40
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2550716
Commit-Queue: Mark Cogan <marq@chromium.org>
Reviewed-by: default avatarJavier Flores <javierrobles@chromium.org>
Cr-Commit-Position: refs/heads/master@{#830180}
parent c3d7ccc5
......@@ -14,7 +14,9 @@
#include "ios/chrome/browser/main/browser_observer.h"
#include "ios/chrome/browser/main/browser_user_data.h"
#include "ios/chrome/browser/web_state_list/web_state_list_observer.h"
#include "ios/web/public/web_state_observer.h"
class AllWebStateObservationForwarder;
namespace base {
class FilePath;
}
......@@ -29,11 +31,13 @@ class WebUsageEnablerBrowserAgent;
// This class is responsible for handling requests of session restoration. It
// can be observed via SeassonRestorationObserver which it uses to notify
// observers of session restoration events. This class also automatically
// save sessions when the active webState changes.
// save sessions when the active webState changes, and when each web state
// completes a navigation.
class SessionRestorationBrowserAgent
: BrowserObserver,
public BrowserUserData<SessionRestorationBrowserAgent>,
WebStateListObserver {
WebStateListObserver,
public web::WebStateObserver {
public:
// Creates an SessionRestorationBrowserAgent scoped to |browser|.
static void CreateForBrowser(Browser* browser,
......@@ -86,13 +90,17 @@ class SessionRestorationBrowserAgent
// BrowserObserver methods
void BrowserDestroyed(Browser* browser) override;
// WebStateListObserver methods
// WebStateListObserver methods.
void WebStateActivatedAt(WebStateList* web_state_list,
web::WebState* old_web_state,
web::WebState* new_web_state,
int active_index,
ActiveWebStateChangeReason reason) override;
// web::WebStateObserver methods.
void DidFinishNavigation(web::WebState* web_state,
web::NavigationContext* navigation_context) override;
// The path to use for all session storage reads and writes. If multi-window
// is enabled, the session ID for this agent is used to determine this path;
// otherwise or if |force_single_window| is true, the state path of the
......@@ -120,6 +128,9 @@ class SessionRestorationBrowserAgent
// True when session restoration is in progress.
bool restoring_session_ = false;
// Observer for the active web state in |browser_|'s web state list.
std::unique_ptr<AllWebStateObservationForwarder> all_web_state_observer_;
};
#endif // IOS_CHROME_BROWSER_SESSIONS_SESSION_RESTORATION_BROWSER_AGENT_H_
......@@ -18,6 +18,7 @@
#import "ios/chrome/browser/sessions/session_window_ios.h"
#import "ios/chrome/browser/ui/util/multi_window_support.h"
#import "ios/chrome/browser/web/page_placeholder_tab_helper.h"
#import "ios/chrome/browser/web_state_list/all_web_state_observation_forwarder.h"
#import "ios/chrome/browser/web_state_list/web_state_list.h"
#import "ios/chrome/browser/web_state_list/web_state_list_serialization.h"
#import "ios/chrome/browser/web_state_list/web_usage_enabler/web_usage_enabler_browser_agent.h"
......@@ -58,7 +59,10 @@ SessionRestorationBrowserAgent::SessionRestorationBrowserAgent(
web_enabler_(WebUsageEnablerBrowserAgent::FromBrowser(browser)),
browser_state_(browser->GetBrowserState()),
session_ios_factory_(
[[SessionIOSFactory alloc] initWithWebStateList:web_state_list_]) {
[[SessionIOSFactory alloc] initWithWebStateList:web_state_list_]),
all_web_state_observer_(
std::make_unique<AllWebStateObservationForwarder>(web_state_list_,
this)) {
browser->AddObserver(this);
web_state_list_->AddObserver(this);
}
......@@ -217,6 +221,9 @@ bool SessionRestorationBrowserAgent::CanSaveSession() {
// Browser Observer methods:
void SessionRestorationBrowserAgent::BrowserDestroyed(Browser* browser) {
DCHECK_EQ(browser->GetWebStateList(), web_state_list_);
// Stop observing web states.
all_web_state_observer_.reset();
// Stop observing web state list.
browser->GetWebStateList()->RemoveObserver(this);
browser->RemoveObserver(this);
}
......@@ -247,3 +254,11 @@ base::FilePath SessionRestorationBrowserAgent::GetSessionStoragePath(
return path;
}
// WebStateObserver methods
void SessionRestorationBrowserAgent::DidFinishNavigation(
web::WebState* web_state,
web::NavigationContext* navigation_context) {
// Save the session each time a navigation finishes.
SaveSession(/*immediately=*/false);
}
......@@ -3755,13 +3755,6 @@ NSString* const kBrowserViewControllerSnackbarCategory =
[self updateToolbar];
}
- (void)webState:(web::WebState*)webState
didFinishNavigation:(web::NavigationContext*)navigation {
SessionRestorationBrowserAgent::FromBrowser(self.browser)
->SaveSession(
/*immediately=*/false);
}
- (void)webState:(web::WebState*)webState didLoadPageWithSuccess:(BOOL)success {
[_toolbarUIUpdater updateState];
if ([self canShowTabStrip]) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment