Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6740)

Unified Diff: chrome/browser/net/sqlite_persistent_cookie_store.cc

Issue 12342030: Revert 184868 (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/net/sqlite_persistent_cookie_store.cc
===================================================================
--- chrome/browser/net/sqlite_persistent_cookie_store.cc (revision 184872)
+++ chrome/browser/net/sqlite_persistent_cookie_store.cc (working copy)
@@ -14,17 +14,17 @@
#include "base/callback.h"
#include "base/file_util.h"
#include "base/files/file_path.h"
-#include "base/location.h"
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/metrics/histogram.h"
-#include "base/sequenced_task_runner.h"
#include "base/string_util.h"
#include "base/synchronization/lock.h"
+#include "base/threading/thread.h"
#include "base/time.h"
#include "chrome/browser/diagnostics/sqlite_diagnostics.h"
#include "chrome/browser/net/clear_on_exit_policy.h"
+#include "content/public/browser/browser_thread.h"
#include "googleurl/src/gurl.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "net/cookies/canonical_cookie.h"
@@ -35,38 +35,36 @@
#include "third_party/sqlite/sqlite3.h"
using base::Time;
+using content::BrowserThread;
-// This class is designed to be shared between any client thread and the
-// background task runner. It batches operations and commits them on a timer.
+// This class is designed to be shared between any calling threads and the
+// database thread. It batches operations and commits them on a timer.
//
// SQLitePersistentCookieStore::Load is called to load all cookies. It
// delegates to Backend::Load, which posts a Backend::LoadAndNotifyOnDBThread
-// task to the background runner. This task calls Backend::ChainLoadCookies(),
-// which repeatedly posts itself to the BG runner to load each eTLD+1's cookies
-// in separate tasks. When this is complete, Backend::CompleteLoadOnIOThread is
-// posted to the client runner, which notifies the caller of
+// task to the DB thread. This task calls Backend::ChainLoadCookies(), which
+// repeatedly posts itself to the DB thread to load each eTLD+1's cookies in
+// separate tasks. When this is complete, Backend::CompleteLoadOnIOThread is
+// posted to the IO thread, which notifies the caller of
// SQLitePersistentCookieStore::Load that the load is complete.
//
// If a priority load request is invoked via SQLitePersistentCookieStore::
// LoadCookiesForKey, it is delegated to Backend::LoadCookiesForKey, which posts
-// Backend::LoadKeyAndNotifyOnDBThread to the BG runner. That routine loads just
+// Backend::LoadKeyAndNotifyOnDBThread to the DB thread. That routine loads just
// that single domain key (eTLD+1)'s cookies, and posts a Backend::
-// CompleteLoadForKeyOnIOThread to the client runner to notify the caller of
+// CompleteLoadForKeyOnIOThread to the IO thread to notify the caller of
// SQLitePersistentCookieStore::LoadCookiesForKey that that load is complete.
//
// Subsequent to loading, mutations may be queued by any thread using
// AddCookie, UpdateCookieAccessTime, and DeleteCookie. These are flushed to
-// disk on the BG runner every 30 seconds, 512 operations, or call to Flush(),
+// disk on the DB thread every 30 seconds, 512 operations, or call to Flush(),
// whichever occurs first.
class SQLitePersistentCookieStore::Backend
: public base::RefCountedThreadSafe<SQLitePersistentCookieStore::Backend> {
public:
- Backend(
- const base::FilePath& path,
- const scoped_refptr<base::SequencedTaskRunner>& client_task_runner,
- const scoped_refptr<base::SequencedTaskRunner>& background_task_runner,
- bool restore_old_session_cookies,
- ClearOnExitPolicy* clear_on_exit_policy)
+ Backend(const base::FilePath& path,
+ bool restore_old_session_cookies,
+ ClearOnExitPolicy* clear_on_exit_policy)
: path_(path),
db_(NULL),
num_pending_(0),
@@ -76,8 +74,6 @@
restore_old_session_cookies_(restore_old_session_cookies),
clear_on_exit_policy_(clear_on_exit_policy),
num_cookies_read_(0),
- client_task_runner_(client_task_runner),
- background_task_runner_(background_task_runner),
num_priority_waiting_(0),
total_priority_requests_(0) {
}
@@ -161,14 +157,14 @@
};
private:
- // Creates or loads the SQLite database on background runner.
- void LoadAndNotifyInBackground(const LoadedCallback& loaded_callback,
- const base::Time& posted_at);
+ // Creates or loads the SQLite database on DB thread.
+ void LoadAndNotifyOnDBThread(const LoadedCallback& loaded_callback,
+ const base::Time& posted_at);
- // Loads cookies for the domain key (eTLD+1) on background runner.
- void LoadKeyAndNotifyInBackground(const std::string& domains,
- const LoadedCallback& loaded_callback,
- const base::Time& posted_at);
+ // Loads cookies for the domain key (eTLD+1) on DB thread.
+ void LoadKeyAndNotifyOnDBThread(const std::string& domains,
+ const LoadedCallback& loaded_callback,
+ const base::Time& posted_at);
// Notifies the CookieMonster when loading completes for a specific domain key
// or for all domain keys. Triggers the callback and passes it all cookies
@@ -178,28 +174,28 @@
// Sends notification when the entire store is loaded, and reports metrics
// for the total time to load and aggregated results from any priority loads
// that occurred.
- void CompleteLoadInForeground(const LoadedCallback& loaded_callback,
- bool load_success);
+ void CompleteLoadOnIOThread(const LoadedCallback& loaded_callback,
+ bool load_success);
// Sends notification when a single priority load completes. Updates priority
// load metric data. The data is sent only after the final load completes.
- void CompleteLoadForKeyInForeground(const LoadedCallback& loaded_callback,
- bool load_success);
+ void CompleteLoadForKeyOnIOThread(const LoadedCallback& loaded_callback,
+ bool load_success);
- // Sends all metrics, including posting a ReportMetricsInBackground task.
+ // Sends all metrics, including posting a ReportMetricsOnDBThread task.
// Called after all priority and regular loading is complete.
void ReportMetrics();
- // Sends background-runner owned metrics (i.e., the combined duration of all
- // BG-runner tasks).
- void ReportMetricsInBackground();
+ // Sends DB-thread owned metrics (i.e., the combined duration of all DB-thread
+ // tasks).
+ void ReportMetricsOnDBThread();
// Initialize the data base.
bool InitializeDatabase();
// Loads cookies for the next domain key from the DB, then either reschedules
- // itself or schedules the provided callback to run on the client runner (if
- // all domains are loaded).
+ // itself or schedules the provided callback to run on the IO thread (if all
+ // domains are loaded).
void ChainLoadCookies(const LoadedCallback& loaded_callback);
// Load all cookies for a set of domains/hosts
@@ -210,7 +206,7 @@
const net::CanonicalCookie& cc);
// Commit our pending operations to the database.
void Commit();
- // Close() executed on the background runner.
+ // Close() executed on the background thread.
void InternalBackgroundClose();
void DeleteSessionCookiesOnStartup();
@@ -220,11 +216,6 @@
void KillDatabase();
void ScheduleKillDatabase();
- void PostBackgroundTask(const tracked_objects::Location& origin,
- const base::Closure& task);
- void PostClientTask(const tracked_objects::Location& origin,
- const base::Closure& task);
-
base::FilePath path_;
scoped_ptr<sql::Connection> db_;
sql::MetaTable meta_table_;
@@ -238,7 +229,7 @@
base::Lock lock_;
// Temporary buffer for cookies loaded from DB. Accumulates cookies to reduce
- // the number of messages sent to the client runner. Sent back in response to
+ // the number of messages sent to the IO thread. Sent back in response to
// individual load requests for domain keys or when all loading completes.
std::vector<net::CanonicalCookie*> cookies_;
@@ -263,17 +254,14 @@
// Policy defining what data is deleted on shutdown.
scoped_refptr<ClearOnExitPolicy> clear_on_exit_policy_;
- // The cumulative time spent loading the cookies on the background runner.
- // Incremented and reported from the background runner.
+ // The cumulative time spent loading the cookies on the DB thread. Incremented
+ // and reported from the DB thread.
base::TimeDelta cookie_load_duration_;
- // The total number of cookies read. Incremented and reported on the
- // background runner.
+ // The total number of cookies read. Incremented and reported on the DB
+ // thread.
int num_cookies_read_;
- scoped_refptr<base::SequencedTaskRunner> client_task_runner_;
- scoped_refptr<base::SequencedTaskRunner> background_task_runner_;
-
// Guards the following metrics-related properties (only accessed when
// starting/completing priority loads or completing the total load).
base::Lock metrics_lock_;
@@ -386,9 +374,10 @@
const LoadedCallback& loaded_callback) {
// This function should be called only once per instance.
DCHECK(!db_.get());
- PostBackgroundTask(FROM_HERE, base::Bind(
- &Backend::LoadAndNotifyInBackground, this,
- loaded_callback, base::Time::Now()));
+ BrowserThread::PostTask(
+ BrowserThread::DB, FROM_HERE,
+ base::Bind(&Backend::LoadAndNotifyOnDBThread, this, loaded_callback,
+ base::Time::Now()));
}
void SQLitePersistentCookieStore::Backend::LoadCookiesForKey(
@@ -402,14 +391,17 @@
total_priority_requests_++;
}
- PostBackgroundTask(FROM_HERE, base::Bind(
- &Backend::LoadKeyAndNotifyInBackground,
- this, key, loaded_callback, base::Time::Now()));
+ BrowserThread::PostTask(
+ BrowserThread::DB, FROM_HERE,
+ base::Bind(&Backend::LoadKeyAndNotifyOnDBThread, this,
+ key,
+ loaded_callback,
+ base::Time::Now()));
}
-void SQLitePersistentCookieStore::Backend::LoadAndNotifyInBackground(
+void SQLitePersistentCookieStore::Backend::LoadAndNotifyOnDBThread(
const LoadedCallback& loaded_callback, const base::Time& posted_at) {
- DCHECK(background_task_runner_->RunsTasksOnCurrentThread());
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
IncrementTimeDelta increment(&cookie_load_duration_);
UMA_HISTOGRAM_CUSTOM_TIMES(
@@ -419,18 +411,20 @@
50);
if (!InitializeDatabase()) {
- PostClientTask(FROM_HERE, base::Bind(
- &Backend::CompleteLoadInForeground, this, loaded_callback, false));
+ BrowserThread::PostTask(
+ BrowserThread::IO, FROM_HERE,
+ base::Bind(&SQLitePersistentCookieStore::Backend::CompleteLoadOnIOThread,
+ this, loaded_callback, false));
} else {
ChainLoadCookies(loaded_callback);
}
}
-void SQLitePersistentCookieStore::Backend::LoadKeyAndNotifyInBackground(
+void SQLitePersistentCookieStore::Backend::LoadKeyAndNotifyOnDBThread(
const std::string& key,
const LoadedCallback& loaded_callback,
const base::Time& posted_at) {
- DCHECK(background_task_runner_->RunsTasksOnCurrentThread());
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
IncrementTimeDelta increment(&cookie_load_duration_);
UMA_HISTOGRAM_CUSTOM_TIMES(
@@ -451,16 +445,16 @@
}
}
- PostClientTask(FROM_HERE, base::Bind(
- &SQLitePersistentCookieStore::Backend::CompleteLoadForKeyInForeground,
- this, loaded_callback, success));
+ BrowserThread::PostTask(
+ BrowserThread::IO, FROM_HERE,
+ base::Bind(
+ &SQLitePersistentCookieStore::Backend::CompleteLoadForKeyOnIOThread,
+ this, loaded_callback, success));
}
-void SQLitePersistentCookieStore::Backend::CompleteLoadForKeyInForeground(
+void SQLitePersistentCookieStore::Backend::CompleteLoadForKeyOnIOThread(
const LoadedCallback& loaded_callback,
bool load_success) {
- DCHECK(client_task_runner_->RunsTasksOnCurrentThread());
-
Notify(loaded_callback, load_success);
{
@@ -474,7 +468,7 @@
}
-void SQLitePersistentCookieStore::Backend::ReportMetricsInBackground() {
+void SQLitePersistentCookieStore::Backend::ReportMetricsOnDBThread() {
UMA_HISTOGRAM_CUSTOM_TIMES(
"Cookie.TimeLoad",
cookie_load_duration_,
@@ -483,8 +477,8 @@
}
void SQLitePersistentCookieStore::Backend::ReportMetrics() {
- PostBackgroundTask(FROM_HERE, base::Bind(
- &SQLitePersistentCookieStore::Backend::ReportMetricsInBackground, this));
+ BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, base::Bind(
+ &SQLitePersistentCookieStore::Backend::ReportMetricsOnDBThread, this));
{
base::AutoLock locked(metrics_lock_);
@@ -504,7 +498,7 @@
}
}
-void SQLitePersistentCookieStore::Backend::CompleteLoadInForeground(
+void SQLitePersistentCookieStore::Backend::CompleteLoadOnIOThread(
const LoadedCallback& loaded_callback, bool load_success) {
Notify(loaded_callback, load_success);
@@ -515,7 +509,7 @@
void SQLitePersistentCookieStore::Backend::Notify(
const LoadedCallback& loaded_callback,
bool load_success) {
- DCHECK(client_task_runner_->RunsTasksOnCurrentThread());
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
std::vector<net::CanonicalCookie*> cookies;
{
@@ -527,7 +521,7 @@
}
bool SQLitePersistentCookieStore::Backend::InitializeDatabase() {
- DCHECK(background_task_runner_->RunsTasksOnCurrentThread());
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
if (initialized_ || corruption_detected_) {
// Return false if we were previously initialized but the DB has since been
@@ -614,7 +608,7 @@
void SQLitePersistentCookieStore::Backend::ChainLoadCookies(
const LoadedCallback& loaded_callback) {
- DCHECK(background_task_runner_->RunsTasksOnCurrentThread());
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
IncrementTimeDelta increment(&cookie_load_duration_);
bool load_success = true;
@@ -631,15 +625,17 @@
}
// If load is successful and there are more domain keys to be loaded,
- // then post a background task to continue chain-load;
- // Otherwise notify on client runner.
+ // then post a DB task to continue chain-load;
+ // Otherwise notify on IO thread.
if (load_success && keys_to_load_.size() > 0) {
- PostBackgroundTask(FROM_HERE, base::Bind(
- &Backend::ChainLoadCookies, this, loaded_callback));
+ BrowserThread::PostTask(
+ BrowserThread::DB, FROM_HERE,
+ base::Bind(&Backend::ChainLoadCookies, this, loaded_callback));
} else {
- PostClientTask(FROM_HERE, base::Bind(
- &Backend::CompleteLoadInForeground, this,
- loaded_callback, load_success));
+ BrowserThread::PostTask(
+ BrowserThread::IO, FROM_HERE,
+ base::Bind(&SQLitePersistentCookieStore::Backend::CompleteLoadOnIOThread,
+ this, loaded_callback, load_success));
if (load_success && !restore_old_session_cookies_)
DeleteSessionCookiesOnStartup();
}
@@ -647,7 +643,7 @@
bool SQLitePersistentCookieStore::Backend::LoadCookiesForDomains(
const std::set<std::string>& domains) {
- DCHECK(background_task_runner_->RunsTasksOnCurrentThread());
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
sql::Statement smt;
if (restore_old_session_cookies_) {
@@ -834,7 +830,7 @@
static const int kCommitIntervalMs = 30 * 1000;
// Commit right away if we have more than 512 outstanding operations.
static const size_t kCommitAfterBatchSize = 512;
- DCHECK(!background_task_runner_->RunsTasksOnCurrentThread());
+ DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::DB));
// We do a full copy of the cookie here, and hopefully just here.
scoped_ptr<PendingOperation> po(new PendingOperation(op, cc));
@@ -848,19 +844,20 @@
if (num_pending == 1) {
// We've gotten our first entry for this batch, fire off the timer.
- if (!background_task_runner_->PostDelayedTask(
- FROM_HERE, base::Bind(&Backend::Commit, this),
- base::TimeDelta::FromMilliseconds(kCommitIntervalMs))) {
- NOTREACHED() << "background_task_runner_ is not running.";
- }
+ BrowserThread::PostDelayedTask(
+ BrowserThread::DB, FROM_HERE,
+ base::Bind(&Backend::Commit, this),
+ base::TimeDelta::FromMilliseconds(kCommitIntervalMs));
} else if (num_pending == kCommitAfterBatchSize) {
// We've reached a big enough batch, fire off a commit now.
- PostBackgroundTask(FROM_HERE, base::Bind(&Backend::Commit, this));
+ BrowserThread::PostTask(
+ BrowserThread::DB, FROM_HERE,
+ base::Bind(&Backend::Commit, this));
}
}
void SQLitePersistentCookieStore::Backend::Commit() {
- DCHECK(background_task_runner_->RunsTasksOnCurrentThread());
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
PendingOperationsList ops;
{
@@ -950,32 +947,33 @@
void SQLitePersistentCookieStore::Backend::Flush(
const base::Closure& callback) {
- DCHECK(!background_task_runner_->RunsTasksOnCurrentThread());
- PostBackgroundTask(FROM_HERE, base::Bind(&Backend::Commit, this));
-
+ DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::DB));
+ BrowserThread::PostTask(
+ BrowserThread::DB, FROM_HERE, base::Bind(&Backend::Commit, this));
if (!callback.is_null()) {
// We want the completion task to run immediately after Commit() returns.
// Posting it from here means there is less chance of another task getting
// onto the message queue first, than if we posted it from Commit() itself.
- PostBackgroundTask(FROM_HERE, callback);
+ BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, callback);
}
}
-// Fire off a close message to the background runner. We could still have a
+// Fire off a close message to the background thread. We could still have a
// pending commit timer or Load operations holding references on us, but if/when
// this fires we will already have been cleaned up and it will be ignored.
void SQLitePersistentCookieStore::Backend::Close() {
- if (background_task_runner_->RunsTasksOnCurrentThread()) {
+ if (BrowserThread::CurrentlyOn(BrowserThread::DB)) {
InternalBackgroundClose();
} else {
- // Must close the backend on the background runner.
- PostBackgroundTask(FROM_HERE,
- base::Bind(&Backend::InternalBackgroundClose, this));
+ // Must close the backend on the background thread.
+ BrowserThread::PostTask(
+ BrowserThread::DB, FROM_HERE,
+ base::Bind(&Backend::InternalBackgroundClose, this));
}
}
void SQLitePersistentCookieStore::Backend::InternalBackgroundClose() {
- DCHECK(background_task_runner_->RunsTasksOnCurrentThread());
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
// Commit any pending operations
Commit();
@@ -989,7 +987,7 @@
}
void SQLitePersistentCookieStore::Backend::DeleteSessionCookiesOnShutdown() {
- DCHECK(background_task_runner_->RunsTasksOnCurrentThread());
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
if (!db_.get())
return;
@@ -1030,17 +1028,18 @@
}
void SQLitePersistentCookieStore::Backend::ScheduleKillDatabase() {
- DCHECK(background_task_runner_->RunsTasksOnCurrentThread());
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
corruption_detected_ = true;
// Don't just do the close/delete here, as we are being called by |db| and
// that seems dangerous.
- PostBackgroundTask(FROM_HERE, base::Bind(&Backend::KillDatabase, this));
+ MessageLoop::current()->PostTask(
+ FROM_HERE, base::Bind(&Backend::KillDatabase, this));
}
void SQLitePersistentCookieStore::Backend::KillDatabase() {
- DCHECK(background_task_runner_->RunsTasksOnCurrentThread());
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
if (db_.get()) {
// This Backend will now be in-memory only. In a future run we will recreate
@@ -1058,38 +1057,17 @@
}
void SQLitePersistentCookieStore::Backend::DeleteSessionCookiesOnStartup() {
- DCHECK(background_task_runner_->RunsTasksOnCurrentThread());
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
if (!db_->Execute("DELETE FROM cookies WHERE persistent == 0"))
LOG(WARNING) << "Unable to delete session cookies.";
}
-void SQLitePersistentCookieStore::Backend::PostBackgroundTask(
- const tracked_objects::Location& origin, const base::Closure& task) {
- if (!background_task_runner_->PostTask(origin, task)) {
- LOG(WARNING) << "Failed to post task from " << origin.ToString()
- << " to background_task_runner_.";
- }
-}
-
-void SQLitePersistentCookieStore::Backend::PostClientTask(
- const tracked_objects::Location& origin, const base::Closure& task) {
- if (!client_task_runner_->PostTask(origin, task)) {
- LOG(WARNING) << "Failed to post task from " << origin.ToString()
- << " to client_task_runner_.";
- }
-}
-
SQLitePersistentCookieStore::SQLitePersistentCookieStore(
const base::FilePath& path,
- const scoped_refptr<base::SequencedTaskRunner>& client_task_runner,
- const scoped_refptr<base::SequencedTaskRunner>& background_task_runner,
bool restore_old_session_cookies,
ClearOnExitPolicy* clear_on_exit_policy)
- : backend_(new Backend(path,
- client_task_runner,
- background_task_runner,
- restore_old_session_cookies,
- clear_on_exit_policy)) {
+ : backend_(
+ new Backend(path, restore_old_session_cookies, clear_on_exit_policy)) {
}
void SQLitePersistentCookieStore::Load(const LoadedCallback& loaded_callback) {
@@ -1126,5 +1104,5 @@
SQLitePersistentCookieStore::~SQLitePersistentCookieStore() {
backend_->Close();
// We release our reference to the Backend, though it will probably still have
- // a reference if the background runner has not run Close() yet.
+ // a reference if the background thread has not run Close() yet.
}
« no previous file with comments | « chrome/browser/net/sqlite_persistent_cookie_store.h ('k') | chrome/browser/net/sqlite_persistent_cookie_store_perftest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698