Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(341)

Side by Side Diff: chrome/browser/autofill/autofill_manager.cc

Issue 10987100: Switch AutofillManager to be UserData on WebContents. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 using webkit::forms::FormData; 69 using webkit::forms::FormData;
69 using webkit::forms::FormDataPredictions; 70 using webkit::forms::FormDataPredictions;
70 using webkit::forms::FormField; 71 using webkit::forms::FormField;
71 72
72 namespace { 73 namespace {
73 74
75 const char* kAutofillManagerWebContentsUserDataKey = "web_contents_autofill";
76
74 // We only send a fraction of the forms to upload server. 77 // We only send a fraction of the forms to upload server.
75 // The rate for positive/negative matches potentially could be different. 78 // The rate for positive/negative matches potentially could be different.
76 const double kAutofillPositiveUploadRateDefaultValue = 0.20; 79 const double kAutofillPositiveUploadRateDefaultValue = 0.20;
77 const double kAutofillNegativeUploadRateDefaultValue = 0.20; 80 const double kAutofillNegativeUploadRateDefaultValue = 0.20;
78 81
79 const size_t kMaxRecentFormSignaturesToRemember = 3; 82 const size_t kMaxRecentFormSignaturesToRemember = 3;
80 83
81 // Set a conservative upper bound on the number of forms we are willing to 84 // Set a conservative upper bound on the number of forms we are willing to
82 // cache, simply to prevent unbounded memory consumption. 85 // cache, simply to prevent unbounded memory consumption.
83 const size_t kMaxFormCacheSize = 100; 86 const size_t kMaxFormCacheSize = 100;
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 168
166 if (matching_types.empty()) 169 if (matching_types.empty())
167 matching_types.insert(UNKNOWN_TYPE); 170 matching_types.insert(UNKNOWN_TYPE);
168 171
169 field->set_possible_types(matching_types); 172 field->set_possible_types(matching_types);
170 } 173 }
171 } 174 }
172 175
173 } // namespace 176 } // namespace
174 177
175 AutofillManager::AutofillManager(autofill::AutofillManagerDelegate* delegate, 178 // static
176 TabContents* tab_contents) 179 void AutofillManager::CreateForWebContentsAndDelegate(
177 : content::WebContentsObserver(tab_contents->web_contents()), 180 content::WebContents* contents,
181 autofill::AutofillManagerDelegate* delegate) {
182 if (FromWebContents(contents))
183 return;
184
185 contents->SetUserData(kAutofillManagerWebContentsUserDataKey,
186 new base::UserDataAdapter<AutofillManager>(
187 new AutofillManager(contents, delegate)));
188 }
189
190 // static
191 AutofillManager* AutofillManager::FromWebContents(
192 content::WebContents* contents) {
193 return base::UserDataAdapter<AutofillManager>::Get(
194 contents, kAutofillManagerWebContentsUserDataKey);
195 }
196
197 AutofillManager::AutofillManager(content::WebContents* web_contents,
198 autofill::AutofillManagerDelegate* delegate)
199 : content::WebContentsObserver(web_contents),
178 manager_delegate_(delegate), 200 manager_delegate_(delegate),
179 tab_contents_(tab_contents),
180 personal_data_(NULL), 201 personal_data_(NULL),
181 download_manager_(delegate->GetBrowserContext(), this), 202 download_manager_(delegate->GetBrowserContext(), this),
182 disable_download_manager_requests_(false), 203 disable_download_manager_requests_(false),
183 metric_logger_(new AutofillMetrics), 204 metric_logger_(new AutofillMetrics),
184 has_logged_autofill_enabled_(false), 205 has_logged_autofill_enabled_(false),
185 has_logged_address_suggestions_count_(false), 206 has_logged_address_suggestions_count_(false),
186 did_show_suggestions_(false), 207 did_show_suggestions_(false),
187 user_did_type_(false), 208 user_did_type_(false),
188 user_did_autofill_(false), 209 user_did_autofill_(false),
189 user_did_edit_autofilled_field_(false), 210 user_did_edit_autofilled_field_(false),
190 password_generation_enabled_(false), 211 password_generation_enabled_(false),
191 external_delegate_(NULL) { 212 external_delegate_(NULL) {
192 // |personal_data_| is NULL when using test-enabled WebContents. 213 // |personal_data_| is NULL when using test-enabled WebContents.
193 personal_data_ = PersonalDataManagerFactory::GetForProfile( 214 personal_data_ = PersonalDataManagerFactory::GetForProfile(
194 delegate->GetOriginalProfile()); 215 delegate->GetOriginalProfile());
195 RegisterWithSyncService(); 216 RegisterWithSyncService();
196 registrar_.Init(manager_delegate_->GetPrefs()); 217 registrar_.Init(manager_delegate_->GetPrefs());
197 registrar_.Add(prefs::kPasswordGenerationEnabled, this); 218 registrar_.Add(prefs::kPasswordGenerationEnabled, this);
219 TabContents* tab_contents = TabContents::FromWebContents(web_contents);
198 notification_registrar_.Add(this, 220 notification_registrar_.Add(this,
199 chrome::NOTIFICATION_TAB_CONTENTS_DESTROYED, 221 chrome::NOTIFICATION_TAB_CONTENTS_DESTROYED,
200 content::Source<TabContents>(tab_contents)); 222 content::Source<TabContents>(tab_contents));
201 } 223 }
202 224
203 AutofillManager::~AutofillManager() { 225 AutofillManager::~AutofillManager() {
204 } 226 }
205 227
206 // static 228 // static
207 void AutofillManager::RegisterUserPrefs(PrefServiceBase* prefs) { 229 void AutofillManager::RegisterUserPrefs(PrefServiceBase* prefs) {
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
347 OnSetDataList) 369 OnSetDataList)
348 IPC_MESSAGE_UNHANDLED(handled = false) 370 IPC_MESSAGE_UNHANDLED(handled = false)
349 IPC_END_MESSAGE_MAP() 371 IPC_END_MESSAGE_MAP()
350 372
351 return handled; 373 return handled;
352 } 374 }
353 375
354 bool AutofillManager::OnFormSubmitted(const FormData& form, 376 bool AutofillManager::OnFormSubmitted(const FormData& form,
355 const TimeTicks& timestamp) { 377 const TimeTicks& timestamp) {
356 // Let AutoComplete know as well. 378 // Let AutoComplete know as well.
357 AutocompleteHistoryManager::FromWebContents(tab_contents_->web_contents())-> 379 AutocompleteHistoryManager::FromWebContents(web_contents())->
358 OnFormSubmitted(form); 380 OnFormSubmitted(form);
359 381
360 if (!IsAutofillEnabled()) 382 if (!IsAutofillEnabled())
361 return false; 383 return false;
362 384
363 if (web_contents()->GetBrowserContext()->IsOffTheRecord()) 385 if (web_contents()->GetBrowserContext()->IsOffTheRecord())
364 return false; 386 return false;
365 387
366 // Don't save data that was submitted through JavaScript. 388 // Don't save data that was submitted through JavaScript.
367 if (!form.user_submitted) 389 if (!form.user_submitted)
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
550 metric_logger_->LogAddressSuggestionsCount(values.size()); 572 metric_logger_->LogAddressSuggestionsCount(values.size());
551 has_logged_address_suggestions_count_ = true; 573 has_logged_address_suggestions_count_ = true;
552 } 574 }
553 } 575 }
554 } 576 }
555 } 577 }
556 578
557 // Add the results from AutoComplete. They come back asynchronously, so we 579 // Add the results from AutoComplete. They come back asynchronously, so we
558 // hand off what we generated and they will send the results back to the 580 // hand off what we generated and they will send the results back to the
559 // renderer. 581 // renderer.
560 AutocompleteHistoryManager::FromWebContents(tab_contents_->web_contents())-> 582 AutocompleteHistoryManager::FromWebContents(web_contents())->
561 OnGetAutocompleteSuggestions( 583 OnGetAutocompleteSuggestions(
562 query_id, field.name, field.value, values, labels, icons, unique_ids); 584 query_id, field.name, field.value, values, labels, icons, unique_ids);
563 } 585 }
564 586
565 void AutofillManager::OnFillAutofillFormData(int query_id, 587 void AutofillManager::OnFillAutofillFormData(int query_id,
566 const FormData& form, 588 const FormData& form,
567 const FormField& field, 589 const FormField& field,
568 int unique_id) { 590 int unique_id) {
569 const std::vector<AutofillProfile*>& profiles = personal_data_->profiles(); 591 const std::vector<AutofillProfile*>& profiles = personal_data_->profiles();
570 const std::vector<CreditCard*>& credit_cards = personal_data_->credit_cards(); 592 const std::vector<CreditCard*>& credit_cards = personal_data_->credit_cards();
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after
876 user_did_type_ = false; 898 user_did_type_ = false;
877 user_did_autofill_ = false; 899 user_did_autofill_ = false;
878 user_did_edit_autofilled_field_ = false; 900 user_did_edit_autofilled_field_ = false;
879 forms_loaded_timestamp_ = TimeTicks(); 901 forms_loaded_timestamp_ = TimeTicks();
880 initial_interaction_timestamp_ = TimeTicks(); 902 initial_interaction_timestamp_ = TimeTicks();
881 903
882 if (external_delegate_) 904 if (external_delegate_)
883 external_delegate_->Reset(); 905 external_delegate_->Reset();
884 } 906 }
885 907
886 AutofillManager::AutofillManager(autofill::AutofillManagerDelegate* delegate, 908 AutofillManager::AutofillManager(content::WebContents* web_contents,
887 TabContents* tab_contents, 909 autofill::AutofillManagerDelegate* delegate,
888 PersonalDataManager* personal_data) 910 PersonalDataManager* personal_data)
889 : content::WebContentsObserver(tab_contents->web_contents()), 911 : content::WebContentsObserver(web_contents),
890 manager_delegate_(delegate), 912 manager_delegate_(delegate),
891 tab_contents_(tab_contents),
892 personal_data_(personal_data), 913 personal_data_(personal_data),
893 download_manager_(delegate->GetBrowserContext(), this), 914 download_manager_(delegate->GetBrowserContext(), this),
894 disable_download_manager_requests_(true), 915 disable_download_manager_requests_(true),
895 metric_logger_(new AutofillMetrics), 916 metric_logger_(new AutofillMetrics),
896 has_logged_autofill_enabled_(false), 917 has_logged_autofill_enabled_(false),
897 has_logged_address_suggestions_count_(false), 918 has_logged_address_suggestions_count_(false),
898 did_show_suggestions_(false), 919 did_show_suggestions_(false),
899 user_did_type_(false), 920 user_did_type_(false),
900 user_did_autofill_(false), 921 user_did_autofill_(false),
901 user_did_edit_autofilled_field_(false), 922 user_did_edit_autofilled_field_(false),
902 password_generation_enabled_(false), 923 password_generation_enabled_(false),
903 external_delegate_(NULL) { 924 external_delegate_(NULL) {
904 DCHECK(tab_contents_);
Jói 2012/10/01 10:45:19 Perhaps keep a DCHECK(web_contents_)?
Avi (use Gerrit) 2012/10/01 15:32:26 Done. I don't remember why I removed it.
905 DCHECK(manager_delegate_); 925 DCHECK(manager_delegate_);
906 RegisterWithSyncService(); 926 RegisterWithSyncService();
907 // Test code doesn't need registrar_. 927 // Test code doesn't need registrar_.
908 } 928 }
909 929
910 void AutofillManager::set_metric_logger(const AutofillMetrics* metric_logger) { 930 void AutofillManager::set_metric_logger(const AutofillMetrics* metric_logger) {
911 metric_logger_.reset(metric_logger); 931 metric_logger_.reset(metric_logger);
912 } 932 }
913 933
914 bool AutofillManager::GetHost(const std::vector<AutofillProfile*>& profiles, 934 bool AutofillManager::GetHost(const std::vector<AutofillProfile*>& profiles,
(...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after
1389 *profile_guid = IDToGUID(profile_id); 1409 *profile_guid = IDToGUID(profile_id);
1390 } 1410 }
1391 1411
1392 void AutofillManager::UpdateInitialInteractionTimestamp( 1412 void AutofillManager::UpdateInitialInteractionTimestamp(
1393 const TimeTicks& interaction_timestamp) { 1413 const TimeTicks& interaction_timestamp) {
1394 if (initial_interaction_timestamp_.is_null() || 1414 if (initial_interaction_timestamp_.is_null() ||
1395 interaction_timestamp < initial_interaction_timestamp_) { 1415 interaction_timestamp < initial_interaction_timestamp_) {
1396 initial_interaction_timestamp_ = interaction_timestamp; 1416 initial_interaction_timestamp_ = interaction_timestamp;
1397 } 1417 }
1398 } 1418 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698