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/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 |
11 | 40 |
12 WebDataServiceWrapper::WebDataServiceWrapper() {} | 41 WebDataServiceWrapper::WebDataServiceWrapper() {} |
13 | 42 |
14 WebDataServiceWrapper::WebDataServiceWrapper(Profile* profile){ | 43 WebDataServiceWrapper::WebDataServiceWrapper(Profile* profile){ |
15 base::FilePath path = profile->GetPath(); | 44 base::FilePath path = profile->GetPath(); |
16 path = path.Append(chrome::kWebDataFilename); | 45 path = path.Append(chrome::kWebDataFilename); |
17 web_data_service_ = new WebDataService(); | 46 |
| 47 // TODO (caitkp): Rework the callbacks here. They're ugly. |
| 48 |
| 49 web_database_ = new WebDatabaseService(path); |
| 50 web_database_->LoadDatabase(WebDatabaseService::InitCallback()); |
| 51 |
| 52 web_data_service_ = new WebDataService( |
| 53 web_database_, base::Bind(&ProfileErrorCallback)); |
18 web_data_service_->Init(path); | 54 web_data_service_->Init(path); |
| 55 |
| 56 autofill_web_data_ = new AutofillWebDataServiceImpl( |
| 57 web_database_, base::Bind(&ProfileErrorCallback)); |
| 58 autofill_web_data_->Init(path); |
| 59 |
| 60 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, |
| 61 base::Bind(&InitSyncableServicesOnDBThread, |
| 62 web_data_service_)); |
19 } | 63 } |
20 | 64 |
21 WebDataServiceWrapper::~WebDataServiceWrapper() { | 65 WebDataServiceWrapper::~WebDataServiceWrapper() { |
22 } | 66 } |
23 | 67 |
24 void WebDataServiceWrapper::Shutdown() { | 68 void WebDataServiceWrapper::Shutdown() { |
25 web_data_service_->ShutdownOnUIThread(); | 69 web_data_service_->ShutdownOnUIThread(); |
| 70 //autofill_web_data_->ShutdownOnUIThread(); |
| 71 web_database_->ShutdownDatabase(); |
26 web_data_service_ = NULL; | 72 web_data_service_ = NULL; |
27 } | 73 } |
28 | 74 |
29 scoped_refptr<WebDataService> WebDataServiceWrapper::GetWebData() { | 75 scoped_refptr<WebDataService> WebDataServiceWrapper::GetWebData() { |
30 return web_data_service_.get(); | 76 return web_data_service_.get(); |
31 } | 77 } |
32 | 78 |
| 79 scoped_refptr<AutofillWebDataService> |
| 80 WebDataServiceWrapper::GetAutofillWebData() { |
| 81 return autofill_web_data_.get(); |
| 82 } |
| 83 |
33 // static | 84 // static |
34 scoped_refptr<AutofillWebDataService> | 85 scoped_refptr<AutofillWebDataService> |
35 AutofillWebDataService::FromBrowserContext(content::BrowserContext* context) { | 86 AutofillWebDataService::FromBrowserContext(content::BrowserContext* context) { |
36 // For this service, the implicit/explicit distinction doesn't | 87 // For this service, the implicit/explicit distinction doesn't |
37 // really matter; it's just used for a DCHECK. So we currently | 88 // really matter; it's just used for a DCHECK. So we currently |
38 // cheat and always say EXPLICIT_ACCESS. | 89 // cheat and always say EXPLICIT_ACCESS. |
39 scoped_refptr<WebDataService> service = WebDataServiceFactory::GetForProfile( | 90 WebDataServiceWrapper* wrapper = |
40 static_cast<Profile*>(context), Profile::EXPLICIT_ACCESS); | 91 WebDataServiceFactory::GetWrapper( |
41 | 92 static_cast<Profile*>(context), Profile::EXPLICIT_ACCESS); |
42 if (service.get()) { | 93 if (wrapper) |
43 return scoped_refptr<AutofillWebDataService>( | 94 return wrapper->GetAutofillWebData(); |
44 new AutofillWebDataServiceImpl(service)); | 95 // |wrapper| can be NULL in Incognito mode. |
45 } else { | 96 return scoped_refptr<AutofillWebDataService>(NULL);; |
46 return scoped_refptr<AutofillWebDataService>(NULL); | |
47 } | |
48 } | 97 } |
49 | 98 |
50 // static | 99 // static |
51 scoped_refptr<WebDataService> WebDataService::FromBrowserContext( | 100 scoped_refptr<WebDataService> WebDataService::FromBrowserContext( |
52 content::BrowserContext* context) { | 101 content::BrowserContext* context) { |
53 // For this service, the implicit/explicit distinction doesn't | 102 // For this service, the implicit/explicit distinction doesn't |
54 // really matter; it's just used for a DCHECK. So we currently | 103 // really matter; it's just used for a DCHECK. So we currently |
55 // cheat and always say EXPLICIT_ACCESS. | 104 // cheat and always say EXPLICIT_ACCESS. |
56 return WebDataServiceFactory::GetForProfile( | 105 return WebDataServiceFactory::GetForProfile( |
57 static_cast<Profile*>(context), Profile::EXPLICIT_ACCESS); | 106 static_cast<Profile*>(context), Profile::EXPLICIT_ACCESS); |
58 } | 107 } |
59 | 108 |
60 WebDataServiceFactory::WebDataServiceFactory() | 109 WebDataServiceFactory::WebDataServiceFactory() |
61 : ProfileKeyedServiceFactory( | 110 : ProfileKeyedServiceFactory( |
62 "WebDataService", | 111 "WebDataService", |
63 ProfileDependencyManager::GetInstance()) { | 112 ProfileDependencyManager::GetInstance()) { |
64 // WebDataServiceFactory has no dependecies. | 113 // WebDataServiceFactory has no dependecies. |
65 } | 114 } |
66 | 115 |
67 WebDataServiceFactory::~WebDataServiceFactory() {} | 116 WebDataServiceFactory::~WebDataServiceFactory() {} |
68 | 117 |
69 // static | 118 // static |
| 119 WebDataServiceWrapper* WebDataServiceFactory::GetWrapper( |
| 120 Profile* profile, Profile::ServiceAccessType access_type) { |
| 121 // If |access_type| starts being used for anything other than this |
| 122 // DCHECK, we need to start taking it as a parameter to |
| 123 // AutofillWebDataServiceImpl::FromBrowserContext (see above). |
| 124 DCHECK(access_type != Profile::IMPLICIT_ACCESS || !profile->IsOffTheRecord()); |
| 125 return static_cast<WebDataServiceWrapper*>( |
| 126 GetInstance()->GetServiceForProfile(profile, true)); |
| 127 } |
| 128 |
| 129 // static |
70 scoped_refptr<WebDataService> WebDataServiceFactory::GetForProfile( | 130 scoped_refptr<WebDataService> WebDataServiceFactory::GetForProfile( |
71 Profile* profile, Profile::ServiceAccessType access_type) { | 131 Profile* profile, Profile::ServiceAccessType access_type) { |
72 // If |access_type| starts being used for anything other than this | 132 // If |access_type| starts being used for anything other than this |
73 // DCHECK, we need to start taking it as a parameter to | 133 // DCHECK, we need to start taking it as a parameter to |
74 // AutofillWebDataServiceImpl::FromBrowserContext (see above). | 134 // AutofillWebDataServiceImpl::FromBrowserContext (see above). |
75 DCHECK(access_type != Profile::IMPLICIT_ACCESS || !profile->IsOffTheRecord()); | 135 DCHECK(access_type != Profile::IMPLICIT_ACCESS || !profile->IsOffTheRecord()); |
76 WebDataServiceWrapper* wrapper = | 136 WebDataServiceWrapper* wrapper = |
77 static_cast<WebDataServiceWrapper*>( | 137 static_cast<WebDataServiceWrapper*>( |
78 GetInstance()->GetServiceForProfile(profile, true)); | 138 GetInstance()->GetServiceForProfile(profile, true)); |
79 if (wrapper) | 139 if (wrapper) |
(...skipping 28 matching lines...) Expand all Loading... |
108 } | 168 } |
109 | 169 |
110 ProfileKeyedService* | 170 ProfileKeyedService* |
111 WebDataServiceFactory::BuildServiceInstanceFor(Profile* profile) const { | 171 WebDataServiceFactory::BuildServiceInstanceFor(Profile* profile) const { |
112 return new WebDataServiceWrapper(profile); | 172 return new WebDataServiceWrapper(profile); |
113 } | 173 } |
114 | 174 |
115 bool WebDataServiceFactory::ServiceIsNULLWhileTesting() const { | 175 bool WebDataServiceFactory::ServiceIsNULLWhileTesting() const { |
116 return true; | 176 return true; |
117 } | 177 } |
OLD | NEW |