Chromium Code Reviews| 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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 87 void PlatformNotificationContextImpl::Shutdown() { | 92 void PlatformNotificationContextImpl::Shutdown() { |
| 88 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 93 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 89 BrowserThread::PostTask( | 94 BrowserThread::PostTask( |
| 90 BrowserThread::IO, FROM_HERE, | 95 BrowserThread::IO, FROM_HERE, |
| 91 base::Bind(&PlatformNotificationContextImpl::ShutdownOnIO, this)); | 96 base::Bind(&PlatformNotificationContextImpl::ShutdownOnIO, this)); |
| 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 |
| 102 services_.clear(); | |
| 103 | |
| 97 // |service_worker_context_| may be NULL in tests. | 104 // |service_worker_context_| may be NULL in tests. |
| 98 if (service_worker_context_) | 105 if (service_worker_context_) |
| 99 service_worker_context_->RemoveObserver(this); | 106 service_worker_context_->RemoveObserver(this); |
| 100 } | 107 } |
| 101 | 108 |
| 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, | |
|
yzshen1
2016/05/18 15:28:56
Not sure whether it is relevant to the hang that y
| |
| 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 | |
| 102 void PlatformNotificationContextImpl::ReadNotificationData( | 142 void PlatformNotificationContextImpl::ReadNotificationData( |
| 103 int64_t notification_id, | 143 int64_t notification_id, |
| 104 const GURL& origin, | 144 const GURL& origin, |
| 105 const ReadResultCallback& callback) { | 145 const ReadResultCallback& callback) { |
| 106 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 146 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 107 LazyInitialize( | 147 LazyInitialize( |
| 108 base::Bind(&PlatformNotificationContextImpl::DoReadNotificationData, this, | 148 base::Bind(&PlatformNotificationContextImpl::DoReadNotificationData, this, |
| 109 notification_id, origin, callback), | 149 notification_id, origin, callback), |
| 110 base::Bind(callback, false /* success */, NotificationDatabaseData())); | 150 base::Bind(callback, false /* success */, NotificationDatabaseData())); |
| 111 } | 151 } |
| (...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 402 | 442 |
| 403 return path_.Append(kPlatformNotificationsDirectory); | 443 return path_.Append(kPlatformNotificationsDirectory); |
| 404 } | 444 } |
| 405 | 445 |
| 406 void PlatformNotificationContextImpl::SetTaskRunnerForTesting( | 446 void PlatformNotificationContextImpl::SetTaskRunnerForTesting( |
| 407 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { | 447 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { |
| 408 task_runner_ = task_runner; | 448 task_runner_ = task_runner; |
| 409 } | 449 } |
| 410 | 450 |
| 411 } // namespace content | 451 } // namespace content |
| OLD | NEW |