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