Skip to content

Commit

Permalink
[iOS] Switching to Browser instead of ChromeBrowserState
Browse files Browse the repository at this point in the history
Replacing ChromeBrowserState by Browser in sync passphrase views.
This is related to: crrev.com/c/2288553

Bug: 1098188
Change-Id: I6d58d792a6a1a10f48ae5a697a8af3e06e00fc45
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2288552
Reviewed-by: Nohemi Fernandez <fernandex@chromium.org>
Commit-Queue: Jérôme Lebel <jlebel@chromium.org>
Cr-Commit-Position: refs/heads/master@{#786745}
  • Loading branch information
Jérôme Lebel authored and Commit Bot committed Jul 9, 2020
1 parent 637b12d commit 6c505ad
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ - (void)openReauthDialogAsSyncIsInAuthError {
- (void)openPassphraseDialog {
SyncEncryptionPassphraseTableViewController* controller =
[[SyncEncryptionPassphraseTableViewController alloc]
initWithBrowserState:self.browser->GetBrowserState()];
initWithBrowser:self.browser];
// TODO(crbug.com/1045047): Use HandlerForProtocol after commands protocol
// clean up.
controller.dispatcher = static_cast<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ - (void)openPassphraseDialog {
// Otherwise, show the full encryption options.
if (self.syncService->GetUserSettings()->IsPassphraseRequired()) {
controllerToPush = [[SyncEncryptionPassphraseTableViewController alloc]
initWithBrowserState:self.browser->GetBrowserState()];
initWithBrowser:self.browser];
} else {
controllerToPush = [[SyncEncryptionTableViewController alloc]
initWithBrowserState:self.browser->GetBrowserState()];
initWithBrowser:self.browser];
}
// TODO(crbug.com/1045047): Use HandlerForProtocol after commands protocol
// clean up.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ @implementation SettingsNavigationController
DCHECK(browser);
SyncEncryptionPassphraseTableViewController* controller =
[[SyncEncryptionPassphraseTableViewController alloc]
initWithBrowserState:browser->GetBrowserState()];
initWithBrowser:browser];
controller.dispatcher = [delegate handlerForSettings];
SettingsNavigationController* nc = [[SettingsNavigationController alloc]
initWithRootViewController:controller
Expand Down Expand Up @@ -513,7 +513,7 @@ - (void)showSyncPassphraseSettingsFromViewController:
(UIViewController*)baseViewController {
SyncEncryptionPassphraseTableViewController* controller =
[[SyncEncryptionPassphraseTableViewController alloc]
initWithBrowserState:self.browser->GetBrowserState()];
initWithBrowser:self.browser];
controller.dispatcher = [self.settingsNavigationDelegate handlerForSettings];
[self pushViewController:controller animated:YES];
}
Expand Down
2 changes: 2 additions & 0 deletions ios/chrome/browser/ui/settings/sync/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ source_set("sync") {
"//ios/chrome/browser",
"//ios/chrome/browser",
"//ios/chrome/browser/browser_state",
"//ios/chrome/browser/main:public",
"//ios/chrome/browser/signin",
"//ios/chrome/browser/sync",
"//ios/chrome/browser/ui:feature_flags",
Expand Down Expand Up @@ -74,6 +75,7 @@ source_set("unit_tests") {
"//ios/chrome/browser",
"//ios/chrome/browser",
"//ios/chrome/browser/browser_state:test_support",
"//ios/chrome/browser/main:test_support",
"//ios/chrome/browser/prefs",
"//ios/chrome/browser/prefs:browser_prefs",
"//ios/chrome/browser/sync",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ - (TableViewItem*)confirmPassphraseItem;

@implementation SyncCreatePassphraseTableViewController

- (instancetype)initWithBrowserState:(ChromeBrowserState*)browserState {
self = [super initWithBrowserState:browserState];
- (instancetype)initWithBrowser:(Browser*)browser {
self = [super initWithBrowser:browser];
if (self) {
self.title =
l10n_util::GetNSString(IDS_IOS_SYNC_ENCRYPTION_CREATE_PASSPHRASE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void TearDown() override {

ChromeTableViewController* InstantiateController() override {
return [[SyncCreatePassphraseTableViewController alloc]
initWithBrowserState:chrome_browser_state_.get()];
initWithBrowser:browser_.get()];
}

SyncCreatePassphraseTableViewController* SyncController() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#import "ios/chrome/browser/sync/sync_observer_bridge.h"
#import "ios/chrome/browser/ui/settings/settings_root_table_view_controller.h"

class ChromeBrowserState;
class Browser;

namespace sync_encryption_passphrase {
typedef NS_ENUM(NSInteger, SectionIdentifier) {
Expand All @@ -36,8 +36,7 @@ typedef NS_ENUM(NSInteger, ItemType) {
@property(nonatomic, copy) NSString* syncErrorMessage;

// |browserState| must not be nil.
- (instancetype)initWithBrowserState:(ChromeBrowserState*)browserState
NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithBrowser:(Browser*)browser NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithStyle:(UITableViewStyle)style NS_UNAVAILABLE;

@end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "ios/chrome/browser/application_context.h"
#include "ios/chrome/browser/browser_state/chrome_browser_state.h"
#include "ios/chrome/browser/chrome_url_constants.h"
#import "ios/chrome/browser/main/browser.h"
#import "ios/chrome/browser/signin/authentication_service.h"
#include "ios/chrome/browser/signin/authentication_service_factory.h"
#include "ios/chrome/browser/signin/identity_manager_factory.h"
Expand Down Expand Up @@ -60,7 +61,6 @@
@interface SyncEncryptionPassphraseTableViewController () <
IdentityManagerObserverBridgeDelegate,
SettingsControllerProtocol> {
ChromeBrowserState* _browserState;
// Whether the decryption progress is currently being shown.
BOOL _isDecryptionProgressShown;
NSString* _savedTitle;
Expand All @@ -71,26 +71,31 @@ @interface SyncEncryptionPassphraseTableViewController () <
UITextField* _passphrase;
}

@property(nonatomic, assign, readonly) Browser* browser;

@end

@implementation SyncEncryptionPassphraseTableViewController

- (instancetype)initWithBrowserState:(ChromeBrowserState*)browserState {
DCHECK(browserState);
@synthesize browser = _browser;

- (instancetype)initWithBrowser:(Browser*)browser {
DCHECK(browser);
UITableViewStyle style = base::FeatureList::IsEnabled(kSettingsRefresh)
? UITableViewStylePlain
: UITableViewStyleGrouped;
self = [super initWithStyle:style];
if (self) {
_browser = browser;
ChromeBrowserState* browserState = self.browser->GetBrowserState();
self.title = l10n_util::GetNSString(IDS_IOS_SYNC_ENTER_PASSPHRASE_TITLE);
self.shouldHideDoneButton = YES;
_browserState = browserState;
NSString* userEmail =
[AuthenticationServiceFactory::GetForBrowserState(_browserState)
[AuthenticationServiceFactory::GetForBrowserState(browserState)
->GetAuthenticatedIdentity() userEmail];
DCHECK(userEmail);
syncer::SyncService* service =
ProfileSyncServiceFactory::GetForBrowserState(_browserState);
ProfileSyncServiceFactory::GetForBrowserState(browserState);
if (service->IsEngineInitialized() &&
service->GetUserSettings()->IsUsingSecondaryPassphrase()) {
base::Time passphrase_time =
Expand All @@ -115,7 +120,7 @@ - (instancetype)initWithBrowserState:(ChromeBrowserState*)browserState {

_identityManagerObserver =
std::make_unique<signin::IdentityManagerObserverBridge>(
IdentityManagerFactory::GetForBrowserState(_browserState), self);
IdentityManagerFactory::GetForBrowserState(browserState), self);
}
return self;
}
Expand All @@ -127,8 +132,9 @@ - (UITextField*)passphrase {
- (NSString*)syncErrorMessage {
if (_syncErrorMessage)
return _syncErrorMessage;
ChromeBrowserState* browserState = self.browser->GetBrowserState();
SyncSetupService* service =
SyncSetupServiceFactory::GetForBrowserState(_browserState);
SyncSetupServiceFactory::GetForBrowserState(browserState);
DCHECK(service);
SyncSetupService::SyncServiceState syncServiceState =
service->GetSyncServiceState();
Expand All @@ -137,7 +143,7 @@ - (NSString*)syncErrorMessage {
if (syncServiceState == SyncSetupService::kSyncServiceNeedsPassphrase)
return nil;

return GetSyncErrorMessageForBrowserState(_browserState);
return GetSyncErrorMessageForBrowserState(browserState);
}

#pragma mark - View lifecycle
Expand Down Expand Up @@ -279,17 +285,18 @@ - (BOOL)forDecryption {

- (void)signInPressed {
DCHECK([_passphrase text].length);
ChromeBrowserState* browserState = self.browser->GetBrowserState();

if (!_syncObserver.get()) {
_syncObserver.reset(new SyncObserverBridge(
self, ProfileSyncServiceFactory::GetForBrowserState(_browserState)));
self, ProfileSyncServiceFactory::GetForBrowserState(browserState)));
}

// Clear out the error message.
self.syncErrorMessage = nil;

syncer::SyncService* service =
ProfileSyncServiceFactory::GetForBrowserState(_browserState);
ProfileSyncServiceFactory::GetForBrowserState(browserState);
DCHECK(service);
// It is possible for a race condition to happen where a user is allowed
// to call the backend with the passphrase before the backend is
Expand Down Expand Up @@ -468,8 +475,9 @@ - (void)textFieldDidEndEditing:(id)sender {
#pragma mark - SyncObserverModelBridge

- (void)onSyncStateChanged {
ChromeBrowserState* browserState = self.browser->GetBrowserState();
syncer::SyncService* service =
ProfileSyncServiceFactory::GetForBrowserState(_browserState);
ProfileSyncServiceFactory::GetForBrowserState(browserState);

if (!service->IsEngineInitialized()) {
return;
Expand Down Expand Up @@ -498,7 +506,8 @@ - (void)onSyncStateChanged {
#pragma mark - IdentityManagerObserverBridgeDelegate

- (void)onEndBatchOfRefreshTokenStateChanges {
if (AuthenticationServiceFactory::GetForBrowserState(_browserState)
ChromeBrowserState* browserState = self.browser->GetBrowserState();
if (AuthenticationServiceFactory::GetForBrowserState(browserState)
->IsAuthenticated()) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void TearDown() override {

ChromeTableViewController* InstantiateController() override {
return [[SyncEncryptionPassphraseTableViewController alloc]
initWithBrowserState:chrome_browser_state_.get()];
initWithBrowser:browser_.get()];
}

SyncEncryptionPassphraseTableViewController* SyncController() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@

#import "ios/chrome/browser/ui/settings/settings_root_table_view_controller.h"

class ChromeBrowserState;
class Browser;

// Controller to allow user to specify encryption passphrase for Sync.
@interface SyncEncryptionTableViewController : SettingsRootTableViewController

// Designated initializer. |browserState| must not be nil.
- (instancetype)initWithBrowserState:(ChromeBrowserState*)browserState
NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithBrowser:(Browser*)browser NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithStyle:(UITableViewStyle)style NS_UNAVAILABLE;

@end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "ios/chrome/browser/application_context.h"
#include "ios/chrome/browser/browser_state/chrome_browser_state.h"
#include "ios/chrome/browser/chrome_url_constants.h"
#import "ios/chrome/browser/main/browser.h"
#include "ios/chrome/browser/sync/profile_sync_service_factory.h"
#import "ios/chrome/browser/sync/sync_observer_bridge.h"
#import "ios/chrome/browser/ui/settings/sync/sync_create_passphrase_table_view_controller.h"
Expand Down Expand Up @@ -53,25 +54,30 @@ typedef NS_ENUM(NSInteger, ItemType) {
} // namespace

@interface SyncEncryptionTableViewController () <SyncObserverModelBridge> {
ChromeBrowserState* _browserState;
std::unique_ptr<SyncObserverBridge> _syncObserver;
BOOL _isUsingSecondaryPassphrase;
}

@property(nonatomic, assign, readonly) Browser* browser;

@end

@implementation SyncEncryptionTableViewController

- (instancetype)initWithBrowserState:(ChromeBrowserState*)browserState {
DCHECK(browserState);
@synthesize browser = _browser;

- (instancetype)initWithBrowser:(Browser*)browser {
DCHECK(browser);
UITableViewStyle style = base::FeatureList::IsEnabled(kSettingsRefresh)
? UITableViewStylePlain
: UITableViewStyleGrouped;
self = [super initWithStyle:style];
if (self) {
_browser = browser;
ChromeBrowserState* browserState = self.browser->GetBrowserState();
self.title = l10n_util::GetNSString(IDS_IOS_SYNC_ENCRYPTION_TITLE);
_browserState = browserState;
syncer::SyncService* syncService =
ProfileSyncServiceFactory::GetForBrowserState(_browserState);
ProfileSyncServiceFactory::GetForBrowserState(browserState);
_isUsingSecondaryPassphrase =
syncService->IsEngineInitialized() &&
syncService->GetUserSettings()->IsUsingSecondaryPassphrase();
Expand Down Expand Up @@ -165,13 +171,14 @@ - (void)tableView:(UITableView*)tableView
switch (item.type) {
case ItemTypePassphrase: {
DCHECK(switches::IsSyncAllowedByFlag());
ChromeBrowserState* browserState = self.browser->GetBrowserState();
syncer::SyncService* service =
ProfileSyncServiceFactory::GetForBrowserState(_browserState);
ProfileSyncServiceFactory::GetForBrowserState(browserState);
if (service->IsEngineInitialized() &&
!service->GetUserSettings()->IsUsingSecondaryPassphrase()) {
SyncCreatePassphraseTableViewController* controller =
[[SyncCreatePassphraseTableViewController alloc]
initWithBrowserState:_browserState];
initWithBrowser:self.browser];
if (controller) {
controller.dispatcher = self.dispatcher;
[self.navigationController pushViewController:controller
Expand All @@ -192,8 +199,9 @@ - (void)tableView:(UITableView*)tableView
#pragma mark SyncObserverModelBridge

- (void)onSyncStateChanged {
ChromeBrowserState* browserState = self.browser->GetBrowserState();
syncer::SyncService* service =
ProfileSyncServiceFactory::GetForBrowserState(_browserState);
ProfileSyncServiceFactory::GetForBrowserState(browserState);
BOOL isNowUsingSecondaryPassphrase =
service->IsEngineInitialized() &&
service->GetUserSettings()->IsUsingSecondaryPassphrase();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "components/strings/grit/components_strings.h"
#include "components/sync/driver/test_sync_service.h"
#include "ios/chrome/browser/browser_state/test_chrome_browser_state.h"
#import "ios/chrome/browser/main/test_browser.h"
#include "ios/chrome/browser/sync/profile_sync_service_factory.h"
#import "ios/chrome/browser/ui/table_view/cells/table_view_text_item.h"
#import "ios/chrome/browser/ui/table_view/chrome_table_view_controller_test.h"
Expand Down Expand Up @@ -39,25 +40,25 @@ void SetUp() override {
test_cbs_builder.AddTestingFactory(
ProfileSyncServiceFactory::GetInstance(),
base::BindRepeating(&CreateTestSyncService));
chrome_browser_state_ = test_cbs_builder.Build();
browser_ = std::make_unique<TestBrowser>();
ChromeTableViewControllerTest::SetUp();

ChromeBrowserState* browserState = browser_.get()->GetBrowserState();
syncer::TestSyncService* test_sync_service =
static_cast<syncer::TestSyncService*>(
ProfileSyncServiceFactory::GetForBrowserState(
chrome_browser_state_.get()));
ProfileSyncServiceFactory::GetForBrowserState(browserState));
test_sync_service->SetIsUsingSecondaryPassphrase(true);

CreateController();
}

ChromeTableViewController* InstantiateController() override {
return [[SyncEncryptionTableViewController alloc]
initWithBrowserState:chrome_browser_state_.get()];
initWithBrowser:browser_.get()];
}

web::WebTaskEnvironment task_environment_;
std::unique_ptr<TestChromeBrowserState> chrome_browser_state_;
std::unique_ptr<Browser> browser_;
};

TEST_F(SyncEncryptionTableViewControllerTest, TestModel) {
Expand Down

0 comments on commit 6c505ad

Please sign in to comment.