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/webdata/web_data_service_factory.h" | 5 #include "chrome/browser/webdata/web_data_service_factory.h" |
6 | 6 |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "chrome/browser/profiles/profile_dependency_manager.h" | 8 #include "chrome/browser/profiles/profile_dependency_manager.h" |
9 #include "chrome/browser/webdata/autofill_web_data_service_impl.h" | 9 #include "chrome/browser/webdata/autofill_web_data_service_impl.h" |
10 #include "chrome/browser/webdata/web_data_service.h" | |
11 #include "chrome/common/chrome_constants.h" | 10 #include "chrome/common/chrome_constants.h" |
12 | 11 |
| 12 WebDataServiceWrapper::WebDataServiceWrapper() {} |
| 13 |
| 14 WebDataServiceWrapper::WebDataServiceWrapper(Profile* profile){ |
| 15 base::FilePath path = profile->GetPath(); |
| 16 path = path.Append(chrome::kWebDataFilename); |
| 17 web_data_service_ = new WebDataService(); |
| 18 web_data_service_->Init(path); |
| 19 } |
| 20 |
| 21 WebDataServiceWrapper::~WebDataServiceWrapper() { |
| 22 } |
| 23 |
| 24 void WebDataServiceWrapper::Shutdown() { |
| 25 web_data_service_->ShutdownOnUIThread(); |
| 26 web_data_service_ = NULL; |
| 27 } |
| 28 |
| 29 scoped_refptr<WebDataService> WebDataServiceWrapper::GetWebData() { |
| 30 return web_data_service_.get(); |
| 31 } |
| 32 |
13 // static | 33 // static |
14 scoped_ptr<AutofillWebDataService> AutofillWebDataService::FromBrowserContext( | 34 scoped_refptr<AutofillWebDataService> |
15 content::BrowserContext* context) { | 35 AutofillWebDataService::FromBrowserContext(content::BrowserContext* context) { |
16 // For this service, the implicit/explicit distinction doesn't | 36 // For this service, the implicit/explicit distinction doesn't |
17 // really matter; it's just used for a DCHECK. So we currently | 37 // really matter; it's just used for a DCHECK. So we currently |
18 // cheat and always say EXPLICIT_ACCESS. | 38 // cheat and always say EXPLICIT_ACCESS. |
19 scoped_refptr<WebDataService> service = WebDataServiceFactory::GetForProfile( | 39 scoped_refptr<WebDataService> service = WebDataServiceFactory::GetForProfile( |
20 static_cast<Profile*>(context), Profile::EXPLICIT_ACCESS); | 40 static_cast<Profile*>(context), Profile::EXPLICIT_ACCESS); |
21 | 41 |
22 if (service.get()) { | 42 if (service.get()) { |
23 return scoped_ptr<AutofillWebDataService>( | 43 return scoped_refptr<AutofillWebDataService>( |
24 new AutofillWebDataServiceImpl(service)); | 44 new AutofillWebDataServiceImpl(service)); |
25 } else { | 45 } else { |
26 return scoped_ptr<AutofillWebDataService>(NULL); | 46 return scoped_refptr<AutofillWebDataService>(NULL); |
27 } | 47 } |
28 } | 48 } |
29 | 49 |
30 // static | 50 // static |
31 scoped_refptr<WebDataService> WebDataService::FromBrowserContext( | 51 scoped_refptr<WebDataService> WebDataService::FromBrowserContext( |
32 content::BrowserContext* context) { | 52 content::BrowserContext* context) { |
33 // For this service, the implicit/explicit distinction doesn't | 53 // For this service, the implicit/explicit distinction doesn't |
34 // really matter; it's just used for a DCHECK. So we currently | 54 // really matter; it's just used for a DCHECK. So we currently |
35 // cheat and always say EXPLICIT_ACCESS. | 55 // cheat and always say EXPLICIT_ACCESS. |
36 return WebDataServiceFactory::GetForProfile( | 56 return WebDataServiceFactory::GetForProfile( |
37 static_cast<Profile*>(context), Profile::EXPLICIT_ACCESS); | 57 static_cast<Profile*>(context), Profile::EXPLICIT_ACCESS); |
38 } | 58 } |
39 | 59 |
40 WebDataServiceFactory::WebDataServiceFactory() | 60 WebDataServiceFactory::WebDataServiceFactory() |
41 : RefcountedProfileKeyedServiceFactory( | 61 : ProfileKeyedServiceFactory( |
42 "WebDataService", | 62 "WebDataService", |
43 ProfileDependencyManager::GetInstance()) { | 63 ProfileDependencyManager::GetInstance()) { |
44 // WebDataServiceFactory has no dependecies. | 64 // WebDataServiceFactory has no dependecies. |
45 } | 65 } |
46 | 66 |
47 WebDataServiceFactory::~WebDataServiceFactory() {} | 67 WebDataServiceFactory::~WebDataServiceFactory() {} |
48 | 68 |
49 // static | 69 // static |
50 scoped_refptr<WebDataService> WebDataServiceFactory::GetForProfile( | 70 scoped_refptr<WebDataService> WebDataServiceFactory::GetForProfile( |
51 Profile* profile, Profile::ServiceAccessType access_type) { | 71 Profile* profile, Profile::ServiceAccessType access_type) { |
52 // If |access_type| starts being used for anything other than this | 72 // If |access_type| starts being used for anything other than this |
53 // DCHECK, we need to start taking it as a parameter to | 73 // DCHECK, we need to start taking it as a parameter to |
54 // AutofillWebDataServiceImpl::FromBrowserContext (see above). | 74 // AutofillWebDataServiceImpl::FromBrowserContext (see above). |
55 DCHECK(access_type != Profile::IMPLICIT_ACCESS || !profile->IsOffTheRecord()); | 75 DCHECK(access_type != Profile::IMPLICIT_ACCESS || !profile->IsOffTheRecord()); |
56 return static_cast<WebDataService*>( | 76 WebDataServiceWrapper* wrapper = |
57 GetInstance()->GetServiceForProfile(profile, true).get()); | 77 static_cast<WebDataServiceWrapper*>( |
| 78 GetInstance()->GetServiceForProfile(profile, true)); |
| 79 if (wrapper) |
| 80 return wrapper->GetWebData(); |
| 81 // |wrapper| can be NULL in Incognito mode. |
| 82 return NULL; |
58 } | 83 } |
59 | 84 |
60 // static | 85 // static |
61 scoped_refptr<WebDataService> WebDataServiceFactory::GetForProfileIfExists( | 86 scoped_refptr<WebDataService> WebDataServiceFactory::GetForProfileIfExists( |
62 Profile* profile, Profile::ServiceAccessType access_type) { | 87 Profile* profile, Profile::ServiceAccessType access_type) { |
63 // If |access_type| starts being used for anything other than this | 88 // If |access_type| starts being used for anything other than this |
64 // DCHECK, we need to start taking it as a parameter to | 89 // DCHECK, we need to start taking it as a parameter to |
65 // AutofillWebDataServiceImpl::FromBrowserContext (see above). | 90 // AutofillWebDataServiceImpl::FromBrowserContext (see above). |
66 DCHECK(access_type != Profile::IMPLICIT_ACCESS || !profile->IsOffTheRecord()); | 91 DCHECK(access_type != Profile::IMPLICIT_ACCESS || !profile->IsOffTheRecord()); |
67 return static_cast<WebDataService*>( | 92 WebDataServiceWrapper* wrapper = |
68 GetInstance()->GetServiceForProfile(profile, false).get()); | 93 static_cast<WebDataServiceWrapper*>( |
| 94 GetInstance()->GetServiceForProfile(profile, true)); |
| 95 if (wrapper) |
| 96 return wrapper->GetWebData(); |
| 97 // |wrapper| can be NULL in Incognito mode. |
| 98 return NULL; |
69 } | 99 } |
70 | 100 |
71 // static | 101 // static |
72 WebDataServiceFactory* WebDataServiceFactory::GetInstance() { | 102 WebDataServiceFactory* WebDataServiceFactory::GetInstance() { |
73 return Singleton<WebDataServiceFactory>::get(); | 103 return Singleton<WebDataServiceFactory>::get(); |
74 } | 104 } |
75 | 105 |
76 bool WebDataServiceFactory::ServiceRedirectedInIncognito() const { | 106 bool WebDataServiceFactory::ServiceRedirectedInIncognito() const { |
77 return true; | 107 return true; |
78 } | 108 } |
79 | 109 |
80 scoped_refptr<RefcountedProfileKeyedService> | 110 ProfileKeyedService* |
81 WebDataServiceFactory::BuildServiceInstanceFor(Profile* profile) const { | 111 WebDataServiceFactory::BuildServiceInstanceFor(Profile* profile) const { |
82 DCHECK(profile); | 112 return new WebDataServiceWrapper(profile); |
83 | |
84 base::FilePath path = profile->GetPath(); | |
85 path = path.Append(chrome::kWebDataFilename); | |
86 | |
87 scoped_refptr<WebDataService> wds(new WebDataService()); | |
88 if (!wds->Init(profile->GetPath())) | |
89 NOTREACHED(); | |
90 return wds.get(); | |
91 } | 113 } |
92 | 114 |
93 bool WebDataServiceFactory::ServiceIsNULLWhileTesting() const { | 115 bool WebDataServiceFactory::ServiceIsNULLWhileTesting() const { |
94 return true; | 116 return true; |
95 } | 117 } |
OLD | NEW |