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