1// Copyright 2014 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#ifndef COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_PASSWORD_MANAGER_CLIENT_H_
6#define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_PASSWORD_MANAGER_CLIENT_H_
7
8#include "base/metrics/field_trial.h"
9#include "components/autofill/core/common/password_form.h"
10#include "components/autofill/core/common/password_form_fill_data.h"
11#include "components/password_manager/core/browser/password_store.h"
12
13class PrefService;
14
15namespace password_manager {
16
17struct CredentialInfo;
18class PasswordFormManager;
19class PasswordManagerDriver;
20class PasswordStore;
21
22// An abstraction of operations that depend on the embedders (e.g. Chrome)
23// environment.
24class PasswordManagerClient {
25 public:
26  PasswordManagerClient() {}
27  virtual ~PasswordManagerClient() {}
28
29  // For automated testing, the save password prompt should sometimes not be
30  // shown, and password immediately saved instead. That can be enforced by
31  // a command-line flag. If auto-saving is enforced, this method returns true.
32  // The default return value is false.
33  virtual bool IsAutomaticPasswordSavingEnabled() const;
34
35  // If the password manager should work for the current page. Default
36  // always returns true.
37  virtual bool IsPasswordManagerEnabledForCurrentPage() const;
38
39  // Return true if |form| should not be available for autofill.
40  virtual bool ShouldFilterAutofillResult(
41      const autofill::PasswordForm& form) = 0;
42
43  // Returns true if |username| and |origin| correspond to the account which is
44  // syncing.
45  virtual bool IsSyncAccountCredential(
46      const std::string& username, const std::string& origin) const = 0;
47
48  // Called when all autofill results have been computed. Client can use
49  // this signal to report statistics. Default implementation is a noop.
50  virtual void AutofillResultsComputed() {}
51
52  // Informs the embedder of a password form that can be saved if the user
53  // allows it. The embedder is not required to prompt the user if it decides
54  // that this form doesn't need to be saved.
55  // Returns true if the prompt was indeed displayed.
56  virtual bool PromptUserToSavePassword(
57      scoped_ptr<PasswordFormManager> form_to_save) = 0;
58
59  // Called when a password is saved in an automated fashion. Embedder may
60  // inform the user that this save has occured.
61  virtual void AutomaticPasswordSave(
62      scoped_ptr<PasswordFormManager> saved_form_manager) = 0;
63
64  // Called when a password is autofilled. |best_matches| contains the
65  // PasswordForm into which a password was filled: the client may choose to
66  // save this to the PasswordStore, for example. Default implementation is a
67  // noop.
68  virtual void PasswordWasAutofilled(
69      const autofill::PasswordFormMap& best_matches) const {}
70
71  // Called when password autofill is blocked by the blacklist. |best_matches|
72  // contains the PasswordForm that flags the current site as being on the
73  // blacklist. The client may choose to remove this from the PasswordStore in
74  // order to unblacklist a site, for example. Default implementation is a noop.
75  virtual void PasswordAutofillWasBlocked(
76      const autofill::PasswordFormMap& best_matches) const {}
77
78  // Called to authenticate the autofill password data.  If authentication is
79  // successful, this should continue filling the form.
80  virtual void AuthenticateAutofillAndFillForm(
81      scoped_ptr<autofill::PasswordFormFillData> fill_data) = 0;
82
83  // Gets prefs associated with this embedder.
84  virtual PrefService* GetPrefs() = 0;
85
86  // Returns the PasswordStore associated with this instance.
87  virtual PasswordStore* GetPasswordStore() = 0;
88
89  // Returns the PasswordManagerDriver instance associated with this instance.
90  virtual PasswordManagerDriver* GetDriver() = 0;
91
92  // Returns the probability that the experiment identified by |experiment_name|
93  // should be enabled. The default implementation returns 0.
94  virtual base::FieldTrial::Probability GetProbabilityForExperiment(
95      const std::string& experiment_name);
96
97  // Returns true if password sync is enabled in the embedder. The default
98  // implementation returns false.
99  virtual bool IsPasswordSyncEnabled();
100
101  // Only for clients which registered with a LogRouter: If called with
102  // |router_can_be_used| set to false, the client may no longer use the
103  // LogRouter. If |router_can_be_used| is true, the LogRouter can be used after
104  // the return from OnLogRouterAvailabilityChanged.
105  virtual void OnLogRouterAvailabilityChanged(bool router_can_be_used);
106
107  // Forward |text| for display to the LogRouter (if registered with one).
108  virtual void LogSavePasswordProgress(const std::string& text);
109
110  // Returns true if logs recorded via LogSavePasswordProgress will be
111  // displayed, and false otherwise.
112  virtual bool IsLoggingActive() const;
113
114  // Returns the authorization prompt policy to be used with the given form.
115  // Only relevant on OSX.
116  virtual PasswordStore::AuthorizationPromptPolicy GetAuthorizationPromptPolicy(
117      const autofill::PasswordForm& form);
118
119  // Called in response to an IPC from the renderer, triggered by a page's call
120  // to 'navigator.credentials.notifyFailedSignIn'.
121  virtual void OnNotifyFailedSignIn(int request_id, const CredentialInfo&) {}
122
123  // Called in response to an IPC from the renderer, triggered by a page's call
124  // to 'navigator.credentials.notifySignedIn'.
125  virtual void OnNotifySignedIn(int request_id, const CredentialInfo&) {}
126
127  // Called in response to an IPC from the renderer, triggered by a page's call
128  // to 'navigator.credentials.notifySignedOut'.
129  virtual void OnNotifySignedOut(int request_id) {}
130
131  // Called in response to an IPC from the renderer, triggered by a page's call
132  // to 'navigator.credentials.request'.
133  virtual void OnRequestCredential(int request_id,
134                                   bool zero_click_only,
135                                   const std::vector<GURL>& federations) {}
136
137 private:
138  DISALLOW_COPY_AND_ASSIGN(PasswordManagerClient);
139};
140
141}  // namespace password_manager
142
143#endif  // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_PASSWORD_MANAGER_CLIENT_H_
144