| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/browser/autofill/form_structure.h" | 5 #include "chrome/browser/autofill/form_structure.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/sha1.h" | 9 #include "base/sha1.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "chrome/browser/autofill/field_types.h" | 12 #include "chrome/browser/autofill/field_types.h" |
| 13 #include "chrome/browser/autofill/form_field.h" | 13 #include "chrome/browser/autofill/form_field.h" |
| 14 #include "third_party/libjingle/files/talk/xmllite/xmlelement.h" | 14 #include "third_party/libjingle/files/talk/xmllite/xmlelement.h" |
| 15 #include "webkit/glue/form_data.h" | 15 #include "webkit/glue/form_data.h" |
| 16 #include "webkit/glue/form_field.h" | 16 #include "webkit/glue/form_field.h" |
| 17 #include "webkit/glue/form_field_values.h" | 17 #include "webkit/glue/form_field_values.h" |
| 18 | 18 |
| 19 const char* kFormMethodGet = "get"; | 19 namespace { |
| 20 |
| 20 const char* kFormMethodPost = "post"; | 21 const char* kFormMethodPost = "post"; |
| 21 | 22 |
| 22 // XML attribute names | 23 // XML attribute names. |
| 23 const char* const kAttributeClientVersion = "clientversion"; | 24 const char* const kAttributeClientVersion = "clientversion"; |
| 24 const char* const kAttributeAutoFillUsed = "autofillused"; | 25 const char* const kAttributeAutoFillUsed = "autofillused"; |
| 25 const char* const kAttributeSignature = "signature"; | 26 const char* const kAttributeSignature = "signature"; |
| 26 const char* const kAttributeFormSignature = "formsignature"; | 27 const char* const kAttributeFormSignature = "formsignature"; |
| 27 const char* const kAttributeDataPresent = "datapresent"; | 28 const char* const kAttributeDataPresent = "datapresent"; |
| 28 | 29 |
| 29 const char* const kXMLElementForm = "form"; | 30 const char* const kXMLElementForm = "form"; |
| 30 const char* const kXMLElementField = "field"; | 31 const char* const kXMLElementField = "field"; |
| 31 const char* const kAttributeAutoFillType = "autofilltype"; | 32 const char* const kAttributeAutoFillType = "autofilltype"; |
| 32 | 33 |
| 33 namespace { | 34 // The only form control type we handle currently. |
| 35 const char* const kControlTypeText = "text"; |
| 36 |
| 37 // The number of fillable fields necessary for a form to be fillable. |
| 38 const size_t kRequiredFillableFields = 3; |
| 34 | 39 |
| 35 static std::string Hash64Bit(const std::string& str) { | 40 static std::string Hash64Bit(const std::string& str) { |
| 36 std::string hash_bin = base::SHA1HashString(str); | 41 std::string hash_bin = base::SHA1HashString(str); |
| 37 DCHECK_EQ(20U, hash_bin.length()); | 42 DCHECK_EQ(20U, hash_bin.length()); |
| 38 | 43 |
| 39 uint64 hash64 = (((static_cast<uint64>(hash_bin[0])) & 0xFF) << 56) | | 44 uint64 hash64 = (((static_cast<uint64>(hash_bin[0])) & 0xFF) << 56) | |
| 40 (((static_cast<uint64>(hash_bin[1])) & 0xFF) << 48) | | 45 (((static_cast<uint64>(hash_bin[1])) & 0xFF) << 48) | |
| 41 (((static_cast<uint64>(hash_bin[2])) & 0xFF) << 40) | | 46 (((static_cast<uint64>(hash_bin[2])) & 0xFF) << 40) | |
| 42 (((static_cast<uint64>(hash_bin[3])) & 0xFF) << 32) | | 47 (((static_cast<uint64>(hash_bin[3])) & 0xFF) << 32) | |
| 43 (((static_cast<uint64>(hash_bin[4])) & 0xFF) << 24) | | 48 (((static_cast<uint64>(hash_bin[4])) & 0xFF) << 24) | |
| 44 (((static_cast<uint64>(hash_bin[5])) & 0xFF) << 16) | | 49 (((static_cast<uint64>(hash_bin[5])) & 0xFF) << 16) | |
| 45 (((static_cast<uint64>(hash_bin[6])) & 0xFF) << 8) | | 50 (((static_cast<uint64>(hash_bin[6])) & 0xFF) << 8) | |
| 46 ((static_cast<uint64>(hash_bin[7])) & 0xFF); | 51 ((static_cast<uint64>(hash_bin[7])) & 0xFF); |
| 47 | 52 |
| 48 return Uint64ToString(hash64); | 53 return Uint64ToString(hash64); |
| 49 } | 54 } |
| 50 | 55 |
| 51 } // namespace | 56 } // namespace |
| 52 | 57 |
| 53 FormStructure::FormStructure(const webkit_glue::FormFieldValues& values) | 58 FormStructure::FormStructure(const webkit_glue::FormFieldValues& values) |
| 54 : form_name_(UTF16ToUTF8(values.form_name)), | 59 : form_name_(UTF16ToUTF8(values.form_name)), |
| 55 source_url_(values.source_url), | 60 source_url_(values.source_url), |
| 56 target_url_(values.target_url) { | 61 target_url_(values.target_url) { |
| 57 // Copy the form fields. | 62 // Copy the form fields. |
| 58 std::vector<webkit_glue::FormField>::const_iterator field; | 63 std::vector<webkit_glue::FormField>::const_iterator field; |
| 59 for (field = values.elements.begin(); | 64 for (field = values.elements.begin(); |
| 60 field != values.elements.end(); field++) { | 65 field != values.elements.end(); field++) { |
| 66 // We currently only handle text fields. This prevents us from thinking we |
| 67 // can autofill other types of controls, e.g., select, password, hidden. |
| 68 if (!LowerCaseEqualsASCII(field->form_control_type(), kControlTypeText)) |
| 69 continue; |
| 70 |
| 61 // Generate a unique name for this field by appending a counter to the name. | 71 // Generate a unique name for this field by appending a counter to the name. |
| 62 string16 unique_name = field->name() + IntToString16(fields_.size() + 1); | 72 string16 unique_name = field->name() + IntToString16(fields_.size() + 1); |
| 63 fields_.push_back(new AutoFillField(*field, unique_name)); | 73 fields_.push_back(new AutoFillField(*field, unique_name)); |
| 64 } | 74 } |
| 65 | 75 |
| 66 // Terminate the vector with a NULL item. | 76 // Terminate the vector with a NULL item. |
| 67 fields_.push_back(NULL); | 77 fields_.push_back(NULL); |
| 68 | 78 |
| 69 std::string method = UTF16ToUTF8(values.method); | 79 std::string method = UTF16ToUTF8(values.method); |
| 70 if (method == kFormMethodPost) { | 80 if (method == kFormMethodPost) { |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 std::string FormStructure::FormSignature() const { | 168 std::string FormStructure::FormSignature() const { |
| 159 std::string form_string = target_url_.host() + | 169 std::string form_string = target_url_.host() + |
| 160 "&" + | 170 "&" + |
| 161 form_name_ + | 171 form_name_ + |
| 162 form_signature_field_names_; | 172 form_signature_field_names_; |
| 163 | 173 |
| 164 return Hash64Bit(form_string); | 174 return Hash64Bit(form_string); |
| 165 } | 175 } |
| 166 | 176 |
| 167 bool FormStructure::IsAutoFillable() const { | 177 bool FormStructure::IsAutoFillable() const { |
| 168 if (field_count() == 0) | 178 if (field_count() < kRequiredFillableFields) |
| 169 return false; | 179 return false; |
| 170 | 180 |
| 171 // Rule out http(s)://*/search?... | 181 // Rule out http(s)://*/search?... |
| 172 // e.g. http://www.google.com/search?q=... | 182 // e.g. http://www.google.com/search?q=... |
| 173 // http://search.yahoo.com/search?p=... | 183 // http://search.yahoo.com/search?p=... |
| 174 if (target_url_.path() == "/search") | 184 if (target_url_.path() == "/search") |
| 175 return false; | 185 return false; |
| 176 | 186 |
| 177 // Disqualify all forms that are likely to be search boxes (like google.com). | |
| 178 if (field_count() == 1) { | |
| 179 std::string name = UTF16ToUTF8(fields_[0]->name()); | |
| 180 if (name == "q") | |
| 181 return false; | |
| 182 } | |
| 183 | |
| 184 if (method_ == GET) | 187 if (method_ == GET) |
| 185 return false; | 188 return false; |
| 186 | 189 |
| 187 return true; | 190 return true; |
| 188 } | 191 } |
| 189 | 192 |
| 190 void FormStructure::set_possible_types(int index, const FieldTypeSet& types) { | 193 void FormStructure::set_possible_types(int index, const FieldTypeSet& types) { |
| 191 int num_fields = static_cast<int>(field_count()); | 194 int num_fields = static_cast<int>(field_count()); |
| 192 DCHECK(index >= 0 && index < num_fields); | 195 DCHECK(index >= 0 && index < num_fields); |
| 193 if (index >= 0 && index < num_fields) | 196 if (index >= 0 && index < num_fields) |
| (...skipping 26 matching lines...) Expand all Loading... |
| 220 | 223 |
| 221 void FormStructure::GetHeuristicFieldInfo(FieldTypeMap* field_type_map) { | 224 void FormStructure::GetHeuristicFieldInfo(FieldTypeMap* field_type_map) { |
| 222 FormFieldSet fields = FormFieldSet(this); | 225 FormFieldSet fields = FormFieldSet(this); |
| 223 | 226 |
| 224 FormFieldSet::const_iterator field; | 227 FormFieldSet::const_iterator field; |
| 225 for (field = fields.begin(); field != fields.end(); field++) { | 228 for (field = fields.begin(); field != fields.end(); field++) { |
| 226 bool ok = (*field)->GetFieldInfo(field_type_map); | 229 bool ok = (*field)->GetFieldInfo(field_type_map); |
| 227 DCHECK(ok); | 230 DCHECK(ok); |
| 228 } | 231 } |
| 229 } | 232 } |
| OLD | NEW |