Commit 31fea35e authored by Stepan Khapugin's avatar Stepan Khapugin Committed by Commit Bot

[iOS][multiball] Move BrowserStateStorageSwitching to BVWrangler.

Moves the implementation of BrowserStateStorageSwitching to
BrowserViewWrangler and inverses the dependency so that MainController
now needs BVWrangler to switch contexts. This makes sense in multiwindow
world where BVWranglers are per-scene.

Bug: none
Change-Id: Idd9b76480c425a3508a476438126b656bf362cef
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2036007Reviewed-by: default avatarMark Cogan <marq@chromium.org>
Commit-Queue: Stepan Khapugin <stkhapugin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#738581}
parent 60e64a4f
...@@ -170,9 +170,6 @@ ...@@ -170,9 +170,6 @@
namespace { namespace {
// Preference key used to store which profile is current.
NSString* kIncognitoCurrentKey = @"IncognitoActive";
// Constants for deferring notifying the AuthenticationService of a new cold // Constants for deferring notifying the AuthenticationService of a new cold
// start. // start.
NSString* const kAuthenticationServiceNotification = NSString* const kAuthenticationServiceNotification =
...@@ -278,8 +275,7 @@ void MainControllerAuthenticationServiceDelegate::ClearBrowsingData( ...@@ -278,8 +275,7 @@ void MainControllerAuthenticationServiceDelegate::ClearBrowsingData(
} // namespace } // namespace
@interface MainController () <BrowserStateStorageSwitching, @interface MainController () <PrefObserverDelegate> {
PrefObserverDelegate> {
IBOutlet UIWindow* _window; IBOutlet UIWindow* _window;
// Weak; owned by the ChromeBrowserProvider. // Weak; owned by the ChromeBrowserProvider.
...@@ -349,11 +345,6 @@ void MainControllerAuthenticationServiceDelegate::ClearBrowsingData( ...@@ -349,11 +345,6 @@ void MainControllerAuthenticationServiceDelegate::ClearBrowsingData(
// Returns whether the restore infobar should be displayed. // Returns whether the restore infobar should be displayed.
- (bool)mustShowRestoreInfobar; - (bool)mustShowRestoreInfobar;
// Switch all global states for the given mode (normal or incognito).
- (void)switchGlobalStateToMode:(ApplicationMode)mode;
// Updates the local storage, cookie store, and sets the global state.
- (void)changeStorageFromBrowserState:(ChromeBrowserState*)oldState
toBrowserState:(ChromeBrowserState*)newState;
// Returns the set of the sessions ids of the tabs in the given |tabModel|. // Returns the set of the sessions ids of the tabs in the given |tabModel|.
- (NSMutableSet*)liveSessionsForTabModel:(TabModel*)tabModel; - (NSMutableSet*)liveSessionsForTabModel:(TabModel*)tabModel;
// Purge the unused snapshots. // Purge the unused snapshots.
...@@ -593,8 +584,7 @@ void MainControllerAuthenticationServiceDelegate::ClearBrowsingData( ...@@ -593,8 +584,7 @@ void MainControllerAuthenticationServiceDelegate::ClearBrowsingData(
webStateListObserver:self.sceneController webStateListObserver:self.sceneController
applicationCommandEndpoint:self.sceneController applicationCommandEndpoint:self.sceneController
browsingDataCommandEndpoint:self browsingDataCommandEndpoint:self
appURLLoadingService:self.appURLLoadingService appURLLoadingService:self.appURLLoadingService];
storageSwitcher:self];
// Force an obvious initialization of the AuthenticationService. This must // Force an obvious initialization of the AuthenticationService. This must
// be done before creation of the UI to ensure the service is initialised // be done before creation of the UI to ensure the service is initialised
...@@ -629,7 +619,8 @@ void MainControllerAuthenticationServiceDelegate::ClearBrowsingData( ...@@ -629,7 +619,8 @@ void MainControllerAuthenticationServiceDelegate::ClearBrowsingData(
if (postCrashLaunch || switchFromIncognito) { if (postCrashLaunch || switchFromIncognito) {
[self.sceneController clearIOSSpecificIncognitoData]; [self.sceneController clearIOSSpecificIncognitoData];
if (switchFromIncognito) if (switchFromIncognito)
[self switchGlobalStateToMode:ApplicationMode::NORMAL]; [self.browserViewWrangler
switchGlobalStateToMode:ApplicationMode::NORMAL];
} }
if (switchFromIncognito) if (switchFromIncognito)
startInIncognito = NO; startInIncognito = NO;
...@@ -1204,11 +1195,7 @@ void MainControllerAuthenticationServiceDelegate::ClearBrowsingData( ...@@ -1204,11 +1195,7 @@ void MainControllerAuthenticationServiceDelegate::ClearBrowsingData(
experimental_flags::AlwaysDisplayFirstRun()) && experimental_flags::AlwaysDisplayFirstRun()) &&
!tests_hook::DisableFirstRun(); !tests_hook::DisableFirstRun();
ChromeBrowserState* browserState = [self.browserViewWrangler switchGlobalStateToMode:launchMode];
(launchMode == ApplicationMode::INCOGNITO)
? self.mainBrowserState->GetOffTheRecordChromeBrowserState()
: self.mainBrowserState;
[self changeStorageFromBrowserState:nullptr toBrowserState:browserState];
TabModel* tabModel; TabModel* tabModel;
if (launchMode == ApplicationMode::INCOGNITO) { if (launchMode == ApplicationMode::INCOGNITO) {
...@@ -1394,24 +1381,7 @@ void MainControllerAuthenticationServiceDelegate::ClearBrowsingData( ...@@ -1394,24 +1381,7 @@ void MainControllerAuthenticationServiceDelegate::ClearBrowsingData(
#pragma mark - Mode Switching
- (void)switchGlobalStateToMode:(ApplicationMode)mode {
const BOOL incognito = (mode == ApplicationMode::INCOGNITO);
// Write the state to disk of what is "active".
NSUserDefaults* standardDefaults = [NSUserDefaults standardUserDefaults];
[standardDefaults setBool:incognito forKey:kIncognitoCurrentKey];
// Save critical state information for switching between normal and
// incognito.
[standardDefaults synchronize];
}
- (void)changeStorageFromBrowserState:(ChromeBrowserState*)oldState
toBrowserState:(ChromeBrowserState*)newState {
ApplicationMode mode = newState->IsOffTheRecord() ? ApplicationMode::INCOGNITO
: ApplicationMode::NORMAL;
[self switchGlobalStateToMode:mode];
}
- (TabModel*)currentTabModel { - (TabModel*)currentTabModel {
return self.currentBVC.tabModel; return self.currentBVC.tabModel;
......
...@@ -84,6 +84,7 @@ source_set("main") { ...@@ -84,6 +84,7 @@ source_set("main") {
deps = [ deps = [
"//base", "//base",
"//components/translate/core/browser", "//components/translate/core/browser",
"//ios/chrome/app:mode",
"//ios/chrome/app/resources:launchscreen_xib", "//ios/chrome/app/resources:launchscreen_xib",
"//ios/chrome/browser", "//ios/chrome/browser",
"//ios/chrome/browser/app_launcher", "//ios/chrome/browser/app_launcher",
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
#include "ios/chrome/app/application_mode.h"
#import "ios/chrome/browser/ui/main/browser_interface_provider.h" #import "ios/chrome/browser/ui/main/browser_interface_provider.h"
@protocol ApplicationCommands; @protocol ApplicationCommands;
...@@ -18,11 +19,12 @@ class ChromeBrowserState; ...@@ -18,11 +19,12 @@ class ChromeBrowserState;
class AppUrlLoadingService; class AppUrlLoadingService;
// Protocol for objects that can handle switching browser state storage. namespace {
@protocol BrowserStateStorageSwitching
- (void)changeStorageFromBrowserState:(ChromeBrowserState*)oldState // Preference key used to store which profile is current.
toBrowserState:(ChromeBrowserState*)newState; NSString* kIncognitoCurrentKey = @"IncognitoActive";
@end
} // namespace
// Wrangler (a class in need of further refactoring) for handling the creation // Wrangler (a class in need of further refactoring) for handling the creation
// and ownership of BrowserViewController instances and their associated // and ownership of BrowserViewController instances and their associated
...@@ -44,8 +46,6 @@ class AppUrlLoadingService; ...@@ -44,8 +46,6 @@ class AppUrlLoadingService;
browsingDataCommandEndpoint: browsingDataCommandEndpoint:
(id<BrowsingDataCommands>)browsingDataCommandEndpoint (id<BrowsingDataCommands>)browsingDataCommandEndpoint
appURLLoadingService:(AppUrlLoadingService*)appURLLoadingService appURLLoadingService:(AppUrlLoadingService*)appURLLoadingService
storageSwitcher:
(id<BrowserStateStorageSwitching>)storageSwitcher
NS_DESIGNATED_INITIALIZER; NS_DESIGNATED_INITIALIZER;
- (instancetype)init NS_UNAVAILABLE; - (instancetype)init NS_UNAVAILABLE;
...@@ -67,6 +67,9 @@ class AppUrlLoadingService; ...@@ -67,6 +67,9 @@ class AppUrlLoadingService;
// Called before the instance is deallocated. // Called before the instance is deallocated.
- (void)shutdown; - (void)shutdown;
// Switch all global states for the given mode (normal or incognito).
- (void)switchGlobalStateToMode:(ApplicationMode)mode;
@end @end
@interface BrowserViewWrangler (Testing) @interface BrowserViewWrangler (Testing)
......
...@@ -95,7 +95,6 @@ ...@@ -95,7 +95,6 @@
ChromeBrowserState* _browserState; ChromeBrowserState* _browserState;
__weak id<ApplicationCommands> _applicationCommandEndpoint; __weak id<ApplicationCommands> _applicationCommandEndpoint;
__weak id<BrowsingDataCommands> _browsingDataCommandEndpoint; __weak id<BrowsingDataCommands> _browsingDataCommandEndpoint;
__weak id<BrowserStateStorageSwitching> _storageSwitcher;
AppUrlLoadingService* _appURLLoadingService; AppUrlLoadingService* _appURLLoadingService;
BOOL _isShutdown; BOOL _isShutdown;
...@@ -158,15 +157,13 @@ ...@@ -158,15 +157,13 @@
(id<ApplicationCommands>)applicationCommandEndpoint (id<ApplicationCommands>)applicationCommandEndpoint
browsingDataCommandEndpoint: browsingDataCommandEndpoint:
(id<BrowsingDataCommands>)browsingDataCommandEndpoint (id<BrowsingDataCommands>)browsingDataCommandEndpoint
appURLLoadingService:(AppUrlLoadingService*)appURLLoadingService appURLLoadingService:
storageSwitcher: (AppUrlLoadingService*)appURLLoadingService {
(id<BrowserStateStorageSwitching>)storageSwitcher {
if ((self = [super init])) { if ((self = [super init])) {
_browserState = browserState; _browserState = browserState;
_applicationCommandEndpoint = applicationCommandEndpoint; _applicationCommandEndpoint = applicationCommandEndpoint;
_browsingDataCommandEndpoint = browsingDataCommandEndpoint; _browsingDataCommandEndpoint = browsingDataCommandEndpoint;
_appURLLoadingService = appURLLoadingService; _appURLLoadingService = appURLLoadingService;
_storageSwitcher = storageSwitcher;
_webStateListObserver = std::make_unique<WebStateListObserverBridge>(self); _webStateListObserver = std::make_unique<WebStateListObserverBridge>(self);
_webStateListForwardingObserver = _webStateListForwardingObserver =
std::make_unique<WebStateListObserverBridge>(observer); std::make_unique<WebStateListObserverBridge>(observer);
...@@ -218,9 +215,8 @@ ...@@ -218,9 +215,8 @@
// Data storage for the browser is always owned by the current BVC, so it // Data storage for the browser is always owned by the current BVC, so it
// must be updated when switching between BVCs. // must be updated when switching between BVCs.
[_storageSwitcher [self changeStorageFromBrowserState:self.currentInterface.browserState
changeStorageFromBrowserState:self.currentInterface.browserState toBrowserState:interface.browserState];
toBrowserState:interface.browserState];
} }
_currentInterface = interface; _currentInterface = interface;
...@@ -317,6 +313,27 @@ ...@@ -317,6 +313,27 @@
[self updateDeviceSharingManager]; [self updateDeviceSharingManager];
} }
#pragma mark - Mode Switching
- (void)switchGlobalStateToMode:(ApplicationMode)mode {
// TODO(crbug.com/1048690): use scene-local storage in multiwindow.
const BOOL incognito = (mode == ApplicationMode::INCOGNITO);
// Write the state to disk of what is "active".
NSUserDefaults* standardDefaults = [NSUserDefaults standardUserDefaults];
[standardDefaults setBool:incognito forKey:kIncognitoCurrentKey];
// Save critical state information for switching between normal and
// incognito.
[standardDefaults synchronize];
}
// Updates the local storage, cookie store, and sets the global state.
- (void)changeStorageFromBrowserState:(ChromeBrowserState*)oldState
toBrowserState:(ChromeBrowserState*)newState {
ApplicationMode mode = newState->IsOffTheRecord() ? ApplicationMode::INCOGNITO
: ApplicationMode::NORMAL;
[self switchGlobalStateToMode:mode];
}
#pragma mark - Other public methods #pragma mark - Other public methods
- (void)updateDeviceSharingManager { - (void)updateDeviceSharingManager {
......
...@@ -42,8 +42,7 @@ TEST_F(BrowserViewWranglerTest, TestInitNilObserver) { ...@@ -42,8 +42,7 @@ TEST_F(BrowserViewWranglerTest, TestInitNilObserver) {
webStateListObserver:nil webStateListObserver:nil
applicationCommandEndpoint:(id<ApplicationCommands>)nil applicationCommandEndpoint:(id<ApplicationCommands>)nil
browsingDataCommandEndpoint:nil browsingDataCommandEndpoint:nil
appURLLoadingService:nil appURLLoadingService:nil];
storageSwitcher:nil];
[wrangler createMainBrowser]; [wrangler createMainBrowser];
// Test that BVC is created on demand. // Test that BVC is created on demand.
BrowserViewController* bvc = wrangler.mainInterface.bvc; BrowserViewController* bvc = wrangler.mainInterface.bvc;
...@@ -74,8 +73,7 @@ TEST_F(BrowserViewWranglerTest, TestBrowserList) { ...@@ -74,8 +73,7 @@ TEST_F(BrowserViewWranglerTest, TestBrowserList) {
webStateListObserver:nil webStateListObserver:nil
applicationCommandEndpoint:nil applicationCommandEndpoint:nil
browsingDataCommandEndpoint:nil browsingDataCommandEndpoint:nil
appURLLoadingService:nil appURLLoadingService:nil];
storageSwitcher:nil];
// After creating the main browser, it should have been added to the browser // After creating the main browser, it should have been added to the browser
// list. // list.
......
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