Commit fe0ad27c authored by Sylvain Defresne's avatar Sylvain Defresne Committed by Chromium LUCI CQ

[ios] Refactor CrashRestoreHelper for multi-window and multi-profile

The code to deal with multi-window enabled/disabled was duplicated in
CrashRestoreHelper where calling the multi-window enabled code with a
session with an empty name was the same as calling code for single
window. Refactor the code to avoid the duplication.

Change the backup directory used for the saved session when restore
is in progress as the single directory approach would not work with
multi-profile.

Bug: 1165798
Change-Id: Ifa6efe051999b77688a8a6ffdb8b9ed3b185e60e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2632677
Commit-Queue: Sylvain Defresne <sdefresne@chromium.org>
Reviewed-by: default avatarStepan Khapugin <stkhapugin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#845174}
parent da7b2ee3
...@@ -485,16 +485,13 @@ void MainControllerAuthenticationServiceDelegate::ClearBrowsingData( ...@@ -485,16 +485,13 @@ void MainControllerAuthenticationServiceDelegate::ClearBrowsingData(
// browser state. // browser state.
BOOL needRestoration = NO; BOOL needRestoration = NO;
if (isPostCrashLaunch) { if (isPostCrashLaunch) {
if (IsMultiwindowSupported()) { NSSet<NSString*>* sessions =
NSSet<NSString*>* sessions = IsMultiwindowSupported()
[[PreviousSessionInfo sharedInstance] connectedSceneSessionsIDs]; ? [[PreviousSessionInfo sharedInstance] connectedSceneSessionsIDs]
needRestoration = : [NSSet setWithArray:@[ @"" ]];
[CrashRestoreHelper moveAsideSessions:sessions
forBrowserState:chromeBrowserState]; needRestoration = [CrashRestoreHelper moveAsideSessions:sessions
} else { forBrowserState:chromeBrowserState];
needRestoration = [CrashRestoreHelper
moveAsideSessionInformationForBrowserState:chromeBrowserState];
}
} }
if (!IsMultipleScenesSupported() && IsMultiwindowSupported()) { if (!IsMultipleScenesSupported() && IsMultiwindowSupported()) {
NSSet<NSString*>* previousSessions = NSSet<NSString*>* previousSessions =
......
...@@ -16,7 +16,8 @@ class ChromeBrowserState; ...@@ -16,7 +16,8 @@ class ChromeBrowserState;
- (instancetype)initWithBrowser:(Browser*)browser; - (instancetype)initWithBrowser:(Browser*)browser;
// Returns YES if a backup file for sessionID can be found on disk. // Returns YES if a backup file for sessionID can be found on disk.
+ (BOOL)isBackedUpSessionID:(NSString*)sessionID; + (BOOL)isBackedUpSessionID:(NSString*)sessionID
browserState:(ChromeBrowserState*)browserState;
// Saves the session information stored on disk for sessions with |sessionIDs| // Saves the session information stored on disk for sessions with |sessionIDs|
// in temporary files and will then delete those from their default location. // in temporary files and will then delete those from their default location.
...@@ -29,17 +30,18 @@ class ChromeBrowserState; ...@@ -29,17 +30,18 @@ class ChromeBrowserState;
+ (BOOL)moveAsideSessions:(NSSet<NSString*>*)sessionIDs + (BOOL)moveAsideSessions:(NSSet<NSString*>*)sessionIDs
forBrowserState:(ChromeBrowserState*)browserState; forBrowserState:(ChromeBrowserState*)browserState;
// Move the session information for Legacy non multiwindow supported OS.
// This method deletes the session from its default location, while
// allowing restoring it back later.
// Returns |YES| if the delettion and backup was successful.
+ (BOOL)moveAsideSessionInformationForBrowserState:
(ChromeBrowserState*)browserState;
// Shows an infobar on the currently active tab of the browser. This infobar // Shows an infobar on the currently active tab of the browser. This infobar
// lets the user restore its session after a crash. // lets the user restore its session after a crash.
- (void)showRestorePrompt; - (void)showRestorePrompt;
@end @end
@interface CrashRestoreHelper (Testing)
// Returns the path for back of session |sessionID| relative in |directory|.
+ (NSString*)backupPathForSessionID:(NSString*)sessionID
directory:(NSString*)directory;
@end
#endif // IOS_CHROME_BROWSER_CRASH_REPORT_CRASH_RESTORE_HELPER_H_ #endif // IOS_CHROME_BROWSER_CRASH_REPORT_CRASH_RESTORE_HELPER_H_
...@@ -28,10 +28,6 @@ ...@@ -28,10 +28,6 @@
using testing::Return; using testing::Return;
@interface CrashRestoreHelper (Test)
+ (NSString*)backupPathForSessionID:(NSString*)sessionID;
@end
namespace { namespace {
class CrashRestoreHelperTest : public PlatformTest { class CrashRestoreHelperTest : public PlatformTest {
...@@ -55,13 +51,14 @@ class CrashRestoreHelperTest : public PlatformTest { ...@@ -55,13 +51,14 @@ class CrashRestoreHelperTest : public PlatformTest {
chrome_browser_state_.get(), chrome_browser_state_.get(),
off_the_record_chrome_browser_state_, off_the_record_chrome_browser_state_,
}; };
NSString* backup_path =
[CrashRestoreHelper backupPathForSessionID:session_id];
[file_manager removeItemAtPath:backup_path error:nil];
NSData* data = [NSData dataWithBytes:"hello" length:5]; NSData* data = [NSData dataWithBytes:"hello" length:5];
for (size_t index = 0; index < base::size(browser_states); ++index) { for (size_t index = 0; index < base::size(browser_states); ++index) {
NSString* state_path = base::SysUTF8ToNSString( NSString* state_path = base::SysUTF8ToNSString(
browser_states[index]->GetStatePath().value()); browser_states[index]->GetStatePath().value());
NSString* backup_path =
[CrashRestoreHelper backupPathForSessionID:session_id
directory:state_path];
[file_manager removeItemAtPath:backup_path error:nil];
NSString* session_path = NSString* session_path =
[SessionServiceIOS sessionPathForSessionID:session_id [SessionServiceIOS sessionPathForSessionID:session_id
directory:state_path]; directory:state_path];
...@@ -103,10 +100,14 @@ class CrashRestoreHelperTest : public PlatformTest { ...@@ -103,10 +100,14 @@ class CrashRestoreHelperTest : public PlatformTest {
// Returns |true| if the session with |session_id| was backed up correctly, // Returns |true| if the session with |session_id| was backed up correctly,
// and deletes the backup file. if |session_id| is nil, the default backup // and deletes the backup file. if |session_id| is nil, the default backup
// session location is used. // session location is used.
bool CheckAndDeleteSessionBackedUp(NSString* session_id) { bool CheckAndDeleteSessionBackedUp(NSString* session_id,
ChromeBrowserState* browser_state) {
NSFileManager* file_manager = [NSFileManager defaultManager]; NSFileManager* file_manager = [NSFileManager defaultManager];
NSString* backup_path = NSString* backup_path = [CrashRestoreHelper
[CrashRestoreHelper backupPathForSessionID:session_id]; backupPathForSessionID:session_id
directory:base::SysUTF8ToNSString(
browser_state->GetStatePath()
.AsUTF8Unsafe())];
if (![file_manager fileExistsAtPath:backup_path]) if (![file_manager fileExistsAtPath:backup_path])
return false; return false;
[file_manager removeItemAtPath:backup_path error:nil]; [file_manager removeItemAtPath:backup_path error:nil];
...@@ -123,14 +124,12 @@ class CrashRestoreHelperTest : public PlatformTest { ...@@ -123,14 +124,12 @@ class CrashRestoreHelperTest : public PlatformTest {
// Tests that moving session work correctly when multiple windows are not // Tests that moving session work correctly when multiple windows are not
// supported. // supported.
TEST_F(CrashRestoreHelperTest, MoveAsideSingleSession) { TEST_F(CrashRestoreHelperTest, MoveAsideSingleSession) {
// This test is only enabled when multi-window is disabled.
if (IsMultiwindowSupported())
return;
ASSERT_TRUE(CreateSession(nil)); ASSERT_TRUE(CreateSession(nil));
[CrashRestoreHelper [CrashRestoreHelper moveAsideSessions:[NSSet setWithArray:@[ @"" ]]
moveAsideSessionInformationForBrowserState:chrome_browser_state_.get()]; forBrowserState:chrome_browser_state_.get()];
EXPECT_TRUE(IsSessionErased(nil)); EXPECT_TRUE(IsSessionErased(nil));
EXPECT_EQ(YES, CheckAndDeleteSessionBackedUp(nil)); EXPECT_EQ(YES,
CheckAndDeleteSessionBackedUp(nil, chrome_browser_state_.get()));
} }
// Tests that moving session work correctly when multiple windows are supported. // Tests that moving session work correctly when multiple windows are supported.
...@@ -145,7 +144,8 @@ TEST_F(CrashRestoreHelperTest, MoveAsideMultipleSessions) { ...@@ -145,7 +144,8 @@ TEST_F(CrashRestoreHelperTest, MoveAsideMultipleSessions) {
forBrowserState:chrome_browser_state_.get()]; forBrowserState:chrome_browser_state_.get()];
for (NSString* session_id in session_ids) { for (NSString* session_id in session_ids) {
EXPECT_TRUE(IsSessionErased(session_id)); EXPECT_TRUE(IsSessionErased(session_id));
EXPECT_EQ(YES, CheckAndDeleteSessionBackedUp(session_id)); EXPECT_EQ(YES, CheckAndDeleteSessionBackedUp(session_id,
chrome_browser_state_.get()));
} }
} }
......
...@@ -215,7 +215,7 @@ NSString* const kSessionFileName = ...@@ -215,7 +215,7 @@ NSString* const kSessionFileName =
+ (NSString*)sessionPathForSessionID:(NSString*)sessionID + (NSString*)sessionPathForSessionID:(NSString*)sessionID
directory:(NSString*)directory { directory:(NSString*)directory {
if (!sessionID) if (!sessionID.length)
return [[self class] sessionPathForDirectory:directory]; return [[self class] sessionPathForDirectory:directory];
return [NSString pathWithComponents:@[ return [NSString pathWithComponents:@[
directory, kSessionDirectory, sessionID, kSessionFileName directory, kSessionDirectory, sessionID, kSessionFileName
......
...@@ -677,12 +677,13 @@ const char kMultiWindowOpenInNewWindowHistogram[] = ...@@ -677,12 +677,13 @@ const char kMultiWindowOpenInNewWindowHistogram[] =
// Only create the restoration helper if the session with the current session // Only create the restoration helper if the session with the current session
// id was backed up successfully. // id was backed up successfully.
if (self.sceneState.appState.sessionRestorationRequired) { if (self.sceneState.appState.sessionRestorationRequired) {
Browser* mainBrowser = self.mainInterface.browser;
if (!IsMultiwindowSupported() || if (!IsMultiwindowSupported() ||
[CrashRestoreHelper [CrashRestoreHelper
isBackedUpSessionID:self.sceneState.sceneSessionID]) { isBackedUpSessionID:self.sceneState.sceneSessionID
browserState:mainBrowser->GetBrowserState()]) {
self.sceneState.appState.startupInformation.restoreHelper = self.sceneState.appState.startupInformation.restoreHelper =
[[CrashRestoreHelper alloc] [[CrashRestoreHelper alloc] initWithBrowser:mainBrowser];
initWithBrowser:self.mainInterface.browser];
} }
} }
......
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