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