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

Side by Side Diff: content/browser/service_worker/service_worker_storage.cc

Issue 633873002: Service Worker: Respect the "clear on exit" content setting (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@contentsettings
Patch Set: just clear in dtor Created 6 years, 2 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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/service_worker/service_worker_storage.h" 5 #include "content/browser/service_worker/service_worker_storage.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/bind_helpers.h" 9 #include "base/bind_helpers.h"
10 #include "base/debug/trace_event.h" 10 #include "base/debug/trace_event.h"
11 #include "base/files/file_util.h" 11 #include "base/files/file_util.h"
12 #include "base/hash.h" 12 #include "base/hash.h"
13 #include "base/message_loop/message_loop.h" 13 #include "base/message_loop/message_loop.h"
14 #include "base/sequenced_task_runner.h" 14 #include "base/sequenced_task_runner.h"
15 #include "base/single_thread_task_runner.h" 15 #include "base/single_thread_task_runner.h"
16 #include "base/task_runner_util.h" 16 #include "base/task_runner_util.h"
17 #include "content/browser/service_worker/service_worker_context_core.h" 17 #include "content/browser/service_worker/service_worker_context_core.h"
18 #include "content/browser/service_worker/service_worker_disk_cache.h" 18 #include "content/browser/service_worker/service_worker_disk_cache.h"
19 #include "content/browser/service_worker/service_worker_info.h" 19 #include "content/browser/service_worker/service_worker_info.h"
20 #include "content/browser/service_worker/service_worker_metrics.h" 20 #include "content/browser/service_worker/service_worker_metrics.h"
21 #include "content/browser/service_worker/service_worker_registration.h" 21 #include "content/browser/service_worker/service_worker_registration.h"
22 #include "content/browser/service_worker/service_worker_utils.h" 22 #include "content/browser/service_worker/service_worker_utils.h"
23 #include "content/browser/service_worker/service_worker_version.h" 23 #include "content/browser/service_worker/service_worker_version.h"
24 #include "content/common/service_worker/service_worker_types.h" 24 #include "content/common/service_worker/service_worker_types.h"
25 #include "content/public/browser/browser_thread.h" 25 #include "content/public/browser/browser_thread.h"
26 #include "net/base/completion_callback.h" 26 #include "net/base/completion_callback.h"
27 #include "net/base/io_buffer.h" 27 #include "net/base/io_buffer.h"
28 #include "net/base/net_errors.h" 28 #include "net/base/net_errors.h"
29 #include "storage/browser/quota/quota_manager_proxy.h" 29 #include "storage/browser/quota/quota_manager_proxy.h"
30 #include "storage/browser/quota/special_storage_policy.h"
30 31
31 namespace content { 32 namespace content {
32 33
33 namespace { 34 namespace {
34 35
35 void RunSoon(const tracked_objects::Location& from_here, 36 void RunSoon(const tracked_objects::Location& from_here,
36 const base::Closure& closure) { 37 const base::Closure& closure) {
37 base::MessageLoop::current()->PostTask(from_here, closure); 38 base::MessageLoop::current()->PostTask(from_here, closure);
38 } 39 }
39 40
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 ServiceWorkerStorage:: 212 ServiceWorkerStorage::
212 DidDeleteRegistrationParams::DidDeleteRegistrationParams() 213 DidDeleteRegistrationParams::DidDeleteRegistrationParams()
213 : registration_id(kInvalidServiceWorkerRegistrationId) { 214 : registration_id(kInvalidServiceWorkerRegistrationId) {
214 } 215 }
215 216
216 ServiceWorkerStorage:: 217 ServiceWorkerStorage::
217 DidDeleteRegistrationParams::~DidDeleteRegistrationParams() { 218 DidDeleteRegistrationParams::~DidDeleteRegistrationParams() {
218 } 219 }
219 220
220 ServiceWorkerStorage::~ServiceWorkerStorage() { 221 ServiceWorkerStorage::~ServiceWorkerStorage() {
222 ClearSessionOnlyOrigins();
221 weak_factory_.InvalidateWeakPtrs(); 223 weak_factory_.InvalidateWeakPtrs();
222 database_task_runner_->DeleteSoon(FROM_HERE, database_.release()); 224 database_task_runner_->DeleteSoon(FROM_HERE, database_.release());
223 } 225 }
224 226
225 // static 227 // static
226 scoped_ptr<ServiceWorkerStorage> ServiceWorkerStorage::Create( 228 scoped_ptr<ServiceWorkerStorage> ServiceWorkerStorage::Create(
227 const base::FilePath& path, 229 const base::FilePath& path,
228 base::WeakPtr<ServiceWorkerContextCore> context, 230 base::WeakPtr<ServiceWorkerContextCore> context,
229 const scoped_refptr<base::SequencedTaskRunner>& database_task_runner, 231 const scoped_refptr<base::SequencedTaskRunner>& database_task_runner,
230 const scoped_refptr<base::SingleThreadTaskRunner>& disk_cache_thread, 232 const scoped_refptr<base::SingleThreadTaskRunner>& disk_cache_thread,
231 storage::QuotaManagerProxy* quota_manager_proxy) { 233 storage::QuotaManagerProxy* quota_manager_proxy,
234 storage::SpecialStoragePolicy* special_storage_policy) {
232 return make_scoped_ptr(new ServiceWorkerStorage(path, 235 return make_scoped_ptr(new ServiceWorkerStorage(path,
233 context, 236 context,
234 database_task_runner, 237 database_task_runner,
235 disk_cache_thread, 238 disk_cache_thread,
236 quota_manager_proxy)); 239 quota_manager_proxy,
240 special_storage_policy));
237 } 241 }
238 242
239 // static 243 // static
240 scoped_ptr<ServiceWorkerStorage> ServiceWorkerStorage::Create( 244 scoped_ptr<ServiceWorkerStorage> ServiceWorkerStorage::Create(
241 base::WeakPtr<ServiceWorkerContextCore> context, 245 base::WeakPtr<ServiceWorkerContextCore> context,
242 ServiceWorkerStorage* old_storage) { 246 ServiceWorkerStorage* old_storage) {
243 return make_scoped_ptr( 247 return make_scoped_ptr(
244 new ServiceWorkerStorage(old_storage->path_, 248 new ServiceWorkerStorage(old_storage->path_,
245 context, 249 context,
246 old_storage->database_task_runner_, 250 old_storage->database_task_runner_,
247 old_storage->disk_cache_thread_, 251 old_storage->disk_cache_thread_,
248 old_storage->quota_manager_proxy_.get())); 252 old_storage->quota_manager_proxy_.get(),
253 old_storage->special_storage_policy_.get()));
249 } 254 }
250 255
251 void ServiceWorkerStorage::FindRegistrationForDocument( 256 void ServiceWorkerStorage::FindRegistrationForDocument(
252 const GURL& document_url, 257 const GURL& document_url,
253 const FindRegistrationCallback& callback) { 258 const FindRegistrationCallback& callback) {
254 DCHECK(!document_url.has_ref()); 259 DCHECK(!document_url.has_ref());
255 TRACE_EVENT_ASYNC_BEGIN1( 260 TRACE_EVENT_ASYNC_BEGIN1(
256 "ServiceWorker", 261 "ServiceWorker",
257 "ServiceWorkerStorage::FindRegistrationForDocument", 262 "ServiceWorkerStorage::FindRegistrationForDocument",
258 base::Hash(document_url.spec()), 263 base::Hash(document_url.spec()),
(...skipping 416 matching lines...) Expand 10 before | Expand all | Expand 10 after
675 if (!has_checked_for_stale_resources_) 680 if (!has_checked_for_stale_resources_)
676 DeleteStaleResources(); 681 DeleteStaleResources();
677 StartPurgingResources(resources); 682 StartPurgingResources(resources);
678 } 683 }
679 684
680 ServiceWorkerStorage::ServiceWorkerStorage( 685 ServiceWorkerStorage::ServiceWorkerStorage(
681 const base::FilePath& path, 686 const base::FilePath& path,
682 base::WeakPtr<ServiceWorkerContextCore> context, 687 base::WeakPtr<ServiceWorkerContextCore> context,
683 const scoped_refptr<base::SequencedTaskRunner>& database_task_runner, 688 const scoped_refptr<base::SequencedTaskRunner>& database_task_runner,
684 const scoped_refptr<base::SingleThreadTaskRunner>& disk_cache_thread, 689 const scoped_refptr<base::SingleThreadTaskRunner>& disk_cache_thread,
685 storage::QuotaManagerProxy* quota_manager_proxy) 690 storage::QuotaManagerProxy* quota_manager_proxy,
691 storage::SpecialStoragePolicy* special_storage_policy)
686 : next_registration_id_(kInvalidServiceWorkerRegistrationId), 692 : next_registration_id_(kInvalidServiceWorkerRegistrationId),
687 next_version_id_(kInvalidServiceWorkerVersionId), 693 next_version_id_(kInvalidServiceWorkerVersionId),
688 next_resource_id_(kInvalidServiceWorkerResourceId), 694 next_resource_id_(kInvalidServiceWorkerResourceId),
689 state_(UNINITIALIZED), 695 state_(UNINITIALIZED),
690 path_(path), 696 path_(path),
691 context_(context), 697 context_(context),
692 database_task_runner_(database_task_runner), 698 database_task_runner_(database_task_runner),
693 disk_cache_thread_(disk_cache_thread), 699 disk_cache_thread_(disk_cache_thread),
694 quota_manager_proxy_(quota_manager_proxy), 700 quota_manager_proxy_(quota_manager_proxy),
701 special_storage_policy_(special_storage_policy),
695 is_purge_pending_(false), 702 is_purge_pending_(false),
696 has_checked_for_stale_resources_(false), 703 has_checked_for_stale_resources_(false),
697 weak_factory_(this) { 704 weak_factory_(this) {
698 database_.reset(new ServiceWorkerDatabase(GetDatabasePath())); 705 database_.reset(new ServiceWorkerDatabase(GetDatabasePath()));
699 } 706 }
700 707
701 base::FilePath ServiceWorkerStorage::GetDatabasePath() { 708 base::FilePath ServiceWorkerStorage::GetDatabasePath() {
702 if (path_.empty()) 709 if (path_.empty())
703 return base::FilePath(); 710 return base::FilePath();
704 return path_.Append(ServiceWorkerContextCore::kServiceWorkerDirectory) 711 return path_.Append(ServiceWorkerContextCore::kServiceWorkerDirectory)
(...skipping 455 matching lines...) Expand 10 before | Expand all | Expand 10 after
1160 1167
1161 void ServiceWorkerStorage::DidCollectStaleResources( 1168 void ServiceWorkerStorage::DidCollectStaleResources(
1162 const std::vector<int64>& stale_resource_ids, 1169 const std::vector<int64>& stale_resource_ids,
1163 ServiceWorkerDatabase::Status status) { 1170 ServiceWorkerDatabase::Status status) {
1164 DCHECK_EQ(ServiceWorkerDatabase::STATUS_OK, status); 1171 DCHECK_EQ(ServiceWorkerDatabase::STATUS_OK, status);
1165 if (status != ServiceWorkerDatabase::STATUS_OK) 1172 if (status != ServiceWorkerDatabase::STATUS_OK)
1166 return; 1173 return;
1167 StartPurgingResources(stale_resource_ids); 1174 StartPurgingResources(stale_resource_ids);
1168 } 1175 }
1169 1176
1177 void ServiceWorkerStorage::ClearSessionOnlyOrigins() {
1178 // Can be null in unittests.
1179 if (!special_storage_policy_.get())
1180 return;
1181
1182 if (!special_storage_policy_->HasSessionOnlyOrigins())
1183 return;
1184
1185 std::set<GURL> session_only_origins;
1186 for (auto origin : registered_origins_) {
michaeln 2014/10/08 01:22:27 ditto const GURL&
falken 2014/10/08 02:36:44 Done.
1187 if (special_storage_policy_->IsStorageSessionOnly(origin))
1188 session_only_origins.insert(origin);
1189 }
1190 database_task_runner_->PostTask(
1191 FROM_HERE,
1192 base::Bind(
1193 &DeleteAllDataForOriginsFromDB,
1194 database_.get(),
1195 session_only_origins));
1196 }
1197
1170 void ServiceWorkerStorage::CollectStaleResourcesFromDB( 1198 void ServiceWorkerStorage::CollectStaleResourcesFromDB(
1171 ServiceWorkerDatabase* database, 1199 ServiceWorkerDatabase* database,
1172 scoped_refptr<base::SequencedTaskRunner> original_task_runner, 1200 scoped_refptr<base::SequencedTaskRunner> original_task_runner,
1173 const GetResourcesCallback& callback) { 1201 const GetResourcesCallback& callback) {
1174 std::set<int64> ids; 1202 std::set<int64> ids;
1175 ServiceWorkerDatabase::Status status = 1203 ServiceWorkerDatabase::Status status =
1176 database->GetUncommittedResourceIds(&ids); 1204 database->GetUncommittedResourceIds(&ids);
1177 if (status != ServiceWorkerDatabase::STATUS_OK) { 1205 if (status != ServiceWorkerDatabase::STATUS_OK) {
1178 original_task_runner->PostTask( 1206 original_task_runner->PostTask(
1179 FROM_HERE, 1207 FROM_HERE,
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
1366 const GURL& origin, 1394 const GURL& origin,
1367 const FindInDBCallback& callback) { 1395 const FindInDBCallback& callback) {
1368 ServiceWorkerDatabase::RegistrationData data; 1396 ServiceWorkerDatabase::RegistrationData data;
1369 ResourceList resources; 1397 ResourceList resources;
1370 ServiceWorkerDatabase::Status status = 1398 ServiceWorkerDatabase::Status status =
1371 database->ReadRegistration(registration_id, origin, &data, &resources); 1399 database->ReadRegistration(registration_id, origin, &data, &resources);
1372 original_task_runner->PostTask( 1400 original_task_runner->PostTask(
1373 FROM_HERE, base::Bind(callback, data, resources, status)); 1401 FROM_HERE, base::Bind(callback, data, resources, status));
1374 } 1402 }
1375 1403
1404 void ServiceWorkerStorage::DeleteAllDataForOriginsFromDB(
1405 ServiceWorkerDatabase* database,
1406 const std::set<GURL>& origins) {
1407 DCHECK(database);
1408
1409 std::vector<int64> newly_purgeable_resources;
1410 database->DeleteAllDataForOrigins(origins, &newly_purgeable_resources);
1411 }
1412
1376 // TODO(nhiroki): The corruption recovery should not be scheduled if the error 1413 // TODO(nhiroki): The corruption recovery should not be scheduled if the error
1377 // is transient and it can get healed soon (e.g. IO error). To do that, the 1414 // is transient and it can get healed soon (e.g. IO error). To do that, the
1378 // database should not disable itself when an error occurs and the storage 1415 // database should not disable itself when an error occurs and the storage
1379 // controls it instead. 1416 // controls it instead.
1380 void ServiceWorkerStorage::ScheduleDeleteAndStartOver() { 1417 void ServiceWorkerStorage::ScheduleDeleteAndStartOver() {
1381 if (state_ == DISABLED) { 1418 if (state_ == DISABLED) {
1382 // Recovery process has already been scheduled. 1419 // Recovery process has already been scheduled.
1383 return; 1420 return;
1384 } 1421 }
1385 Disable(); 1422 Disable();
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
1420 // Give up the corruption recovery until the browser restarts. 1457 // Give up the corruption recovery until the browser restarts.
1421 LOG(ERROR) << "Failed to delete the diskcache."; 1458 LOG(ERROR) << "Failed to delete the diskcache.";
1422 callback.Run(SERVICE_WORKER_ERROR_FAILED); 1459 callback.Run(SERVICE_WORKER_ERROR_FAILED);
1423 return; 1460 return;
1424 } 1461 }
1425 DVLOG(1) << "Deleted ServiceWorkerDiskCache successfully."; 1462 DVLOG(1) << "Deleted ServiceWorkerDiskCache successfully.";
1426 callback.Run(SERVICE_WORKER_OK); 1463 callback.Run(SERVICE_WORKER_OK);
1427 } 1464 }
1428 1465
1429 } // namespace content 1466 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698