OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <ostream> | 5 #include <ostream> |
| 6 #include <sstream> |
6 | 7 |
| 8 #include "base/json/json_writer.h" |
7 #include "base/strings/string16.h" | 9 #include "base/strings/string16.h" |
8 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
9 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "base/values.h" |
10 #include "components/autofill/core/common/password_form.h" | 13 #include "components/autofill/core/common/password_form.h" |
11 | 14 |
12 namespace autofill { | 15 namespace autofill { |
13 | 16 |
| 17 namespace { |
| 18 |
| 19 // Serializes a PasswordForm to a JSON object. Used only for logging in tests. |
| 20 void PasswordFormToJSON(const PasswordForm& form, |
| 21 base::DictionaryValue* target) { |
| 22 target->SetInteger("scheme", form.scheme); |
| 23 target->SetString("signon_realm", form.signon_realm); |
| 24 target->SetString("signon_realm", form.signon_realm); |
| 25 target->SetString("original_signon_realm", form.original_signon_realm); |
| 26 target->SetString("origin", form.origin.possibly_invalid_spec()); |
| 27 target->SetString("action", form.action.possibly_invalid_spec()); |
| 28 target->SetString("submit_element", form.submit_element); |
| 29 target->SetString("username_elem", form.username_element); |
| 30 target->SetBoolean("username_marked_by_site", form.username_marked_by_site); |
| 31 target->SetString("username_value", form.username_value); |
| 32 target->SetString("password_elem", form.password_element); |
| 33 target->SetString("password_value", form.password_value); |
| 34 target->SetString("new_password_element", form.new_password_element); |
| 35 target->SetString("new_password_value", form.new_password_value); |
| 36 target->SetString("other_possible_usernames", |
| 37 JoinString(form.other_possible_usernames, '|')); |
| 38 target->SetBoolean("autocomplete_set", form.password_autocomplete_set); |
| 39 target->SetBoolean("blacklisted", form.blacklisted_by_user); |
| 40 target->SetBoolean("preferred", form.preferred); |
| 41 target->SetBoolean("ssl_valid", form.ssl_valid); |
| 42 target->SetDouble("date_created", form.date_created.ToDoubleT()); |
| 43 target->SetDouble("date_synced", form.date_synced.ToDoubleT()); |
| 44 target->SetInteger("type", form.type); |
| 45 target->SetInteger("times_used", form.times_used); |
| 46 std::ostringstream form_data_string_stream; |
| 47 form_data_string_stream << form.form_data; |
| 48 target->SetString("form_data", form_data_string_stream.str()); |
| 49 target->SetInteger("generation_upload_status", form.generation_upload_status); |
| 50 target->SetString("display_name", form.display_name); |
| 51 target->SetString("avatar_url", form.avatar_url.possibly_invalid_spec()); |
| 52 target->SetString("federation_url", |
| 53 form.federation_url.possibly_invalid_spec()); |
| 54 target->SetBoolean("skip_next_zero_click", form.skip_zero_click); |
| 55 std::ostringstream layout_string_stream; |
| 56 layout_string_stream << form.layout; |
| 57 target->SetString("layout", layout_string_stream.str()); |
| 58 } |
| 59 |
| 60 } // namespace |
| 61 |
14 PasswordForm::PasswordForm() | 62 PasswordForm::PasswordForm() |
15 : scheme(SCHEME_HTML), | 63 : scheme(SCHEME_HTML), |
16 username_marked_by_site(false), | 64 username_marked_by_site(false), |
17 password_autocomplete_set(true), | 65 password_autocomplete_set(true), |
18 ssl_valid(false), | 66 ssl_valid(false), |
19 preferred(false), | 67 preferred(false), |
20 blacklisted_by_user(false), | 68 blacklisted_by_user(false), |
21 type(TYPE_MANUAL), | 69 type(TYPE_MANUAL), |
22 times_used(0), | 70 times_used(0), |
23 generation_upload_status(NO_SIGNAL_SENT), | 71 generation_upload_status(NO_SIGNAL_SENT), |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 os << "LAYOUT_OTHER"; | 122 os << "LAYOUT_OTHER"; |
75 break; | 123 break; |
76 case PasswordForm::Layout::LAYOUT_LOGIN_AND_SIGNUP: | 124 case PasswordForm::Layout::LAYOUT_LOGIN_AND_SIGNUP: |
77 os << "LAYOUT_LOGIN_AND_SIGNUP"; | 125 os << "LAYOUT_LOGIN_AND_SIGNUP"; |
78 break; | 126 break; |
79 } | 127 } |
80 return os; | 128 return os; |
81 } | 129 } |
82 | 130 |
83 std::ostream& operator<<(std::ostream& os, const PasswordForm& form) { | 131 std::ostream& operator<<(std::ostream& os, const PasswordForm& form) { |
84 return os << "scheme: " << form.scheme | 132 base::DictionaryValue form_json; |
85 << " signon_realm: " << form.signon_realm | 133 PasswordFormToJSON(form, &form_json); |
86 << " original_signon_realm: " << form.original_signon_realm | 134 |
87 << " origin: " << form.origin | 135 // Serialize the default PasswordForm, and remove values from the result that |
88 << " action: " << form.action | 136 // are equal to this to make the results more concise. |
89 << " submit_element: " << base::UTF16ToUTF8(form.submit_element) | 137 base::DictionaryValue default_form_json; |
90 << " username_elem: " << base::UTF16ToUTF8(form.username_element) | 138 PasswordFormToJSON(PasswordForm(), &default_form_json); |
91 << " username_marked_by_site: " << form.username_marked_by_site | 139 for (base::DictionaryValue::Iterator it_default_key_values(default_form_json); |
92 << " username_value: " << base::UTF16ToUTF8(form.username_value) | 140 !it_default_key_values.IsAtEnd(); it_default_key_values.Advance()) { |
93 << " password_elem: " << base::UTF16ToUTF8(form.password_element) | 141 const base::Value* actual_value; |
94 << " password_value: " << base::UTF16ToUTF8(form.password_value) | 142 if (form_json.Get(it_default_key_values.key(), &actual_value) && |
95 << " new_password_element: " | 143 it_default_key_values.value().Equals(actual_value)) { |
96 << base::UTF16ToUTF8(form.new_password_element) | 144 form_json.Remove(it_default_key_values.key(), nullptr); |
97 << " new_password_value: " | 145 } |
98 << base::UTF16ToUTF8(form.new_password_value) | 146 } |
99 << " other_possible_usernames: " | 147 |
100 << JoinString(form.other_possible_usernames, '|') | 148 std::string form_as_string; |
101 << " autocomplete_set:" << form.password_autocomplete_set | 149 base::JSONWriter::WriteWithOptions( |
102 << " blacklisted: " << form.blacklisted_by_user | 150 &form_json, base::JSONWriter::OPTIONS_PRETTY_PRINT, &form_as_string); |
103 << " preferred: " << form.preferred | 151 base::TrimWhitespaceASCII(form_as_string, base::TRIM_ALL, &form_as_string); |
104 << " ssl_valid: " << form.ssl_valid | 152 return os << "PasswordForm(" << form_as_string << ")"; |
105 << " date_created: " << form.date_created.ToDoubleT() | 153 } |
106 << " date_synced: " << form.date_synced.ToDoubleT() | 154 |
107 << " type: " << form.type | 155 std::ostream& operator<<(std::ostream& os, PasswordForm* form) { |
108 << " times_used: " << form.times_used | 156 return os << "&" << *form; |
109 << " form_data: " << form.form_data | |
110 << " generation_upload_status: " << form.generation_upload_status | |
111 << " display_name: " << base::UTF16ToUTF8(form.display_name) | |
112 << " avatar_url: " << form.avatar_url | |
113 << " federation_url: " << form.federation_url | |
114 << " skip_next_zero_click: " << form.skip_zero_click | |
115 << " layout: " << form.layout; | |
116 } | 157 } |
117 | 158 |
118 } // namespace autofill | 159 } // namespace autofill |
OLD | NEW |