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