| 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 "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "content/public/browser/notification_database_data.h" | 10 #include "content/public/browser/notification_database_data.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 NotificationDatabase* CreateDatabaseInMemory() { | 22 NotificationDatabase* CreateDatabaseInMemory() { |
| 23 return new NotificationDatabase(base::FilePath()); | 23 return new NotificationDatabase(base::FilePath()); |
| 24 } | 24 } |
| 25 | 25 |
| 26 // Creates a new NotificationDatabase instance in |path|. | 26 // Creates a new NotificationDatabase instance in |path|. |
| 27 NotificationDatabase* CreateDatabaseOnFileSystem( | 27 NotificationDatabase* CreateDatabaseOnFileSystem( |
| 28 const base::FilePath& path) { | 28 const base::FilePath& path) { |
| 29 return new NotificationDatabase(path); | 29 return new NotificationDatabase(path); |
| 30 } | 30 } |
| 31 | 31 |
| 32 // Creates a new notification for |service_worker_registration_id| belonging |
| 33 // to |origin| and writes it to the database. The written notification id |
| 34 // will be stored in |notification_id|. |
| 35 void CreateAndWriteNotification(NotificationDatabase* database, |
| 36 const GURL& origin, |
| 37 int64_t service_worker_registration_id, |
| 38 int64_t* notification_id) { |
| 39 NotificationDatabaseData database_data; |
| 40 database_data.origin = origin; |
| 41 database_data.service_worker_registration_id = |
| 42 service_worker_registration_id; |
| 43 |
| 44 ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 45 database->WriteNotificationData(origin, |
| 46 database_data, |
| 47 notification_id)); |
| 48 } |
| 49 |
| 32 // Returns if |database| has been opened. | 50 // Returns if |database| has been opened. |
| 33 bool IsDatabaseOpen(NotificationDatabase* database) { | 51 bool IsDatabaseOpen(NotificationDatabase* database) { |
| 34 return database->IsOpen(); | 52 return database->IsOpen(); |
| 35 } | 53 } |
| 36 | 54 |
| 37 // Returns if |database| is an in-memory only database. | 55 // Returns if |database| is an in-memory only database. |
| 38 bool IsInMemoryDatabase(NotificationDatabase* database) { | 56 bool IsInMemoryDatabase(NotificationDatabase* database) { |
| 39 return database->IsInMemoryDatabase(); | 57 return database->IsInMemoryDatabase(); |
| 40 } | 58 } |
| 41 | 59 |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 ASSERT_TRUE(database_dir.CreateUniqueTempDir()); | 141 ASSERT_TRUE(database_dir.CreateUniqueTempDir()); |
| 124 | 142 |
| 125 scoped_ptr<NotificationDatabase> database( | 143 scoped_ptr<NotificationDatabase> database( |
| 126 CreateDatabaseOnFileSystem(database_dir.path())); | 144 CreateDatabaseOnFileSystem(database_dir.path())); |
| 127 | 145 |
| 128 ASSERT_EQ(NotificationDatabase::STATUS_OK, | 146 ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 129 database->Open(true /* create_if_missing */)); | 147 database->Open(true /* create_if_missing */)); |
| 130 | 148 |
| 131 GURL origin("https://example.com"); | 149 GURL origin("https://example.com"); |
| 132 | 150 |
| 133 NotificationDatabaseData database_data; | |
| 134 int64_t notification_id = 0; | 151 int64_t notification_id = 0; |
| 135 | 152 |
| 136 // Verify that getting two ids on the same database instance results in | 153 // Verify that getting two ids on the same database instance results in |
| 137 // incrementing values. Notification ids will start at 1. | 154 // incrementing values. Notification ids will start at 1. |
| 138 ASSERT_EQ(NotificationDatabase::STATUS_OK, | 155 ASSERT_NO_FATAL_FAILURE(CreateAndWriteNotification( |
| 139 database->WriteNotificationData(origin, | 156 database.get(), origin, 0 /* sw_registration_id */, ¬ification_id)); |
| 140 database_data, | |
| 141 ¬ification_id)); | |
| 142 EXPECT_EQ(notification_id, 1); | 157 EXPECT_EQ(notification_id, 1); |
| 143 | 158 |
| 144 ASSERT_EQ(NotificationDatabase::STATUS_OK, | 159 ASSERT_NO_FATAL_FAILURE(CreateAndWriteNotification( |
| 145 database->WriteNotificationData(origin, | 160 database.get(), origin, 0 /* sw_registration_id */, ¬ification_id)); |
| 146 database_data, | |
| 147 ¬ification_id)); | |
| 148 EXPECT_EQ(notification_id, 2); | 161 EXPECT_EQ(notification_id, 2); |
| 149 | 162 |
| 150 database.reset(CreateDatabaseOnFileSystem(database_dir.path())); | 163 database.reset(CreateDatabaseOnFileSystem(database_dir.path())); |
| 151 ASSERT_EQ(NotificationDatabase::STATUS_OK, | 164 ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 152 database->Open(false /* create_if_missing */)); | 165 database->Open(false /* create_if_missing */)); |
| 153 | 166 |
| 154 // Verify that the next notification id was stored in the database, and | 167 // Verify that the next notification id was stored in the database, and |
| 155 // continues where we expect it to be, even after closing and opening it. | 168 // continues where we expect it to be, even after closing and opening it. |
| 156 ASSERT_EQ(NotificationDatabase::STATUS_OK, | 169 ASSERT_NO_FATAL_FAILURE(CreateAndWriteNotification( |
| 157 database->WriteNotificationData(origin, | 170 database.get(), origin, 0 /* sw_registration_id */, ¬ification_id)); |
| 158 database_data, | |
| 159 ¬ification_id)); | |
| 160 EXPECT_EQ(notification_id, 3); | 171 EXPECT_EQ(notification_id, 3); |
| 161 } | 172 } |
| 162 | 173 |
| 163 TEST_F(NotificationDatabaseTest, NotificationIdCorruption) { | 174 TEST_F(NotificationDatabaseTest, NotificationIdCorruption) { |
| 164 base::ScopedTempDir database_dir; | 175 base::ScopedTempDir database_dir; |
| 165 ASSERT_TRUE(database_dir.CreateUniqueTempDir()); | 176 ASSERT_TRUE(database_dir.CreateUniqueTempDir()); |
| 166 | 177 |
| 167 scoped_ptr<NotificationDatabase> database( | 178 scoped_ptr<NotificationDatabase> database( |
| 168 CreateDatabaseOnFileSystem(database_dir.path())); | 179 CreateDatabaseOnFileSystem(database_dir.path())); |
| 169 | 180 |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 EXPECT_EQ(notification_data.tag, read_notification_data.tag); | 309 EXPECT_EQ(notification_data.tag, read_notification_data.tag); |
| 299 EXPECT_EQ(notification_data.icon, read_notification_data.icon); | 310 EXPECT_EQ(notification_data.icon, read_notification_data.icon); |
| 300 EXPECT_EQ(notification_data.silent, read_notification_data.silent); | 311 EXPECT_EQ(notification_data.silent, read_notification_data.silent); |
| 301 } | 312 } |
| 302 | 313 |
| 303 TEST_F(NotificationDatabaseTest, ReadWriteMultipleNotificationData) { | 314 TEST_F(NotificationDatabaseTest, ReadWriteMultipleNotificationData) { |
| 304 scoped_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); | 315 scoped_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); |
| 305 ASSERT_EQ(NotificationDatabase::STATUS_OK, | 316 ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 306 database->Open(true /* create_if_missing */)); | 317 database->Open(true /* create_if_missing */)); |
| 307 | 318 |
| 308 NotificationDatabaseData database_data; | |
| 309 GURL origin("https://example.com"); | 319 GURL origin("https://example.com"); |
| 320 int64_t notification_id = 0; |
| 310 | 321 |
| 311 // Write ten notifications to the database, each with a unique title and | 322 // Write ten notifications to the database, each with a unique title and |
| 312 // notification id (it is the responsibility of the user to increment this). | 323 // notification id (it is the responsibility of the user to increment this). |
| 313 for (int i = 1; i <= 10; ++i) { | 324 for (int i = 1; i <= 10; ++i) { |
| 314 database_data.notification_id = i; | 325 ASSERT_NO_FATAL_FAILURE(CreateAndWriteNotification( |
| 315 database_data.notification_data.title = base::IntToString16(i); | 326 database.get(), origin, i /* sw_registration_id */, ¬ification_id)); |
| 316 | |
| 317 int64_t notification_id = 0; | |
| 318 ASSERT_EQ(NotificationDatabase::STATUS_OK, | |
| 319 database->WriteNotificationData(origin, | |
| 320 database_data, | |
| 321 ¬ification_id)); | |
| 322 EXPECT_EQ(notification_id, i); | 327 EXPECT_EQ(notification_id, i); |
| 323 } | 328 } |
| 324 | 329 |
| 330 NotificationDatabaseData database_data; |
| 331 |
| 325 // Read the ten notifications from the database, and verify that the titles | 332 // Read the ten notifications from the database, and verify that the titles |
| 326 // of each of them matches with how they were created. | 333 // of each of them matches with how they were created. |
| 327 for (int i = 1; i <= 10; ++i) { | 334 for (int i = 1; i <= 10; ++i) { |
| 328 ASSERT_EQ(NotificationDatabase::STATUS_OK, | 335 ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 329 database->ReadNotificationData(i /* notification_id */, | 336 database->ReadNotificationData(i /* notification_id */, |
| 330 origin, | 337 origin, |
| 331 &database_data)); | 338 &database_data)); |
| 332 | 339 |
| 333 EXPECT_EQ(base::IntToString16(i), database_data.notification_data.title); | 340 EXPECT_EQ(i, database_data.service_worker_registration_id); |
| 334 } | 341 } |
| 335 } | 342 } |
| 336 | 343 |
| 337 TEST_F(NotificationDatabaseTest, DeleteInvalidNotificationData) { | 344 TEST_F(NotificationDatabaseTest, DeleteInvalidNotificationData) { |
| 338 scoped_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); | 345 scoped_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); |
| 339 ASSERT_EQ(NotificationDatabase::STATUS_OK, | 346 ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 340 database->Open(true /* create_if_missing */)); | 347 database->Open(true /* create_if_missing */)); |
| 341 | 348 |
| 342 // Deleting non-existing notifications is not considered to be a failure. | 349 // Deleting non-existing notifications is not considered to be a failure. |
| 343 ASSERT_EQ(NotificationDatabase::STATUS_OK, | 350 ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 EXPECT_EQ(NotificationDatabase::STATUS_OK, | 405 EXPECT_EQ(NotificationDatabase::STATUS_OK, |
| 399 database->DeleteNotificationData(notification_id, | 406 database->DeleteNotificationData(notification_id, |
| 400 GURL("https://chrome.com"))); | 407 GURL("https://chrome.com"))); |
| 401 | 408 |
| 402 EXPECT_EQ(NotificationDatabase::STATUS_OK, | 409 EXPECT_EQ(NotificationDatabase::STATUS_OK, |
| 403 database->ReadNotificationData(notification_id, | 410 database->ReadNotificationData(notification_id, |
| 404 origin, | 411 origin, |
| 405 &database_data)); | 412 &database_data)); |
| 406 } | 413 } |
| 407 | 414 |
| 415 TEST_F(NotificationDatabaseTest, ReadAllServiceWorkerNotificationData) { |
| 416 scoped_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); |
| 417 ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 418 database->Open(true /* create_if_missing */)); |
| 419 |
| 420 GURL origin("https://example.com"); |
| 421 |
| 422 int64_t service_worker_registration_id = 42; |
| 423 int64_t notification_id = 0; |
| 424 |
| 425 // Write ten notifications belonging to |origin| to the database. |
| 426 for (int i = 0; i < 10; ++i) { |
| 427 ASSERT_NO_FATAL_FAILURE(CreateAndWriteNotification( |
| 428 database.get(), |
| 429 origin, |
| 430 service_worker_registration_id, |
| 431 ¬ification_id)); |
| 432 } |
| 433 |
| 434 // Write one notification not belonging to |origin| to the database, but with |
| 435 // the same Service Worker registration id. |
| 436 ASSERT_NO_FATAL_FAILURE(CreateAndWriteNotification( |
| 437 database.get(), |
| 438 GURL("https://foobar.com"), |
| 439 service_worker_registration_id, |
| 440 ¬ification_id)); |
| 441 |
| 442 // Write one notification belonging to |origin| to the database, but with a |
| 443 // different Service Worker registration id. |
| 444 ASSERT_NO_FATAL_FAILURE(CreateAndWriteNotification( |
| 445 database.get(), |
| 446 origin, |
| 447 service_worker_registration_id + 1, |
| 448 ¬ification_id)); |
| 449 |
| 450 // Read all notifications from the database which belong to |origin| and |
| 451 // |service_worker_registration_id|. There should be ten. |
| 452 std::vector<NotificationDatabaseData> notifications; |
| 453 |
| 454 ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 455 database->ReadAllNotificationDataForServiceWorkerRegistration( |
| 456 origin, |
| 457 service_worker_registration_id, |
| 458 ¬ifications)); |
| 459 |
| 460 EXPECT_EQ(10u, notifications.size()); |
| 461 } |
| 462 |
| 463 TEST_F(NotificationDatabaseTest, ReadAllNotificationDataAfterDelete) { |
| 464 scoped_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); |
| 465 ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 466 database->Open(true /* create_if_missing */)); |
| 467 |
| 468 GURL origin("https://example.com"); |
| 469 |
| 470 int64_t service_worker_registration_id = 42; |
| 471 int64_t notification_id = 0; |
| 472 |
| 473 // Write ten notifications belonging to |origin| to the database. |
| 474 for (int i = 0; i < 10; ++i) { |
| 475 ASSERT_NO_FATAL_FAILURE(CreateAndWriteNotification( |
| 476 database.get(), |
| 477 origin, |
| 478 service_worker_registration_id, |
| 479 ¬ification_id)); |
| 480 } |
| 481 |
| 482 // Remove the most recent notification from the database. |
| 483 ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 484 database->DeleteNotificationData(notification_id, origin)); |
| 485 |
| 486 // Make sure that only nine notifications remain. |
| 487 std::vector<NotificationDatabaseData> notifications; |
| 488 |
| 489 ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 490 database->ReadAllNotificationDataForServiceWorkerRegistration( |
| 491 origin, |
| 492 service_worker_registration_id, |
| 493 ¬ifications)); |
| 494 |
| 495 EXPECT_EQ(9u, notifications.size()); |
| 496 } |
| 497 |
| 498 TEST_F(NotificationDatabaseTest, DeleteAllServiceWorkerNotificationData) { |
| 499 scoped_ptr<NotificationDatabase> database(CreateDatabaseInMemory()); |
| 500 ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 501 database->Open(true /* create_if_missing */)); |
| 502 |
| 503 GURL origin("https://example.com"); |
| 504 |
| 505 int64_t service_worker_registration_id = 42; |
| 506 int64_t notification_id = 0; |
| 507 |
| 508 // Write ten notifications belonging to |origin| to the database. |
| 509 for (int i = 0; i < 10; ++i) { |
| 510 ASSERT_NO_FATAL_FAILURE(CreateAndWriteNotification( |
| 511 database.get(), |
| 512 origin, |
| 513 service_worker_registration_id, |
| 514 ¬ification_id)); |
| 515 } |
| 516 |
| 517 // Write one notification to the database with a different Service Worker |
| 518 // registration id, but still the same origin. |
| 519 ASSERT_NO_FATAL_FAILURE(CreateAndWriteNotification( |
| 520 database.get(), |
| 521 origin, |
| 522 service_worker_registration_id + 1, |
| 523 ¬ification_id)); |
| 524 |
| 525 std::set<int64_t> deleted_notification_ids; |
| 526 |
| 527 // Delete all the notifications from the database which used to belong to the |
| 528 // origin and Service Worker registration id. Ten should be deleted. |
| 529 ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 530 database->DeleteAllNotificationDataForServiceWorkerRegistration( |
| 531 origin, |
| 532 service_worker_registration_id, |
| 533 &deleted_notification_ids)); |
| 534 |
| 535 EXPECT_EQ(10u, deleted_notification_ids.size()); |
| 536 |
| 537 std::vector<NotificationDatabaseData> notifications; |
| 538 |
| 539 // Make sure that all the notifications which are said to be deleted actually |
| 540 // have been removed from the database. |
| 541 ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 542 database->ReadAllNotificationDataForServiceWorkerRegistration( |
| 543 origin, |
| 544 service_worker_registration_id, |
| 545 ¬ifications)); |
| 546 |
| 547 EXPECT_EQ(0u, notifications.size()); |
| 548 |
| 549 NotificationDatabaseData database_data; |
| 550 |
| 551 // Make sure that the non-associated notification still exists. |
| 552 ASSERT_EQ(NotificationDatabase::STATUS_OK, |
| 553 database->ReadNotificationData(notification_id, |
| 554 origin, |
| 555 &database_data)); |
| 556 } |
| 557 |
| 408 } // namespace content | 558 } // namespace content |
| OLD | NEW |