[M47_2526] Chromium upversion to m47_2526 branch
[platform/framework/web/chromium-efl.git] / tizen_src / ewk / efl_integration / browser / password_manager / password_store_factory.cc
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Copyright 2014 Samsung Electronics. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5
6 #if defined(TIZEN_AUTOFILL_SUPPORT)
7
8 #include "browser/password_manager/password_store_factory.h"
9
10 #include "base/command_line.h"
11 #include "base/environment.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/path_service.h"
14 #include "base/files/file_path.h"
15 #include "browser/webdata/web_data_service.h"
16 #include "browser/webdata/web_data_service_factory.h"
17 #include "components/password_manager/core/browser/login_database.h"
18 #include "components/password_manager/core/browser/password_store.h"
19 #include "components/password_manager/core/browser/password_store_default.h"
20 #include "content/common/paths_efl.h"
21 #include "content/public/browser/browser_thread.h"
22
23 using namespace password_manager;
24 using password_manager::PasswordStore;
25
26 PasswordStoreService::PasswordStoreService(
27     scoped_refptr<PasswordStore> password_store)
28     : password_store_(password_store)
29 {
30 }
31
32 PasswordStoreService::~PasswordStoreService()
33 {
34 }
35
36 scoped_refptr<PasswordStore> PasswordStoreService::GetPasswordStore()
37 {
38   return password_store_;
39 }
40
41 // static
42 scoped_refptr<PasswordStore> PasswordStoreFactory::GetPasswordStore()
43 {
44   PasswordStoreService* service = GetInstance()->GetService();
45   if (!service)
46     return NULL;
47   return service->GetPasswordStore();
48 }
49
50 // static
51 PasswordStoreFactory* PasswordStoreFactory::GetInstance()
52 {
53   return base::Singleton<PasswordStoreFactory>::get();
54 }
55
56 PasswordStoreFactory::PasswordStoreFactory()
57     : service_(NULL) {
58   WebDataServiceFactory::GetInstance();
59   Init();
60 }
61
62 PasswordStoreFactory::~PasswordStoreFactory()
63 {
64 }
65
66 void PasswordStoreFactory::Init()
67 {
68   base::FilePath db_path;
69   if (!PathService::Get(PathsEfl::WEB_DATABASE_DIR, &db_path)) {
70     LOG(ERROR) << "Could not access login database path.";
71     return;
72   }
73
74   base::FilePath login_db_file_path = db_path.Append(FILE_PATH_LITERAL(".LoginData.db"));
75
76   scoped_ptr<LoginDatabase> login_db(new LoginDatabase(login_db_file_path));
77   {
78     base::ThreadRestrictions::ScopedAllowIO allow_io;
79     if (!login_db->Init()) {
80       LOG(ERROR) << "Could not initialize login database.";
81       return;
82     }
83   }
84
85   scoped_refptr<base::SingleThreadTaskRunner> main_thread_runner(
86       base::ThreadTaskRunnerHandle::Get());
87   scoped_refptr<base::SingleThreadTaskRunner> db_thread_runner(
88       content::BrowserThread::GetMessageLoopProxyForThread(
89           content::BrowserThread::DB));
90
91   scoped_refptr<PasswordStore> ps;
92
93   ps = new PasswordStoreDefault(
94       main_thread_runner, db_thread_runner, login_db.Pass());
95   if (!ps.get() || !ps->Init(syncer::SyncableService::StartSyncFlare())) {
96     NOTREACHED() << "Could not initialize password manager.";
97     return;
98   }
99
100   service_ = new PasswordStoreService(ps);
101 }
102
103 #endif // TIZEN_AUTOFILL_SUPPORT