Commit b8760c06 authored by Mohammad Refaat's avatar Mohammad Refaat Committed by Commit Bot

Use Browser in CrashRestoreHelper instead of TabModel

TabModel is provided to the CrashRestoreHelper as a SessionWindowRestore
protocol delegate. and it's used to restore sessions from there. Replace
that with browser and use sessionRestorationBrowserAgent from the
CrashRestoreHelper instead.
Also while at it, i changed the members ivars to use trailing underscore
instead of the leading underscore.

Bug: 1010164
Change-Id: I53360ac9a34f7f9e42aacd459e233f40c5def60b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2024028
Commit-Queue: Mohammad Refaat <mrefaat@chromium.org>
Reviewed-by: default avatarMark Cogan <marq@chromium.org>
Cr-Commit-Position: refs/heads/master@{#736047}
parent 0311a528
......@@ -569,7 +569,7 @@ void MainControllerAuthenticationServiceDelegate::ClearBrowsingData(
BOOL postCrashLaunch = [self mustShowRestoreInfobar];
if (postCrashLaunch) {
self.restoreHelper =
[[CrashRestoreHelper alloc] initWithBrowserState:chromeBrowserState];
[[CrashRestoreHelper alloc] initWithBrowser:self.mainBrowser];
[self.restoreHelper moveAsideSessionInformation];
}
......@@ -668,11 +668,7 @@ void MainControllerAuthenticationServiceDelegate::ClearBrowsingData(
// The startup parameters may create new tabs or navigations. If the restore
// infobar is displayed now, it may be dismissed immediately and the user
// will never be able to restore the session.
TabModel* currentTabModel = [self currentTabModel];
[self.restoreHelper
showRestoreIfNeededUsingWebState:currentTabModel.webStateList
->GetActiveWebState()
sessionRestorer:currentTabModel];
[self.restoreHelper showRestoreIfNeeded];
self.restoreHelper = nil;
}
......
......@@ -63,7 +63,9 @@ source_set("crash_report_internal") {
"//ios/chrome/browser/browser_state",
"//ios/chrome/browser/infobars",
"//ios/chrome/browser/infobars:public",
"//ios/chrome/browser/main:public",
"//ios/chrome/browser/sessions",
"//ios/chrome/browser/sessions:restoration_agent",
"//ios/chrome/browser/sessions:serialisation",
"//ios/chrome/browser/sessions:session_service",
"//ios/chrome/browser/ui/infobars:feature_flags",
......@@ -92,6 +94,7 @@ source_set("unit_tests") {
"//ios/chrome/browser/browser_state",
"//ios/chrome/browser/browser_state:test_support",
"//ios/chrome/browser/crash_report/breadcrumbs",
"//ios/chrome/browser/main:test_support",
"//ios/chrome/browser/sessions:serialisation",
"//ios/chrome/browser/sessions:session_service",
"//ios/chrome/test/ocmock",
......
......@@ -7,17 +7,12 @@
#import <Foundation/Foundation.h>
class ChromeBrowserState;
@protocol SessionWindowRestoring;
namespace web {
class WebState;
}
class Browser;
// Helper class for handling session restoration after a crash.
@interface CrashRestoreHelper : NSObject
- (instancetype)initWithBrowserState:(ChromeBrowserState*)browserState;
- (instancetype)initWithBrowser:(Browser*)browser;
// Saves the session information stored on disk in temporary files and will
// then delete those from their default location. This will ensure that the
......@@ -25,10 +20,9 @@ class WebState;
// sessions.
- (void)moveAsideSessionInformation;
// Shows an infobar on the currently selected tab of the given |tabModel|. This
// infobar lets the user restore its session after a crash.
- (void)showRestoreIfNeededUsingWebState:(web::WebState*)webState
sessionRestorer:(id<SessionWindowRestoring>)restorer;
// Shows an infobar on the currently active tab of the browser. This infobar
// lets the user restore its session after a crash.
- (void)showRestoreIfNeeded;
@end
......
......@@ -24,11 +24,12 @@
#include "ios/chrome/browser/infobars/infobar_ios.h"
#include "ios/chrome/browser/infobars/infobar_manager_impl.h"
#include "ios/chrome/browser/infobars/infobar_utils.h"
#import "ios/chrome/browser/main/browser.h"
#include "ios/chrome/browser/sessions/ios_chrome_tab_restore_service_factory.h"
#import "ios/chrome/browser/sessions/session_ios.h"
#import "ios/chrome/browser/sessions/session_restoration_browser_agent.h"
#import "ios/chrome/browser/sessions/session_service_ios.h"
#import "ios/chrome/browser/sessions/session_window_ios.h"
#import "ios/chrome/browser/sessions/session_window_restoring.h"
#import "ios/chrome/browser/ui/infobars/infobar_feature.h"
#include "ios/chrome/browser/web_state_list/web_state_list.h"
#include "ios/chrome/grit/ios_theme_resources.h"
......@@ -204,42 +205,39 @@ int SessionCrashedInfoBarDelegate::GetIconId() const {
} // namespace
@implementation CrashRestoreHelper {
ChromeBrowserState* _browserState;
BOOL _needRestoration;
std::unique_ptr<InfoBarManagerObserverBridge> _infoBarBridge;
// Object that will handle session restoration.
id<SessionWindowRestoring> _restorer;
Browser* browser_;
BOOL needRestoration_;
std::unique_ptr<InfoBarManagerObserverBridge> infoBarBridge_;
// Indicate that the session has been restored to tabs or to recently closed
// and should not be rerestored.
BOOL _sessionRestored;
BOOL sessionRestored_;
}
- (id)initWithBrowserState:(ChromeBrowserState*)browserState {
- (instancetype)initWithBrowser:(Browser*)browser {
if (self = [super init]) {
_browserState = browserState;
browser_ = browser;
}
return self;
}
- (void)showRestoreIfNeededUsingWebState:(web::WebState*)webState
sessionRestorer:(id<SessionWindowRestoring>)restorer {
if (!_needRestoration)
- (void)showRestoreIfNeeded {
if (!needRestoration_)
return;
// Get the active webState to show the infobar on it.
web::WebState* webState = browser_->GetWebStateList()->GetActiveWebState();
// The last session didn't exit cleanly. Show an infobar to the user so
// that they can restore if they want. The delegate deletes itself when
// it is closed.
DCHECK(webState);
infobars::InfoBarManager* infoBarManager =
InfoBarManagerImpl::FromWebState(webState);
_restorer = restorer;
SessionCrashedInfoBarDelegate::Create(infoBarManager, self);
[ConfirmInfobarMetricsRecorder
recordConfirmInfobarEvent:MobileMessagesConfirmInfobarEvents::Presented
forInfobarConfirmType:InfobarConfirmType::kInfobarConfirmTypeRestore];
_infoBarBridge.reset(new InfoBarManagerObserverBridge(infoBarManager, self));
infoBarBridge_.reset(new InfoBarManagerObserverBridge(infoBarManager, self));
}
- (BOOL)deleteSessionForBrowserState:(ChromeBrowserState*)browserState
......@@ -291,17 +289,17 @@ int SessionCrashedInfoBarDelegate::GetIconId() const {
// This may be the first time that the OTR browser state is being accessed, so
// ensure that the OTR ChromeBrowserState is created first.
ChromeBrowserState* otrBrowserState =
_browserState->GetOffTheRecordChromeBrowserState();
browser_->GetBrowserState()->GetOffTheRecordChromeBrowserState();
[self deleteSessionForBrowserState:otrBrowserState backupFile:nil];
_needRestoration =
[self deleteSessionForBrowserState:_browserState
needRestoration_ =
[self deleteSessionForBrowserState:browser_->GetBrowserState()
backupFile:[self sessionBackupPath]];
}
- (BOOL)restoreSessionsAfterCrash {
DCHECK(!_sessionRestored);
_sessionRestored = YES;
_infoBarBridge.reset();
DCHECK(!sessionRestored_);
sessionRestored_ = YES;
infoBarBridge_.reset();
SessionIOS* session = [[SessionServiceIOS sharedService]
loadSessionFromPath:[self sessionBackupPath]];
......@@ -310,13 +308,13 @@ int SessionCrashedInfoBarDelegate::GetIconId() const {
DCHECK_EQ(session.sessionWindows.count, 1u);
breakpad_helper::WillStartCrashRestoration();
return [_restorer restoreSessionWindow:session.sessionWindows[0]
forInitialRestore:NO];
return SessionRestorationBrowserAgent::FromBrowser(browser_)
->RestoreSessionWindow(session.sessionWindows[0]);
}
- (void)infoBarRemoved:(infobars::InfoBar*)infobar {
DCHECK(infobar->delegate());
if (_sessionRestored ||
if (sessionRestored_ ||
infobar->delegate()->GetIdentifier() !=
infobars::InfoBarDelegate::SESSION_CRASHED_INFOBAR_DELEGATE_IOS) {
return;
......@@ -325,7 +323,7 @@ int SessionCrashedInfoBarDelegate::GetIconId() const {
// If the infobar is dismissed without restoring the tabs (either by closing
// it with the cross or after a navigation), all the entries will be added to
// the recently closed tabs.
_sessionRestored = YES;
sessionRestored_ = YES;
SessionIOS* session = [[SessionServiceIOS sharedService]
loadSessionFromPath:[self sessionBackupPath]];
......@@ -336,10 +334,11 @@ int SessionCrashedInfoBarDelegate::GetIconId() const {
return;
sessions::TabRestoreService* const tabRestoreService =
IOSChromeTabRestoreServiceFactory::GetForBrowserState(_browserState);
IOSChromeTabRestoreServiceFactory::GetForBrowserState(
browser_->GetBrowserState());
tabRestoreService->LoadTabsFromLastSession();
web::WebState::CreateParams params(_browserState);
web::WebState::CreateParams params(browser_->GetBrowserState());
for (CRWSessionStorage* session in sessions) {
auto live_tab = std::make_unique<sessions::RestoreIOSLiveTab>(session);
// Add all tabs at the 0 position as the position is relative to an old
......
......@@ -13,6 +13,7 @@
#include "ios/chrome/browser/browser_state/chrome_browser_state.h"
#include "ios/chrome/browser/browser_state/test_chrome_browser_state.h"
#include "ios/chrome/browser/crash_report/crash_restore_helper.h"
#import "ios/chrome/browser/main/test_browser.h"
#import "ios/chrome/browser/sessions/session_service_ios.h"
#include "ios/web/public/test/web_task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
......@@ -40,13 +41,14 @@ class CrashRestoreHelperTest : public PlatformTest {
chrome_browser_state_ = test_cbs_builder.Build();
off_the_record_chrome_browser_state_ =
chrome_browser_state_->GetOffTheRecordChromeBrowserState();
helper_ = [[CrashRestoreHelper alloc]
initWithBrowserState:chrome_browser_state_.get()];
test_browser_ = std::make_unique<TestBrowser>(chrome_browser_state_.get());
helper_ = [[CrashRestoreHelper alloc] initWithBrowser:test_browser_.get()];
}
protected:
web::WebTaskEnvironment task_environment_;
std::unique_ptr<TestChromeBrowserState> chrome_browser_state_;
std::unique_ptr<TestBrowser> test_browser_;
ChromeBrowserState* off_the_record_chrome_browser_state_;
CrashRestoreHelper* helper_;
};
......
......@@ -1080,11 +1080,7 @@ enum class TabSwitcherDismissalMode { NONE, NORMAL, INCOGNITO };
// Now that all the operations on the tabs have been done, display the
// restore infobar if needed.
dispatch_async(dispatch_get_main_queue(), ^{
TabModel* currentTabModel = [self currentTabModel];
[self.mainController.restoreHelper
showRestoreIfNeededUsingWebState:currentTabModel.webStateList
->GetActiveWebState()
sessionRestorer:currentTabModel];
[self.mainController.restoreHelper showRestoreIfNeeded];
self.mainController.restoreHelper = nil;
});
}
......
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