| 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/platform_notification_context_impl.h" | 5 #include "content/browser/notifications/platform_notification_context_impl.h" |
| 6 | 6 |
| 7 #include "base/bind_helpers.h" | 7 #include "base/bind_helpers.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/metrics/histogram_macros.h" | 9 #include "base/metrics/histogram_macros.h" |
| 10 #include "base/threading/sequenced_worker_pool.h" | 10 #include "base/threading/sequenced_worker_pool.h" |
| 11 #include "content/browser/notifications/notification_database.h" | 11 #include "content/browser/notifications/notification_database.h" |
| 12 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 12 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 13 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
| 14 #include "content/public/browser/content_browser_client.h" |
| 14 #include "content/public/browser/notification_database_data.h" | 15 #include "content/public/browser/notification_database_data.h" |
| 16 #include "content/public/browser/platform_notification_service.h" |
| 15 | 17 |
| 16 using base::DoNothing; | 18 using base::DoNothing; |
| 17 | 19 |
| 18 namespace content { | 20 namespace content { |
| 19 | 21 |
| 20 // Name of the directory in the user's profile directory where the notification | 22 // Name of the directory in the user's profile directory where the notification |
| 21 // database files should be stored. | 23 // database files should be stored. |
| 22 const base::FilePath::CharType kPlatformNotificationsDirectory[] = | 24 const base::FilePath::CharType kPlatformNotificationsDirectory[] = |
| 23 FILE_PATH_LITERAL("Platform Notifications"); | 25 FILE_PATH_LITERAL("Platform Notifications"); |
| 24 | 26 |
| 25 PlatformNotificationContextImpl::PlatformNotificationContextImpl( | 27 PlatformNotificationContextImpl::PlatformNotificationContextImpl( |
| 26 const base::FilePath& path, | 28 const base::FilePath& path, |
| 29 BrowserContext* browser_context, |
| 27 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context) | 30 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context) |
| 28 : path_(path), | 31 : path_(path), |
| 32 browser_context_(browser_context), |
| 29 service_worker_context_(service_worker_context) { | 33 service_worker_context_(service_worker_context) { |
| 30 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 34 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 31 } | 35 } |
| 32 | 36 |
| 33 PlatformNotificationContextImpl::~PlatformNotificationContextImpl() { | 37 PlatformNotificationContextImpl::~PlatformNotificationContextImpl() { |
| 34 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 38 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 35 | 39 |
| 36 // If the database has been initialized, it must be deleted on the task runner | 40 // If the database has been initialized, it must be deleted on the task runner |
| 37 // thread as closing it may cause file I/O. | 41 // thread as closing it may cause file I/O. |
| 38 if (database_) { | 42 if (database_) { |
| 39 DCHECK(task_runner_); | 43 DCHECK(task_runner_); |
| 40 task_runner_->DeleteSoon(FROM_HERE, database_.release()); | 44 task_runner_->DeleteSoon(FROM_HERE, database_.release()); |
| 41 } | 45 } |
| 42 } | 46 } |
| 43 | 47 |
| 44 void PlatformNotificationContextImpl::Initialize() { | 48 void PlatformNotificationContextImpl::Initialize() { |
| 45 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 49 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 50 PlatformNotificationService* service = |
| 51 GetContentClient()->browser()->GetPlatformNotificationService(); |
| 52 if (service) { |
| 53 std::set<std::string> displayed_notifications; |
| 54 |
| 55 bool notification_synchronization_supported = |
| 56 service->GetDisplayedPersistentNotifications(browser_context_, |
| 57 &displayed_notifications); |
| 58 |
| 59 // Synchronize the notifications stored in the database with the set of |
| 60 // displaying notifications in |displayed_notifications|. This is necessary |
| 61 // because flakiness may cause a platform to inform Chrome of a notification |
| 62 // that has since been closed, or because the platform does not support |
| 63 // notifications that exceed the lifetime of the browser process. |
| 64 |
| 65 // TODO(peter): Synchronizing the actual notifications will be done when the |
| 66 // persistent notification ids are stable. For M44 we need to support the |
| 67 // case where there may be no notifications after a Chrome restart. |
| 68 if (notification_synchronization_supported && |
| 69 !displayed_notifications.size()) { |
| 70 prune_database_on_open_ = true; |
| 71 } |
| 72 } |
| 73 |
| 46 BrowserThread::PostTask( | 74 BrowserThread::PostTask( |
| 47 BrowserThread::IO, | 75 BrowserThread::IO, |
| 48 FROM_HERE, | 76 FROM_HERE, |
| 49 base::Bind(&PlatformNotificationContextImpl::InitializeOnIO, this)); | 77 base::Bind(&PlatformNotificationContextImpl::InitializeOnIO, this)); |
| 50 } | 78 } |
| 51 | 79 |
| 52 void PlatformNotificationContextImpl::InitializeOnIO() { | 80 void PlatformNotificationContextImpl::InitializeOnIO() { |
| 53 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 81 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 54 | 82 |
| 55 // |service_worker_context_| may be NULL in tests. | 83 // |service_worker_context_| may be NULL in tests. |
| (...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 323 return; | 351 return; |
| 324 } | 352 } |
| 325 | 353 |
| 326 database_.reset(new NotificationDatabase(GetDatabasePath())); | 354 database_.reset(new NotificationDatabase(GetDatabasePath())); |
| 327 NotificationDatabase::Status status = | 355 NotificationDatabase::Status status = |
| 328 database_->Open(true /* create_if_missing */); | 356 database_->Open(true /* create_if_missing */); |
| 329 | 357 |
| 330 UMA_HISTOGRAM_ENUMERATION("Notifications.Database.OpenResult", | 358 UMA_HISTOGRAM_ENUMERATION("Notifications.Database.OpenResult", |
| 331 status, NotificationDatabase::STATUS_COUNT); | 359 status, NotificationDatabase::STATUS_COUNT); |
| 332 | 360 |
| 361 // TODO(peter): Do finer-grained synchronization here. |
| 362 if (prune_database_on_open_) { |
| 363 prune_database_on_open_ = false; |
| 364 DestroyDatabase(); |
| 365 |
| 366 database_.reset(new NotificationDatabase(GetDatabasePath())); |
| 367 status = database_->Open(true /* create_if_missing */); |
| 368 |
| 369 // TODO(peter): Find the appropriate UMA to cover in regards to |
| 370 // synchronizing notifications after the implementation is complete. |
| 371 } |
| 372 |
| 333 // When the database could not be opened due to corruption, destroy it, blow | 373 // When the database could not be opened due to corruption, destroy it, blow |
| 334 // away the contents of the directory and try re-opening the database. | 374 // away the contents of the directory and try re-opening the database. |
| 335 if (status == NotificationDatabase::STATUS_ERROR_CORRUPTED) { | 375 if (status == NotificationDatabase::STATUS_ERROR_CORRUPTED) { |
| 336 if (DestroyDatabase()) { | 376 if (DestroyDatabase()) { |
| 377 database_.reset(new NotificationDatabase(GetDatabasePath())); |
| 337 status = database_->Open(true /* create_if_missing */); | 378 status = database_->Open(true /* create_if_missing */); |
| 338 | 379 |
| 339 UMA_HISTOGRAM_ENUMERATION( | 380 UMA_HISTOGRAM_ENUMERATION( |
| 340 "Notifications.Database.OpenAfterCorruptionResult", | 381 "Notifications.Database.OpenAfterCorruptionResult", |
| 341 status, NotificationDatabase::STATUS_COUNT); | 382 status, NotificationDatabase::STATUS_COUNT); |
| 342 } | 383 } |
| 343 } | 384 } |
| 344 | 385 |
| 345 if (status == NotificationDatabase::STATUS_OK) { | 386 if (status == NotificationDatabase::STATUS_OK) { |
| 346 success_closure.Run(); | 387 success_closure.Run(); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 379 | 420 |
| 380 return path_.Append(kPlatformNotificationsDirectory); | 421 return path_.Append(kPlatformNotificationsDirectory); |
| 381 } | 422 } |
| 382 | 423 |
| 383 void PlatformNotificationContextImpl::SetTaskRunnerForTesting( | 424 void PlatformNotificationContextImpl::SetTaskRunnerForTesting( |
| 384 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { | 425 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { |
| 385 task_runner_ = task_runner; | 426 task_runner_ = task_runner; |
| 386 } | 427 } |
| 387 | 428 |
| 388 } // namespace content | 429 } // namespace content |
| OLD | NEW |