1// Copyright 2019 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#import "ios/chrome/browser/ui/passwords/password_breach_mediator.h"
6
7#include "base/strings/sys_string_conversions.h"
8#include "components/password_manager/core/browser/leak_detection_dialog_utils.h"
9#include "components/password_manager/core/browser/password_manager_metrics_util.h"
10#include "components/password_manager/core/common/password_manager_features.h"
11#import "ios/chrome/browser/ui/commands/application_commands.h"
12#import "ios/chrome/browser/ui/commands/open_new_tab_command.h"
13#import "ios/chrome/browser/ui/passwords/password_breach_consumer.h"
14#import "ios/chrome/browser/ui/passwords/password_breach_presenter.h"
15#include "ios/chrome/browser/ui/ui_feature_flags.h"
16
17#if !defined(__has_feature) || !__has_feature(objc_arc)
18#error "This file requires ARC support."
19#endif
20
21using base::SysUTF16ToNSString;
22using password_manager::CredentialLeakType;
23using password_manager::GetAcceptButtonLabel;
24using password_manager::GetCancelButtonLabel;
25using password_manager::GetDescription;
26using password_manager::GetLeakDialogType;
27using password_manager::GetTitle;
28using password_manager::GetPasswordCheckupURL;
29using password_manager::ShouldCheckPasswords;
30using password_manager::metrics_util::LeakDialogDismissalReason;
31using password_manager::metrics_util::LeakDialogType;
32using password_manager::metrics_util::LogLeakDialogTypeAndDismissalReason;
33
34@interface PasswordBreachMediator ()
35
36// Leak type of the dialog.
37@property(nonatomic, assign) LeakDialogType leakType;
38
39// Dismiss reason, used for metrics.
40@property(nonatomic, assign) LeakDialogDismissalReason dismissReason;
41
42// The presenter of the feature.
43@property(nonatomic, weak) id<PasswordBreachPresenter> presenter;
44
45@end
46
47@implementation PasswordBreachMediator
48
49- (instancetype)initWithConsumer:(id<PasswordBreachConsumer>)consumer
50                       presenter:(id<PasswordBreachPresenter>)presenter
51                             URL:(const GURL&)URL
52                        leakType:(CredentialLeakType)leakType {
53  self = [super init];
54  if (self) {
55    _presenter = presenter;
56    _leakType = GetLeakDialogType(leakType);
57    _dismissReason = LeakDialogDismissalReason::kNoDirectInteraction;
58
59    NSString* subtitle = SysUTF16ToNSString(GetDescription(leakType, URL));
60    NSString* primaryActionString =
61        SysUTF16ToNSString(GetAcceptButtonLabel(leakType));
62    [consumer setTitleString:SysUTF16ToNSString(GetTitle(leakType))
63                subtitleString:subtitle
64           primaryActionString:primaryActionString
65        primaryActionAvailable:ShouldCheckPasswords(leakType)];
66  }
67  return self;
68}
69
70- (void)disconnect {
71  LogLeakDialogTypeAndDismissalReason(self.leakType, self.dismissReason);
72}
73
74#pragma mark - ConfirmationAlertActionHandler
75
76- (void)confirmationAlertDismissAction {
77  self.dismissReason = LeakDialogDismissalReason::kClickedOk;
78  [self.presenter stop];
79}
80
81- (void)confirmationAlertPrimaryAction {
82  self.dismissReason = LeakDialogDismissalReason::kClickedCheckPasswords;
83  // Opening Password page will stop the presentation in the presenter.
84  // No need to send |stop|.
85  [self.presenter startPasswordCheck];
86}
87
88- (void)confirmationAlertSecondaryAction {
89  // No-op.
90}
91
92- (void)confirmationAlertLearnMoreAction {
93  [self.presenter presentLearnMore];
94}
95
96@end
97