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 @@
namespace {
// Preference key used to store which profile is current.
NSString* kIncognitoCurrentKey = @"IncognitoActive";
// Constants for deferring notifying the AuthenticationService of a new cold
// start.
NSString* const kAuthenticationServiceNotification =
......@@ -278,8 +275,7 @@ void MainControllerAuthenticationServiceDelegate::ClearBrowsingData(
} // namespace
@interface MainController () <BrowserStateStorageSwitching,
PrefObserverDelegate> {
@interface MainController () <PrefObserverDelegate> {
IBOutlet UIWindow* _window;
// Weak; owned by the ChromeBrowserProvider.
......@@ -349,11 +345,6 @@ void MainControllerAuthenticationServiceDelegate::ClearBrowsingData(
// Returns whether the restore infobar should be displayed.
- (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|.
- (NSMutableSet*)liveSessionsForTabModel:(TabModel*)tabModel;
// Purge the unused snapshots.
......@@ -593,8 +584,7 @@ void MainControllerAuthenticationServiceDelegate::ClearBrowsingData(
webStateListObserver:self.sceneController
applicationCommandEndpoint:self.sceneController
browsingDataCommandEndpoint:self
appURLLoadingService:self.appURLLoadingService
storageSwitcher:self];
appURLLoadingService:self.appURLLoadingService];
// Force an obvious initialization of the AuthenticationService. This must
// be done before creation of the UI to ensure the service is initialised
......@@ -629,7 +619,8 @@ void MainControllerAuthenticationServiceDelegate::ClearBrowsingData(
if (postCrashLaunch || switchFromIncognito) {
[self.sceneController clearIOSSpecificIncognitoData];
if (switchFromIncognito)
[self switchGlobalStateToMode:ApplicationMode::NORMAL];
[self.browserViewWrangler
switchGlobalStateToMode:ApplicationMode::NORMAL];
}
if (switchFromIncognito)
startInIncognito = NO;
......@@ -1204,11 +1195,7 @@ void MainControllerAuthenticationServiceDelegate::ClearBrowsingData(
experimental_flags::AlwaysDisplayFirstRun()) &&
!tests_hook::DisableFirstRun();
ChromeBrowserState* browserState =
(launchMode == ApplicationMode::INCOGNITO)
? self.mainBrowserState->GetOffTheRecordChromeBrowserState()
: self.mainBrowserState;
[self changeStorageFromBrowserState:nullptr toBrowserState:browserState];
[self.browserViewWrangler switchGlobalStateToMode:launchMode];
TabModel* tabModel;
if (launchMode == ApplicationMode::INCOGNITO) {
......@@ -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 {
return self.currentBVC.tabModel;
......
......@@ -84,6 +84,7 @@ source_set("main") {
deps = [
"//base",
"//components/translate/core/browser",
"//ios/chrome/app:mode",
"//ios/chrome/app/resources:launchscreen_xib",
"//ios/chrome/browser",
"//ios/chrome/browser/app_launcher",
......
......@@ -7,6 +7,7 @@
#import <UIKit/UIKit.h>
#include "ios/chrome/app/application_mode.h"
#import "ios/chrome/browser/ui/main/browser_interface_provider.h"
@protocol ApplicationCommands;
......@@ -18,11 +19,12 @@ class ChromeBrowserState;
class AppUrlLoadingService;
// Protocol for objects that can handle switching browser state storage.
@protocol BrowserStateStorageSwitching
- (void)changeStorageFromBrowserState:(ChromeBrowserState*)oldState
toBrowserState:(ChromeBrowserState*)newState;
@end
namespace {
// Preference key used to store which profile is current.
NSString* kIncognitoCurrentKey = @"IncognitoActive";
} // namespace
// Wrangler (a class in need of further refactoring) for handling the creation
// and ownership of BrowserViewController instances and their associated
......@@ -44,8 +46,6 @@ class AppUrlLoadingService;
browsingDataCommandEndpoint:
(id<BrowsingDataCommands>)browsingDataCommandEndpoint
appURLLoadingService:(AppUrlLoadingService*)appURLLoadingService
storageSwitcher:
(id<BrowserStateStorageSwitching>)storageSwitcher
NS_DESIGNATED_INITIALIZER;
- (instancetype)init NS_UNAVAILABLE;
......@@ -67,6 +67,9 @@ class AppUrlLoadingService;
// Called before the instance is deallocated.
- (void)shutdown;
// Switch all global states for the given mode (normal or incognito).
- (void)switchGlobalStateToMode:(ApplicationMode)mode;
@end
@interface BrowserViewWrangler (Testing)
......
......@@ -95,7 +95,6 @@
ChromeBrowserState* _browserState;
__weak id<ApplicationCommands> _applicationCommandEndpoint;
__weak id<BrowsingDataCommands> _browsingDataCommandEndpoint;
__weak id<BrowserStateStorageSwitching> _storageSwitcher;
AppUrlLoadingService* _appURLLoadingService;
BOOL _isShutdown;
......@@ -158,15 +157,13 @@
(id<ApplicationCommands>)applicationCommandEndpoint
browsingDataCommandEndpoint:
(id<BrowsingDataCommands>)browsingDataCommandEndpoint
appURLLoadingService:(AppUrlLoadingService*)appURLLoadingService
storageSwitcher:
(id<BrowserStateStorageSwitching>)storageSwitcher {
appURLLoadingService:
(AppUrlLoadingService*)appURLLoadingService {
if ((self = [super init])) {
_browserState = browserState;
_applicationCommandEndpoint = applicationCommandEndpoint;
_browsingDataCommandEndpoint = browsingDataCommandEndpoint;
_appURLLoadingService = appURLLoadingService;
_storageSwitcher = storageSwitcher;
_webStateListObserver = std::make_unique<WebStateListObserverBridge>(self);
_webStateListForwardingObserver =
std::make_unique<WebStateListObserverBridge>(observer);
......@@ -218,9 +215,8 @@
// Data storage for the browser is always owned by the current BVC, so it
// must be updated when switching between BVCs.
[_storageSwitcher
changeStorageFromBrowserState:self.currentInterface.browserState
toBrowserState:interface.browserState];
[self changeStorageFromBrowserState:self.currentInterface.browserState
toBrowserState:interface.browserState];
}
_currentInterface = interface;
......@@ -317,6 +313,27 @@
[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
- (void)updateDeviceSharingManager {
......
......@@ -42,8 +42,7 @@ TEST_F(BrowserViewWranglerTest, TestInitNilObserver) {
webStateListObserver:nil
applicationCommandEndpoint:(id<ApplicationCommands>)nil
browsingDataCommandEndpoint:nil
appURLLoadingService:nil
storageSwitcher:nil];
appURLLoadingService:nil];
[wrangler createMainBrowser];
// Test that BVC is created on demand.
BrowserViewController* bvc = wrangler.mainInterface.bvc;
......@@ -74,8 +73,7 @@ TEST_F(BrowserViewWranglerTest, TestBrowserList) {
webStateListObserver:nil
applicationCommandEndpoint:nil
browsingDataCommandEndpoint:nil
appURLLoadingService:nil
storageSwitcher:nil];
appURLLoadingService:nil];
// After creating the main browser, it should have been added to the browser
// 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