Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / passwords / manage_passwords_bubble_model.cc
1 // Copyright 2013 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 #include "chrome/browser/ui/passwords/manage_passwords_bubble_model.h"
6
7 #include "base/strings/string_split.h"
8 #include "base/strings/string_util.h"
9 #include "chrome/browser/password_manager/password_store_factory.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/browser_finder.h"
12 #include "chrome/browser/ui/passwords/manage_passwords_ui_controller.h"
13 #include "components/password_manager/core/browser/password_store.h"
14 #include "components/password_manager/core/common/password_manager_ui.h"
15 #include "grit/generated_resources.h"
16 #include "ui/base/l10n/l10n_util.h"
17
18 using autofill::PasswordFormMap;
19 using content::WebContents;
20 namespace metrics_util = password_manager::metrics_util;
21
22 namespace {
23
24 void SetupLinkifiedText(const base::string16& string_with_separator,
25                         base::string16* text,
26                         gfx::Range* link_range) {
27   std::vector<base::string16> pieces;
28   base::SplitStringDontTrim(string_with_separator,
29                             '|',  // separator
30                             &pieces);
31   DCHECK_EQ(3u, pieces.size());
32   *link_range = gfx::Range(pieces[0].size(),
33                            pieces[0].size() + pieces[1].size());
34   *text = JoinString(pieces, base::string16());
35 }
36
37 }  // namespace
38
39 ManagePasswordsBubbleModel::ManagePasswordsBubbleModel(
40     content::WebContents* web_contents)
41     : content::WebContentsObserver(web_contents),
42       display_disposition_(
43           metrics_util::AUTOMATIC_WITH_PASSWORD_PENDING),
44       dismissal_reason_(metrics_util::NOT_DISPLAYED) {
45   ManagePasswordsUIController* controller =
46       ManagePasswordsUIController::FromWebContents(web_contents);
47
48   // TODO(mkwst): Reverse this logic. The controller should populate the model
49   // directly rather than the model pulling from the controller. Perhaps like
50   // `controller->PopulateModel(this)`.
51   state_ = controller->state();
52   if (password_manager::ui::IsPendingState(state_))
53     pending_credentials_ = controller->PendingCredentials();
54   best_matches_ = controller->best_matches();
55
56   if (password_manager::ui::IsPendingState(state_)) {
57     title_ = l10n_util::GetStringUTF16(IDS_SAVE_PASSWORD);
58   } else if (state_ == password_manager::ui::BLACKLIST_STATE) {
59     title_ = l10n_util::GetStringUTF16(IDS_MANAGE_PASSWORDS_BLACKLISTED_TITLE);
60   } else if (state_ == password_manager::ui::CONFIRMATION_STATE) {
61     title_ =
62         l10n_util::GetStringUTF16(IDS_MANAGE_PASSWORDS_CONFIRM_GENERATED_TITLE);
63   } else {
64     title_ = l10n_util::GetStringUTF16(IDS_MANAGE_PASSWORDS_TITLE);
65   }
66
67   SetupLinkifiedText(
68       l10n_util::GetStringUTF16(IDS_MANAGE_PASSWORDS_CONFIRM_GENERATED_TEXT),
69       &save_confirmation_text_,
70       &save_confirmation_link_range_);
71
72   manage_link_ =
73       l10n_util::GetStringUTF16(IDS_OPTIONS_PASSWORDS_MANAGE_PASSWORDS_LINK);
74 }
75
76 ManagePasswordsBubbleModel::~ManagePasswordsBubbleModel() {}
77
78 void ManagePasswordsBubbleModel::OnBubbleShown(
79     ManagePasswordsBubble::DisplayReason reason) {
80   if (reason == ManagePasswordsBubble::USER_ACTION) {
81     if (password_manager::ui::IsPendingState(state_)) {
82       display_disposition_ = metrics_util::MANUAL_WITH_PASSWORD_PENDING;
83     } else if (state_ == password_manager::ui::BLACKLIST_STATE) {
84       display_disposition_ = metrics_util::MANUAL_BLACKLISTED;
85     } else {
86       display_disposition_ = metrics_util::MANUAL_MANAGE_PASSWORDS;
87     }
88   } else {
89     if (state_ == password_manager::ui::CONFIRMATION_STATE) {
90       display_disposition_ =
91           metrics_util::AUTOMATIC_GENERATED_PASSWORD_CONFIRMATION;
92     } else {
93       display_disposition_ = metrics_util::AUTOMATIC_WITH_PASSWORD_PENDING;
94     }
95   }
96   metrics_util::LogUIDisplayDisposition(display_disposition_);
97
98   // Default to a dismissal reason of "no interaction". If the user interacts
99   // with the button in such a way that it closes, we'll reset this value
100   // accordingly.
101   dismissal_reason_ = metrics_util::NO_DIRECT_INTERACTION;
102 }
103
104 void ManagePasswordsBubbleModel::OnBubbleHidden() {
105   if (dismissal_reason_ == metrics_util::NOT_DISPLAYED)
106     return;
107
108   metrics_util::LogUIDismissalReason(dismissal_reason_);
109 }
110
111 void ManagePasswordsBubbleModel::OnNopeClicked() {
112   dismissal_reason_ = metrics_util::CLICKED_NOPE;
113   state_ = password_manager::ui::PENDING_PASSWORD_STATE;
114 }
115
116 void ManagePasswordsBubbleModel::OnNeverForThisSiteClicked() {
117   dismissal_reason_ = metrics_util::CLICKED_NEVER;
118   ManagePasswordsUIController* manage_passwords_ui_controller =
119       ManagePasswordsUIController::FromWebContents(web_contents());
120   manage_passwords_ui_controller->NeverSavePassword();
121   state_ = password_manager::ui::BLACKLIST_STATE;
122 }
123
124 void ManagePasswordsBubbleModel::OnUnblacklistClicked() {
125   dismissal_reason_ = metrics_util::CLICKED_UNBLACKLIST;
126   ManagePasswordsUIController* manage_passwords_ui_controller =
127       ManagePasswordsUIController::FromWebContents(web_contents());
128   manage_passwords_ui_controller->UnblacklistSite();
129   state_ = password_manager::ui::MANAGE_STATE;
130 }
131
132 void ManagePasswordsBubbleModel::OnSaveClicked() {
133   dismissal_reason_ = metrics_util::CLICKED_SAVE;
134   ManagePasswordsUIController* manage_passwords_ui_controller =
135       ManagePasswordsUIController::FromWebContents(web_contents());
136   manage_passwords_ui_controller->SavePassword();
137   state_ = password_manager::ui::MANAGE_STATE;
138 }
139
140 void ManagePasswordsBubbleModel::OnDoneClicked() {
141   dismissal_reason_ = metrics_util::CLICKED_DONE;
142 }
143
144 // TODO(gcasto): Is it worth having this be separate from OnDoneClicked()?
145 // User intent is pretty similar in both cases.
146 void ManagePasswordsBubbleModel::OnOKClicked() {
147   dismissal_reason_ = metrics_util::CLICKED_OK;
148 }
149
150 void ManagePasswordsBubbleModel::OnManageLinkClicked() {
151   dismissal_reason_ = metrics_util::CLICKED_MANAGE;
152   ManagePasswordsUIController::FromWebContents(web_contents())
153       ->NavigateToPasswordManagerSettingsPage();
154 }
155
156 // TODO(gcasto): Is it worth having a new dismissal reason to distinguish
157 // the two management cases? User intention is pretty similar between the two,
158 // but the context in which they are shown is pretty different since one is
159 // from an explict action and the other isn't.
160 void ManagePasswordsBubbleModel::OnRemoteManageLinkClicked() {
161   dismissal_reason_ = metrics_util::CLICKED_MANAGE;
162   ManagePasswordsUIController::FromWebContents(web_contents())
163       ->NavigateToAccountCentralManagementPage();
164 }
165
166 void ManagePasswordsBubbleModel::OnPasswordAction(
167     const autofill::PasswordForm& password_form,
168     PasswordAction action) {
169   if (!web_contents())
170     return;
171   Profile* profile =
172       Profile::FromBrowserContext(web_contents()->GetBrowserContext());
173   password_manager::PasswordStore* password_store =
174       PasswordStoreFactory::GetForProfile(profile, Profile::EXPLICIT_ACCESS)
175           .get();
176   DCHECK(password_store);
177   if (action == REMOVE_PASSWORD)
178     password_store->RemoveLogin(password_form);
179   else
180     password_store->AddLogin(password_form);
181 }