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