1 // Copyright 2014 Samsung Electronics. 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.
5 #if defined(TIZEN_AUTOFILL_SUPPORT)
7 #include "browser/autofill/personal_data_manager_factory.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "browser/webdata/web_data_service_factory.h"
12 #include "components/autofill/core/browser/personal_data_manager.h"
13 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
14 #include "components/user_prefs/user_prefs.h"
15 #include "content/common/content_client_export.h"
16 #include "content/public/common/content_client.h"
20 PersonalDataManagerFactory* PersonalDataManagerFactory::GetInstance() {
21 return base::Singleton<PersonalDataManagerFactory>::get();
24 void PersonalDataManagerFactory::PersonalDataManagerAdd(content::BrowserContext* ctx) {
28 uint64_t uniqueId = reinterpret_cast<uint64_t>(ctx);
29 PersonalDataManager* mgr = personal_data_manager_id_map_.Lookup(uniqueId);
33 PrefService* srv = user_prefs::UserPrefs::Get(ctx);
35 content::ContentBrowserClient* client = content::GetContentClientExport()->browser();
36 mgr = new PersonalDataManager(client->GetApplicationLocale());
37 mgr->Init(WebDataServiceFactory::GetAutofillWebDataForProfile(), srv, NULL, ctx->IsOffTheRecord());
38 mgr->AddObserver(this);
39 personal_data_manager_id_map_.AddWithID(mgr, uniqueId);
44 void PersonalDataManagerFactory::PersonalDataManagerRemove(content::BrowserContext* ctx) {
45 uint64_t uniqueId = reinterpret_cast<uint64_t>(ctx);
46 personal_data_manager_id_map_.Remove(uniqueId);
49 PersonalDataManager* PersonalDataManagerFactory::PersonalDataManagerForContext(
50 content::BrowserContext* ctx) {
51 uint64_t uniqueId = reinterpret_cast<uint64_t>(ctx);
52 return personal_data_manager_id_map_.Lookup(uniqueId);
55 int PersonalDataManagerFactory::GetProfileCount(
56 content::BrowserContext* ctx) {
57 PersonalDataManager* mgr = PersonalDataManagerForContext(ctx);
59 return mgr->GetProfiles().size();
63 AutofillProfile* PersonalDataManagerFactory::GetProfileByIndex(
64 content::BrowserContext* ctx, int index) {
65 PersonalDataManager* mgr = PersonalDataManagerForContext(ctx);
67 const std::vector<AutofillProfile*>& profiles = mgr->GetProfiles();
68 size_t index_size_t = static_cast<size_t>(index);
69 DCHECK_LT(index_size_t, profiles.size());
70 return (profiles[index_size_t]);
75 void PersonalDataManagerFactory::AddProfile(content::BrowserContext* ctx, const AutofillProfile& profile) {
76 PersonalDataManager* mgr = PersonalDataManagerForContext(ctx);
78 mgr->AddProfile(profile);
81 void PersonalDataManagerFactory::UpdateProfile(content::BrowserContext* ctx, const AutofillProfile& profile) {
82 PersonalDataManager* mgr = PersonalDataManagerForContext(ctx);
84 mgr->UpdateProfile(profile);
87 void PersonalDataManagerFactory::RemoveByGUID(content::BrowserContext* ctx, const std::string& guid) {
88 PersonalDataManager* mgr = PersonalDataManagerForContext(ctx);
90 mgr->RemoveByGUID(guid);
93 AutofillProfile* PersonalDataManagerFactory::GetProfileByGUID(content::BrowserContext* ctx, const std::string& guid) {
94 PersonalDataManager* mgr = PersonalDataManagerForContext(ctx);
96 return mgr->GetProfileByGUID(guid);
101 void PersonalDataManagerFactory::AddCreditCard(content::BrowserContext* ctx, const CreditCard& credit_card) {
102 PersonalDataManager* mgr = PersonalDataManagerForContext(ctx);
104 mgr->AddCreditCard(credit_card);
107 void PersonalDataManagerFactory::UpdateCreditCard(content::BrowserContext* ctx, const CreditCard& credit_card) {
108 PersonalDataManager* mgr = PersonalDataManagerForContext(ctx);
110 mgr->UpdateCreditCard(credit_card);
113 CreditCard* PersonalDataManagerFactory::GetCreditCardByGUID(content::BrowserContext* ctx, const std::string& guid) {
114 PersonalDataManager* mgr = PersonalDataManagerForContext(ctx);
116 return mgr->GetCreditCardByGUID(guid);
121 int PersonalDataManagerFactory::GetCreditCardCount(content::BrowserContext* ctx) {
122 PersonalDataManager* mgr = PersonalDataManagerForContext(ctx);
124 return mgr->GetCreditCards().size();
129 CreditCard* PersonalDataManagerFactory::GetCreditCardByIndex(content::BrowserContext* ctx, int index) {
130 PersonalDataManager* mgr = PersonalDataManagerForContext(ctx);
132 const std::vector<CreditCard*>& credit_cards = mgr->GetCreditCards();
133 size_t index_size_t = static_cast<size_t>(index);
134 DCHECK_LT(index_size_t, credit_cards.size());
135 return (credit_cards[index_size_t]);
140 PersonalDataManagerFactory::PersonalDataManagerFactory()
142 , callback_user_data_(NULL) {
145 PersonalDataManagerFactory::~PersonalDataManagerFactory() {
146 CHECK(personal_data_manager_id_map_.IsEmpty());
149 void PersonalDataManagerFactory::SetCallback(
150 Ewk_Context_Form_Autofill_Profile_Changed_Callback callback,
152 callback_ = callback;
153 callback_user_data_ = user_data;
156 void PersonalDataManagerFactory::OnPersonalDataChanged() {
159 callback_(callback_user_data_);
162 } // namespace autofill
164 #endif // TIZEN_AUTOFILL_SUPPORT