diff options
author | pkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-23 21:23:53 +0000 |
---|---|---|
committer | pkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-23 21:23:53 +0000 |
commit | 9cddbe374f0d3d702532803bf4b2a15e76a918e4 (patch) | |
tree | d5bf36e3456fb7471be1a8b51ca30a66c1c68d08 /chrome | |
parent | f704c5b267c4eaca90408f485c5d921174300867 (diff) | |
download | chromium_src-9cddbe374f0d3d702532803bf4b2a15e76a918e4.zip chromium_src-9cddbe374f0d3d702532803bf4b2a15e76a918e4.tar.gz chromium_src-9cddbe374f0d3d702532803bf4b2a15e76a918e4.tar.bz2 |
Add functions to purge the Web Database memory and get the service without creating it, for the memory purger.
BUG=23400
TEST=none
Review URL: http://codereview.chromium.org/326014
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@29945 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r-- | chrome/browser/automation/automation_profile_impl.h | 3 | ||||
-rw-r--r-- | chrome/browser/profile.cc | 8 | ||||
-rw-r--r-- | chrome/browser/profile.h | 5 | ||||
-rw-r--r-- | chrome/browser/webdata/web_data_service.cc | 50 | ||||
-rw-r--r-- | chrome/browser/webdata/web_data_service.h | 17 | ||||
-rw-r--r-- | chrome/test/testing_profile.h | 3 |
6 files changed, 73 insertions, 13 deletions
diff --git a/chrome/browser/automation/automation_profile_impl.h b/chrome/browser/automation/automation_profile_impl.h index c893aa1..b549aca 100644 --- a/chrome/browser/automation/automation_profile_impl.h +++ b/chrome/browser/automation/automation_profile_impl.h @@ -87,6 +87,9 @@ class AutomationProfileImpl : public Profile { virtual WebDataService* GetWebDataService(ServiceAccessType access) { return original_profile_->GetWebDataService(access); } + virtual WebDataService* GetWebDataServiceWithoutCreating() { + return original_profile_->GetWebDataServiceWithoutCreating(); + } virtual PasswordStore* GetPasswordStore(ServiceAccessType access) { return original_profile_->GetPasswordStore(access); } diff --git a/chrome/browser/profile.cc b/chrome/browser/profile.cc index 2f6b153..b8af2e9 100644 --- a/chrome/browser/profile.cc +++ b/chrome/browser/profile.cc @@ -310,6 +310,10 @@ class OffTheRecordProfileImpl : public Profile, } } + virtual WebDataService* GetWebDataServiceWithoutCreating() { + return profile_->GetWebDataServiceWithoutCreating(); + } + virtual PasswordStore* GetPasswordStore(ServiceAccessType sat) { if (sat == EXPLICIT_ACCESS) { return profile_->GetPasswordStore(sat); @@ -1009,6 +1013,10 @@ WebDataService* ProfileImpl::GetWebDataService(ServiceAccessType sat) { return web_data_service_.get(); } +WebDataService* ProfileImpl::GetWebDataServiceWithoutCreating() { + return web_data_service_.get(); +} + void ProfileImpl::CreateWebDataService() { DCHECK(!created_web_data_service_ && web_data_service_.get() == NULL); created_web_data_service_ = true; diff --git a/chrome/browser/profile.h b/chrome/browser/profile.h index d190989..741b5cf 100644 --- a/chrome/browser/profile.h +++ b/chrome/browser/profile.h @@ -209,6 +209,10 @@ class Profile { // the ServiceAccessType definition above. virtual WebDataService* GetWebDataService(ServiceAccessType access) = 0; + // Similar to GetWebDataService(), but won't create the web data service if it + // doesn't already exist. + virtual WebDataService* GetWebDataServiceWithoutCreating() = 0; + // Returns the PasswordStore for this profile. This is owned by the Profile. virtual PasswordStore* GetPasswordStore(ServiceAccessType access) = 0; @@ -402,6 +406,7 @@ class ProfileImpl : public Profile, virtual HistoryService* GetHistoryService(ServiceAccessType sat); virtual HistoryService* GetHistoryServiceWithoutCreating(); virtual WebDataService* GetWebDataService(ServiceAccessType sat); + virtual WebDataService* GetWebDataServiceWithoutCreating(); virtual PasswordStore* GetPasswordStore(ServiceAccessType sat); virtual PrefService* GetPrefs(); virtual TemplateURLModel* GetTemplateURLModel(); diff --git a/chrome/browser/webdata/web_data_service.cc b/chrome/browser/webdata/web_data_service.cc index d1cc7f8..fe00c5d 100644 --- a/chrome/browser/webdata/web_data_service.cc +++ b/chrome/browser/webdata/web_data_service.cc @@ -21,6 +21,7 @@ using webkit_glue::PasswordForm; WebDataService::WebDataService() : thread_(NULL), db_(NULL), + failed_init_(false), should_commit_(false), next_request_handle_(1) { } @@ -38,8 +39,9 @@ bool WebDataService::Init(const FilePath& profile_path) { } bool WebDataService::InitWithPath(const FilePath& path) { - thread_ = new base::Thread("Chrome_WebDataThread"); + path_ = path; + thread_ = new base::Thread("Chrome_WebDataThread"); if (!thread_->Start()) { delete thread_; thread_ = NULL; @@ -47,8 +49,7 @@ bool WebDataService::InitWithPath(const FilePath& path) { } ScheduleTask(NewRunnableMethod(this, - &WebDataService::InitializeDatabase, - path)); + &WebDataService::InitializeDatabaseIfNecessary)); return true; } @@ -82,6 +83,10 @@ bool WebDataService::IsRunning() const { return thread_ != NULL; } +void WebDataService::UnloadDatabase() { + ScheduleTask(NewRunnableMethod(this, &WebDataService::ShutdownDatabase)); +} + void WebDataService::ScheduleCommit() { if (should_commit_ == false) { should_commit_ = true; @@ -398,15 +403,18 @@ void WebDataService::Commit() { } } -void WebDataService::InitializeDatabase(const FilePath& path) { - DCHECK(!db_); +void WebDataService::InitializeDatabaseIfNecessary() { + if (db_ || failed_init_ || path_.empty()) + return; + // In the rare case where the db fails to initialize a dialog may get shown // the blocks the caller, yet allows other messages through. For this reason // we only set db_ to the created database if creation is successful. That // way other methods won't do anything as db_ is still NULL. WebDatabase* db = new WebDatabase(); - if (!db->Init(path)) { + if (!db->Init(path_)) { NOTREACHED() << "Cannot initialize the web database"; + failed_init_ = true; delete db; return; } @@ -416,6 +424,8 @@ void WebDataService::InitializeDatabase(const FilePath& path) { } void WebDataService::ShutdownDatabase() { + should_commit_ = false; + if (db_) { db_->CommitTransaction(); delete db_; @@ -427,6 +437,7 @@ void WebDataService::ShutdownDatabase() { // Keywords. // void WebDataService::AddKeywordImpl(GenericRequest<TemplateURL>* request) { + InitializeDatabaseIfNecessary(); if (db_ && !request->IsCancelled()) { db_->AddKeyword(request->GetArgument()); ScheduleCommit(); @@ -435,7 +446,8 @@ void WebDataService::AddKeywordImpl(GenericRequest<TemplateURL>* request) { } void WebDataService::RemoveKeywordImpl( - GenericRequest<TemplateURL::IDType>* request) { + GenericRequest<TemplateURL::IDType>* request) { + InitializeDatabaseIfNecessary(); if (db_ && !request->IsCancelled()) { DCHECK(request->GetArgument()); db_->RemoveKeyword(request->GetArgument()); @@ -444,8 +456,8 @@ void WebDataService::RemoveKeywordImpl( request->RequestComplete(); } -void WebDataService::UpdateKeywordImpl( - GenericRequest<TemplateURL>* request) { +void WebDataService::UpdateKeywordImpl(GenericRequest<TemplateURL>* request) { + InitializeDatabaseIfNecessary(); if (db_ && !request->IsCancelled()) { if (!db_->UpdateKeyword(request->GetArgument())) NOTREACHED(); @@ -455,6 +467,7 @@ void WebDataService::UpdateKeywordImpl( } void WebDataService::GetKeywordsImpl(WebDataRequest* request) { + InitializeDatabaseIfNecessary(); if (db_ && !request->IsCancelled()) { WDKeywordsResult result; db_->GetKeywords(&result.keywords); @@ -467,7 +480,8 @@ void WebDataService::GetKeywordsImpl(WebDataRequest* request) { } void WebDataService::SetDefaultSearchProviderImpl( - GenericRequest<TemplateURL::IDType>* request) { + GenericRequest<TemplateURL::IDType>* request) { + InitializeDatabaseIfNecessary(); if (db_ && !request->IsCancelled()) { if (!db_->SetDefaultSearchProviderID(request->GetArgument())) NOTREACHED(); @@ -478,6 +492,7 @@ void WebDataService::SetDefaultSearchProviderImpl( void WebDataService::SetBuiltinKeywordVersionImpl( GenericRequest<int>* request) { + InitializeDatabaseIfNecessary(); if (db_ && !request->IsCancelled()) { if (!db_->SetBuitinKeywordVersion(request->GetArgument())) NOTREACHED(); @@ -490,6 +505,7 @@ void WebDataService::SetBuiltinKeywordVersionImpl( // Password manager support. // void WebDataService::AddLoginImpl(GenericRequest<PasswordForm>* request) { + InitializeDatabaseIfNecessary(); if (db_ && !request->IsCancelled()) { if (db_->AddLogin(request->GetArgument())) ScheduleCommit(); @@ -498,6 +514,7 @@ void WebDataService::AddLoginImpl(GenericRequest<PasswordForm>* request) { } void WebDataService::UpdateLoginImpl(GenericRequest<PasswordForm>* request) { + InitializeDatabaseIfNecessary(); if (db_ && !request->IsCancelled()) { if (db_->UpdateLogin(request->GetArgument())) ScheduleCommit(); @@ -506,6 +523,7 @@ void WebDataService::UpdateLoginImpl(GenericRequest<PasswordForm>* request) { } void WebDataService::RemoveLoginImpl(GenericRequest<PasswordForm>* request) { + InitializeDatabaseIfNecessary(); if (db_ && !request->IsCancelled()) { if (db_->RemoveLogin(request->GetArgument())) ScheduleCommit(); @@ -515,6 +533,7 @@ void WebDataService::RemoveLoginImpl(GenericRequest<PasswordForm>* request) { void WebDataService::RemoveLoginsCreatedBetweenImpl( GenericRequest2<Time, Time>* request) { + InitializeDatabaseIfNecessary(); if (db_ && !request->IsCancelled()) { if (db_->RemoveLoginsCreatedBetween(request->GetArgument1(), request->GetArgument2())) @@ -524,6 +543,7 @@ void WebDataService::RemoveLoginsCreatedBetweenImpl( } void WebDataService::GetLoginsImpl(GenericRequest<PasswordForm>* request) { + InitializeDatabaseIfNecessary(); if (db_ && !request->IsCancelled()) { std::vector<PasswordForm*> forms; db_->GetLogins(request->GetArgument(), &forms); @@ -534,6 +554,7 @@ void WebDataService::GetLoginsImpl(GenericRequest<PasswordForm>* request) { } void WebDataService::GetAutofillableLoginsImpl(WebDataRequest* request) { + InitializeDatabaseIfNecessary(); if (db_ && !request->IsCancelled()) { std::vector<PasswordForm*> forms; db_->GetAllLogins(&forms, false); @@ -544,6 +565,7 @@ void WebDataService::GetAutofillableLoginsImpl(WebDataRequest* request) { } void WebDataService::GetBlacklistLoginsImpl(WebDataRequest* request) { + InitializeDatabaseIfNecessary(); if (db_ && !request->IsCancelled()) { std::vector<PasswordForm*> all_forms; db_->GetAllLogins(&all_forms, true); @@ -571,6 +593,7 @@ void WebDataService::GetBlacklistLoginsImpl(WebDataRequest* request) { void WebDataService::AddFormFieldValuesImpl( GenericRequest<std::vector<FormField> >* request) { + InitializeDatabaseIfNecessary(); if (db_ && !request->IsCancelled()) { if (db_->AddFormFieldValues(request->GetArgument())) ScheduleCommit(); @@ -580,6 +603,7 @@ void WebDataService::AddFormFieldValuesImpl( void WebDataService::GetFormValuesForElementNameImpl(WebDataRequest* request, const string16& name, const string16& prefix, int limit) { + InitializeDatabaseIfNecessary(); if (db_ && !request->IsCancelled()) { std::vector<string16> values; db_->GetFormValuesForElementName(name, prefix, &values, limit); @@ -592,6 +616,7 @@ void WebDataService::GetFormValuesForElementNameImpl(WebDataRequest* request, void WebDataService::RemoveFormElementsAddedBetweenImpl( GenericRequest2<Time, Time>* request) { + InitializeDatabaseIfNecessary(); if (db_ && !request->IsCancelled()) { if (db_->RemoveFormElementsAddedBetween(request->GetArgument1(), request->GetArgument2())) @@ -602,6 +627,7 @@ void WebDataService::RemoveFormElementsAddedBetweenImpl( void WebDataService::RemoveFormValueForElementNameImpl( GenericRequest2<string16, string16>* request) { + InitializeDatabaseIfNecessary(); if (db_ && !request->IsCancelled()) { if (db_->RemoveFormElement(request->GetArgument1(), request->GetArgument2())) @@ -618,6 +644,7 @@ void WebDataService::RemoveFormValueForElementNameImpl( void WebDataService::SetWebAppImageImpl( GenericRequest2<GURL, SkBitmap>* request) { + InitializeDatabaseIfNecessary(); if (db_ && !request->IsCancelled()) { db_->SetWebAppImage(request->GetArgument1(), request->GetArgument2()); ScheduleCommit(); @@ -627,6 +654,7 @@ void WebDataService::SetWebAppImageImpl( void WebDataService::SetWebAppHasAllImagesImpl( GenericRequest2<GURL, bool>* request) { + InitializeDatabaseIfNecessary(); if (db_ && !request->IsCancelled()) { db_->SetWebAppHasAllImages(request->GetArgument1(), request->GetArgument2()); @@ -636,6 +664,7 @@ void WebDataService::SetWebAppHasAllImagesImpl( } void WebDataService::RemoveWebAppImpl(GenericRequest<GURL>* request) { + InitializeDatabaseIfNecessary(); if (db_ && !request->IsCancelled()) { db_->RemoveWebApp(request->GetArgument()); ScheduleCommit(); @@ -644,6 +673,7 @@ void WebDataService::RemoveWebAppImpl(GenericRequest<GURL>* request) { } void WebDataService::GetWebAppImagesImpl(GenericRequest<GURL>* request) { + InitializeDatabaseIfNecessary(); if (db_ && !request->IsCancelled()) { WDAppImagesResult result; result.has_all_images = db_->GetWebAppHasAllImages(request->GetArgument()); diff --git a/chrome/browser/webdata/web_data_service.h b/chrome/browser/webdata/web_data_service.h index 82b0eb1..4409de8 100644 --- a/chrome/browser/webdata/web_data_service.h +++ b/chrome/browser/webdata/web_data_service.h @@ -8,6 +8,7 @@ #include <map> #include <vector> +#include "base/file_path.h" #include "base/lock.h" #include "base/ref_counted.h" #include "chrome/browser/search_engines/template_url.h" @@ -17,7 +18,6 @@ #if defined(OS_WIN) struct IE7PasswordInfo; #endif -class FilePath; class MessageLoop; class Task; class WebDatabase; @@ -160,6 +160,10 @@ class WebDataService : public base::RefCountedThreadSafe<WebDataService> { // Returns false if Shutdown() has been called. bool IsRunning() const; + // Unloads the database without actually shutting down the service. This can + // be used to temporarily reduce the browser process' memory footprint. + void UnloadDatabase(); + ////////////////////////////////////////////////////////////////////////////// // // Internal requests @@ -420,8 +424,8 @@ class WebDataService : public base::RefCountedThreadSafe<WebDataService> { typedef GenericRequest2<std::vector<const TemplateURL*>, std::vector<TemplateURL*> > SetKeywordsRequest; - // Initialize the database with the provided path. - void InitializeDatabase(const FilePath& path); + // Initialize the database, if it hasn't already been initialized. + void InitializeDatabaseIfNecessary(); // Commit any pending transaction and deletes the database. void ShutdownDatabase(); @@ -502,9 +506,16 @@ class WebDataService : public base::RefCountedThreadSafe<WebDataService> { // Our worker thread. All requests are processed from that thread. base::Thread* thread_; + // The path with which to initialize the database. + FilePath path_; + // Our database. WebDatabase* db_; + // Whether the database failed to initialize. We use this to avoid + // continually trying to reinit. + bool failed_init_; + // Whether we should commit the database. bool should_commit_; diff --git a/chrome/test/testing_profile.h b/chrome/test/testing_profile.h index 41756b2..2aa3971 100644 --- a/chrome/test/testing_profile.h +++ b/chrome/test/testing_profile.h @@ -101,6 +101,9 @@ class TestingProfile : public Profile { virtual WebDataService* GetWebDataService(ServiceAccessType access) { return NULL; } + virtual WebDataService* GetWebDataServiceWithoutCreating() { + return NULL; + } virtual PasswordStore* GetPasswordStore(ServiceAccessType access) { return NULL; } |