[M47_2526] Chromium upversion to m47_2526 branch
[platform/framework/web/chromium-efl.git] / tizen_src / ewk / efl_integration / browser / autofill / personal_data_manager_factory.cc
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.
4
5 #if defined(TIZEN_AUTOFILL_SUPPORT)
6
7 #include "browser/autofill/personal_data_manager_factory.h"
8
9 #include "eweb_view.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"
17
18 namespace autofill {
19 // static
20 PersonalDataManagerFactory* PersonalDataManagerFactory::GetInstance() {
21   return base::Singleton<PersonalDataManagerFactory>::get();
22 }
23
24 void PersonalDataManagerFactory::PersonalDataManagerAdd(content::BrowserContext* ctx) {
25   DCHECK(ctx);
26
27   if (ctx) {
28     uint64_t uniqueId = reinterpret_cast<uint64_t>(ctx);
29     PersonalDataManager* mgr = personal_data_manager_id_map_.Lookup(uniqueId);
30
31     if (!mgr) {
32       // TODO: LOCALE!
33       PrefService* srv = user_prefs::UserPrefs::Get(ctx);
34       CHECK(srv);
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);
40     }
41   }
42 }
43
44 void PersonalDataManagerFactory::PersonalDataManagerRemove(content::BrowserContext* ctx) {
45     uint64_t uniqueId = reinterpret_cast<uint64_t>(ctx);
46     personal_data_manager_id_map_.Remove(uniqueId);
47 }
48
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);
53 }
54
55 int PersonalDataManagerFactory::GetProfileCount(
56    content::BrowserContext* ctx) {
57   PersonalDataManager* mgr = PersonalDataManagerForContext(ctx);
58   if (mgr)
59     return mgr->GetProfiles().size();
60   return 0;
61 }
62
63 AutofillProfile* PersonalDataManagerFactory::GetProfileByIndex(
64     content::BrowserContext* ctx, int index) {
65   PersonalDataManager* mgr = PersonalDataManagerForContext(ctx);
66   if (mgr) {
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]);
71   }
72   return NULL;
73 }
74
75 void PersonalDataManagerFactory::AddProfile(content::BrowserContext* ctx, const AutofillProfile& profile) {
76   PersonalDataManager* mgr = PersonalDataManagerForContext(ctx);
77   if (mgr)
78     mgr->AddProfile(profile);
79 }
80
81 void PersonalDataManagerFactory::UpdateProfile(content::BrowserContext* ctx, const AutofillProfile& profile) {
82   PersonalDataManager* mgr = PersonalDataManagerForContext(ctx);
83   if (mgr)
84     mgr->UpdateProfile(profile);
85 }
86
87 void PersonalDataManagerFactory::RemoveByGUID(content::BrowserContext* ctx, const std::string& guid) {
88   PersonalDataManager* mgr = PersonalDataManagerForContext(ctx);
89   if (mgr)
90     mgr->RemoveByGUID(guid);
91 }
92
93 AutofillProfile* PersonalDataManagerFactory::GetProfileByGUID(content::BrowserContext* ctx, const std::string& guid) {
94   PersonalDataManager* mgr = PersonalDataManagerForContext(ctx);
95   if (mgr)
96     return mgr->GetProfileByGUID(guid);
97
98   return NULL;
99 }
100
101 void PersonalDataManagerFactory::AddCreditCard(content::BrowserContext* ctx, const CreditCard& credit_card) {
102   PersonalDataManager* mgr = PersonalDataManagerForContext(ctx);
103   if (mgr)
104     mgr->AddCreditCard(credit_card);
105 }
106
107 void PersonalDataManagerFactory::UpdateCreditCard(content::BrowserContext* ctx, const CreditCard& credit_card) {
108   PersonalDataManager* mgr = PersonalDataManagerForContext(ctx);
109   if (mgr)
110     mgr->UpdateCreditCard(credit_card);
111 }
112
113 CreditCard* PersonalDataManagerFactory::GetCreditCardByGUID(content::BrowserContext* ctx, const std::string& guid) {
114   PersonalDataManager* mgr = PersonalDataManagerForContext(ctx);
115   if (mgr)
116     return mgr->GetCreditCardByGUID(guid);
117
118   return NULL;
119 }
120
121 int PersonalDataManagerFactory::GetCreditCardCount(content::BrowserContext* ctx) {
122   PersonalDataManager* mgr = PersonalDataManagerForContext(ctx);
123   if (mgr)
124     return mgr->GetCreditCards().size();
125
126   return 0;
127 }
128
129 CreditCard* PersonalDataManagerFactory::GetCreditCardByIndex(content::BrowserContext* ctx, int index) {
130   PersonalDataManager* mgr = PersonalDataManagerForContext(ctx);
131   if (mgr) {
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]);
136   }
137   return NULL;
138 }
139
140 PersonalDataManagerFactory::PersonalDataManagerFactory()
141   : callback_(NULL)
142   , callback_user_data_(NULL) {
143 }
144
145 PersonalDataManagerFactory::~PersonalDataManagerFactory() {
146   CHECK(personal_data_manager_id_map_.IsEmpty());
147 }
148
149 void PersonalDataManagerFactory::SetCallback(
150     Ewk_Context_Form_Autofill_Profile_Changed_Callback callback,
151     void* user_data) {
152   callback_ = callback;
153   callback_user_data_ = user_data;
154 }
155
156 void PersonalDataManagerFactory::OnPersonalDataChanged() {
157   if(!callback_)
158     return;
159   callback_(callback_user_data_);
160 }
161
162 }  // namespace autofill
163
164 #endif // TIZEN_AUTOFILL_SUPPORT