OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 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/autofill/form_structure.h" |
| 6 |
| 7 #include "base/basictypes.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/sha1.h" |
| 10 #include "base/string_util.h" |
| 11 #include "base/utf_string_conversions.h" |
| 12 #include "chrome/browser/autofill/field_types.h" |
| 13 #include "third_party/libjingle/files/talk/xmllite/xmlelement.h" |
| 14 #include "webkit/glue/form_field.h" |
| 15 #include "webkit/glue/form_field_values.h" |
| 16 |
| 17 const char* kFormMethodGet = "get"; |
| 18 const char* kFormMethodPost = "post"; |
| 19 |
| 20 // XML attribute names |
| 21 const char* const kAttributeClientVersion = "clientversion"; |
| 22 const char* const kAttributeAutoFillUsed = "autofillused"; |
| 23 const char* const kAttributeSignature = "signature"; |
| 24 const char* const kAttributeFormSignature = "formsignature"; |
| 25 const char* const kAttributeDataPresent = "datapresent"; |
| 26 |
| 27 const char* const kXMLElementForm = "form"; |
| 28 const char* const kXMLElementField = "field"; |
| 29 const char* const kAttributeAutoFillType = "autofilltype"; |
| 30 |
| 31 namespace { |
| 32 |
| 33 static std::string Hash64Bit(const std::string& str) { |
| 34 std::string hash_bin = base::SHA1HashString(str); |
| 35 DCHECK(hash_bin.length() == 20); |
| 36 |
| 37 uint64 hash64 = (((static_cast<uint64>(hash_bin[0])) & 0xFF) << 56) | |
| 38 (((static_cast<uint64>(hash_bin[1])) & 0xFF) << 48) | |
| 39 (((static_cast<uint64>(hash_bin[2])) & 0xFF) << 40) | |
| 40 (((static_cast<uint64>(hash_bin[3])) & 0xFF) << 32) | |
| 41 (((static_cast<uint64>(hash_bin[4])) & 0xFF) << 24) | |
| 42 (((static_cast<uint64>(hash_bin[5])) & 0xFF) << 16) | |
| 43 (((static_cast<uint64>(hash_bin[6])) & 0xFF) << 8) | |
| 44 ((static_cast<uint64>(hash_bin[7])) & 0xFF); |
| 45 |
| 46 return Uint64ToString(hash64); |
| 47 } |
| 48 |
| 49 } // namespace |
| 50 |
| 51 FormStructure::FormStructure(const webkit_glue::FormFieldValues& values) |
| 52 : form_name_(UTF16ToUTF8(values.form_name)), |
| 53 source_url_(values.source_url), |
| 54 target_url_(values.target_url) { |
| 55 // Copy the form fields. |
| 56 std::vector<webkit_glue::FormField>::const_iterator field; |
| 57 for (field = values.elements.begin(); |
| 58 field != values.elements.end(); field++) { |
| 59 fields_.push_back(AutoFillField(*field)); |
| 60 } |
| 61 |
| 62 std::string method = UTF16ToUTF8(values.method); |
| 63 if (method == kFormMethodPost) { |
| 64 method_ = POST; |
| 65 } else { |
| 66 // Either the method is 'get', or we don't know. In this case we default |
| 67 // to GET. |
| 68 method_ = GET; |
| 69 } |
| 70 } |
| 71 |
| 72 bool FormStructure::EncodeUploadRequest(bool auto_fill_used, |
| 73 std::string* encoded_xml) const { |
| 74 bool auto_fillable = IsAutoFillable(); |
| 75 DCHECK(auto_fillable); // Caller should've checked for search pages. |
| 76 if (!auto_fillable) |
| 77 return false; |
| 78 |
| 79 buzz::XmlElement autofill_upload(buzz::QName("autofillupload")); |
| 80 |
| 81 // Attributes for the <autofillupload> element. |
| 82 // |
| 83 // TODO(jhawkins): Work with toolbar devs to make a spec for autofill clients. |
| 84 // For now these values are hacked from the toolbar code. |
| 85 autofill_upload.SetAttr(buzz::QName(kAttributeClientVersion), |
| 86 "6.1.1715.1442/en (GGLL)"); |
| 87 |
| 88 autofill_upload.SetAttr(buzz::QName(kAttributeFormSignature), |
| 89 FormSignature()); |
| 90 |
| 91 autofill_upload.SetAttr(buzz::QName(kAttributeAutoFillUsed), |
| 92 auto_fill_used ? "true" : "false"); |
| 93 |
| 94 // TODO(jhawkins): Hook this up to the personal data manager. |
| 95 // personaldata_manager_->GetDataPresent(); |
| 96 autofill_upload.SetAttr(buzz::QName(kAttributeDataPresent), ""); |
| 97 |
| 98 // Add the child nodes for the form fields. |
| 99 std::vector<AutoFillField>::const_iterator field; |
| 100 for (field = fields_.begin(); field != fields_.end(); field++) { |
| 101 FieldTypeSet types = field->possible_types(); |
| 102 for (FieldTypeSet::const_iterator type = types.begin(); |
| 103 type != types.end(); type++) { |
| 104 buzz::XmlElement *field_element = new buzz::XmlElement( |
| 105 buzz::QName(kXMLElementField)); |
| 106 |
| 107 field_element->SetAttr(buzz::QName(kAttributeSignature), |
| 108 field->FieldSignature()); |
| 109 |
| 110 field_element->SetAttr(buzz::QName(kAttributeAutoFillType), |
| 111 IntToString(*type)); |
| 112 |
| 113 autofill_upload.AddElement(field_element); |
| 114 } |
| 115 } |
| 116 |
| 117 // Obtain the XML structure as a string. |
| 118 *encoded_xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; |
| 119 *encoded_xml += autofill_upload.Str().c_str(); |
| 120 |
| 121 return true; |
| 122 } |
| 123 |
| 124 std::string FormStructure::FormSignature() const { |
| 125 std::string form_string = target_url_.host() + |
| 126 "&" + |
| 127 form_name_ + |
| 128 form_signature_field_names_; |
| 129 |
| 130 return Hash64Bit(form_string); |
| 131 } |
| 132 |
| 133 bool FormStructure::IsAutoFillable() const { |
| 134 if (fields_.size() == 0) |
| 135 return false; |
| 136 |
| 137 // Rule out http(s)://*/search?... |
| 138 // e.g. http://www.google.com/search?q=... |
| 139 // http://search.yahoo.com/search?p=... |
| 140 if (target_url_.path() == "/search") |
| 141 return false; |
| 142 |
| 143 // Disqualify all forms that are likely to be search boxes (like google.com). |
| 144 if (fields_.size() == 1) { |
| 145 std::string name = UTF16ToUTF8(fields_[0].name()); |
| 146 if (name == "q") |
| 147 return false; |
| 148 } |
| 149 |
| 150 if (method_ == GET) |
| 151 return false; |
| 152 |
| 153 return true; |
| 154 } |
OLD | NEW |