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