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

[ios] Use base::FilePath for SessionServiceIOS/CrashRestoreHelper APIs

Requiring the BrowserState's path to be passed as a NSString* forces
all callers to convert the base::FilePath to NSString* making the API
harder to use (as the conversion is non trivial).

Instead change the API of both classes to use base::FilePath directly
and to do the conversion internally where it can be centralized in a
few location makes the API easier to use.

Bug: 1165798
Change-Id: Ida8294210f83875fcbeffa88a3e7f5ae78a2316f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2635668
Commit-Queue: Sylvain Defresne <sdefresne@chromium.org>
Reviewed-by: default avatarOlivier Robin <olivierrobin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#846070}
parent a1aafb0b
...@@ -276,10 +276,11 @@ void BrowsingDataRemoverImpl::RemoveImpl(base::Time delete_begin, ...@@ -276,10 +276,11 @@ void BrowsingDataRemoverImpl::RemoveImpl(base::Time delete_begin,
if (IsRemoveDataMaskSet(mask, BrowsingDataRemoveMask::REMOVE_HISTORY)) { if (IsRemoveDataMaskSet(mask, BrowsingDataRemoveMask::REMOVE_HISTORY)) {
if (session_service_) { if (session_service_) {
NSString* state_path = base::SysUTF8ToNSString( const base::FilePath& state_path = browser_state_->GetStatePath();
browser_state_->GetStatePath().AsUTF8Unsafe()); [session_service_
[session_service_ deleteAllSessionFilesInBrowserStateDirectory:state_path deleteAllSessionFilesInDirectory:state_path
completion:CreatePendingTaskCompletionClosure()]; completion:
CreatePendingTaskCompletionClosure()];
} }
// Remove the screenshots taken by the system when backgrounding the // Remove the screenshots taken by the system when backgrounding the
...@@ -558,10 +559,9 @@ void BrowsingDataRemoverImpl::RemoveImpl(base::Time delete_begin, ...@@ -558,10 +559,9 @@ void BrowsingDataRemoverImpl::RemoveImpl(base::Time delete_begin,
void BrowsingDataRemoverImpl::RemoveSessionsData( void BrowsingDataRemoverImpl::RemoveSessionsData(
NSArray<NSString*>* session_ids) { NSArray<NSString*>* session_ids) {
[[SessionServiceIOS sharedService] [[SessionServiceIOS sharedService]
deleteSessions:session_ids deleteSessions:session_ids
fromBrowserStateDirectory:base::SysUTF8ToNSString( directory:browser_state_->GetStatePath()
browser_state_->GetStatePath() completion:base::DoNothing()];
.AsUTF8Unsafe())];
} }
// TODO(crbug.com/619783): removing data from WkWebsiteDataStore should be // TODO(crbug.com/619783): removing data from WkWebsiteDataStore should be
......
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#include "base/files/file_path.h"
class Browser; class Browser;
class ChromeBrowserState; class ChromeBrowserState;
...@@ -40,7 +42,7 @@ class ChromeBrowserState; ...@@ -40,7 +42,7 @@ class ChromeBrowserState;
// Returns the path for back of session |sessionID| relative in |directory|. // Returns the path for back of session |sessionID| relative in |directory|.
+ (NSString*)backupPathForSessionID:(NSString*)sessionID + (NSString*)backupPathForSessionID:(NSString*)sessionID
directory:(NSString*)directory; directory:(const base::FilePath&)directory;
@end @end
......
...@@ -71,12 +71,19 @@ ...@@ -71,12 +71,19 @@
namespace { namespace {
NSString* const kSessionBackupDirectory = // The name for directory which contains all session backup subdirectories for
@"Backups"; // The name for directory which contains all session backup // multiple sessions.
// subdirectories for multiple sessions. const base::FilePath::CharType kSessionBackupDirectory[] =
FILE_PATH_LITERAL("Backups");
NSString* const kSessionBackupFileName =
@"session.backup.plist"; // The session file name on disk. // The session file name on disk.
const base::FilePath::CharType kSessionBackupFileName[] =
FILE_PATH_LITERAL("session.backup.plist");
// Convert |path| to NSString.
NSString* PathAsNSString(const base::FilePath& path) {
return base::SysUTF8ToNSString(path.AsUTF8Unsafe());
}
class InfoBarManagerObserverBridge : infobars::InfoBarManager::Observer { class InfoBarManagerObserverBridge : infobars::InfoBarManager::Observer {
public: public:
...@@ -261,8 +268,7 @@ int SessionCrashedInfoBarDelegate::GetIconId() const { ...@@ -261,8 +268,7 @@ int SessionCrashedInfoBarDelegate::GetIconId() const {
forBrowserState:(ChromeBrowserState*)browserState forBrowserState:(ChromeBrowserState*)browserState
shouldBackup:(BOOL)shouldBackup { shouldBackup:(BOOL)shouldBackup {
BOOL partialSuccess = NO; BOOL partialSuccess = NO;
NSString* stashPath = const base::FilePath& stashPath = browserState->GetStatePath();
base::SysUTF8ToNSString(browserState->GetStatePath().value());
for (NSString* sessionID in sessionIDs) { for (NSString* sessionID in sessionIDs) {
NSString* sessionPath = NSString* sessionPath =
...@@ -325,27 +331,25 @@ int SessionCrashedInfoBarDelegate::GetIconId() const { ...@@ -325,27 +331,25 @@ int SessionCrashedInfoBarDelegate::GetIconId() const {
} }
+ (NSString*)backupPathForSessionID:(NSString*)sessionID + (NSString*)backupPathForSessionID:(NSString*)sessionID
directory:(NSString*)directory { directory:(const base::FilePath&)directory {
// TODO(crbug.com/1165798): remove when the sessionID is guaranteed to
// always be an non-empty string.
if (!sessionID.length) if (!sessionID.length)
return [directory stringByAppendingPathComponent:kSessionBackupFileName]; return PathAsNSString(directory.Append(kSessionBackupFileName));
return [NSString pathWithComponents:@[ return PathAsNSString(directory.Append(kSessionBackupDirectory)
directory, .Append(base::SysNSStringToUTF8(sessionID))
kSessionBackupDirectory, .Append(kSessionBackupFileName));
sessionID,
kSessionBackupFileName,
]];
} }
+ (NSArray<NSString*>*)backedupSessionIDsForBrowserState: + (NSArray<NSString*>*)backedupSessionIDsForBrowserState:
(ChromeBrowserState*)browserState { (ChromeBrowserState*)browserState {
if (!IsMultiwindowSupported()) if (!IsMultiwindowSupported())
return @[ @"" ]; return @[ @"" ];
NSString* stashPath = const base::FilePath backupDirectory =
base::SysUTF8ToNSString(browserState->GetStatePath().AsUTF8Unsafe()); browserState->GetStatePath().Append(kSessionBackupDirectory);
return [[NSFileManager defaultManager] return [[NSFileManager defaultManager]
contentsOfDirectoryAtPath: contentsOfDirectoryAtPath:PathAsNSString(backupDirectory)
[stashPath stringByAppendingPathComponent:kSessionBackupDirectory]
error:nil]; error:nil];
} }
...@@ -389,8 +393,7 @@ int SessionCrashedInfoBarDelegate::GetIconId() const { ...@@ -389,8 +393,7 @@ int SessionCrashedInfoBarDelegate::GetIconId() const {
+ (BOOL)restoreSessionsAfterCrashForBrowserState: + (BOOL)restoreSessionsAfterCrashForBrowserState:
(ChromeBrowserState*)browserState { (ChromeBrowserState*)browserState {
NSString* stashPath = const base::FilePath& stashPath = browserState->GetStatePath();
base::SysUTF8ToNSString(browserState->GetStatePath().AsUTF8Unsafe());
BrowserList* browserList = BrowserList* browserList =
BrowserListFactory::GetForBrowserState(browserState); BrowserListFactory::GetForBrowserState(browserState);
...@@ -472,8 +475,7 @@ int SessionCrashedInfoBarDelegate::GetIconId() const { ...@@ -472,8 +475,7 @@ int SessionCrashedInfoBarDelegate::GetIconId() const {
_sessionRestored = YES; _sessionRestored = YES;
ChromeBrowserState* browserState = _browser->GetBrowserState(); ChromeBrowserState* browserState = _browser->GetBrowserState();
NSString* stashPath = const base::FilePath& stashPath = browserState->GetStatePath();
base::SysUTF8ToNSString(browserState->GetStatePath().AsUTF8Unsafe());
NSArray<NSString*>* sessionsIDs = NSArray<NSString*>* sessionsIDs =
[CrashRestoreHelper backedupSessionIDsForBrowserState:browserState]; [CrashRestoreHelper backedupSessionIDsForBrowserState:browserState];
......
...@@ -53,8 +53,7 @@ class CrashRestoreHelperTest : public PlatformTest { ...@@ -53,8 +53,7 @@ class CrashRestoreHelperTest : public PlatformTest {
}; };
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( const base::FilePath& state_path = browser_states[index]->GetStatePath();
browser_states[index]->GetStatePath().value());
NSString* backup_path = NSString* backup_path =
[CrashRestoreHelper backupPathForSessionID:session_id [CrashRestoreHelper backupPathForSessionID:session_id
directory:state_path]; directory:state_path];
...@@ -86,8 +85,7 @@ class CrashRestoreHelperTest : public PlatformTest { ...@@ -86,8 +85,7 @@ class CrashRestoreHelperTest : public PlatformTest {
}; };
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( const base::FilePath& state_path = browser_states[index]->GetStatePath();
browser_states[index]->GetStatePath().value());
NSString* session_path = NSString* session_path =
[SessionServiceIOS sessionPathForSessionID:session_id [SessionServiceIOS sessionPathForSessionID:session_id
directory:state_path]; directory:state_path];
...@@ -105,9 +103,7 @@ class CrashRestoreHelperTest : public PlatformTest { ...@@ -105,9 +103,7 @@ class CrashRestoreHelperTest : public PlatformTest {
NSFileManager* file_manager = [NSFileManager defaultManager]; NSFileManager* file_manager = [NSFileManager defaultManager];
NSString* backup_path = [CrashRestoreHelper NSString* backup_path = [CrashRestoreHelper
backupPathForSessionID:session_id backupPathForSessionID:session_id
directory:base::SysUTF8ToNSString( directory:browser_state->GetStatePath()];
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];
......
...@@ -176,7 +176,6 @@ bool SessionRestorationBrowserAgent::RestoreSession() { ...@@ -176,7 +176,6 @@ bool SessionRestorationBrowserAgent::RestoreSession() {
PreviousSessionInfo* session_info = [PreviousSessionInfo sharedInstance]; PreviousSessionInfo* session_info = [PreviousSessionInfo sharedInstance];
auto scoped_restore = [session_info startSessionRestoration]; auto scoped_restore = [session_info startSessionRestoration];
const base::FilePath& path = browser_state_->GetStatePath();
NSString* session_id = NSString* session_id =
(IsMultiwindowSupported() && session_info.isMultiWindowEnabledSession) (IsMultiwindowSupported() && session_info.isMultiWindowEnabledSession)
? base::SysUTF8ToNSString(session_identifier_) ? base::SysUTF8ToNSString(session_identifier_)
...@@ -184,7 +183,7 @@ bool SessionRestorationBrowserAgent::RestoreSession() { ...@@ -184,7 +183,7 @@ bool SessionRestorationBrowserAgent::RestoreSession() {
SessionIOS* session = [session_service_ SessionIOS* session = [session_service_
loadSessionWithSessionID:session_id loadSessionWithSessionID:session_id
directory:base::SysUTF8ToNSString(path.AsUTF8Unsafe())]; directory:browser_state_->GetStatePath()];
SessionWindowIOS* session_window = nil; SessionWindowIOS* session_window = nil;
if (session) { if (session) {
...@@ -203,10 +202,9 @@ void SessionRestorationBrowserAgent::SaveSession(bool immediately) { ...@@ -203,10 +202,9 @@ void SessionRestorationBrowserAgent::SaveSession(bool immediately) {
if (!CanSaveSession()) if (!CanSaveSession())
return; return;
const base::FilePath& path = browser_state_->GetStatePath();
[session_service_ saveSession:session_ios_factory_ [session_service_ saveSession:session_ios_factory_
sessionID:base::SysUTF8ToNSString(session_identifier_) sessionID:base::SysUTF8ToNSString(session_identifier_)
directory:base::SysUTF8ToNSString(path.AsUTF8Unsafe()) directory:browser_state_->GetStatePath()
immediately:immediately]; immediately:immediately];
} }
......
...@@ -223,8 +223,7 @@ TEST_F(SessionRestorationBrowserAgentTest, SaveAndRestoreEmptySession) { ...@@ -223,8 +223,7 @@ TEST_F(SessionRestorationBrowserAgentTest, SaveAndRestoreEmptySession) {
[test_session_service_ setPerformIO:NO]; [test_session_service_ setPerformIO:NO];
// Restore, expect that there are no sessions. // Restore, expect that there are no sessions.
NSString* state_path = base::SysUTF8ToNSString( const base::FilePath& state_path = chrome_browser_state_->GetStatePath();
chrome_browser_state_->GetStatePath().AsUTF8Unsafe());
SessionIOS* session = SessionIOS* session =
[test_session_service_ loadSessionWithSessionID:nil directory:state_path]; [test_session_service_ loadSessionWithSessionID:nil directory:state_path];
ASSERT_EQ(1u, session.sessionWindows.count); ASSERT_EQ(1u, session.sessionWindows.count);
...@@ -253,8 +252,7 @@ TEST_F(SessionRestorationBrowserAgentTest, SaveAndRestoreSession) { ...@@ -253,8 +252,7 @@ TEST_F(SessionRestorationBrowserAgentTest, SaveAndRestoreSession) {
// close all the webStates // close all the webStates
web_state_list_->CloseAllWebStates(WebStateList::CLOSE_NO_FLAGS); web_state_list_->CloseAllWebStates(WebStateList::CLOSE_NO_FLAGS);
NSString* state_path = base::SysUTF8ToNSString( const base::FilePath& state_path = chrome_browser_state_->GetStatePath();
chrome_browser_state_->GetStatePath().AsUTF8Unsafe());
SessionIOS* session = SessionIOS* session =
[test_session_service_ loadSessionWithSessionID:nil directory:state_path]; [test_session_service_ loadSessionWithSessionID:nil directory:state_path];
ASSERT_EQ(1u, session.sessionWindows.count); ASSERT_EQ(1u, session.sessionWindows.count);
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#include "base/callback.h" #include "base/callback.h"
#include "base/files/file_path.h"
#include "base/sequenced_task_runner.h" #include "base/sequenced_task_runner.h"
@class SessionIOS; @class SessionIOS;
...@@ -35,32 +36,31 @@ ...@@ -35,32 +36,31 @@
// avoid blocking the UI thread. // avoid blocking the UI thread.
- (void)saveSession:(__weak SessionIOSFactory*)factory - (void)saveSession:(__weak SessionIOSFactory*)factory
sessionID:(NSString*)sessionID sessionID:(NSString*)sessionID
directory:(NSString*)directory directory:(const base::FilePath&)directory
immediately:(BOOL)immediately; immediately:(BOOL)immediately;
// Loads a session (list of tabs) from the save location derived from the scene // Loads a session (list of tabs) from the save location derived from the scene
// identifier |sessionID| and the ChromeBrowserState |directory|. // identifier |sessionID| and the ChromeBrowserState |directory|.
- (SessionIOS*)loadSessionWithSessionID:(NSString*)sessionID - (SessionIOS*)loadSessionWithSessionID:(NSString*)sessionID
directory:(NSString*)directory; directory:(const base::FilePath&)directory;
// Loads the session from |sessionPath| on the main thread. Returns nil in case // Loads the session from |sessionPath| on the main thread. Returns nil in case
// of errors. // of errors.
- (SessionIOS*)loadSessionFromPath:(NSString*)sessionPath; - (SessionIOS*)loadSessionFromPath:(NSString*)sessionPath;
// Schedules deletion of the all session files from a specific browser state // Schedules deletion of the all session files from a specific |directory|.
// |directory|. - (void)deleteAllSessionFilesInDirectory:(const base::FilePath&)directory
- (void)deleteAllSessionFilesInBrowserStateDirectory:(NSString*)directory completion:(base::OnceClosure)callback;
completion:
(base::OnceClosure)callback;
// Schedule deletion of session directories with |sessionIDs| which resides in // Schedule deletion of session directories with |sessionIDs| which resides in
// a specific browser state |directory|. // a specific browser state |directory|.
- (void)deleteSessions:(NSArray<NSString*>*)sessionIDs - (void)deleteSessions:(NSArray<NSString*>*)sessionIDs
fromBrowserStateDirectory:(NSString*)directory; directory:(const base::FilePath&)directory
completion:(base::OnceClosure)callback;
// Returns the path of the session with |sessionID| within a |directory|. // Returns the path of the session with |sessionID| within a |directory|.
+ (NSString*)sessionPathForSessionID:(NSString*)sessionID + (NSString*)sessionPathForSessionID:(NSString*)sessionID
directory:(NSString*)directory; directory:(const base::FilePath&)directory;
@end @end
......
...@@ -45,11 +45,20 @@ ...@@ -45,11 +45,20 @@
namespace { namespace {
const NSTimeInterval kSaveDelay = 2.5; // Value taken from Desktop Chrome. const NSTimeInterval kSaveDelay = 2.5; // Value taken from Desktop Chrome.
NSString* const kRootObjectKey = @"root"; // Key for the root object. NSString* const kRootObjectKey = @"root"; // Key for the root object.
NSString* const kSessionDirectory =
@"Sessions"; // The directory name inside BrowserState directory which // The directory name inside BrowserStatedirectory which contain all sessions
// contain all sessions directories. // directories.
NSString* const kSessionFileName = const base::FilePath::CharType kSessionDirectory[] =
@"session.plist"; // The session file name on disk. FILE_PATH_LITERAL("Sessions");
// The session file name on disk.
const base::FilePath::CharType kSessionFileName[] =
FILE_PATH_LITERAL("session.plist");
// Convert |path| to NSString.
NSString* PathAsNSString(const base::FilePath& path) {
return base::SysUTF8ToNSString(path.AsUTF8Unsafe());
}
} }
@implementation NSKeyedUnarchiver (CrLegacySessionCompatibility) @implementation NSKeyedUnarchiver (CrLegacySessionCompatibility)
...@@ -104,7 +113,7 @@ NSString* const kSessionFileName = ...@@ -104,7 +113,7 @@ NSString* const kSessionFileName =
- (void)saveSession:(__weak SessionIOSFactory*)factory - (void)saveSession:(__weak SessionIOSFactory*)factory
sessionID:(NSString*)sessionID sessionID:(NSString*)sessionID
directory:(NSString*)directory directory:(const base::FilePath&)directory
immediately:(BOOL)immediately { immediately:(BOOL)immediately {
NSString* sessionPath = [[self class] sessionPathForSessionID:sessionID NSString* sessionPath = [[self class] sessionPathForSessionID:sessionID
directory:directory]; directory:directory];
...@@ -123,7 +132,7 @@ NSString* const kSessionFileName = ...@@ -123,7 +132,7 @@ NSString* const kSessionFileName =
} }
- (SessionIOS*)loadSessionWithSessionID:(NSString*)sessionID - (SessionIOS*)loadSessionWithSessionID:(NSString*)sessionID
directory:(NSString*)directory { directory:(const base::FilePath&)directory {
NSString* sessionPath = [[self class] sessionPathForSessionID:sessionID NSString* sessionPath = [[self class] sessionPathForSessionID:sessionID
directory:directory]; directory:directory];
base::TimeTicks start_time = base::TimeTicks::Now(); base::TimeTicks start_time = base::TimeTicks::Now();
...@@ -175,59 +184,46 @@ NSString* const kSessionFileName = ...@@ -175,59 +184,46 @@ NSString* const kSessionFileName =
return base::mac::ObjCCastStrict<SessionIOS>(rootObject); return base::mac::ObjCCastStrict<SessionIOS>(rootObject);
} }
- (void)deleteAllSessionFilesInBrowserStateDirectory:(NSString*)directory - (void)deleteAllSessionFilesInDirectory:(const base::FilePath&)directory
completion: completion:(base::OnceClosure)callback {
(base::OnceClosure)callback { const base::FilePath sessionDirectory = directory.Append(kSessionDirectory);
NSMutableArray<NSString*>* sessionFilesPaths = [[NSMutableArray alloc] init];
NSString* sessionsDirectoryPath =
[directory stringByAppendingPathComponent:kSessionDirectory];
NSArray<NSString*>* allSessionIDs = [[NSFileManager defaultManager] NSArray<NSString*>* allSessionIDs = [[NSFileManager defaultManager]
contentsOfDirectoryAtPath:sessionsDirectoryPath contentsOfDirectoryAtPath:PathAsNSString(sessionDirectory)
error:nil]; error:nil];
for (NSString* sessionID in allSessionIDs) {
NSString* sessionPath =
[SessionServiceIOS sessionPathForSessionID:sessionID
directory:directory];
[sessionFilesPaths addObject:sessionPath];
}
// If there were no session ids, then scenes are not supported fall back to // If there were no session ids, then scenes are not supported fall back to
// the original location // the original location.
if (sessionFilesPaths.count == 0) { if ([allSessionIDs count] == 0) {
[sessionFilesPaths allSessionIDs = @[ @"" ];
addObject:[[self class] sessionPathForSessionID:nil
directory:directory]];
} }
[self deletePaths:sessionFilesPaths completion:std::move(callback)]; [self deleteSessions:allSessionIDs
directory:directory
completion:std::move(callback)];
} }
- (void)deleteSessions:(NSArray<NSString*>*)sessionIDs - (void)deleteSessions:(NSArray<NSString*>*)sessionIDs
fromBrowserStateDirectory:(NSString*)directory { directory:(const base::FilePath&)directory
NSString* sessionsDirectoryPath = completion:(base::OnceClosure)callback {
[directory stringByAppendingPathComponent:kSessionDirectory];
NSMutableArray<NSString*>* paths = NSMutableArray<NSString*>* paths =
[NSMutableArray arrayWithCapacity:sessionIDs.count]; [NSMutableArray arrayWithCapacity:sessionIDs.count];
for (NSString* sessionID : sessionIDs) { for (NSString* sessionID : sessionIDs) {
[paths addObject:[sessionsDirectoryPath [paths addObject:[SessionServiceIOS sessionPathForSessionID:sessionID
stringByAppendingPathComponent:sessionID]]; directory:directory]];
} }
[self deletePaths:paths completion:base::DoNothing()]; [self deletePaths:paths completion:std::move(callback)];
} }
+ (NSString*)sessionPathForSessionID:(NSString*)sessionID + (NSString*)sessionPathForSessionID:(NSString*)sessionID
directory:(NSString*)directory { directory:(const base::FilePath&)directory {
// TODO(crbug.com/1165798): remove when the sessionID is guaranteed to // TODO(crbug.com/1165798): remove when the sessionID is guaranteed to
// always be an non-empty string. // always be an non-empty string.
if (!sessionID.length) if (!sessionID.length)
return [directory stringByAppendingPathComponent:kSessionFileName]; return PathAsNSString(directory.Append(kSessionFileName));
return [NSString pathWithComponents:@[ return PathAsNSString(directory.Append(kSessionDirectory)
directory, .Append(base::SysNSStringToUTF8(sessionID))
kSessionDirectory, .Append(kSessionFileName));
sessionID,
kSessionFileName,
]];
} }
#pragma mark - Private methods #pragma mark - Private methods
......
...@@ -46,9 +46,8 @@ class SessionServiceTest : public PlatformTest { ...@@ -46,9 +46,8 @@ class SessionServiceTest : public PlatformTest {
void SetUp() override { void SetUp() override {
PlatformTest::SetUp(); PlatformTest::SetUp();
ASSERT_TRUE(scoped_temp_directory_.CreateUniqueTempDir()); ASSERT_TRUE(scoped_temp_directory_.CreateUniqueTempDir());
base::FilePath directory_name = scoped_temp_directory_.GetPath(); directory_ =
directory_name = directory_name.Append(FILE_PATH_LITERAL("sessions")); scoped_temp_directory_.GetPath().Append(FILE_PATH_LITERAL("Sessions"));
directory_ = base::SysUTF8ToNSString(directory_name.AsUTF8Unsafe());
scoped_refptr<base::SequencedTaskRunner> task_runner = scoped_refptr<base::SequencedTaskRunner> task_runner =
base::ThreadTaskRunnerHandle::Get(); base::ThreadTaskRunnerHandle::Get();
...@@ -93,30 +92,34 @@ class SessionServiceTest : public PlatformTest { ...@@ -93,30 +92,34 @@ class SessionServiceTest : public PlatformTest {
SessionServiceIOS* session_service() { return session_service_; } SessionServiceIOS* session_service() { return session_service_; }
NSString* directory() { return directory_; } const base::FilePath& directory() const { return directory_; }
NSString* directory_as_nsstring() const {
return base::SysUTF8ToNSString(directory().AsUTF8Unsafe());
}
private: private:
base::ScopedTempDir scoped_temp_directory_; base::ScopedTempDir scoped_temp_directory_;
base::test::TaskEnvironment task_environment_; base::test::TaskEnvironment task_environment_;
SessionServiceIOS* session_service_; SessionServiceIOS* session_service_ = nil;
NSString* directory_;
FakeWebStateListDelegate web_state_list_delegate_; FakeWebStateListDelegate web_state_list_delegate_;
base::FilePath directory_;
DISALLOW_COPY_AND_ASSIGN(SessionServiceTest); DISALLOW_COPY_AND_ASSIGN(SessionServiceTest);
}; };
TEST_F(SessionServiceTest, SessionPathForDirectory) { TEST_F(SessionServiceTest, SessionPathForDirectory) {
const base::FilePath root(FILE_PATH_LITERAL("root"));
EXPECT_NSEQ(@"root/session.plist", EXPECT_NSEQ(@"root/session.plist",
[SessionServiceIOS sessionPathForSessionID:nil [SessionServiceIOS sessionPathForSessionID:nil directory:root]);
directory:@"root"]);
EXPECT_NSEQ(@"root/session.plist", EXPECT_NSEQ(@"root/session.plist",
[SessionServiceIOS sessionPathForSessionID:@"" [SessionServiceIOS sessionPathForSessionID:@"" directory:root]);
directory:@"root"]);
EXPECT_NSEQ(@"root/Sessions/session-id/session.plist", EXPECT_NSEQ(@"root/Sessions/session-id/session.plist",
[SessionServiceIOS sessionPathForSessionID:@"session-id" [SessionServiceIOS sessionPathForSessionID:@"session-id"
directory:@"root"]); directory:root]);
} }
TEST_F(SessionServiceTest, SaveSessionWindowToPath) { TEST_F(SessionServiceTest, SaveSessionWindowToPath) {
...@@ -134,14 +137,16 @@ TEST_F(SessionServiceTest, SaveSessionWindowToPath) { ...@@ -134,14 +137,16 @@ TEST_F(SessionServiceTest, SaveSessionWindowToPath) {
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
NSFileManager* file_manager = [NSFileManager defaultManager]; NSFileManager* file_manager = [NSFileManager defaultManager];
EXPECT_TRUE([file_manager removeItemAtPath:directory() error:nullptr]); EXPECT_TRUE([file_manager removeItemAtPath:directory_as_nsstring()
error:nullptr]);
} }
TEST_F(SessionServiceTest, SaveSessionWindowToPathDirectoryExists) { TEST_F(SessionServiceTest, SaveSessionWindowToPathDirectoryExists) {
ASSERT_TRUE([[NSFileManager defaultManager] createDirectoryAtPath:directory() ASSERT_TRUE([[NSFileManager defaultManager]
withIntermediateDirectories:YES createDirectoryAtPath:directory_as_nsstring()
attributes:nil withIntermediateDirectories:YES
error:nullptr]); attributes:nil
error:nullptr]);
std::unique_ptr<WebStateList> web_state_list = CreateWebStateList(0); std::unique_ptr<WebStateList> web_state_list = CreateWebStateList(0);
SessionIOSFactory* factory = SessionIOSFactory* factory =
[[SessionIOSFactory alloc] initWithWebStateList:web_state_list.get()]; [[SessionIOSFactory alloc] initWithWebStateList:web_state_list.get()];
...@@ -157,7 +162,8 @@ TEST_F(SessionServiceTest, SaveSessionWindowToPathDirectoryExists) { ...@@ -157,7 +162,8 @@ TEST_F(SessionServiceTest, SaveSessionWindowToPathDirectoryExists) {
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
NSFileManager* file_manager = [NSFileManager defaultManager]; NSFileManager* file_manager = [NSFileManager defaultManager];
EXPECT_TRUE([file_manager removeItemAtPath:directory() error:nullptr]); EXPECT_TRUE([file_manager removeItemAtPath:directory_as_nsstring()
error:nullptr]);
} }
TEST_F(SessionServiceTest, LoadSessionFromDirectoryNoFile) { TEST_F(SessionServiceTest, LoadSessionFromDirectoryNoFile) {
......
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
- (void)saveSession:(__weak SessionIOSFactory*)factory - (void)saveSession:(__weak SessionIOSFactory*)factory
sessionID:(NSString*)sessionID sessionID:(NSString*)sessionID
directory:(NSString*)directory directory:(const base::FilePath&)directory
immediately:(BOOL)immediately { immediately:(BOOL)immediately {
NSString* sessionPath = [[self class] sessionPathForSessionID:sessionID NSString* sessionPath = [[self class] sessionPathForSessionID:sessionID
directory:directory]; directory:directory];
......
...@@ -96,8 +96,7 @@ void PerfTestWithBVC::SetUp() { ...@@ -96,8 +96,7 @@ void PerfTestWithBVC::SetUp() {
chrome_browser_state_.get()); chrome_browser_state_.get());
// Use the session to create a window which will contain the tabs. // Use the session to create a window which will contain the tabs.
NSString* state_path = base::SysUTF8ToNSString( const base::FilePath& state_path = chrome_browser_state_->GetStatePath();
chrome_browser_state_->GetStatePath().AsUTF8Unsafe());
SessionIOS* session = SessionIOS* session =
[[SessionServiceIOS sharedService] loadSessionWithSessionID:nil [[SessionServiceIOS sharedService] loadSessionWithSessionID:nil
directory:state_path]; directory:state_path];
......
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