save_password_progress_logger.cc revision c5cede9ae108bb15f6b7a8aea21c7e1fefa2834c
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#include "components/autofill/core/common/save_password_progress_logger.h"
6
7#include "base/json/json_writer.h"
8#include "base/logging.h"
9#include "base/numerics/safe_conversions.h"
10#include "base/values.h"
11#include "components/autofill/core/common/password_form.h"
12
13using base::checked_cast;
14using base::Value;
15using base::DictionaryValue;
16using base::FundamentalValue;
17using base::StringValue;
18
19namespace autofill {
20
21namespace {
22
23// Removes privacy sensitive parts of |url| (currently all but host and scheme).
24std::string ScrubURL(const GURL& url) {
25  if (url.is_valid())
26    return url.GetWithEmptyPath().spec();
27  return std::string();
28}
29
30std::string FormSchemeToString(PasswordForm::Scheme scheme) {
31  switch (scheme) {
32    case PasswordForm::SCHEME_HTML:
33      return "HTML";
34    case PasswordForm::SCHEME_BASIC:
35      return "BASIC";
36    case PasswordForm::SCHEME_DIGEST:
37      return "DIGEST";
38    case PasswordForm::SCHEME_OTHER:
39      return "OTHER";
40  }
41  NOTREACHED();  // Win compilers don't believe this is unreachable.
42  return std::string();
43}
44
45StringValue DecisionToStringValue(
46    SavePasswordProgressLogger::Decision decision) {
47  switch (decision) {
48    case SavePasswordProgressLogger::DECISION_SAVE:
49      return StringValue("SAVE the password");
50    case SavePasswordProgressLogger::DECISION_ASK:
51      return StringValue("ASK the user whether to save the password");
52    case SavePasswordProgressLogger::DECISION_DROP:
53      return StringValue("DROP the password");
54  }
55  NOTREACHED();  // Win compilers don't believe this is unreachable.
56  return StringValue(std::string());
57}
58
59}  // namespace
60
61SavePasswordProgressLogger::SavePasswordProgressLogger() {}
62
63SavePasswordProgressLogger::~SavePasswordProgressLogger() {}
64
65void SavePasswordProgressLogger::LogPasswordForm(const std::string& message,
66                                                 const PasswordForm& form) {
67  DictionaryValue log;
68  // Do not use the "<<" operator for PasswordForms, because it also prints
69  // passwords. Also, that operator is only for testing.
70  log.SetString("scheme", FormSchemeToString(form.scheme));
71  log.SetString("signon realm", ScrubURL(GURL(form.signon_realm)));
72  log.SetString("original signon realm",
73                ScrubURL(GURL(form.original_signon_realm)));
74  log.SetString("origin", ScrubURL(form.origin));
75  log.SetString("action", ScrubURL(form.action));
76  log.SetString("username element", form.username_element);
77  log.SetString("password element", form.password_element);
78  log.SetBoolean("password autocomplete set", form.password_autocomplete_set);
79  log.SetString("old password element", form.old_password_element);
80  log.SetBoolean("ssl valid", form.ssl_valid);
81  log.SetBoolean("password generated",
82                 form.type == PasswordForm::TYPE_GENERATED);
83  log.SetInteger("times used", form.times_used);
84  log.SetBoolean("use additional authentication",
85                 form.use_additional_authentication);
86  log.SetBoolean("is PSL match", form.IsPublicSuffixMatch());
87  LogValue(message, log);
88}
89
90void SavePasswordProgressLogger::LogHTMLForm(const std::string& message,
91                                             const std::string& name_or_id,
92                                             const std::string& method,
93                                             const GURL& action) {
94  DictionaryValue log;
95  log.SetString("name_or_id", name_or_id);
96  log.SetString("method", method);
97  log.SetString("action", ScrubURL(action));
98  LogValue(message, log);
99}
100
101void SavePasswordProgressLogger::LogURL(const std::string& message,
102                                        const GURL& url) {
103  LogValue(message, StringValue(ScrubURL(url)));
104}
105
106void SavePasswordProgressLogger::LogBoolean(const std::string& message,
107                                            bool value) {
108  LogValue(message, FundamentalValue(value));
109}
110
111void SavePasswordProgressLogger::LogNumber(const std::string& message,
112                                           int value) {
113  LogValue(message, FundamentalValue(value));
114}
115
116void SavePasswordProgressLogger::LogNumber(const std::string& message,
117                                           size_t value) {
118  LogValue(message, FundamentalValue(checked_cast<int, size_t>(value)));
119}
120
121void SavePasswordProgressLogger::LogFinalDecision(Decision decision) {
122  LogValue("Final decision taken", DecisionToStringValue(decision));
123}
124
125void SavePasswordProgressLogger::LogMessage(const std::string& message) {
126  LogValue("Message", StringValue(message));
127}
128
129void SavePasswordProgressLogger::LogValue(const std::string& name,
130                                          const Value& log) {
131  std::string log_string;
132  bool conversion_to_string_successful = base::JSONWriter::WriteWithOptions(
133      &log, base::JSONWriter::OPTIONS_PRETTY_PRINT, &log_string);
134  DCHECK(conversion_to_string_successful);
135  SendLog(name + ": " + log_string);
136}
137
138}  // namespace autofill
139