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

Side by Side Diff: content/browser/appcache/appcache_storage_impl.cc

Issue 2940023002: TaskScheduler: migrate appcache database over to use it. (Closed)
Patch Set: comments + rebase Created 3 years, 5 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 unified diff | Download patch
OLDNEW
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 "content/browser/appcache/appcache_storage_impl.h" 5 #include "content/browser/appcache/appcache_storage_impl.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <functional> 10 #include <functional>
11 #include <limits> 11 #include <limits>
12 #include <set> 12 #include <set>
13 #include <vector> 13 #include <vector>
14 14
15 #include "base/bind.h" 15 #include "base/bind.h"
16 #include "base/bind_helpers.h" 16 #include "base/bind_helpers.h"
17 #include "base/files/file_util.h" 17 #include "base/files/file_util.h"
18 #include "base/location.h" 18 #include "base/location.h"
19 #include "base/logging.h" 19 #include "base/logging.h"
20 #include "base/profiler/scoped_tracker.h" 20 #include "base/profiler/scoped_tracker.h"
21 #include "base/single_thread_task_runner.h" 21 #include "base/single_thread_task_runner.h"
22 #include "base/stl_util.h" 22 #include "base/stl_util.h"
23 #include "base/strings/string_util.h" 23 #include "base/strings/string_util.h"
24 #include "base/threading/thread_task_runner_handle.h" 24 #include "base/threading/sequenced_task_runner_handle.h"
25 #include "base/trace_event/trace_event.h" 25 #include "base/trace_event/trace_event.h"
26 #include "content/browser/appcache/appcache.h" 26 #include "content/browser/appcache/appcache.h"
27 #include "content/browser/appcache/appcache_database.h" 27 #include "content/browser/appcache/appcache_database.h"
28 #include "content/browser/appcache/appcache_entry.h" 28 #include "content/browser/appcache/appcache_entry.h"
29 #include "content/browser/appcache/appcache_group.h" 29 #include "content/browser/appcache/appcache_group.h"
30 #include "content/browser/appcache/appcache_histograms.h" 30 #include "content/browser/appcache/appcache_histograms.h"
31 #include "content/browser/appcache/appcache_quota_client.h" 31 #include "content/browser/appcache/appcache_quota_client.h"
32 #include "content/browser/appcache/appcache_response.h" 32 #include "content/browser/appcache/appcache_response.h"
33 #include "content/browser/appcache/appcache_service_impl.h" 33 #include "content/browser/appcache/appcache_service_impl.h"
34 #include "net/base/cache_type.h" 34 #include "net/base/cache_type.h"
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 } // namespace 137 } // namespace
138 138
139 // DatabaseTask ----------------------------------------- 139 // DatabaseTask -----------------------------------------
140 140
141 class AppCacheStorageImpl::DatabaseTask 141 class AppCacheStorageImpl::DatabaseTask
142 : public base::RefCountedThreadSafe<DatabaseTask> { 142 : public base::RefCountedThreadSafe<DatabaseTask> {
143 public: 143 public:
144 explicit DatabaseTask(AppCacheStorageImpl* storage) 144 explicit DatabaseTask(AppCacheStorageImpl* storage)
145 : storage_(storage), 145 : storage_(storage),
146 database_(storage->database_), 146 database_(storage->database_),
147 io_thread_(base::ThreadTaskRunnerHandle::Get()) { 147 io_thread_(base::SequencedTaskRunnerHandle::Get()) {
148 DCHECK(io_thread_.get()); 148 DCHECK(io_thread_.get());
149 } 149 }
150 150
151 void AddDelegate(DelegateReference* delegate_reference) { 151 void AddDelegate(DelegateReference* delegate_reference) {
152 delegates_.push_back(make_scoped_refptr(delegate_reference)); 152 delegates_.push_back(make_scoped_refptr(delegate_reference));
153 } 153 }
154 154
155 // Schedules a task to be Run() on the DB thread. Tasks 155 // Schedules a task to be Run() on the DB thread. Tasks
156 // are run in the order in which they are scheduled. 156 // are run in the order in which they are scheduled.
157 void Schedule(); 157 void Schedule();
(...skipping 20 matching lines...) Expand all
178 178
179 AppCacheStorageImpl* storage_; 179 AppCacheStorageImpl* storage_;
180 AppCacheDatabase* database_; 180 AppCacheDatabase* database_;
181 DelegateReferenceVector delegates_; 181 DelegateReferenceVector delegates_;
182 182
183 private: 183 private:
184 void CallRun(base::TimeTicks schedule_time); 184 void CallRun(base::TimeTicks schedule_time);
185 void CallRunCompleted(base::TimeTicks schedule_time); 185 void CallRunCompleted(base::TimeTicks schedule_time);
186 void OnFatalError(); 186 void OnFatalError();
187 187
188 scoped_refptr<base::SingleThreadTaskRunner> io_thread_; 188 scoped_refptr<base::SequencedTaskRunner> io_thread_;
189 }; 189 };
190 190
191 void AppCacheStorageImpl::DatabaseTask::Schedule() { 191 void AppCacheStorageImpl::DatabaseTask::Schedule() {
192 DCHECK(storage_); 192 DCHECK(storage_);
193 DCHECK(io_thread_->BelongsToCurrentThread()); 193 DCHECK(io_thread_->RunsTasksInCurrentSequence());
194 if (!storage_->database_) 194 if (!storage_->database_)
195 return; 195 return;
196 196
197 if (storage_->db_thread_->PostTask( 197 if (storage_->db_task_runner_->PostTask(
198 FROM_HERE, 198 FROM_HERE,
199 base::Bind(&DatabaseTask::CallRun, this, base::TimeTicks::Now()))) { 199 base::Bind(&DatabaseTask::CallRun, this, base::TimeTicks::Now()))) {
200 storage_->scheduled_database_tasks_.push_back(this); 200 storage_->scheduled_database_tasks_.push_back(this);
201 } else { 201 } else {
202 NOTREACHED() << "Thread for database tasks is not running."; 202 NOTREACHED() << "Thread for database tasks is not running.";
203 } 203 }
204 } 204 }
205 205
206 void AppCacheStorageImpl::DatabaseTask::CancelCompletion() { 206 void AppCacheStorageImpl::DatabaseTask::CancelCompletion() {
207 DCHECK(io_thread_->BelongsToCurrentThread()); 207 DCHECK(io_thread_->RunsTasksInCurrentSequence());
208 delegates_.clear(); 208 delegates_.clear();
209 storage_ = NULL; 209 storage_ = NULL;
210 } 210 }
211 211
212 void AppCacheStorageImpl::DatabaseTask::CallRun( 212 void AppCacheStorageImpl::DatabaseTask::CallRun(
213 base::TimeTicks schedule_time) { 213 base::TimeTicks schedule_time) {
214 AppCacheHistograms::AddTaskQueueTimeSample( 214 AppCacheHistograms::AddTaskQueueTimeSample(
215 base::TimeTicks::Now() - schedule_time); 215 base::TimeTicks::Now() - schedule_time);
216 if (!database_->is_disabled()) { 216 if (!database_->is_disabled()) {
217 base::TimeTicks run_time = base::TimeTicks::Now(); 217 base::TimeTicks run_time = base::TimeTicks::Now();
(...skipping 15 matching lines...) Expand all
233 FROM_HERE, 233 FROM_HERE,
234 base::Bind(&DatabaseTask::CallRunCompleted, this, 234 base::Bind(&DatabaseTask::CallRunCompleted, this,
235 base::TimeTicks::Now())); 235 base::TimeTicks::Now()));
236 } 236 }
237 237
238 void AppCacheStorageImpl::DatabaseTask::CallRunCompleted( 238 void AppCacheStorageImpl::DatabaseTask::CallRunCompleted(
239 base::TimeTicks schedule_time) { 239 base::TimeTicks schedule_time) {
240 AppCacheHistograms::AddCompletionQueueTimeSample( 240 AppCacheHistograms::AddCompletionQueueTimeSample(
241 base::TimeTicks::Now() - schedule_time); 241 base::TimeTicks::Now() - schedule_time);
242 if (storage_) { 242 if (storage_) {
243 DCHECK(io_thread_->BelongsToCurrentThread()); 243 DCHECK(io_thread_->RunsTasksInCurrentSequence());
244 DCHECK(storage_->scheduled_database_tasks_.front() == this); 244 DCHECK(storage_->scheduled_database_tasks_.front() == this);
245 storage_->scheduled_database_tasks_.pop_front(); 245 storage_->scheduled_database_tasks_.pop_front();
246 base::TimeTicks run_time = base::TimeTicks::Now(); 246 base::TimeTicks run_time = base::TimeTicks::Now();
247 RunCompleted(); 247 RunCompleted();
248 AppCacheHistograms::AddCompletionRunTimeSample( 248 AppCacheHistograms::AddCompletionRunTimeSample(
249 base::TimeTicks::Now() - run_time); 249 base::TimeTicks::Now() - run_time);
250 delegates_.clear(); 250 delegates_.clear();
251 } 251 }
252 } 252 }
253 253
254 void AppCacheStorageImpl::DatabaseTask::OnFatalError() { 254 void AppCacheStorageImpl::DatabaseTask::OnFatalError() {
255 if (storage_) { 255 if (storage_) {
256 DCHECK(io_thread_->BelongsToCurrentThread()); 256 DCHECK(io_thread_->RunsTasksInCurrentSequence());
257 storage_->Disable(); 257 storage_->Disable();
258 storage_->DeleteAndStartOver(); 258 storage_->DeleteAndStartOver();
259 } 259 }
260 } 260 }
261 261
262 // InitTask ------- 262 // InitTask -------
263 263
264 class AppCacheStorageImpl::InitTask : public DatabaseTask { 264 class AppCacheStorageImpl::InitTask : public DatabaseTask {
265 public: 265 public:
266 explicit InitTask(AppCacheStorageImpl* storage) 266 explicit InitTask(AppCacheStorageImpl* storage)
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
314 314
315 void AppCacheStorageImpl::InitTask::RunCompleted() { 315 void AppCacheStorageImpl::InitTask::RunCompleted() {
316 storage_->last_group_id_ = last_group_id_; 316 storage_->last_group_id_ = last_group_id_;
317 storage_->last_cache_id_ = last_cache_id_; 317 storage_->last_cache_id_ = last_cache_id_;
318 storage_->last_response_id_ = last_response_id_; 318 storage_->last_response_id_ = last_response_id_;
319 storage_->last_deletable_response_rowid_ = last_deletable_response_rowid_; 319 storage_->last_deletable_response_rowid_ = last_deletable_response_rowid_;
320 320
321 if (!storage_->is_disabled()) { 321 if (!storage_->is_disabled()) {
322 storage_->usage_map_.swap(usage_map_); 322 storage_->usage_map_.swap(usage_map_);
323 const base::TimeDelta kDelay = base::TimeDelta::FromMinutes(5); 323 const base::TimeDelta kDelay = base::TimeDelta::FromMinutes(5);
324 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( 324 base::SequencedTaskRunnerHandle::Get()->PostDelayedTask(
325 FROM_HERE, 325 FROM_HERE,
326 base::Bind(&AppCacheStorageImpl::DelayedStartDeletingUnusedResponses, 326 base::Bind(&AppCacheStorageImpl::DelayedStartDeletingUnusedResponses,
327 storage_->weak_factory_.GetWeakPtr()), 327 storage_->weak_factory_.GetWeakPtr()),
328 kDelay); 328 kDelay);
329 } 329 }
330 330
331 if (storage_->service()->quota_client()) 331 if (storage_->service()->quota_client())
332 storage_->service()->quota_client()->NotifyAppCacheReady(); 332 storage_->service()->quota_client()->NotifyAppCacheReady();
333 } 333 }
334 334
(...skipping 1083 matching lines...) Expand 10 before | Expand all | Expand 10 after
1418 weak_factory_(this) { 1418 weak_factory_(this) {
1419 } 1419 }
1420 1420
1421 AppCacheStorageImpl::~AppCacheStorageImpl() { 1421 AppCacheStorageImpl::~AppCacheStorageImpl() {
1422 for (auto* task : pending_quota_queries_) 1422 for (auto* task : pending_quota_queries_)
1423 task->CancelCompletion(); 1423 task->CancelCompletion();
1424 for (auto* task : scheduled_database_tasks_) 1424 for (auto* task : scheduled_database_tasks_)
1425 task->CancelCompletion(); 1425 task->CancelCompletion();
1426 1426
1427 if (database_ && 1427 if (database_ &&
1428 !db_thread_->PostTask( 1428 !db_task_runner_->PostTask(
1429 FROM_HERE, 1429 FROM_HERE,
1430 base::Bind(&ClearSessionOnlyOrigins, 1430 base::Bind(&ClearSessionOnlyOrigins, database_,
1431 database_,
1432 make_scoped_refptr(service_->special_storage_policy()), 1431 make_scoped_refptr(service_->special_storage_policy()),
1433 service()->force_keep_session_state()))) { 1432 service()->force_keep_session_state()))) {
1434 delete database_; 1433 delete database_;
1435 } 1434 }
1436 database_ = NULL; // So no further database tasks can be scheduled. 1435 database_ = NULL; // So no further database tasks can be scheduled.
1437 } 1436 }
1438 1437
1439 void AppCacheStorageImpl::Initialize( 1438 void AppCacheStorageImpl::Initialize(
1440 const base::FilePath& cache_directory, 1439 const base::FilePath& cache_directory,
1441 const scoped_refptr<base::SingleThreadTaskRunner>& db_thread, 1440 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner,
1442 const scoped_refptr<base::SingleThreadTaskRunner>& cache_thread) { 1441 const scoped_refptr<base::SingleThreadTaskRunner>& cache_thread) {
1443 DCHECK(db_thread.get());
1444
1445 cache_directory_ = cache_directory; 1442 cache_directory_ = cache_directory;
1446 is_incognito_ = cache_directory_.empty(); 1443 is_incognito_ = cache_directory_.empty();
1447 1444
1448 base::FilePath db_file_path; 1445 base::FilePath db_file_path;
1449 if (!is_incognito_) 1446 if (!is_incognito_)
1450 db_file_path = cache_directory_.Append(kAppCacheDatabaseName); 1447 db_file_path = cache_directory_.Append(kAppCacheDatabaseName);
1451 database_ = new AppCacheDatabase(db_file_path); 1448 database_ = new AppCacheDatabase(db_file_path);
1452 1449
1453 db_thread_ = db_thread; 1450 db_task_runner_ = db_task_runner;
1454 cache_thread_ = cache_thread; 1451 cache_thread_ = cache_thread;
1455 1452
1456 scoped_refptr<InitTask> task(new InitTask(this)); 1453 scoped_refptr<InitTask> task(new InitTask(this));
1457 task->Schedule(); 1454 task->Schedule();
1458 } 1455 }
1459 1456
1460 void AppCacheStorageImpl::Disable() { 1457 void AppCacheStorageImpl::Disable() {
1461 if (is_disabled_) 1458 if (is_disabled_)
1462 return; 1459 return;
1463 VLOG(1) << "Disabling appcache storage."; 1460 VLOG(1) << "Disabling appcache storage.";
(...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after
1799 deletable_response_ids_.insert( 1796 deletable_response_ids_.insert(
1800 deletable_response_ids_.end(), 1797 deletable_response_ids_.end(),
1801 response_ids.begin(), response_ids.end()); 1798 response_ids.begin(), response_ids.end());
1802 if (!is_response_deletion_scheduled_) 1799 if (!is_response_deletion_scheduled_)
1803 ScheduleDeleteOneResponse(); 1800 ScheduleDeleteOneResponse();
1804 } 1801 }
1805 1802
1806 void AppCacheStorageImpl::ScheduleDeleteOneResponse() { 1803 void AppCacheStorageImpl::ScheduleDeleteOneResponse() {
1807 DCHECK(!is_response_deletion_scheduled_); 1804 DCHECK(!is_response_deletion_scheduled_);
1808 const base::TimeDelta kBriefDelay = base::TimeDelta::FromMilliseconds(10); 1805 const base::TimeDelta kBriefDelay = base::TimeDelta::FromMilliseconds(10);
1809 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( 1806 base::SequencedTaskRunnerHandle::Get()->PostDelayedTask(
1810 FROM_HERE, base::Bind(&AppCacheStorageImpl::DeleteOneResponse, 1807 FROM_HERE,
1811 weak_factory_.GetWeakPtr()), 1808 base::Bind(&AppCacheStorageImpl::DeleteOneResponse,
1809 weak_factory_.GetWeakPtr()),
1812 kBriefDelay); 1810 kBriefDelay);
1813 is_response_deletion_scheduled_ = true; 1811 is_response_deletion_scheduled_ = true;
1814 } 1812 }
1815 1813
1816 void AppCacheStorageImpl::DeleteOneResponse() { 1814 void AppCacheStorageImpl::DeleteOneResponse() {
1817 DCHECK(is_response_deletion_scheduled_); 1815 DCHECK(is_response_deletion_scheduled_);
1818 DCHECK(!deletable_response_ids_.empty()); 1816 DCHECK(!deletable_response_ids_.empty());
1819 1817
1820 if (is_disabled_) { 1818 if (is_disabled_) {
1821 deletable_response_ids_.clear(); 1819 deletable_response_ids_.clear();
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
1884 PendingForeignMarkings::iterator iter = pending_foreign_markings_.begin(); 1882 PendingForeignMarkings::iterator iter = pending_foreign_markings_.begin();
1885 while (iter != pending_foreign_markings_.end()) { 1883 while (iter != pending_foreign_markings_.end()) {
1886 if (iter->second == cache_id) 1884 if (iter->second == cache_id)
1887 urls->push_back(iter->first); 1885 urls->push_back(iter->first);
1888 ++iter; 1886 ++iter;
1889 } 1887 }
1890 } 1888 }
1891 1889
1892 void AppCacheStorageImpl::ScheduleSimpleTask(const base::Closure& task) { 1890 void AppCacheStorageImpl::ScheduleSimpleTask(const base::Closure& task) {
1893 pending_simple_tasks_.push_back(task); 1891 pending_simple_tasks_.push_back(task);
1894 base::ThreadTaskRunnerHandle::Get()->PostTask( 1892 base::SequencedTaskRunnerHandle::Get()->PostTask(
1895 FROM_HERE, base::Bind(&AppCacheStorageImpl::RunOnePendingSimpleTask, 1893 FROM_HERE, base::Bind(&AppCacheStorageImpl::RunOnePendingSimpleTask,
1896 weak_factory_.GetWeakPtr())); 1894 weak_factory_.GetWeakPtr()));
1897 } 1895 }
1898 1896
1899 void AppCacheStorageImpl::RunOnePendingSimpleTask() { 1897 void AppCacheStorageImpl::RunOnePendingSimpleTask() {
1900 DCHECK(!pending_simple_tasks_.empty()); 1898 DCHECK(!pending_simple_tasks_.empty());
1901 base::Closure task = pending_simple_tasks_.front(); 1899 base::Closure task = pending_simple_tasks_.front();
1902 pending_simple_tasks_.pop_front(); 1900 pending_simple_tasks_.pop_front();
1903 task.Run(); 1901 task.Run();
1904 } 1902 }
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
1954 // prior to deleting the files and calling reinit. 1952 // prior to deleting the files and calling reinit.
1955 cache_thread_->PostTaskAndReply( 1953 cache_thread_->PostTaskAndReply(
1956 FROM_HERE, 1954 FROM_HERE,
1957 base::Bind(&base::DoNothing), 1955 base::Bind(&base::DoNothing),
1958 base::Bind(&AppCacheStorageImpl::DeleteAndStartOverPart2, 1956 base::Bind(&AppCacheStorageImpl::DeleteAndStartOverPart2,
1959 weak_factory_.GetWeakPtr())); 1957 weak_factory_.GetWeakPtr()));
1960 } 1958 }
1961 } 1959 }
1962 1960
1963 void AppCacheStorageImpl::DeleteAndStartOverPart2() { 1961 void AppCacheStorageImpl::DeleteAndStartOverPart2() {
1964 db_thread_->PostTaskAndReply( 1962 db_task_runner_->PostTaskAndReply(
1965 FROM_HERE, 1963 FROM_HERE,
1966 base::Bind(base::IgnoreResult(&base::DeleteFile), cache_directory_, true), 1964 base::Bind(base::IgnoreResult(&base::DeleteFile), cache_directory_, true),
1967 base::Bind(&AppCacheStorageImpl::CallScheduleReinitialize, 1965 base::Bind(&AppCacheStorageImpl::CallScheduleReinitialize,
1968 weak_factory_.GetWeakPtr())); 1966 weak_factory_.GetWeakPtr()));
1969 } 1967 }
1970 1968
1971 void AppCacheStorageImpl::CallScheduleReinitialize() { 1969 void AppCacheStorageImpl::CallScheduleReinitialize() {
1972 service_->ScheduleReinitialize(); 1970 service_->ScheduleReinitialize();
1973 // note: 'this' may be deleted at this point. 1971 // note: 'this' may be deleted at this point.
1974 } 1972 }
(...skipping 10 matching lines...) Expand all
1985 1983
1986 void AppCacheStorageImpl::OnLazyCommitTimer() { 1984 void AppCacheStorageImpl::OnLazyCommitTimer() {
1987 lazy_commit_timer_.Stop(); 1985 lazy_commit_timer_.Stop();
1988 if (is_disabled()) 1986 if (is_disabled())
1989 return; 1987 return;
1990 scoped_refptr<DatabaseTask> task(new CommitLastAccessTimesTask(this)); 1988 scoped_refptr<DatabaseTask> task(new CommitLastAccessTimesTask(this));
1991 task->Schedule(); 1989 task->Schedule();
1992 } 1990 }
1993 1991
1994 } // namespace content 1992 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/appcache/appcache_storage_impl.h ('k') | content/browser/appcache/appcache_storage_impl_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698