1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/webdata/web_data_service_factory.h"
#include "base/bind.h"
#include "base/files/file_path.h"
#include "base/memory/singleton.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/incognito_helpers.h"
#include "chrome/browser/sync/glue/sync_start_util.h"
#include "chrome/browser/ui/profile_error_dialog.h"
#include "chrome/grit/chromium_strings.h"
#include "chrome/grit/generated_resources.h"
#include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/search_engines/keyword_web_data_service.h"
#include "components/signin/core/browser/webdata/token_web_data.h"
#include "components/webdata_services/web_data_service_wrapper.h"
#include "content/public/browser/browser_thread.h"
#if defined(OS_WIN)
#include "components/password_manager/core/browser/webdata/password_web_data_service_win.h"
#endif
using content::BrowserThread;
namespace {
// Converts a WebDataServiceWrapper::ErrorType to ProfileErrorType.
ProfileErrorType ProfileErrorFromWebDataServiceWrapperError(
WebDataServiceWrapper::ErrorType error_type) {
switch (error_type) {
case WebDataServiceWrapper::ERROR_LOADING_AUTOFILL:
return PROFILE_ERROR_DB_AUTOFILL_WEB_DATA;
case WebDataServiceWrapper::ERROR_LOADING_KEYWORD:
return PROFILE_ERROR_DB_KEYWORD_WEB_DATA;
case WebDataServiceWrapper::ERROR_LOADING_TOKEN:
return PROFILE_ERROR_DB_TOKEN_WEB_DATA;
case WebDataServiceWrapper::ERROR_LOADING_PASSWORD:
return PROFILE_ERROR_DB_WEB_DATA;
default:
NOTREACHED()
<< "Unknown WebDataServiceWrapper::ErrorType: " << error_type;
return PROFILE_ERROR_DB_WEB_DATA;
}
}
// Callback to show error dialog on profile load error.
void ProfileErrorCallback(WebDataServiceWrapper::ErrorType error_type,
sql::InitStatus status) {
ShowProfileErrorDialog(
ProfileErrorFromWebDataServiceWrapperError(error_type),
(status == sql::INIT_FAILURE) ?
IDS_COULDNT_OPEN_PROFILE_ERROR : IDS_PROFILE_TOO_NEW_ERROR);
}
} // namespace
WebDataServiceFactory::WebDataServiceFactory()
: BrowserContextKeyedServiceFactory(
"WebDataService",
BrowserContextDependencyManager::GetInstance()) {
// WebDataServiceFactory has no dependecies.
}
WebDataServiceFactory::~WebDataServiceFactory() {
}
// static
WebDataServiceWrapper* WebDataServiceFactory::GetForProfile(
Profile* profile,
Profile::ServiceAccessType access_type) {
// If |access_type| starts being used for anything other than this
// DCHECK, we need to start taking it as a parameter to
// the *WebDataService::FromBrowserContext() functions (see above).
DCHECK(access_type != Profile::IMPLICIT_ACCESS || !profile->IsOffTheRecord());
return static_cast<WebDataServiceWrapper*>(
GetInstance()->GetServiceForBrowserContext(profile, true));
}
// static
WebDataServiceWrapper* WebDataServiceFactory::GetForProfileIfExists(
Profile* profile,
Profile::ServiceAccessType access_type) {
// If |access_type| starts being used for anything other than this
// DCHECK, we need to start taking it as a parameter to
// the *WebDataService::FromBrowserContext() functions (see above).
DCHECK(access_type != Profile::IMPLICIT_ACCESS || !profile->IsOffTheRecord());
return static_cast<WebDataServiceWrapper*>(
GetInstance()->GetServiceForBrowserContext(profile, false));
}
// static
scoped_refptr<autofill::AutofillWebDataService>
WebDataServiceFactory::GetAutofillWebDataForProfile(
Profile* profile,
Profile::ServiceAccessType access_type) {
WebDataServiceWrapper* wrapper =
WebDataServiceFactory::GetForProfile(profile, access_type);
// |wrapper| can be null in Incognito mode.
return wrapper ?
wrapper->GetAutofillWebData() :
scoped_refptr<autofill::AutofillWebDataService>(nullptr);
}
// static
scoped_refptr<KeywordWebDataService>
WebDataServiceFactory::GetKeywordWebDataForProfile(
Profile* profile,
Profile::ServiceAccessType access_type) {
WebDataServiceWrapper* wrapper =
WebDataServiceFactory::GetForProfile(profile, access_type);
// |wrapper| can be null in Incognito mode.
return wrapper ?
wrapper->GetKeywordWebData() :
scoped_refptr<KeywordWebDataService>(nullptr);
}
// static
scoped_refptr<TokenWebData> WebDataServiceFactory::GetTokenWebDataForProfile(
Profile* profile,
Profile::ServiceAccessType access_type) {
WebDataServiceWrapper* wrapper =
WebDataServiceFactory::GetForProfile(profile, access_type);
// |wrapper| can be null in Incognito mode.
return wrapper ?
wrapper->GetTokenWebData() : scoped_refptr<TokenWebData>(nullptr);
}
#if defined(OS_WIN)
// static
scoped_refptr<PasswordWebDataService>
WebDataServiceFactory::GetPasswordWebDataForProfile(
Profile* profile,
Profile::ServiceAccessType access_type) {
WebDataServiceWrapper* wrapper =
WebDataServiceFactory::GetForProfile(profile, access_type);
// |wrapper| can be null in Incognito mode.
return wrapper ?
wrapper->GetPasswordWebData() :
scoped_refptr<PasswordWebDataService>(nullptr);
}
#endif
// static
WebDataServiceFactory* WebDataServiceFactory::GetInstance() {
return Singleton<WebDataServiceFactory>::get();
}
content::BrowserContext* WebDataServiceFactory::GetBrowserContextToUse(
content::BrowserContext* context) const {
return chrome::GetBrowserContextRedirectedInIncognito(context);
}
KeyedService* WebDataServiceFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const {
const base::FilePath& profile_path = context->GetPath();
return new WebDataServiceWrapper(
profile_path, g_browser_process->GetApplicationLocale(),
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB),
sync_start_util::GetFlareForSyncableService(profile_path),
&ProfileErrorCallback);
}
bool WebDataServiceFactory::ServiceIsNULLWhileTesting() const {
return true;
}
|