| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/notifications/notification_database.h" | 5 #include "content/browser/notifications/notification_database.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 229 DCHECK_GE(notification_id, kFirstNotificationId); | 229 DCHECK_GE(notification_id, kFirstNotificationId); |
| 230 DCHECK(origin.is_valid()); | 230 DCHECK(origin.is_valid()); |
| 231 | 231 |
| 232 std::string key = CreateDataKey(origin, notification_id); | 232 std::string key = CreateDataKey(origin, notification_id); |
| 233 return LevelDBStatusToStatus(db_->Delete(leveldb::WriteOptions(), key)); | 233 return LevelDBStatusToStatus(db_->Delete(leveldb::WriteOptions(), key)); |
| 234 } | 234 } |
| 235 | 235 |
| 236 NotificationDatabase::Status | 236 NotificationDatabase::Status |
| 237 NotificationDatabase::DeleteAllNotificationDataForOrigin( | 237 NotificationDatabase::DeleteAllNotificationDataForOrigin( |
| 238 const GURL& origin, | 238 const GURL& origin, |
| 239 const std::string& tag, |
| 239 std::set<int64_t>* deleted_notification_set) { | 240 std::set<int64_t>* deleted_notification_set) { |
| 240 return DeleteAllNotificationDataInternal( | 241 return DeleteAllNotificationDataInternal(origin, tag, |
| 241 origin, kInvalidServiceWorkerRegistrationId, deleted_notification_set); | 242 kInvalidServiceWorkerRegistrationId, |
| 243 deleted_notification_set); |
| 242 } | 244 } |
| 243 | 245 |
| 244 NotificationDatabase::Status | 246 NotificationDatabase::Status |
| 245 NotificationDatabase::DeleteAllNotificationDataForServiceWorkerRegistration( | 247 NotificationDatabase::DeleteAllNotificationDataForServiceWorkerRegistration( |
| 246 const GURL& origin, | 248 const GURL& origin, |
| 247 int64_t service_worker_registration_id, | 249 int64_t service_worker_registration_id, |
| 248 std::set<int64_t>* deleted_notification_set) { | 250 std::set<int64_t>* deleted_notification_set) { |
| 249 return DeleteAllNotificationDataInternal( | 251 return DeleteAllNotificationDataInternal(origin, "" /* tag */, |
| 250 origin, service_worker_registration_id, deleted_notification_set); | 252 service_worker_registration_id, |
| 253 deleted_notification_set); |
| 251 } | 254 } |
| 252 | 255 |
| 253 NotificationDatabase::Status NotificationDatabase::Destroy() { | 256 NotificationDatabase::Status NotificationDatabase::Destroy() { |
| 254 DCHECK(sequence_checker_.CalledOnValidSequence()); | 257 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 255 | 258 |
| 256 leveldb::Options options; | 259 leveldb::Options options; |
| 257 if (IsInMemoryDatabase()) { | 260 if (IsInMemoryDatabase()) { |
| 258 if (!env_) | 261 if (!env_) |
| 259 return STATUS_OK; // The database has not been initialized. | 262 return STATUS_OK; // The database has not been initialized. |
| 260 | 263 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 321 | 324 |
| 322 notification_data_vector->push_back(notification_database_data); | 325 notification_data_vector->push_back(notification_database_data); |
| 323 } | 326 } |
| 324 | 327 |
| 325 return LevelDBStatusToStatus(iter->status()); | 328 return LevelDBStatusToStatus(iter->status()); |
| 326 } | 329 } |
| 327 | 330 |
| 328 NotificationDatabase::Status | 331 NotificationDatabase::Status |
| 329 NotificationDatabase::DeleteAllNotificationDataInternal( | 332 NotificationDatabase::DeleteAllNotificationDataInternal( |
| 330 const GURL& origin, | 333 const GURL& origin, |
| 334 const std::string& tag, |
| 331 int64_t service_worker_registration_id, | 335 int64_t service_worker_registration_id, |
| 332 std::set<int64_t>* deleted_notification_set) { | 336 std::set<int64_t>* deleted_notification_set) { |
| 333 DCHECK(sequence_checker_.CalledOnValidSequence()); | 337 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 334 DCHECK(deleted_notification_set); | 338 DCHECK(deleted_notification_set); |
| 335 DCHECK(origin.is_valid()); | 339 DCHECK(origin.is_valid()); |
| 336 | 340 |
| 337 const std::string prefix = CreateDataPrefix(origin); | 341 const std::string prefix = CreateDataPrefix(origin); |
| 342 const bool should_deserialize = |
| 343 service_worker_registration_id != kInvalidServiceWorkerRegistrationId || |
| 344 !tag.empty(); |
| 338 | 345 |
| 339 leveldb::Slice prefix_slice(prefix); | 346 leveldb::Slice prefix_slice(prefix); |
| 340 leveldb::WriteBatch batch; | 347 leveldb::WriteBatch batch; |
| 341 | 348 |
| 342 NotificationDatabaseData notification_database_data; | 349 NotificationDatabaseData notification_database_data; |
| 343 std::unique_ptr<leveldb::Iterator> iter( | 350 std::unique_ptr<leveldb::Iterator> iter( |
| 344 db_->NewIterator(leveldb::ReadOptions())); | 351 db_->NewIterator(leveldb::ReadOptions())); |
| 345 for (iter->Seek(prefix_slice); iter->Valid(); iter->Next()) { | 352 for (iter->Seek(prefix_slice); iter->Valid(); iter->Next()) { |
| 346 if (!iter->key().starts_with(prefix_slice)) | 353 if (!iter->key().starts_with(prefix_slice)) |
| 347 break; | 354 break; |
| 348 | 355 |
| 349 if (service_worker_registration_id != kInvalidServiceWorkerRegistrationId) { | 356 if (should_deserialize) { |
| 350 Status status = DeserializedNotificationData(iter->value().ToString(), | 357 Status status = DeserializedNotificationData(iter->value().ToString(), |
| 351 ¬ification_database_data); | 358 ¬ification_database_data); |
| 352 if (status != STATUS_OK) | 359 if (status != STATUS_OK) |
| 353 return status; | 360 return status; |
| 354 | 361 |
| 355 if (notification_database_data.service_worker_registration_id != | 362 if (!tag.empty() && |
| 356 service_worker_registration_id) { | 363 notification_database_data.notification_data.tag != tag) { |
| 364 continue; |
| 365 } |
| 366 |
| 367 if (service_worker_registration_id != |
| 368 kInvalidServiceWorkerRegistrationId && |
| 369 notification_database_data.service_worker_registration_id != |
| 370 service_worker_registration_id) { |
| 357 continue; | 371 continue; |
| 358 } | 372 } |
| 359 } | 373 } |
| 360 | 374 |
| 361 leveldb::Slice notification_id_slice = iter->key(); | 375 leveldb::Slice notification_id_slice = iter->key(); |
| 362 notification_id_slice.remove_prefix(prefix_slice.size()); | 376 notification_id_slice.remove_prefix(prefix_slice.size()); |
| 363 | 377 |
| 364 int64_t notification_id = 0; | 378 int64_t notification_id = 0; |
| 365 if (!base::StringToInt64(notification_id_slice.ToString(), | 379 if (!base::StringToInt64(notification_id_slice.ToString(), |
| 366 ¬ification_id)) { | 380 ¬ification_id)) { |
| 367 return STATUS_ERROR_CORRUPTED; | 381 return STATUS_ERROR_CORRUPTED; |
| 368 } | 382 } |
| 369 | 383 |
| 370 deleted_notification_set->insert(notification_id); | 384 deleted_notification_set->insert(notification_id); |
| 371 batch.Delete(iter->key()); | 385 batch.Delete(iter->key()); |
| 372 } | 386 } |
| 373 | 387 |
| 374 if (deleted_notification_set->empty()) | 388 if (deleted_notification_set->empty()) |
| 375 return STATUS_OK; | 389 return STATUS_OK; |
| 376 | 390 |
| 377 return LevelDBStatusToStatus(db_->Write(leveldb::WriteOptions(), &batch)); | 391 return LevelDBStatusToStatus(db_->Write(leveldb::WriteOptions(), &batch)); |
| 378 } | 392 } |
| 379 | 393 |
| 380 } // namespace content | 394 } // namespace content |
| OLD | NEW |