Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/autocomplete_history_manager.h" | 5 #include "chrome/browser/autofill/autocomplete_history_manager.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/string16.h" | 9 #include "base/string16.h" |
| 10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
| 11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "chrome/browser/api/prefs/pref_service_base.h" | 12 #include "chrome/browser/api/prefs/pref_service_base.h" |
| 13 #include "chrome/browser/autofill/autofill_external_delegate.h" | 13 #include "chrome/browser/autofill/autofill_external_delegate.h" |
| 14 #include "chrome/browser/autofill/credit_card.h" | 14 #include "chrome/browser/autofill/credit_card.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/browser/webdata/web_data_service_factory.h" | |
| 17 #include "chrome/common/autofill_messages.h" | 16 #include "chrome/common/autofill_messages.h" |
| 18 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
| 19 #include "content/public/browser/render_view_host.h" | 18 #include "content/public/browser/render_view_host.h" |
| 20 #include "content/public/browser/web_contents.h" | 19 #include "content/public/browser/web_contents.h" |
| 21 #include "webkit/forms/form_data.h" | 20 #include "webkit/forms/form_data.h" |
| 22 | 21 |
| 23 using base::StringPiece16; | 22 using base::StringPiece16; |
| 24 using content::WebContents; | 23 using content::WebContents; |
| 25 using webkit::forms::FormData; | 24 using webkit::forms::FormData; |
| 26 using webkit::forms::FormField; | 25 using webkit::forms::FormField; |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 109 } // namespace | 108 } // namespace |
| 110 | 109 |
| 111 AutocompleteHistoryManager::AutocompleteHistoryManager( | 110 AutocompleteHistoryManager::AutocompleteHistoryManager( |
| 112 WebContents* web_contents) | 111 WebContents* web_contents) |
| 113 : content::WebContentsObserver(web_contents), | 112 : content::WebContentsObserver(web_contents), |
| 114 pending_query_handle_(0), | 113 pending_query_handle_(0), |
| 115 query_id_(0), | 114 query_id_(0), |
| 116 external_delegate_(NULL) { | 115 external_delegate_(NULL) { |
| 117 profile_ = Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 116 profile_ = Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 118 // May be NULL in unit tests. | 117 // May be NULL in unit tests. |
| 119 web_data_service_ = WebDataServiceFactory::GetForProfile( | 118 autofill_data_ = AutofillWebDataService::ForContext(profile_); |
| 120 profile_, Profile::EXPLICIT_ACCESS); | |
| 121 autofill_enabled_.Init( | 119 autofill_enabled_.Init( |
| 122 prefs::kAutofillEnabled, PrefServiceBase::ForProfile(profile_), NULL); | 120 prefs::kAutofillEnabled, PrefServiceBase::ForProfile(profile_), NULL); |
| 123 } | 121 } |
| 124 | 122 |
| 125 AutocompleteHistoryManager::~AutocompleteHistoryManager() { | 123 AutocompleteHistoryManager::~AutocompleteHistoryManager() { |
| 126 CancelPendingQuery(); | 124 CancelPendingQuery(); |
| 127 } | 125 } |
| 128 | 126 |
| 129 bool AutocompleteHistoryManager::OnMessageReceived( | 127 bool AutocompleteHistoryManager::OnMessageReceived( |
| 130 const IPC::Message& message) { | 128 const IPC::Message& message) { |
| 131 bool handled = true; | 129 bool handled = true; |
| 132 IPC_BEGIN_MESSAGE_MAP(AutocompleteHistoryManager, message) | 130 IPC_BEGIN_MESSAGE_MAP(AutocompleteHistoryManager, message) |
| 133 IPC_MESSAGE_HANDLER(AutofillHostMsg_RemoveAutocompleteEntry, | 131 IPC_MESSAGE_HANDLER(AutofillHostMsg_RemoveAutocompleteEntry, |
| 134 OnRemoveAutocompleteEntry) | 132 OnRemoveAutocompleteEntry) |
| 135 IPC_MESSAGE_UNHANDLED(handled = false) | 133 IPC_MESSAGE_UNHANDLED(handled = false) |
| 136 IPC_END_MESSAGE_MAP() | 134 IPC_END_MESSAGE_MAP() |
| 137 return handled; | 135 return handled; |
| 138 } | 136 } |
| 139 | 137 |
| 140 void AutocompleteHistoryManager::OnWebDataServiceRequestDone( | 138 void AutocompleteHistoryManager::OnWebDataServiceRequestDone( |
| 141 WebDataService::Handle h, | 139 WebDataServiceBase::Handle h, |
| 142 const WDTypedResult* result) { | 140 const WDTypedResult* result) { |
| 143 DCHECK(pending_query_handle_); | 141 DCHECK(pending_query_handle_); |
| 144 pending_query_handle_ = 0; | 142 pending_query_handle_ = 0; |
| 145 | 143 |
| 146 if (!*autofill_enabled_) { | 144 if (!*autofill_enabled_) { |
| 147 SendSuggestions(NULL); | 145 SendSuggestions(NULL); |
| 148 return; | 146 return; |
| 149 } | 147 } |
| 150 | 148 |
| 151 DCHECK(result); | 149 DCHECK(result); |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 177 query_id_ = query_id; | 175 query_id_ = query_id; |
| 178 autofill_values_ = autofill_values; | 176 autofill_values_ = autofill_values; |
| 179 autofill_labels_ = autofill_labels; | 177 autofill_labels_ = autofill_labels; |
| 180 autofill_icons_ = autofill_icons; | 178 autofill_icons_ = autofill_icons; |
| 181 autofill_unique_ids_ = autofill_unique_ids; | 179 autofill_unique_ids_ = autofill_unique_ids; |
| 182 if (!*autofill_enabled_) { | 180 if (!*autofill_enabled_) { |
| 183 SendSuggestions(NULL); | 181 SendSuggestions(NULL); |
| 184 return; | 182 return; |
| 185 } | 183 } |
| 186 | 184 |
| 187 if (web_data_service_.get()) { | 185 if (autofill_data_.get()) { |
| 188 pending_query_handle_ = web_data_service_->GetFormValuesForElementName( | 186 pending_query_handle_ = autofill_data_->GetFormValuesForElementName( |
| 189 name, prefix, kMaxAutocompleteMenuItems, this); | 187 name, prefix, kMaxAutocompleteMenuItems, this); |
| 190 } | 188 } |
| 191 } | 189 } |
| 192 | 190 |
| 193 void AutocompleteHistoryManager::OnFormSubmitted(const FormData& form) { | 191 void AutocompleteHistoryManager::OnFormSubmitted(const FormData& form) { |
| 194 if (!*autofill_enabled_) | 192 if (!*autofill_enabled_) |
| 195 return; | 193 return; |
| 196 | 194 |
| 197 if (profile_->IsOffTheRecord()) | 195 if (profile_->IsOffTheRecord()) |
| 198 return; | 196 return; |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 213 iter != form.fields.end(); ++iter) { | 211 iter != form.fields.end(); ++iter) { |
| 214 if (!iter->value.empty() && | 212 if (!iter->value.empty() && |
| 215 !iter->name.empty() && | 213 !iter->name.empty() && |
| 216 IsTextField(*iter) && | 214 IsTextField(*iter) && |
| 217 !CreditCard::IsValidCreditCardNumber(iter->value) && | 215 !CreditCard::IsValidCreditCardNumber(iter->value) && |
| 218 !IsSSN(iter->value)) { | 216 !IsSSN(iter->value)) { |
| 219 values.push_back(*iter); | 217 values.push_back(*iter); |
| 220 } | 218 } |
| 221 } | 219 } |
| 222 | 220 |
| 223 if (!values.empty() && web_data_service_.get()) | 221 if (!values.empty() && autofill_data_.get()) |
| 224 web_data_service_->AddFormFields(values); | 222 autofill_data_->AddFormFields(values); |
| 225 } | 223 } |
| 226 | 224 |
| 227 void AutocompleteHistoryManager::OnRemoveAutocompleteEntry( | 225 void AutocompleteHistoryManager::OnRemoveAutocompleteEntry( |
| 228 const string16& name, const string16& value) { | 226 const string16& name, const string16& value) { |
| 229 if (web_data_service_.get()) | 227 if (autofill_data_.get()) |
| 230 web_data_service_->RemoveFormValueForElementName(name, value); | 228 autofill_data_->RemoveFormValueForElementName(name, value); |
| 231 } | 229 } |
| 232 | 230 |
| 233 void AutocompleteHistoryManager::SetExternalDelegate( | 231 void AutocompleteHistoryManager::SetExternalDelegate( |
| 234 AutofillExternalDelegate* delegate) { | 232 AutofillExternalDelegate* delegate) { |
| 235 external_delegate_ = delegate; | 233 external_delegate_ = delegate; |
| 236 } | 234 } |
| 237 | 235 |
| 238 AutocompleteHistoryManager::AutocompleteHistoryManager( | 236 AutocompleteHistoryManager::AutocompleteHistoryManager( |
| 239 WebContents* web_contents, | 237 WebContents* web_contents, |
| 240 Profile* profile, | 238 Profile* profile, |
| 241 WebDataService* wds) | 239 scoped_ptr<AutofillWebDataService> awd) |
| 242 : content::WebContentsObserver(web_contents), | 240 : content::WebContentsObserver(web_contents), |
| 243 profile_(profile), | 241 profile_(profile), |
| 244 web_data_service_(wds), | 242 autofill_data_(awd.Pass()), |
|
erikwright (departed)
2012/09/13 01:45:11
I'm more familiar with release() in this scenario.
Jói
2012/09/13 12:34:50
Pass() is the right thing to use in this scenario
| |
| 245 pending_query_handle_(0), | 243 pending_query_handle_(0), |
| 246 query_id_(0), | 244 query_id_(0), |
| 247 external_delegate_(NULL) { | 245 external_delegate_(NULL) { |
| 248 autofill_enabled_.Init( | 246 autofill_enabled_.Init( |
| 249 prefs::kAutofillEnabled, PrefServiceBase::ForProfile(profile_), NULL); | 247 prefs::kAutofillEnabled, PrefServiceBase::ForProfile(profile_), NULL); |
| 250 } | 248 } |
| 251 | 249 |
| 252 void AutocompleteHistoryManager::CancelPendingQuery() { | 250 void AutocompleteHistoryManager::CancelPendingQuery() { |
| 253 if (pending_query_handle_) { | 251 if (pending_query_handle_) { |
| 254 SendSuggestions(NULL); | 252 SendSuggestions(NULL); |
| 255 if (web_data_service_.get()) | 253 if (autofill_data_.get()) |
| 256 web_data_service_->CancelRequest(pending_query_handle_); | 254 autofill_data_->CancelRequest(pending_query_handle_); |
| 257 pending_query_handle_ = 0; | 255 pending_query_handle_ = 0; |
| 258 } | 256 } |
| 259 } | 257 } |
| 260 | 258 |
| 261 void AutocompleteHistoryManager::SendSuggestions( | 259 void AutocompleteHistoryManager::SendSuggestions( |
| 262 const std::vector<string16>* suggestions) { | 260 const std::vector<string16>* suggestions) { |
| 263 if (suggestions) { | 261 if (suggestions) { |
| 264 // Combine Autofill and Autocomplete values into values and labels. | 262 // Combine Autofill and Autocomplete values into values and labels. |
| 265 for (size_t i = 0; i < suggestions->size(); ++i) { | 263 for (size_t i = 0; i < suggestions->size(); ++i) { |
| 266 bool unique = true; | 264 bool unique = true; |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 296 autofill_icons_, | 294 autofill_icons_, |
| 297 autofill_unique_ids_)); | 295 autofill_unique_ids_)); |
| 298 } | 296 } |
| 299 | 297 |
| 300 query_id_ = 0; | 298 query_id_ = 0; |
| 301 autofill_values_.clear(); | 299 autofill_values_.clear(); |
| 302 autofill_labels_.clear(); | 300 autofill_labels_.clear(); |
| 303 autofill_icons_.clear(); | 301 autofill_icons_.clear(); |
| 304 autofill_unique_ids_.clear(); | 302 autofill_unique_ids_.clear(); |
| 305 } | 303 } |
| OLD | NEW |