summaryrefslogtreecommitdiffstats
path: root/chrome/browser/webdata
diff options
context:
space:
mode:
Diffstat (limited to 'chrome/browser/webdata')
-rw-r--r--chrome/browser/webdata/web_data_service.cc41
-rw-r--r--chrome/browser/webdata/web_data_service.h14
-rw-r--r--chrome/browser/webdata/web_data_service_factory.cc61
-rw-r--r--chrome/browser/webdata/web_data_service_factory.h44
-rw-r--r--chrome/browser/webdata/web_data_service_unittest.cc8
5 files changed, 144 insertions, 24 deletions
diff --git a/chrome/browser/webdata/web_data_service.cc b/chrome/browser/webdata/web_data_service.cc
index 6cf2631..ee99257 100644
--- a/chrome/browser/webdata/web_data_service.cc
+++ b/chrome/browser/webdata/web_data_service.cc
@@ -28,6 +28,9 @@
#include "chrome/browser/webdata/web_intents_table.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_notification_types.h"
+#ifdef DEBUG
+#include "content/public/browser/browser_thread.h"
+#endif
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"
@@ -78,14 +81,19 @@ WDKeywordsResult::WDKeywordsResult()
WDKeywordsResult::~WDKeywordsResult() {}
WebDataService::WebDataService()
- : is_running_(false),
- db_(NULL),
- autocomplete_syncable_service_(NULL),
- autofill_profile_syncable_service_(NULL),
- failed_init_(false),
- should_commit_(false),
- next_request_handle_(1),
- main_loop_(MessageLoop::current()) {
+ : RefcountedProfileKeyedService(BrowserThread::UI),
+ is_running_(false),
+ db_(NULL),
+ autocomplete_syncable_service_(NULL),
+ autofill_profile_syncable_service_(NULL),
+ failed_init_(false),
+ should_commit_(false),
+ next_request_handle_(1),
+ main_loop_(MessageLoop::current()) {
+ // WebDataService requires DB thread if instantiated.
+ // Set WebDataServiceFactory::GetInstance()->SetTestingFactory(&profile, NULL)
+ // if you do not want to instantiate WebDataService in your test.
+ DCHECK(BrowserThread::IsWellKnownThread(BrowserThread::DB));
}
// static
@@ -102,18 +110,18 @@ void WebDataService::NotifyOfMultipleAutofillChanges(
make_scoped_refptr(web_data_service)));
}
+void WebDataService::ShutdownOnUIThread() {
+ ScheduleTask(FROM_HERE,
+ Bind(&WebDataService::ShutdownSyncableServices, this));
+ UnloadDatabase();
+}
+
bool WebDataService::Init(const FilePath& profile_path) {
FilePath path = profile_path;
path = path.Append(chrome::kWebDataFilename);
return InitWithPath(path);
}
-void WebDataService::Shutdown() {
- ScheduleTask(FROM_HERE,
- Bind(&WebDataService::ShutdownSyncableServices, this));
- UnloadDatabase();
-}
-
bool WebDataService::IsRunning() const {
return is_running_;
}
@@ -554,6 +562,7 @@ void WebDataService::RemoveAutofillProfilesAndCreditCardsModifiedBetween(
WebDataService::~WebDataService() {
if (is_running_ && db_) {
DLOG_ASSERT("WebDataService dtor called without Shutdown");
+ NOTREACHED();
}
}
@@ -565,7 +574,9 @@ bool WebDataService::InitWithPath(const FilePath& path) {
// [ http://crbug.com/100745 ], call |AutofillCountry::ApplicationLocale()| to
// cache the application locale before we try to access it on the DB thread.
// This should be safe to remove once [ http://crbug.com/100845 ] is fixed.
- AutofillCountry::ApplicationLocale();
+ // Do not do it if the thread is not UI (can happen only in some tests).
+ if (BrowserThread::CurrentlyOn(BrowserThread::UI))
+ AutofillCountry::ApplicationLocale();
ScheduleTask(FROM_HERE,
Bind(&WebDataService::InitializeDatabaseIfNecessary, this));
diff --git a/chrome/browser/webdata/web_data_service.h b/chrome/browser/webdata/web_data_service.h
index aea638a..49d327c 100644
--- a/chrome/browser/webdata/web_data_service.h
+++ b/chrome/browser/webdata/web_data_service.h
@@ -20,6 +20,7 @@
#include "base/message_loop_helpers.h"
#include "base/memory/ref_counted.h"
#include "base/synchronization/lock.h"
+#include "chrome/browser/profiles/refcounted_profile_keyed_service.h"
#include "chrome/browser/search_engines/template_url.h"
#include "chrome/browser/search_engines/template_url_id.h"
#include "chrome/browser/webdata/keyword_table.h"
@@ -184,9 +185,7 @@ template <class T> class WDObjectResult : public WDTypedResult {
class WebDataServiceConsumer;
-class WebDataService
- : public base::RefCountedThreadSafe<
- WebDataService, content::BrowserThread::DeleteOnUIThread> {
+class WebDataService : public RefcountedProfileKeyedService {
public:
// All requests return an opaque handle of the following type.
typedef int Handle;
@@ -300,14 +299,15 @@ class WebDataService
// |web_data_service| may be NULL for testing purposes.
static void NotifyOfMultipleAutofillChanges(WebDataService* web_data_service);
+ // RefcountedProfileKeyedService override:
+ // Shutdown the web data service. The service can no longer be used after this
+ // call.
+ virtual void ShutdownOnUIThread() OVERRIDE;
+
// Initializes the web data service. Returns false on failure
// Takes the path of the profile directory as its argument.
bool Init(const FilePath& profile_path);
- // Shutdown the web data service. The service can no longer be used after this
- // call.
- void Shutdown();
-
// Returns false if Shutdown() has been called.
bool IsRunning() const;
diff --git a/chrome/browser/webdata/web_data_service_factory.cc b/chrome/browser/webdata/web_data_service_factory.cc
new file mode 100644
index 0000000..d86b80d
--- /dev/null
+++ b/chrome/browser/webdata/web_data_service_factory.cc
@@ -0,0 +1,61 @@
+// 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/file_path.h"
+#include "chrome/browser/profiles/profile_dependency_manager.h"
+#include "chrome/browser/webdata/web_data_service.h"
+#include "chrome/common/chrome_constants.h"
+
+WebDataServiceFactory::WebDataServiceFactory()
+ : RefcountedProfileKeyedServiceFactory(
+ "WebDataService",
+ ProfileDependencyManager::GetInstance()) {
+ // WebDataServiceFactory has no dependecies.
+}
+
+WebDataServiceFactory::~WebDataServiceFactory() {}
+
+// static
+scoped_refptr<WebDataService> WebDataServiceFactory::GetForProfile(
+ Profile* profile, Profile::ServiceAccessType access_type) {
+ DCHECK(access_type != Profile::IMPLICIT_ACCESS || !profile->IsOffTheRecord());
+ return static_cast<WebDataService*>(
+ GetInstance()->GetServiceForProfile(profile, true).get());
+}
+
+// static
+scoped_refptr<WebDataService> WebDataServiceFactory::GetForProfileIfExists(
+ Profile* profile, Profile::ServiceAccessType access_type) {
+ DCHECK(access_type != Profile::IMPLICIT_ACCESS || !profile->IsOffTheRecord());
+ return static_cast<WebDataService*>(
+ GetInstance()->GetServiceForProfile(profile, false).get());
+}
+
+// static
+WebDataServiceFactory* WebDataServiceFactory::GetInstance() {
+ return Singleton<WebDataServiceFactory>::get();
+}
+
+bool WebDataServiceFactory::ServiceRedirectedInIncognito() {
+ return false;
+}
+
+scoped_refptr<RefcountedProfileKeyedService>
+WebDataServiceFactory::BuildServiceInstanceFor(Profile* profile) const {
+ DCHECK(profile);
+
+ FilePath path = profile->GetPath();
+ path = path.Append(chrome::kWebDataFilename);
+
+ scoped_refptr<WebDataService> wds(new WebDataService());
+ if (!wds->Init(profile->GetPath()))
+ NOTREACHED();
+ return wds.get();
+}
+
+bool WebDataServiceFactory::ServiceIsNULLWhileTesting() {
+ return true;
+}
diff --git a/chrome/browser/webdata/web_data_service_factory.h b/chrome/browser/webdata/web_data_service_factory.h
new file mode 100644
index 0000000..78c179b
--- /dev/null
+++ b/chrome/browser/webdata/web_data_service_factory.h
@@ -0,0 +1,44 @@
+// 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.
+
+#ifndef CHROME_BROWSER_WEBDATA_WEB_DATA_SERVICE_FACTORY_H__
+#define CHROME_BROWSER_WEBDATA_WEB_DATA_SERVICE_FACTORY_H__
+#pragma once
+
+#include "base/basictypes.h"
+#include "base/memory/singleton.h"
+#include "chrome/browser/profiles/profile.h"
+#include "chrome/browser/profiles/refcounted_profile_keyed_service_factory.h"
+
+class WebDataService;
+
+class WebDataServiceFactory : public RefcountedProfileKeyedServiceFactory {
+ public:
+ // Returns the |WebDataService| associated with the |profile|.
+ // |access_type| is either EXPLICIT_ACCESS or IMPLICIT_ACCESS
+ // (see its definition).
+ static scoped_refptr<WebDataService> GetForProfile(
+ Profile* profile, Profile::ServiceAccessType access_type);
+
+ // Similar to GetForProfile(), but won't create the web data service if it
+ // doesn't already exist.
+ static scoped_refptr<WebDataService> GetForProfileIfExists(
+ Profile* profile, Profile::ServiceAccessType access_type);
+
+ static WebDataServiceFactory* GetInstance();
+
+ private:
+ friend struct DefaultSingletonTraits<WebDataServiceFactory>;
+
+ WebDataServiceFactory();
+ virtual ~WebDataServiceFactory();
+
+ // |ProfileKeyedBaseFactory| methods:
+ virtual bool ServiceRedirectedInIncognito() OVERRIDE;
+ virtual scoped_refptr<RefcountedProfileKeyedService> BuildServiceInstanceFor(
+ Profile* profile) const OVERRIDE;
+ virtual bool ServiceIsNULLWhileTesting() OVERRIDE;
+};
+
+#endif // CHROME_BROWSER_WEBDATA_WEB_DATA_SERVICE_FACTORY_H__
diff --git a/chrome/browser/webdata/web_data_service_unittest.cc b/chrome/browser/webdata/web_data_service_unittest.cc
index 4cb1927..1023bb1 100644
--- a/chrome/browser/webdata/web_data_service_unittest.cc
+++ b/chrome/browser/webdata/web_data_service_unittest.cc
@@ -88,8 +88,12 @@ class WebDataServiceTest : public testing::Test {
}
virtual void TearDown() {
- if (wds_.get())
- wds_->Shutdown();
+ wds_->ShutdownOnUIThread();
+ wds_ = NULL;
+ base::WaitableEvent done(false, false);
+ BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
+ base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done)));
+ done.Wait();
db_thread_.Stop();
MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());