| 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/stl_util.h" |
| 10 #include "base/threading/sequenced_worker_pool.h" | 11 #include "base/threading/sequenced_worker_pool.h" |
| 12 #include "content/browser/notifications/blink_notification_service_impl.h" |
| 11 #include "content/browser/notifications/notification_database.h" | 13 #include "content/browser/notifications/notification_database.h" |
| 12 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 14 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 15 #include "content/public/browser/browser_context.h" |
| 13 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
| 14 #include "content/public/browser/content_browser_client.h" | 17 #include "content/public/browser/content_browser_client.h" |
| 15 #include "content/public/browser/notification_database_data.h" | 18 #include "content/public/browser/notification_database_data.h" |
| 16 #include "content/public/browser/platform_notification_service.h" | 19 #include "content/public/browser/platform_notification_service.h" |
| 17 | 20 |
| 18 using base::DoNothing; | 21 using base::DoNothing; |
| 19 | 22 |
| 20 namespace content { | 23 namespace content { |
| 21 | 24 |
| 22 // Name of the directory in the user's profile directory where the notification | 25 // Name of the directory in the user's profile directory where the notification |
| 23 // database files should be stored. | 26 // database files should be stored. |
| 24 const base::FilePath::CharType kPlatformNotificationsDirectory[] = | 27 const base::FilePath::CharType kPlatformNotificationsDirectory[] = |
| 25 FILE_PATH_LITERAL("Platform Notifications"); | 28 FILE_PATH_LITERAL("Platform Notifications"); |
| 26 | 29 |
| 27 PlatformNotificationContextImpl::PlatformNotificationContextImpl( | 30 PlatformNotificationContextImpl::PlatformNotificationContextImpl( |
| 28 const base::FilePath& path, | 31 const base::FilePath& path, |
| 29 BrowserContext* browser_context, | 32 BrowserContext* browser_context, |
| 30 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context) | 33 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context) |
| 31 : path_(path), | 34 : path_(path), |
| 32 browser_context_(browser_context), | 35 browser_context_(browser_context), |
| 33 service_worker_context_(service_worker_context) { | 36 service_worker_context_(service_worker_context), |
| 37 weak_factory_to_io_(this) { |
| 34 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 38 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 35 } | 39 } |
| 36 | 40 |
| 37 PlatformNotificationContextImpl::~PlatformNotificationContextImpl() { | 41 PlatformNotificationContextImpl::~PlatformNotificationContextImpl() { |
| 38 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 42 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 39 | 43 |
| 40 // If the database has been initialized, it must be deleted on the task runner | 44 // If the database has been initialized, it must be deleted on the task runner |
| 41 // thread as closing it may cause file I/O. | 45 // thread as closing it may cause file I/O. |
| 42 if (database_) { | 46 if (database_) { |
| 43 DCHECK(task_runner_); | 47 DCHECK(task_runner_); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 void PlatformNotificationContextImpl::Shutdown() { | 91 void PlatformNotificationContextImpl::Shutdown() { |
| 88 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 92 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 89 BrowserThread::PostTask( | 93 BrowserThread::PostTask( |
| 90 BrowserThread::IO, FROM_HERE, | 94 BrowserThread::IO, FROM_HERE, |
| 91 base::Bind(&PlatformNotificationContextImpl::ShutdownOnIO, this)); | 95 base::Bind(&PlatformNotificationContextImpl::ShutdownOnIO, this)); |
| 92 } | 96 } |
| 93 | 97 |
| 94 void PlatformNotificationContextImpl::ShutdownOnIO() { | 98 void PlatformNotificationContextImpl::ShutdownOnIO() { |
| 95 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 99 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 96 | 100 |
| 101 STLDeleteElements(&services_); |
| 102 |
| 97 // |service_worker_context_| may be NULL in tests. | 103 // |service_worker_context_| may be NULL in tests. |
| 98 if (service_worker_context_) | 104 if (service_worker_context_) |
| 99 service_worker_context_->RemoveObserver(this); | 105 service_worker_context_->RemoveObserver(this); |
| 100 } | 106 } |
| 101 | 107 |
| 108 void PlatformNotificationContextImpl::CreateService( |
| 109 int render_process_id, |
| 110 mojo::InterfaceRequest<blink::mojom::NotificationService> request) { |
| 111 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 112 BrowserThread::PostTask( |
| 113 BrowserThread::IO, FROM_HERE, |
| 114 base::Bind(&PlatformNotificationContextImpl::CreateServiceOnIO, |
| 115 weak_factory_to_io_.GetWeakPtr(), render_process_id, |
| 116 browser_context_->GetResourceContext(), |
| 117 base::Passed(&request))); |
| 118 } |
| 119 |
| 120 void PlatformNotificationContextImpl::CreateServiceOnIO( |
| 121 int render_process_id, |
| 122 ResourceContext* resource_context, |
| 123 mojo::InterfaceRequest<blink::mojom::NotificationService> request) { |
| 124 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 125 services_.insert(new BlinkNotificationServiceImpl( |
| 126 this, resource_context, render_process_id, std::move(request))); |
| 127 } |
| 128 |
| 129 void PlatformNotificationContextImpl::RemoveService( |
| 130 BlinkNotificationServiceImpl* service) { |
| 131 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 132 DCHECK(ContainsValue(services_, service)); |
| 133 |
| 134 services_.erase(service); |
| 135 delete service; |
| 136 } |
| 137 |
| 102 void PlatformNotificationContextImpl::ReadNotificationData( | 138 void PlatformNotificationContextImpl::ReadNotificationData( |
| 103 int64_t notification_id, | 139 int64_t notification_id, |
| 104 const GURL& origin, | 140 const GURL& origin, |
| 105 const ReadResultCallback& callback) { | 141 const ReadResultCallback& callback) { |
| 106 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 142 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 107 LazyInitialize( | 143 LazyInitialize( |
| 108 base::Bind(&PlatformNotificationContextImpl::DoReadNotificationData, this, | 144 base::Bind(&PlatformNotificationContextImpl::DoReadNotificationData, this, |
| 109 notification_id, origin, callback), | 145 notification_id, origin, callback), |
| 110 base::Bind(callback, false /* success */, NotificationDatabaseData())); | 146 base::Bind(callback, false /* success */, NotificationDatabaseData())); |
| 111 } | 147 } |
| (...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 402 | 438 |
| 403 return path_.Append(kPlatformNotificationsDirectory); | 439 return path_.Append(kPlatformNotificationsDirectory); |
| 404 } | 440 } |
| 405 | 441 |
| 406 void PlatformNotificationContextImpl::SetTaskRunnerForTesting( | 442 void PlatformNotificationContextImpl::SetTaskRunnerForTesting( |
| 407 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { | 443 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { |
| 408 task_runner_ = task_runner; | 444 task_runner_ = task_runner; |
| 409 } | 445 } |
| 410 | 446 |
| 411 } // namespace content | 447 } // namespace content |
| OLD | NEW |