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/autofill_manager.h" | 5 #include "chrome/browser/autofill/autofill_manager.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <limits> | 9 #include <limits> |
10 #include <map> | 10 #include <map> |
11 #include <set> | 11 #include <set> |
12 #include <utility> | 12 #include <utility> |
13 | 13 |
14 #include "base/bind.h" | 14 #include "base/bind.h" |
15 #include "base/command_line.h" | 15 #include "base/command_line.h" |
16 #include "base/guid.h" | 16 #include "base/guid.h" |
17 #include "base/logging.h" | 17 #include "base/logging.h" |
18 #include "base/string16.h" | 18 #include "base/string16.h" |
19 #include "base/string_util.h" | 19 #include "base/string_util.h" |
| 20 #include "base/supports_user_data.h" |
20 #include "base/threading/sequenced_worker_pool.h" | 21 #include "base/threading/sequenced_worker_pool.h" |
21 #include "base/utf_string_conversions.h" | 22 #include "base/utf_string_conversions.h" |
22 #include "chrome/browser/api/infobars/infobar_service.h" | 23 #include "chrome/browser/api/infobars/infobar_service.h" |
23 #include "chrome/browser/api/prefs/pref_service_base.h" | 24 #include "chrome/browser/api/prefs/pref_service_base.h" |
24 #include "chrome/browser/api/sync/profile_sync_service_base.h" | 25 #include "chrome/browser/api/sync/profile_sync_service_base.h" |
25 #include "chrome/browser/autofill/autocomplete_history_manager.h" | 26 #include "chrome/browser/autofill/autocomplete_history_manager.h" |
26 #include "chrome/browser/autofill/autofill_cc_infobar_delegate.h" | 27 #include "chrome/browser/autofill/autofill_cc_infobar_delegate.h" |
27 #include "chrome/browser/autofill/autofill_external_delegate.h" | 28 #include "chrome/browser/autofill/autofill_external_delegate.h" |
28 #include "chrome/browser/autofill/autofill_field.h" | 29 #include "chrome/browser/autofill/autofill_field.h" |
29 #include "chrome/browser/autofill/autofill_manager_delegate.h" | 30 #include "chrome/browser/autofill/autofill_manager_delegate.h" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 #include "ui/base/l10n/l10n_util.h" | 62 #include "ui/base/l10n/l10n_util.h" |
62 #include "ui/gfx/rect.h" | 63 #include "ui/gfx/rect.h" |
63 | 64 |
64 using base::TimeTicks; | 65 using base::TimeTicks; |
65 using content::BrowserThread; | 66 using content::BrowserThread; |
66 using content::RenderViewHost; | 67 using content::RenderViewHost; |
67 using switches::kEnableAutofillFeedback; | 68 using switches::kEnableAutofillFeedback; |
68 | 69 |
69 namespace { | 70 namespace { |
70 | 71 |
| 72 const char* kAutofillManagerWebContentsUserDataKey = "web_contents_autofill"; |
| 73 |
71 // We only send a fraction of the forms to upload server. | 74 // We only send a fraction of the forms to upload server. |
72 // The rate for positive/negative matches potentially could be different. | 75 // The rate for positive/negative matches potentially could be different. |
73 const double kAutofillPositiveUploadRateDefaultValue = 0.20; | 76 const double kAutofillPositiveUploadRateDefaultValue = 0.20; |
74 const double kAutofillNegativeUploadRateDefaultValue = 0.20; | 77 const double kAutofillNegativeUploadRateDefaultValue = 0.20; |
75 | 78 |
76 const size_t kMaxRecentFormSignaturesToRemember = 3; | 79 const size_t kMaxRecentFormSignaturesToRemember = 3; |
77 | 80 |
78 // Set a conservative upper bound on the number of forms we are willing to | 81 // Set a conservative upper bound on the number of forms we are willing to |
79 // cache, simply to prevent unbounded memory consumption. | 82 // cache, simply to prevent unbounded memory consumption. |
80 const size_t kMaxFormCacheSize = 100; | 83 const size_t kMaxFormCacheSize = 100; |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 | 165 |
163 if (matching_types.empty()) | 166 if (matching_types.empty()) |
164 matching_types.insert(UNKNOWN_TYPE); | 167 matching_types.insert(UNKNOWN_TYPE); |
165 | 168 |
166 field->set_possible_types(matching_types); | 169 field->set_possible_types(matching_types); |
167 } | 170 } |
168 } | 171 } |
169 | 172 |
170 } // namespace | 173 } // namespace |
171 | 174 |
172 AutofillManager::AutofillManager(autofill::AutofillManagerDelegate* delegate, | 175 // static |
173 TabContents* tab_contents) | 176 void AutofillManager::CreateForWebContentsAndDelegate( |
174 : content::WebContentsObserver(tab_contents->web_contents()), | 177 content::WebContents* contents, |
| 178 autofill::AutofillManagerDelegate* delegate) { |
| 179 if (FromWebContents(contents)) |
| 180 return; |
| 181 |
| 182 contents->SetUserData(kAutofillManagerWebContentsUserDataKey, |
| 183 new base::UserDataAdapter<AutofillManager>( |
| 184 new AutofillManager(contents, delegate))); |
| 185 } |
| 186 |
| 187 // static |
| 188 AutofillManager* AutofillManager::FromWebContents( |
| 189 content::WebContents* contents) { |
| 190 return base::UserDataAdapter<AutofillManager>::Get( |
| 191 contents, kAutofillManagerWebContentsUserDataKey); |
| 192 } |
| 193 |
| 194 AutofillManager::AutofillManager(content::WebContents* web_contents, |
| 195 autofill::AutofillManagerDelegate* delegate) |
| 196 : content::WebContentsObserver(web_contents), |
175 manager_delegate_(delegate), | 197 manager_delegate_(delegate), |
176 tab_contents_(tab_contents), | |
177 personal_data_(NULL), | 198 personal_data_(NULL), |
178 download_manager_(delegate->GetBrowserContext(), this), | 199 download_manager_(delegate->GetBrowserContext(), this), |
179 disable_download_manager_requests_(false), | 200 disable_download_manager_requests_(false), |
180 metric_logger_(new AutofillMetrics), | 201 metric_logger_(new AutofillMetrics), |
181 has_logged_autofill_enabled_(false), | 202 has_logged_autofill_enabled_(false), |
182 has_logged_address_suggestions_count_(false), | 203 has_logged_address_suggestions_count_(false), |
183 did_show_suggestions_(false), | 204 did_show_suggestions_(false), |
184 user_did_type_(false), | 205 user_did_type_(false), |
185 user_did_autofill_(false), | 206 user_did_autofill_(false), |
186 user_did_edit_autofilled_field_(false), | 207 user_did_edit_autofilled_field_(false), |
187 password_generation_enabled_(false), | 208 password_generation_enabled_(false), |
188 external_delegate_(NULL) { | 209 external_delegate_(NULL) { |
189 // |personal_data_| is NULL when using test-enabled WebContents. | 210 // |personal_data_| is NULL when using test-enabled WebContents. |
190 personal_data_ = PersonalDataManagerFactory::GetForProfile( | 211 personal_data_ = PersonalDataManagerFactory::GetForProfile( |
191 delegate->GetOriginalProfile()); | 212 delegate->GetOriginalProfile()); |
192 RegisterWithSyncService(); | 213 RegisterWithSyncService(); |
193 registrar_.Init(manager_delegate_->GetPrefs()); | 214 registrar_.Init(manager_delegate_->GetPrefs()); |
194 registrar_.Add(prefs::kPasswordGenerationEnabled, this); | 215 registrar_.Add(prefs::kPasswordGenerationEnabled, this); |
| 216 TabContents* tab_contents = TabContents::FromWebContents(web_contents); |
195 notification_registrar_.Add(this, | 217 notification_registrar_.Add(this, |
196 chrome::NOTIFICATION_TAB_CONTENTS_DESTROYED, | 218 chrome::NOTIFICATION_TAB_CONTENTS_DESTROYED, |
197 content::Source<TabContents>(tab_contents)); | 219 content::Source<TabContents>(tab_contents)); |
198 } | 220 } |
199 | 221 |
200 AutofillManager::~AutofillManager() { | 222 AutofillManager::~AutofillManager() { |
201 } | 223 } |
202 | 224 |
203 // static | 225 // static |
204 void AutofillManager::RegisterUserPrefs(PrefServiceBase* prefs) { | 226 void AutofillManager::RegisterUserPrefs(PrefServiceBase* prefs) { |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
344 OnSetDataList) | 366 OnSetDataList) |
345 IPC_MESSAGE_UNHANDLED(handled = false) | 367 IPC_MESSAGE_UNHANDLED(handled = false) |
346 IPC_END_MESSAGE_MAP() | 368 IPC_END_MESSAGE_MAP() |
347 | 369 |
348 return handled; | 370 return handled; |
349 } | 371 } |
350 | 372 |
351 bool AutofillManager::OnFormSubmitted(const FormData& form, | 373 bool AutofillManager::OnFormSubmitted(const FormData& form, |
352 const TimeTicks& timestamp) { | 374 const TimeTicks& timestamp) { |
353 // Let AutoComplete know as well. | 375 // Let AutoComplete know as well. |
354 AutocompleteHistoryManager::FromWebContents(tab_contents_->web_contents())-> | 376 AutocompleteHistoryManager::FromWebContents(web_contents())-> |
355 OnFormSubmitted(form); | 377 OnFormSubmitted(form); |
356 | 378 |
357 if (!IsAutofillEnabled()) | 379 if (!IsAutofillEnabled()) |
358 return false; | 380 return false; |
359 | 381 |
360 if (web_contents()->GetBrowserContext()->IsOffTheRecord()) | 382 if (web_contents()->GetBrowserContext()->IsOffTheRecord()) |
361 return false; | 383 return false; |
362 | 384 |
363 // Don't save data that was submitted through JavaScript. | 385 // Don't save data that was submitted through JavaScript. |
364 if (!form.user_submitted) | 386 if (!form.user_submitted) |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
547 metric_logger_->LogAddressSuggestionsCount(values.size()); | 569 metric_logger_->LogAddressSuggestionsCount(values.size()); |
548 has_logged_address_suggestions_count_ = true; | 570 has_logged_address_suggestions_count_ = true; |
549 } | 571 } |
550 } | 572 } |
551 } | 573 } |
552 } | 574 } |
553 | 575 |
554 // Add the results from AutoComplete. They come back asynchronously, so we | 576 // Add the results from AutoComplete. They come back asynchronously, so we |
555 // hand off what we generated and they will send the results back to the | 577 // hand off what we generated and they will send the results back to the |
556 // renderer. | 578 // renderer. |
557 AutocompleteHistoryManager::FromWebContents(tab_contents_->web_contents())-> | 579 AutocompleteHistoryManager::FromWebContents(web_contents())-> |
558 OnGetAutocompleteSuggestions( | 580 OnGetAutocompleteSuggestions( |
559 query_id, field.name, field.value, values, labels, icons, unique_ids); | 581 query_id, field.name, field.value, values, labels, icons, unique_ids); |
560 } | 582 } |
561 | 583 |
562 void AutofillManager::OnFillAutofillFormData(int query_id, | 584 void AutofillManager::OnFillAutofillFormData(int query_id, |
563 const FormData& form, | 585 const FormData& form, |
564 const FormFieldData& field, | 586 const FormFieldData& field, |
565 int unique_id) { | 587 int unique_id) { |
566 const std::vector<AutofillProfile*>& profiles = personal_data_->profiles(); | 588 const std::vector<AutofillProfile*>& profiles = personal_data_->profiles(); |
567 const std::vector<CreditCard*>& credit_cards = personal_data_->credit_cards(); | 589 const std::vector<CreditCard*>& credit_cards = personal_data_->credit_cards(); |
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
873 user_did_type_ = false; | 895 user_did_type_ = false; |
874 user_did_autofill_ = false; | 896 user_did_autofill_ = false; |
875 user_did_edit_autofilled_field_ = false; | 897 user_did_edit_autofilled_field_ = false; |
876 forms_loaded_timestamp_ = TimeTicks(); | 898 forms_loaded_timestamp_ = TimeTicks(); |
877 initial_interaction_timestamp_ = TimeTicks(); | 899 initial_interaction_timestamp_ = TimeTicks(); |
878 | 900 |
879 if (external_delegate_) | 901 if (external_delegate_) |
880 external_delegate_->Reset(); | 902 external_delegate_->Reset(); |
881 } | 903 } |
882 | 904 |
883 AutofillManager::AutofillManager(autofill::AutofillManagerDelegate* delegate, | 905 AutofillManager::AutofillManager(content::WebContents* web_contents, |
884 TabContents* tab_contents, | 906 autofill::AutofillManagerDelegate* delegate, |
885 PersonalDataManager* personal_data) | 907 PersonalDataManager* personal_data) |
886 : content::WebContentsObserver(tab_contents->web_contents()), | 908 : content::WebContentsObserver(web_contents), |
887 manager_delegate_(delegate), | 909 manager_delegate_(delegate), |
888 tab_contents_(tab_contents), | |
889 personal_data_(personal_data), | 910 personal_data_(personal_data), |
890 download_manager_(delegate->GetBrowserContext(), this), | 911 download_manager_(delegate->GetBrowserContext(), this), |
891 disable_download_manager_requests_(true), | 912 disable_download_manager_requests_(true), |
892 metric_logger_(new AutofillMetrics), | 913 metric_logger_(new AutofillMetrics), |
893 has_logged_autofill_enabled_(false), | 914 has_logged_autofill_enabled_(false), |
894 has_logged_address_suggestions_count_(false), | 915 has_logged_address_suggestions_count_(false), |
895 did_show_suggestions_(false), | 916 did_show_suggestions_(false), |
896 user_did_type_(false), | 917 user_did_type_(false), |
897 user_did_autofill_(false), | 918 user_did_autofill_(false), |
898 user_did_edit_autofilled_field_(false), | 919 user_did_edit_autofilled_field_(false), |
899 password_generation_enabled_(false), | 920 password_generation_enabled_(false), |
900 external_delegate_(NULL) { | 921 external_delegate_(NULL) { |
901 DCHECK(tab_contents_); | 922 DCHECK(web_contents); |
902 DCHECK(manager_delegate_); | 923 DCHECK(manager_delegate_); |
903 RegisterWithSyncService(); | 924 RegisterWithSyncService(); |
904 // Test code doesn't need registrar_. | 925 // Test code doesn't need registrar_. |
905 } | 926 } |
906 | 927 |
907 void AutofillManager::set_metric_logger(const AutofillMetrics* metric_logger) { | 928 void AutofillManager::set_metric_logger(const AutofillMetrics* metric_logger) { |
908 metric_logger_.reset(metric_logger); | 929 metric_logger_.reset(metric_logger); |
909 } | 930 } |
910 | 931 |
911 bool AutofillManager::GetHost(const std::vector<AutofillProfile*>& profiles, | 932 bool AutofillManager::GetHost(const std::vector<AutofillProfile*>& profiles, |
(...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1386 *profile_guid = IDToGUID(profile_id); | 1407 *profile_guid = IDToGUID(profile_id); |
1387 } | 1408 } |
1388 | 1409 |
1389 void AutofillManager::UpdateInitialInteractionTimestamp( | 1410 void AutofillManager::UpdateInitialInteractionTimestamp( |
1390 const TimeTicks& interaction_timestamp) { | 1411 const TimeTicks& interaction_timestamp) { |
1391 if (initial_interaction_timestamp_.is_null() || | 1412 if (initial_interaction_timestamp_.is_null() || |
1392 interaction_timestamp < initial_interaction_timestamp_) { | 1413 interaction_timestamp < initial_interaction_timestamp_) { |
1393 initial_interaction_timestamp_ = interaction_timestamp; | 1414 initial_interaction_timestamp_ = interaction_timestamp; |
1394 } | 1415 } |
1395 } | 1416 } |
OLD | NEW |