blob: b0bbfe56e0eda209fa1c384e1cc28be79d8757ac [file] [log] [blame]
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/chrome/browser/ui/bookmarks/bookmark_promo_controller.h"
#include <memory>
#include "components/signin/public/identity_manager/account_info.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/signin/public/identity_manager/objc/identity_manager_observer_bridge.h"
#include "ios/chrome/browser/browser_state/chrome_browser_state.h"
#include "ios/chrome/browser/signin/identity_manager_factory.h"
#import "ios/chrome/browser/ui/authentication/cells/signin_promo_view_configurator.h"
#import "ios/chrome/browser/ui/authentication/cells/signin_promo_view_consumer.h"
#import "ios/chrome/browser/ui/authentication/signin_promo_view_mediator.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
@interface BookmarkPromoController ()<SigninPromoViewConsumer,
IdentityManagerObserverBridgeDelegate> {
bool _isIncognito;
ChromeBrowserState* _browserState;
std::unique_ptr<signin::IdentityManagerObserverBridge>
_identityManagerObserverBridge;
}
// Mediator to use for the sign-in promo view displayed in the bookmark view.
@property(nonatomic, readwrite, strong)
SigninPromoViewMediator* signinPromoViewMediator;
@end
@implementation BookmarkPromoController
@synthesize delegate = _delegate;
@synthesize shouldShowSigninPromo = _shouldShowSigninPromo;
@synthesize signinPromoViewMediator = _signinPromoViewMediator;
- (instancetype)initWithBrowserState:(ChromeBrowserState*)browserState
delegate:
(id<BookmarkPromoControllerDelegate>)delegate
presenter:(id<SigninPresenter>)presenter {
self = [super init];
if (self) {
_delegate = delegate;
// Incognito browserState can go away before this class is released, this
// code avoids keeping a pointer to it.
_isIncognito = browserState->IsOffTheRecord();
if (!_isIncognito) {
_browserState = browserState;
_identityManagerObserverBridge.reset(
new signin::IdentityManagerObserverBridge(
IdentityManagerFactory::GetForBrowserState(_browserState), self));
_signinPromoViewMediator = [[SigninPromoViewMediator alloc]
initWithBrowserState:_browserState
accessPoint:signin_metrics::AccessPoint::
ACCESS_POINT_BOOKMARK_MANAGER
presenter:presenter];
_signinPromoViewMediator.consumer = self;
}
[self updateShouldShowSigninPromo];
}
return self;
}
- (void)dealloc {
[_signinPromoViewMediator signinPromoViewIsRemoved];
}
- (void)hidePromoCell {
DCHECK(!_isIncognito);
DCHECK(_browserState);
self.shouldShowSigninPromo = NO;
}
- (void)setShouldShowSigninPromo:(BOOL)shouldShowSigninPromo {
if (_shouldShowSigninPromo != shouldShowSigninPromo) {
_shouldShowSigninPromo = shouldShowSigninPromo;
[self.delegate promoStateChanged:shouldShowSigninPromo];
}
}
- (void)updateShouldShowSigninPromo {
self.shouldShowSigninPromo = NO;
if (_isIncognito)
return;
DCHECK(_browserState);
if ([SigninPromoViewMediator
shouldDisplaySigninPromoViewWithAccessPoint:
signin_metrics::AccessPoint::ACCESS_POINT_BOOKMARK_MANAGER
browserState:_browserState]) {
signin::IdentityManager* identityManager =
IdentityManagerFactory::GetForBrowserState(_browserState);
self.shouldShowSigninPromo =
!identityManager->HasPrimaryAccount(signin::ConsentLevel::kSync);
}
}
#pragma mark - IdentityManagerObserverBridgeDelegate
// Called when a user changes the syncing state.
- (void)onPrimaryAccountChanged:
(const signin::PrimaryAccountChangeEvent&)event {
switch (event.GetEventTypeFor(signin::ConsentLevel::kSync)) {
case signin::PrimaryAccountChangeEvent::Type::kSet:
if (!self.signinPromoViewMediator.signinInProgress) {
self.shouldShowSigninPromo = NO;
}
break;
case signin::PrimaryAccountChangeEvent::Type::kCleared:
[self updateShouldShowSigninPromo];
break;
case signin::PrimaryAccountChangeEvent::Type::kNone:
break;
}
}
#pragma mark - SigninPromoViewConsumer
- (void)configureSigninPromoWithConfigurator:
(SigninPromoViewConfigurator*)configurator
identityChanged:(BOOL)identityChanged {
[self.delegate configureSigninPromoWithConfigurator:configurator
identityChanged:identityChanged];
}
- (void)signinDidFinish {
[self updateShouldShowSigninPromo];
}
- (void)signinPromoViewMediatorCloseButtonWasTapped:
(SigninPromoViewMediator*)mediator {
[self updateShouldShowSigninPromo];
}
@end