diff options
author | kalman <kalman@chromium.org> | 2015-09-04 13:28:59 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-09-04 20:29:40 +0000 |
commit | d1ab2c352c45427399f9f0730ac87e66e319d277 (patch) | |
tree | efd4634a1a40e49f45ea0f68387b64361701f159 | |
parent | ff506f629332dbeb3efb3df73cf8ff5b168312e8 (diff) | |
download | chromium_src-d1ab2c352c45427399f9f0730ac87e66e319d277.zip chromium_src-d1ab2c352c45427399f9f0730ac87e66e319d277.tar.gz chromium_src-d1ab2c352c45427399f9f0730ac87e66e319d277.tar.bz2 |
Move WorkerTaskRunner observer interface to WorkerThread.
This moves WorkerTaskRunner::Observer to WorkerThread::Observer, and renames
WorkerTaskRunner::Add/RemoveStopObserver to
WorkerThread::DidStartCurrentWorkerThread/WillStopCurrentWorkerThread.
Some other code made sense to shuffle around while doing so.
BUG=501569
R=kinuko@chromium.org
Review URL: https://codereview.chromium.org/1307643006
Cr-Commit-Position: refs/heads/master@{#347479}
36 files changed, 287 insertions, 254 deletions
diff --git a/content/child/background_sync/background_sync_provider_thread_proxy.cc b/content/child/background_sync/background_sync_provider_thread_proxy.cc index af0e786..3d735fb 100644 --- a/content/child/background_sync/background_sync_provider_thread_proxy.cc +++ b/content/child/background_sync/background_sync_provider_thread_proxy.cc @@ -85,7 +85,7 @@ BackgroundSyncProviderThreadProxy::GetThreadInstance( if (g_sync_provider_tls.Pointer()->Get()) return g_sync_provider_tls.Pointer()->Get(); - if (!WorkerTaskRunner::Instance()->CurrentWorkerId()) { + if (!WorkerThread::GetCurrentId()) { // This could happen if GetThreadInstance is called very late (say by a // garbage collected SyncRegistration). return nullptr; @@ -94,7 +94,7 @@ BackgroundSyncProviderThreadProxy::GetThreadInstance( BackgroundSyncProviderThreadProxy* instance = new BackgroundSyncProviderThreadProxy(main_thread_task_runner, sync_provider); - WorkerTaskRunner::Instance()->AddStopObserver(instance); + WorkerThread::AddObserver(instance); return instance; } @@ -108,12 +108,12 @@ void BackgroundSyncProviderThreadProxy::registerBackgroundSync( DCHECK(callbacks); main_thread_task_runner_->PostTask( FROM_HERE, - base::Bind(&BackgroundSyncProvider::registerBackgroundSync, - base::Unretained(sync_provider_), options, - service_worker_registration, requested_from_service_worker, - new CallbackThreadAdapter<blink::WebSyncRegistrationCallbacks>( - make_scoped_ptr(callbacks), - WorkerTaskRunner::Instance()->CurrentWorkerId()))); + base::Bind( + &BackgroundSyncProvider::registerBackgroundSync, + base::Unretained(sync_provider_), options, + service_worker_registration, requested_from_service_worker, + new CallbackThreadAdapter<blink::WebSyncRegistrationCallbacks>( + make_scoped_ptr(callbacks), WorkerThread::GetCurrentId()))); } void BackgroundSyncProviderThreadProxy::unregisterBackgroundSync( @@ -133,8 +133,7 @@ void BackgroundSyncProviderThreadProxy::unregisterBackgroundSync( // for thread-safety. static_cast<base::string16>(tag), service_worker_registration, new CallbackThreadAdapter<blink::WebSyncUnregistrationCallbacks>( - make_scoped_ptr(callbacks), - WorkerTaskRunner::Instance()->CurrentWorkerId()))); + make_scoped_ptr(callbacks), WorkerThread::GetCurrentId()))); } void BackgroundSyncProviderThreadProxy::getRegistration( @@ -146,14 +145,14 @@ void BackgroundSyncProviderThreadProxy::getRegistration( DCHECK(callbacks); main_thread_task_runner_->PostTask( FROM_HERE, - base::Bind(&BackgroundSyncProvider::getRegistration, - base::Unretained(sync_provider_), periodicity, - // We cast WebString to string16 before crossing threads - // for thread-safety. - static_cast<base::string16>(tag), service_worker_registration, - new CallbackThreadAdapter<blink::WebSyncRegistrationCallbacks>( - make_scoped_ptr(callbacks), - WorkerTaskRunner::Instance()->CurrentWorkerId()))); + base::Bind( + &BackgroundSyncProvider::getRegistration, + base::Unretained(sync_provider_), periodicity, + // We cast WebString to string16 before crossing threads + // for thread-safety. + static_cast<base::string16>(tag), service_worker_registration, + new CallbackThreadAdapter<blink::WebSyncRegistrationCallbacks>( + make_scoped_ptr(callbacks), WorkerThread::GetCurrentId()))); } void BackgroundSyncProviderThreadProxy::getRegistrations( @@ -169,8 +168,7 @@ void BackgroundSyncProviderThreadProxy::getRegistrations( base::Unretained(sync_provider_), periodicity, service_worker_registration, new CallbackThreadAdapter<blink::WebSyncGetRegistrationsCallbacks>( - make_scoped_ptr(callbacks), - WorkerTaskRunner::Instance()->CurrentWorkerId()))); + make_scoped_ptr(callbacks), WorkerThread::GetCurrentId()))); } void BackgroundSyncProviderThreadProxy::getPermissionStatus( @@ -186,11 +184,10 @@ void BackgroundSyncProviderThreadProxy::getPermissionStatus( base::Unretained(sync_provider_), periodicity, service_worker_registration, new CallbackThreadAdapter<blink::WebSyncGetPermissionStatusCallbacks>( - make_scoped_ptr(callbacks), - WorkerTaskRunner::Instance()->CurrentWorkerId()))); + make_scoped_ptr(callbacks), WorkerThread::GetCurrentId()))); } -void BackgroundSyncProviderThreadProxy::OnWorkerRunLoopStopped() { +void BackgroundSyncProviderThreadProxy::WillStopCurrentWorkerThread() { delete this; } diff --git a/content/child/background_sync/background_sync_provider_thread_proxy.h b/content/child/background_sync/background_sync_provider_thread_proxy.h index 048c68f..7b6e8fe 100644 --- a/content/child/background_sync/background_sync_provider_thread_proxy.h +++ b/content/child/background_sync/background_sync_provider_thread_proxy.h @@ -7,6 +7,7 @@ #include "base/macros.h" #include "content/child/worker_task_runner.h" +#include "content/public/child/worker_thread.h" #include "third_party/WebKit/public/platform/modules/background_sync/WebSyncProvider.h" namespace base { @@ -25,7 +26,7 @@ class BackgroundSyncProvider; // call the WebSyncProvider methods, and wrapping the callbacks passed in with // code to switch back to the original calling thread. class BackgroundSyncProviderThreadProxy : public blink::WebSyncProvider, - public WorkerTaskRunner::Observer { + public WorkerThread::Observer { public: static BackgroundSyncProviderThreadProxy* GetThreadInstance( base::SingleThreadTaskRunner* main_thread_task_runner, @@ -57,8 +58,8 @@ class BackgroundSyncProviderThreadProxy : public blink::WebSyncProvider, blink::WebServiceWorkerRegistration* service_worker_registration, blink::WebSyncGetPermissionStatusCallbacks* callbacks); - // WorkerTaskRunner::Observer implementation. - void OnWorkerRunLoopStopped() override; + // WorkerThread::Observer implementation. + void WillStopCurrentWorkerThread() override; private: BackgroundSyncProviderThreadProxy( diff --git a/content/child/blink_platform_impl.cc b/content/child/blink_platform_impl.cc index 1ff9675..4e7615f 100644 --- a/content/child/blink_platform_impl.cc +++ b/content/child/blink_platform_impl.cc @@ -1144,12 +1144,15 @@ blink::WebGestureCurve* BlinkPlatformImpl::createFlingAnimationCurve( void BlinkPlatformImpl::didStartWorkerRunLoop() { WorkerTaskRunner* worker_task_runner = WorkerTaskRunner::Instance(); - worker_task_runner->OnWorkerRunLoopStarted(); + worker_task_runner->DidStartWorkerRunLoop(); } void BlinkPlatformImpl::didStopWorkerRunLoop() { + // TODO(kalman): blink::Platform::didStopWorkerRunLoop should be called + // willStopWorkerRunLoop, because at this point the run loop hasn't been + // stopped. WillStopWorkerRunLoop is the correct name. WorkerTaskRunner* worker_task_runner = WorkerTaskRunner::Instance(); - worker_task_runner->OnWorkerRunLoopStopped(); + worker_task_runner->WillStopWorkerRunLoop(); } blink::WebCrypto* BlinkPlatformImpl::crypto() { diff --git a/content/child/fileapi/webfilesystem_impl.cc b/content/child/fileapi/webfilesystem_impl.cc index 9aba734..39f7481 100644 --- a/content/child/fileapi/webfilesystem_impl.cc +++ b/content/child/fileapi/webfilesystem_impl.cc @@ -382,13 +382,13 @@ WebFileSystemImpl* WebFileSystemImpl::ThreadSpecificInstance( return g_webfilesystem_tls.Pointer()->Get(); WebFileSystemImpl* filesystem = new WebFileSystemImpl(main_thread_task_runner); - if (WorkerTaskRunner::Instance()->CurrentWorkerId()) - WorkerTaskRunner::Instance()->AddStopObserver(filesystem); + if (WorkerThread::GetCurrentId()) + WorkerThread::AddObserver(filesystem); return filesystem; } void WebFileSystemImpl::DeleteThreadSpecificInstance() { - DCHECK(!WorkerTaskRunner::Instance()->CurrentWorkerId()); + DCHECK(!WorkerThread::GetCurrentId()); if (g_webfilesystem_tls.Pointer()->Get()) delete g_webfilesystem_tls.Pointer()->Get(); } @@ -404,7 +404,7 @@ WebFileSystemImpl::~WebFileSystemImpl() { g_webfilesystem_tls.Pointer()->Set(NULL); } -void WebFileSystemImpl::OnWorkerRunLoopStopped() { +void WebFileSystemImpl::WillStopCurrentWorkerThread() { delete this; } diff --git a/content/child/fileapi/webfilesystem_impl.h b/content/child/fileapi/webfilesystem_impl.h index 66c7826..e9d6cc8 100644 --- a/content/child/fileapi/webfilesystem_impl.h +++ b/content/child/fileapi/webfilesystem_impl.h @@ -11,7 +11,7 @@ #include "base/compiler_specific.h" #include "base/memory/ref_counted.h" #include "base/threading/non_thread_safe.h" -#include "content/child/worker_task_runner.h" +#include "content/public/child/worker_thread.h" #include "third_party/WebKit/public/platform/WebFileSystem.h" namespace base { @@ -28,7 +28,7 @@ class WebFileWriterClient; namespace content { class WebFileSystemImpl : public blink::WebFileSystem, - public WorkerTaskRunner::Observer, + public WorkerThread::Observer, public base::NonThreadSafe { public: class WaitableCallbackResults; @@ -41,7 +41,7 @@ class WebFileSystemImpl : public blink::WebFileSystem, main_thread_task_runner); // Deletes thread-specific instance (if exists). For workers it deletes - // itself in OnWorkerRunLoopStopped(), but for an instance created on the + // itself in WillStopCurrentWorkerThread(), but for an instance created on the // main thread this method must be called. static void DeleteThreadSpecificInstance(); @@ -50,8 +50,8 @@ class WebFileSystemImpl : public blink::WebFileSystem, main_thread_task_runner); virtual ~WebFileSystemImpl(); - // WorkerTaskRunner::Observer implementation. - void OnWorkerRunLoopStopped() override; + // WorkerThread::Observer implementation. + void WillStopCurrentWorkerThread() override; // WebFileSystem implementation. virtual void openFileSystem( diff --git a/content/child/fileapi/webfilewriter_impl.cc b/content/child/fileapi/webfilewriter_impl.cc index ef16def..701e706 100644 --- a/content/child/fileapi/webfilewriter_impl.cc +++ b/content/child/fileapi/webfilewriter_impl.cc @@ -9,7 +9,7 @@ #include "base/thread_task_runner_handle.h" #include "content/child/child_thread_impl.h" #include "content/child/fileapi/file_system_dispatcher.h" -#include "content/child/worker_task_runner.h" +#include "content/public/child/worker_thread.h" namespace content { @@ -32,7 +32,7 @@ class WebFileWriterImpl::WriterBridge public: WriterBridge(WebFileWriterImpl::Type type) : request_id_(0), - running_on_worker_(WorkerTaskRunner::Instance()->CurrentWorkerId() > 0), + running_on_worker_(WorkerThread::GetCurrentId() > 0), task_runner_(running_on_worker_ ? base::ThreadTaskRunnerHandle::Get() : nullptr), written_bytes_(0) { diff --git a/content/child/geofencing/geofencing_dispatcher.cc b/content/child/geofencing/geofencing_dispatcher.cc index e067008..ae2f9e2 100644 --- a/content/child/geofencing/geofencing_dispatcher.cc +++ b/content/child/geofencing/geofencing_dispatcher.cc @@ -28,7 +28,7 @@ base::LazyInstance<base::ThreadLocalPointer<void>>::Leaky g_dispatcher_tls = void* const kHasBeenDeleted = reinterpret_cast<void*>(0x1); int CurrentWorkerId() { - return WorkerTaskRunner::Instance()->CurrentWorkerId(); + return WorkerThread::GetCurrentId(); } } // namespace @@ -145,8 +145,8 @@ GeofencingDispatcher* GeofencingDispatcher::GetOrCreateThreadSpecificInstance( GeofencingDispatcher* dispatcher = new GeofencingDispatcher(thread_safe_sender); - if (WorkerTaskRunner::Instance()->CurrentWorkerId()) - WorkerTaskRunner::Instance()->AddStopObserver(dispatcher); + if (WorkerThread::GetCurrentId()) + WorkerThread::AddObserver(dispatcher); return dispatcher; } @@ -217,7 +217,7 @@ void GeofencingDispatcher::OnGetRegisteredRegionsComplete( get_registered_regions_requests_.Remove(request_id); } -void GeofencingDispatcher::OnWorkerRunLoopStopped() { +void GeofencingDispatcher::WillStopCurrentWorkerThread() { delete this; } diff --git a/content/child/geofencing/geofencing_dispatcher.h b/content/child/geofencing/geofencing_dispatcher.h index 269dfe7..bd12543 100644 --- a/content/child/geofencing/geofencing_dispatcher.h +++ b/content/child/geofencing/geofencing_dispatcher.h @@ -5,11 +5,14 @@ #ifndef CONTENT_CHILD_GEOFENCING_GEOFENCING_DISPATCHER_H_ #define CONTENT_CHILD_GEOFENCING_GEOFENCING_DISPATCHER_H_ +#include <map> +#include <string> + #include "base/id_map.h" #include "base/memory/ref_counted.h" #include "base/memory/scoped_ptr.h" -#include "content/child/worker_task_runner.h" #include "content/common/geofencing_types.h" +#include "content/public/child/worker_thread.h" #include "third_party/WebKit/public/platform/WebGeofencingProvider.h" namespace base { @@ -24,7 +27,7 @@ class Message; namespace content { class ThreadSafeSender; -class GeofencingDispatcher : public WorkerTaskRunner::Observer { +class GeofencingDispatcher : public WorkerThread::Observer { public: explicit GeofencingDispatcher(ThreadSafeSender* sender); ~GeofencingDispatcher() override; @@ -76,8 +79,8 @@ class GeofencingDispatcher : public WorkerTaskRunner::Observer { GeofencingStatus status, const std::map<std::string, blink::WebCircularGeofencingRegion>& regions); - // WorkerTaskRunner::Observer implementation. - void OnWorkerRunLoopStopped() override; + // WorkerThread::Observer implementation. + void WillStopCurrentWorkerThread() override; scoped_refptr<ThreadSafeSender> thread_safe_sender_; IDMap<blink::WebGeofencingCallbacks, IDMapOwnPointer> diff --git a/content/child/indexed_db/indexed_db_dispatcher.cc b/content/child/indexed_db/indexed_db_dispatcher.cc index 3862e3b..beecc89 100644 --- a/content/child/indexed_db/indexed_db_dispatcher.cc +++ b/content/child/indexed_db/indexed_db_dispatcher.cc @@ -73,12 +73,14 @@ IndexedDBDispatcher* IndexedDBDispatcher::ThreadSpecificInstance( return g_idb_dispatcher_tls.Pointer()->Get(); IndexedDBDispatcher* dispatcher = new IndexedDBDispatcher(thread_safe_sender); - if (WorkerTaskRunner::Instance()->CurrentWorkerId()) - WorkerTaskRunner::Instance()->AddStopObserver(dispatcher); + if (WorkerThread::GetCurrentId()) + WorkerThread::AddObserver(dispatcher); return dispatcher; } -void IndexedDBDispatcher::OnWorkerRunLoopStopped() { delete this; } +void IndexedDBDispatcher::WillStopCurrentWorkerThread() { + delete this; +} WebIDBMetadata IndexedDBDispatcher::ConvertMetadata( const IndexedDBDatabaseMetadata& idb_metadata) { diff --git a/content/child/indexed_db/indexed_db_dispatcher.h b/content/child/indexed_db/indexed_db_dispatcher.h index 854878d..67a11ab 100644 --- a/content/child/indexed_db/indexed_db_dispatcher.h +++ b/content/child/indexed_db/indexed_db_dispatcher.h @@ -13,9 +13,9 @@ #include "base/id_map.h" #include "base/memory/ref_counted.h" #include "base/strings/nullable_string16.h" -#include "content/child/worker_task_runner.h" #include "content/common/content_export.h" #include "content/common/indexed_db/indexed_db_constants.h" +#include "content/public/child/worker_thread.h" #include "ipc/ipc_sync_message_filter.h" #include "third_party/WebKit/public/platform/WebBlobInfo.h" #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBCallbacks.h" @@ -44,7 +44,7 @@ class ThreadSafeSender; // Handle the indexed db related communication for this context thread - the // main thread and each worker thread have their own copies. -class CONTENT_EXPORT IndexedDBDispatcher : public WorkerTaskRunner::Observer { +class CONTENT_EXPORT IndexedDBDispatcher : public WorkerThread::Observer { public: // Constructor made public to allow RenderThreadImpl to own a copy without // failing a NOTREACHED in ThreadSpecificInstance in tests that instantiate @@ -58,8 +58,8 @@ class CONTENT_EXPORT IndexedDBDispatcher : public WorkerTaskRunner::Observer { static IndexedDBDispatcher* ThreadSpecificInstance( ThreadSafeSender* thread_safe_sender); - // WorkerTaskRunner::Observer implementation. - void OnWorkerRunLoopStopped() override; + // WorkerThread::Observer implementation. + void WillStopCurrentWorkerThread() override; static blink::WebIDBMetadata ConvertMetadata( const IndexedDBDatabaseMetadata& idb_metadata); @@ -188,9 +188,7 @@ class CONTENT_EXPORT IndexedDBDispatcher : public WorkerTaskRunner::Observer { enum { kAllCursors = -1 }; - static int32 CurrentWorkerId() { - return WorkerTaskRunner::Instance()->CurrentWorkerId(); - } + static int32 CurrentWorkerId() { return WorkerThread::GetCurrentId(); } template <typename T> void init_params(T* params, blink::WebIDBCallbacks* callbacks_ptr) { diff --git a/content/child/navigator_connect/service_port_dispatcher_impl.cc b/content/child/navigator_connect/service_port_dispatcher_impl.cc index ec74025..977f2b9 100644 --- a/content/child/navigator_connect/service_port_dispatcher_impl.cc +++ b/content/child/navigator_connect/service_port_dispatcher_impl.cc @@ -46,17 +46,17 @@ void ServicePortDispatcherImpl::Create( } ServicePortDispatcherImpl::~ServicePortDispatcherImpl() { - WorkerTaskRunner::Instance()->RemoveStopObserver(this); + WorkerThread::RemoveObserver(this); } ServicePortDispatcherImpl::ServicePortDispatcherImpl( base::WeakPtr<blink::WebServiceWorkerContextProxy> proxy, mojo::InterfaceRequest<ServicePortDispatcher> request) : binding_(this, request.Pass()), proxy_(proxy) { - WorkerTaskRunner::Instance()->AddStopObserver(this); + WorkerThread::AddObserver(this); } -void ServicePortDispatcherImpl::OnWorkerRunLoopStopped() { +void ServicePortDispatcherImpl::WillStopCurrentWorkerThread() { delete this; } diff --git a/content/child/navigator_connect/service_port_dispatcher_impl.h b/content/child/navigator_connect/service_port_dispatcher_impl.h index 079e077..a03ae71 100644 --- a/content/child/navigator_connect/service_port_dispatcher_impl.h +++ b/content/child/navigator_connect/service_port_dispatcher_impl.h @@ -7,8 +7,8 @@ #include "base/macros.h" #include "base/memory/weak_ptr.h" -#include "content/child/worker_task_runner.h" #include "content/common/service_port_service.mojom.h" +#include "content/public/child/worker_thread.h" #include "third_party/mojo/src/mojo/public/cpp/bindings/strong_binding.h" namespace blink { @@ -23,7 +23,7 @@ namespace content { // channel and the lifetime of the worker thread. If either the channel is // disconnected or the worker thread stops the instance deletes itself. class ServicePortDispatcherImpl : public ServicePortDispatcher, - public WorkerTaskRunner::Observer { + public WorkerThread::Observer { public: static void Create(base::WeakPtr<blink::WebServiceWorkerContextProxy> proxy, mojo::InterfaceRequest<ServicePortDispatcher> request); @@ -35,8 +35,8 @@ class ServicePortDispatcherImpl : public ServicePortDispatcher, base::WeakPtr<blink::WebServiceWorkerContextProxy> proxy, mojo::InterfaceRequest<ServicePortDispatcher> request); - // WorkerTaskRunner::Observer implementation. - void OnWorkerRunLoopStopped() override; + // WorkerThread::Observer implementation. + void WillStopCurrentWorkerThread() override; // ServicePortDispatcher implementation. void Connect(const mojo::String& target_url, diff --git a/content/child/notifications/notification_manager.cc b/content/child/notifications/notification_manager.cc index 19ac1c3..920cba7 100644 --- a/content/child/notifications/notification_manager.cc +++ b/content/child/notifications/notification_manager.cc @@ -15,7 +15,6 @@ #include "content/child/notifications/notification_dispatcher.h" #include "content/child/service_worker/web_service_worker_registration_impl.h" #include "content/child/thread_safe_sender.h" -#include "content/child/worker_task_runner.h" #include "content/common/notification_constants.h" #include "content/public/common/platform_notification_data.h" #include "third_party/WebKit/public/platform/WebSecurityOrigin.h" @@ -28,7 +27,7 @@ namespace content { namespace { int CurrentWorkerId() { - return WorkerTaskRunner::Instance()->CurrentWorkerId(); + return WorkerThread::GetCurrentId(); } } // namespace @@ -60,11 +59,11 @@ NotificationManager* NotificationManager::ThreadSpecificInstance( NotificationManager* manager = new NotificationManager( thread_safe_sender, main_thread_task_runner, notification_dispatcher); if (CurrentWorkerId()) - WorkerTaskRunner::Instance()->AddStopObserver(manager); + WorkerThread::AddObserver(manager); return manager; } -void NotificationManager::OnWorkerRunLoopStopped() { +void NotificationManager::WillStopCurrentWorkerThread() { delete this; } diff --git a/content/child/notifications/notification_manager.h b/content/child/notifications/notification_manager.h index 5aed100..962ba35 100644 --- a/content/child/notifications/notification_manager.h +++ b/content/child/notifications/notification_manager.h @@ -14,8 +14,8 @@ #include "base/single_thread_task_runner.h" #include "content/child/notifications/notification_dispatcher.h" #include "content/child/notifications/pending_notifications_tracker.h" -#include "content/child/worker_task_runner.h" #include "content/common/platform_notification_messages.h" +#include "content/public/child/worker_thread.h" #include "third_party/WebKit/public/platform/modules/notifications/WebNotificationManager.h" class SkBitmap; @@ -26,7 +26,7 @@ struct PlatformNotificationData; class ThreadSafeSender; class NotificationManager : public blink::WebNotificationManager, - public WorkerTaskRunner::Observer { + public WorkerThread::Observer { public: ~NotificationManager() override; @@ -37,8 +37,8 @@ class NotificationManager : public blink::WebNotificationManager, base::SingleThreadTaskRunner* main_thread_task_runner, NotificationDispatcher* notification_dispatcher); - // WorkerTaskRunner::Observer implementation. - void OnWorkerRunLoopStopped() override; + // WorkerThread::Observer implementation. + void WillStopCurrentWorkerThread() override; // blink::WebNotificationManager implementation. virtual void show(const blink::WebSecurityOrigin& origin, diff --git a/content/child/permissions/permission_dispatcher_thread_proxy.cc b/content/child/permissions/permission_dispatcher_thread_proxy.cc index 01c4a8c..b2af016 100644 --- a/content/child/permissions/permission_dispatcher_thread_proxy.cc +++ b/content/child/permissions/permission_dispatcher_thread_proxy.cc @@ -11,7 +11,7 @@ #include "base/single_thread_task_runner.h" #include "base/threading/thread_local.h" #include "content/child/permissions/permission_dispatcher.h" -#include "content/child/worker_task_runner.h" +#include "content/public/child/worker_thread.h" #include "third_party/WebKit/public/platform/WebURL.h" #include "third_party/WebKit/public/platform/modules/permissions/WebPermissionObserver.h" @@ -37,8 +37,8 @@ PermissionDispatcherThreadProxy::GetThreadInstance( PermissionDispatcherThreadProxy* instance = new PermissionDispatcherThreadProxy(main_thread_task_runner, permission_dispatcher); - DCHECK(WorkerTaskRunner::Instance()->CurrentWorkerId()); - WorkerTaskRunner::Instance()->AddStopObserver(instance); + DCHECK(WorkerThread::GetCurrentId()); + WorkerThread::AddObserver(instance); return instance; } @@ -59,13 +59,10 @@ void PermissionDispatcherThreadProxy::queryPermission( const blink::WebURL& origin, blink::WebPermissionCallback* callback) { main_thread_task_runner_->PostTask( - FROM_HERE, - base::Bind(&PermissionDispatcher::QueryPermissionForWorker, - base::Unretained(permission_dispatcher_), - type, - origin.string().utf8(), - base::Unretained(callback), - WorkerTaskRunner::Instance()->CurrentWorkerId())); + FROM_HERE, base::Bind(&PermissionDispatcher::QueryPermissionForWorker, + base::Unretained(permission_dispatcher_), type, + origin.string().utf8(), base::Unretained(callback), + WorkerThread::GetCurrentId())); } void PermissionDispatcherThreadProxy::requestPermission( @@ -73,13 +70,10 @@ void PermissionDispatcherThreadProxy::requestPermission( const blink::WebURL& origin, blink::WebPermissionCallback* callback) { main_thread_task_runner_->PostTask( - FROM_HERE, - base::Bind(&PermissionDispatcher::RequestPermissionForWorker, - base::Unretained(permission_dispatcher_), - type, - origin.string().utf8(), - base::Unretained(callback), - WorkerTaskRunner::Instance()->CurrentWorkerId())); + FROM_HERE, base::Bind(&PermissionDispatcher::RequestPermissionForWorker, + base::Unretained(permission_dispatcher_), type, + origin.string().utf8(), base::Unretained(callback), + WorkerThread::GetCurrentId())); } void PermissionDispatcherThreadProxy::requestPermissions( @@ -87,13 +81,10 @@ void PermissionDispatcherThreadProxy::requestPermissions( const blink::WebURL& origin, blink::WebPermissionsCallback* callback) { main_thread_task_runner_->PostTask( - FROM_HERE, - base::Bind(&PermissionDispatcher::RequestPermissionsForWorker, - base::Unretained(permission_dispatcher_), - types, - origin.string().utf8(), - base::Unretained(callback), - WorkerTaskRunner::Instance()->CurrentWorkerId())); + FROM_HERE, base::Bind(&PermissionDispatcher::RequestPermissionsForWorker, + base::Unretained(permission_dispatcher_), types, + origin.string().utf8(), base::Unretained(callback), + WorkerThread::GetCurrentId())); } void PermissionDispatcherThreadProxy::revokePermission( @@ -101,13 +92,10 @@ void PermissionDispatcherThreadProxy::revokePermission( const blink::WebURL& origin, blink::WebPermissionCallback* callback) { main_thread_task_runner_->PostTask( - FROM_HERE, - base::Bind(&PermissionDispatcher::RevokePermissionForWorker, - base::Unretained(permission_dispatcher_), - type, - origin.string().utf8(), - base::Unretained(callback), - WorkerTaskRunner::Instance()->CurrentWorkerId())); + FROM_HERE, base::Bind(&PermissionDispatcher::RevokePermissionForWorker, + base::Unretained(permission_dispatcher_), type, + origin.string().utf8(), base::Unretained(callback), + WorkerThread::GetCurrentId())); } void PermissionDispatcherThreadProxy::startListening( @@ -119,16 +107,14 @@ void PermissionDispatcherThreadProxy::startListening( RegisterObserver(observer); - main_thread_task_runner_->PostTask(FROM_HERE, - base::Bind(&PermissionDispatcher::StartListeningForWorker, - base::Unretained(permission_dispatcher_), - type, - origin.string().utf8(), - WorkerTaskRunner::Instance()->CurrentWorkerId(), + main_thread_task_runner_->PostTask( + FROM_HERE, + base::Bind( + &PermissionDispatcher::StartListeningForWorker, + base::Unretained(permission_dispatcher_), type, + origin.string().utf8(), WorkerThread::GetCurrentId(), base::Bind(&PermissionDispatcherThreadProxy::OnPermissionChanged, - base::Unretained(this), - type, - origin.string().utf8(), + base::Unretained(this), type, origin.string().utf8(), base::Unretained(observer)))); } @@ -147,21 +133,18 @@ void PermissionDispatcherThreadProxy::OnPermissionChanged( observer->permissionChanged(type, status); - main_thread_task_runner_->PostTask(FROM_HERE, - base::Bind(&PermissionDispatcher::GetNextPermissionChangeForWorker, - base::Unretained(permission_dispatcher_), - type, - origin, - status, - WorkerTaskRunner::Instance()->CurrentWorkerId(), + main_thread_task_runner_->PostTask( + FROM_HERE, + base::Bind( + &PermissionDispatcher::GetNextPermissionChangeForWorker, + base::Unretained(permission_dispatcher_), type, origin, status, + WorkerThread::GetCurrentId(), base::Bind(&PermissionDispatcherThreadProxy::OnPermissionChanged, - base::Unretained(this), - type, - origin, + base::Unretained(this), type, origin, base::Unretained(observer)))); } -void PermissionDispatcherThreadProxy::OnWorkerRunLoopStopped() { +void PermissionDispatcherThreadProxy::WillStopCurrentWorkerThread() { delete this; } diff --git a/content/child/permissions/permission_dispatcher_thread_proxy.h b/content/child/permissions/permission_dispatcher_thread_proxy.h index d290367..180a261 100644 --- a/content/child/permissions/permission_dispatcher_thread_proxy.h +++ b/content/child/permissions/permission_dispatcher_thread_proxy.h @@ -9,7 +9,7 @@ #include "base/macros.h" #include "base/memory/ref_counted.h" #include "content/child/permissions/permission_observers_registry.h" -#include "content/child/worker_task_runner.h" +#include "content/public/child/worker_thread.h" #include "third_party/WebKit/public/platform/modules/permissions/WebPermissionClient.h" namespace base { @@ -23,10 +23,9 @@ class PermissionDispatcher; // PermissionDispatcherThreadProxy is a a proxy to the PermissionDispatcher for // callers running on a different thread than the main thread. There is one // instance of that class per thread. -class PermissionDispatcherThreadProxy : - public blink::WebPermissionClient, - public PermissionObserversRegistry, - public WorkerTaskRunner::Observer { +class PermissionDispatcherThreadProxy : public blink::WebPermissionClient, + public PermissionObserversRegistry, + public WorkerThread::Observer { public: static PermissionDispatcherThreadProxy* GetThreadInstance( base::SingleThreadTaskRunner* main_thread_task_runner, @@ -51,8 +50,8 @@ class PermissionDispatcherThreadProxy : blink::WebPermissionObserver* observer); virtual void stopListening(blink::WebPermissionObserver* observer); - // WorkerTaskRunner::Observer implementation. - void OnWorkerRunLoopStopped() override; + // WorkerThread::Observer implementation. + void WillStopCurrentWorkerThread() override; private: PermissionDispatcherThreadProxy( diff --git a/content/child/push_messaging/push_provider.cc b/content/child/push_messaging/push_provider.cc index 0894601..ba82b0f 100644 --- a/content/child/push_messaging/push_provider.cc +++ b/content/child/push_messaging/push_provider.cc @@ -11,7 +11,6 @@ #include "content/child/push_messaging/push_dispatcher.h" #include "content/child/service_worker/web_service_worker_registration_impl.h" #include "content/child/thread_safe_sender.h" -#include "content/child/worker_task_runner.h" #include "content/common/push_messaging_messages.h" #include "third_party/WebKit/public/platform/WebString.h" #include "third_party/WebKit/public/platform/modules/push_messaging/WebPushSubscription.h" @@ -21,7 +20,7 @@ namespace content { namespace { int CurrentWorkerId() { - return WorkerTaskRunner::Instance()->CurrentWorkerId(); + return WorkerThread::GetCurrentId(); } // Returns the id of the given |service_worker_registration|, which @@ -57,11 +56,11 @@ PushProvider* PushProvider::ThreadSpecificInstance( PushProvider* provider = new PushProvider(thread_safe_sender, push_dispatcher); if (CurrentWorkerId()) - WorkerTaskRunner::Instance()->AddStopObserver(provider); + WorkerThread::AddObserver(provider); return provider; } -void PushProvider::OnWorkerRunLoopStopped() { +void PushProvider::WillStopCurrentWorkerThread() { delete this; } diff --git a/content/child/push_messaging/push_provider.h b/content/child/push_messaging/push_provider.h index 3826150..9a93dd2 100644 --- a/content/child/push_messaging/push_provider.h +++ b/content/child/push_messaging/push_provider.h @@ -12,7 +12,7 @@ #include "base/id_map.h" #include "base/memory/ref_counted.h" #include "content/child/push_messaging/push_dispatcher.h" -#include "content/child/worker_task_runner.h" +#include "content/public/child/worker_thread.h" #include "content/public/common/push_messaging_status.h" #include "third_party/WebKit/public/platform/modules/push_messaging/WebPushError.h" #include "third_party/WebKit/public/platform/modules/push_messaging/WebPushProvider.h" @@ -28,7 +28,7 @@ namespace content { class ThreadSafeSender; class PushProvider : public blink::WebPushProvider, - public WorkerTaskRunner::Observer { + public WorkerThread::Observer { public: ~PushProvider() override; @@ -38,8 +38,8 @@ class PushProvider : public blink::WebPushProvider, ThreadSafeSender* thread_safe_sender, PushDispatcher* push_dispatcher); - // WorkerTaskRunner::Observer implementation. - void OnWorkerRunLoopStopped() override; + // WorkerThread::Observer implementation. + void WillStopCurrentWorkerThread() override; // blink::WebPushProvider implementation. virtual void subscribe( diff --git a/content/child/quota_dispatcher.cc b/content/child/quota_dispatcher.cc index fa766db..c63894a 100644 --- a/content/child/quota_dispatcher.cc +++ b/content/child/quota_dispatcher.cc @@ -53,7 +53,7 @@ class WebStorageQuotaDispatcherCallback : public QuotaDispatcher::Callback { }; int CurrentWorkerId() { - return WorkerTaskRunner::Instance()->CurrentWorkerId(); + return WorkerThread::GetCurrentId(); } } // namespace @@ -83,12 +83,12 @@ QuotaDispatcher* QuotaDispatcher::ThreadSpecificInstance( QuotaDispatcher* dispatcher = new QuotaDispatcher( thread_safe_sender, quota_message_filter); - if (WorkerTaskRunner::Instance()->CurrentWorkerId()) - WorkerTaskRunner::Instance()->AddStopObserver(dispatcher); + if (WorkerThread::GetCurrentId()) + WorkerThread::AddObserver(dispatcher); return dispatcher; } -void QuotaDispatcher::OnWorkerRunLoopStopped() { +void QuotaDispatcher::WillStopCurrentWorkerThread() { delete this; } diff --git a/content/child/quota_dispatcher.h b/content/child/quota_dispatcher.h index f4111e6..a141290 100644 --- a/content/child/quota_dispatcher.h +++ b/content/child/quota_dispatcher.h @@ -11,7 +11,7 @@ #include "base/basictypes.h" #include "base/id_map.h" #include "base/memory/ref_counted.h" -#include "content/child/worker_task_runner.h" +#include "content/public/child/worker_thread.h" #include "storage/common/quota/quota_types.h" class GURL; @@ -33,7 +33,7 @@ class QuotaMessageFilter; // process from/to the main browser process. There is one instance // per each thread. Thread-specific instance can be obtained by // ThreadSpecificInstance(). -class QuotaDispatcher : public WorkerTaskRunner::Observer { +class QuotaDispatcher : public WorkerThread::Observer { public: class Callback { public: @@ -53,8 +53,8 @@ class QuotaDispatcher : public WorkerTaskRunner::Observer { ThreadSafeSender* thread_safe_sender, QuotaMessageFilter* quota_message_filter); - // WorkerTaskRunner::Observer implementation. - void OnWorkerRunLoopStopped() override; + // WorkerThread::Observer implementation. + void WillStopCurrentWorkerThread() override; void OnMessageReceived(const IPC::Message& msg); diff --git a/content/child/service_worker/service_worker_dispatcher.cc b/content/child/service_worker/service_worker_dispatcher.cc index 03e9ee9..a3cf500 100644 --- a/content/child/service_worker/service_worker_dispatcher.cc +++ b/content/child/service_worker/service_worker_dispatcher.cc @@ -40,7 +40,7 @@ base::LazyInstance<ThreadLocalPointer<void>>::Leaky g_dispatcher_tls = void* const kHasBeenDeleted = reinterpret_cast<void*>(0x1); int CurrentWorkerId() { - return WorkerTaskRunner::Instance()->CurrentWorkerId(); + return WorkerThread::GetCurrentId(); } } // namespace @@ -254,8 +254,8 @@ ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance( ServiceWorkerDispatcher* dispatcher = new ServiceWorkerDispatcher(thread_safe_sender, main_thread_task_runner); - if (WorkerTaskRunner::Instance()->CurrentWorkerId()) - WorkerTaskRunner::Instance()->AddStopObserver(dispatcher); + if (WorkerThread::GetCurrentId()) + WorkerThread::AddObserver(dispatcher); return dispatcher; } @@ -266,7 +266,7 @@ ServiceWorkerDispatcher* ServiceWorkerDispatcher::GetThreadSpecificInstance() { g_dispatcher_tls.Pointer()->Get()); } -void ServiceWorkerDispatcher::OnWorkerRunLoopStopped() { +void ServiceWorkerDispatcher::WillStopCurrentWorkerThread() { delete this; } diff --git a/content/child/service_worker/service_worker_dispatcher.h b/content/child/service_worker/service_worker_dispatcher.h index c596ab4..2215156 100644 --- a/content/child/service_worker/service_worker_dispatcher.h +++ b/content/child/service_worker/service_worker_dispatcher.h @@ -11,7 +11,7 @@ #include "base/id_map.h" #include "base/memory/ref_counted.h" #include "base/strings/string16.h" -#include "content/child/worker_task_runner.h" +#include "content/public/child/worker_thread.h" #include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWorkerError.h" #include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWorkerProvider.h" #include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWorkerRegistration.h" @@ -48,8 +48,7 @@ struct ServiceWorkerVersionAttributes; // This class manages communication with the browser process about // registration of the service worker, exposed to renderer and worker // scripts through methods like navigator.registerServiceWorker(). -class CONTENT_EXPORT ServiceWorkerDispatcher - : public WorkerTaskRunner::Observer { +class CONTENT_EXPORT ServiceWorkerDispatcher : public WorkerThread::Observer { public: typedef blink::WebServiceWorkerProvider::WebServiceWorkerRegistrationCallbacks WebServiceWorkerRegistrationCallbacks; @@ -180,8 +179,8 @@ class CONTENT_EXPORT ServiceWorkerDispatcher friend class WebServiceWorkerImpl; friend class WebServiceWorkerRegistrationImpl; - // WorkerTaskRunner::Observer implementation. - void OnWorkerRunLoopStopped() override; + // WorkerThread::Observer implementation. + void WillStopCurrentWorkerThread() override; void OnAssociateRegistrationWithServiceWorker( int thread_id, diff --git a/content/child/worker_task_runner.cc b/content/child/worker_task_runner.cc index bd469c1..875e102 100644 --- a/content/child/worker_task_runner.cc +++ b/content/child/worker_task_runner.cc @@ -13,6 +13,7 @@ #include "base/single_thread_task_runner.h" #include "base/stl_util.h" #include "base/thread_task_runner_handle.h" +#include "content/child/worker_thread_impl.h" namespace content { @@ -37,11 +38,6 @@ class DoNothingTaskRunner : public base::TaskRunner { } // namespace -struct WorkerTaskRunner::ThreadLocalState { - ThreadLocalState() {} - base::ObserverList<WorkerTaskRunner::Observer> stop_observers_; -}; - WorkerTaskRunner::WorkerTaskRunner() : task_runner_for_dead_worker_(new DoNothingTaskRunner()) { } @@ -63,52 +59,33 @@ int WorkerTaskRunner::PostTaskToAllThreads(const base::Closure& closure) { return static_cast<int>(task_runner_map_.size()); } -int WorkerTaskRunner::CurrentWorkerId() { - if (!current_tls_.Get()) - return 0; - return base::PlatformThread::CurrentId(); -} - WorkerTaskRunner* WorkerTaskRunner::Instance() { static base::LazyInstance<WorkerTaskRunner>::Leaky worker_task_runner = LAZY_INSTANCE_INITIALIZER; return worker_task_runner.Pointer(); } -void WorkerTaskRunner::AddStopObserver(Observer* obs) { - DCHECK(CurrentWorkerId() > 0); - current_tls_.Get()->stop_observers_.AddObserver(obs); -} - -void WorkerTaskRunner::RemoveStopObserver(Observer* obs) { - DCHECK(CurrentWorkerId() > 0); - current_tls_.Get()->stop_observers_.RemoveObserver(obs); -} - WorkerTaskRunner::~WorkerTaskRunner() { } -void WorkerTaskRunner::OnWorkerRunLoopStarted() { - DCHECK(!current_tls_.Get()); +void WorkerTaskRunner::DidStartWorkerRunLoop() { DCHECK(!base::PlatformThread::CurrentRef().is_null()); - current_tls_.Set(new ThreadLocalState()); - + // Note: call into WorkerThreadImpl rather than this class observing + // WorkerThreadImpl, to ensure that the task runner exists before anybody is + // notified that the worker thread has started. + WorkerThreadImpl::DidStartCurrentWorkerThread(); int id = base::PlatformThread::CurrentId(); base::AutoLock locker_(task_runner_map_lock_); task_runner_map_[id] = base::ThreadTaskRunnerHandle::Get().get(); CHECK(task_runner_map_[id]); } -void WorkerTaskRunner::OnWorkerRunLoopStopped() { - DCHECK(current_tls_.Get()); - FOR_EACH_OBSERVER(Observer, current_tls_.Get()->stop_observers_, - OnWorkerRunLoopStopped()); +void WorkerTaskRunner::WillStopWorkerRunLoop() { + WorkerThreadImpl::WillStopCurrentWorkerThread(); { base::AutoLock locker(task_runner_map_lock_); - task_runner_map_.erase(CurrentWorkerId()); + task_runner_map_.erase(WorkerThread::GetCurrentId()); } - delete current_tls_.Get(); - current_tls_.Set(NULL); } base::TaskRunner* WorkerTaskRunner::GetTaskRunnerFor(int worker_id) { diff --git a/content/child/worker_task_runner.h b/content/child/worker_task_runner.h index c3797bc..24ae41a 100644 --- a/content/child/worker_task_runner.h +++ b/content/child/worker_task_runner.h @@ -11,8 +11,8 @@ #include "base/memory/ref_counted.h" #include "base/synchronization/lock.h" #include "base/threading/platform_thread.h" -#include "base/threading/thread_local.h" #include "content/common/content_export.h" +#include "content/public/child/worker_thread.h" namespace base { class TaskRunner; @@ -26,22 +26,10 @@ class CONTENT_EXPORT WorkerTaskRunner { bool PostTask(int id, const base::Closure& task); int PostTaskToAllThreads(const base::Closure& task); - int CurrentWorkerId(); static WorkerTaskRunner* Instance(); - class CONTENT_EXPORT Observer { - public: - virtual ~Observer() {} - virtual void OnWorkerRunLoopStopped() = 0; - }; - // Add/Remove an observer that will get notified when the current worker run - // loop is stopping. This observer will not get notified when other threads - // are stopping. It's only valid to call these on a worker thread. - void AddStopObserver(Observer* observer); - void RemoveStopObserver(Observer* observer); - - void OnWorkerRunLoopStarted(); - void OnWorkerRunLoopStopped(); + void DidStartWorkerRunLoop(); + void WillStopWorkerRunLoop(); base::TaskRunner* GetTaskRunnerFor(int worker_id); @@ -60,9 +48,6 @@ class CONTENT_EXPORT WorkerTaskRunner { // which silently discards all the tasks it receives. scoped_refptr<base::TaskRunner> task_runner_for_dead_worker_; - struct ThreadLocalState; - base::ThreadLocalPointer<ThreadLocalState> current_tls_; - IDToTaskRunnerMap task_runner_map_; base::Lock task_runner_map_lock_; }; diff --git a/content/child/worker_task_runner_unittest.cc b/content/child/worker_task_runner_unittest.cc index 20eddfb..5e932b5 100644 --- a/content/child/worker_task_runner_unittest.cc +++ b/content/child/worker_task_runner_unittest.cc @@ -6,6 +6,7 @@ #include "base/logging.h" #include "base/message_loop/message_loop.h" +#include "content/public/child/worker_thread.h" #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" @@ -13,38 +14,32 @@ namespace content { class WorkerTaskRunnerTest : public testing::Test { public: - void FakeStart() { - task_runner_.OnWorkerRunLoopStarted(); - } - void FakeStop() { - task_runner_.OnWorkerRunLoopStopped(); - } + void FakeStart() { task_runner_.DidStartWorkerRunLoop(); } + void FakeStop() { task_runner_.WillStopWorkerRunLoop(); } WorkerTaskRunner task_runner_; private: base::MessageLoop message_loop_; }; -class MockObserver : public WorkerTaskRunner::Observer { +class MockObserver : public WorkerThread::Observer { public: - MOCK_METHOD0(OnWorkerRunLoopStopped, void()); + MOCK_METHOD0(WillStopCurrentWorkerThread, void()); void RemoveSelfOnNotify() { - ON_CALL(*this, OnWorkerRunLoopStopped()).WillByDefault( - testing::Invoke(this, &MockObserver::RemoveSelf)); - } - void RemoveSelf() { - runner_->RemoveStopObserver(this); + ON_CALL(*this, WillStopCurrentWorkerThread()) + .WillByDefault(testing::Invoke(this, &MockObserver::RemoveSelf)); } + void RemoveSelf() { WorkerThread::RemoveObserver(this); } WorkerTaskRunner* runner_; }; TEST_F(WorkerTaskRunnerTest, BasicObservingAndWorkerId) { - ASSERT_EQ(0, task_runner_.CurrentWorkerId()); + ASSERT_EQ(0, WorkerThread::GetCurrentId()); MockObserver o; - EXPECT_CALL(o, OnWorkerRunLoopStopped()).Times(1); + EXPECT_CALL(o, WillStopCurrentWorkerThread()).Times(1); FakeStart(); - task_runner_.AddStopObserver(&o); - ASSERT_LT(0, task_runner_.CurrentWorkerId()); + WorkerThread::AddObserver(&o); + ASSERT_LT(0, WorkerThread::GetCurrentId()); FakeStop(); } @@ -52,9 +47,9 @@ TEST_F(WorkerTaskRunnerTest, CanRemoveSelfDuringNotification) { MockObserver o; o.RemoveSelfOnNotify(); o.runner_ = &task_runner_; - EXPECT_CALL(o, OnWorkerRunLoopStopped()).Times(1); + EXPECT_CALL(o, WillStopCurrentWorkerThread()).Times(1); FakeStart(); - task_runner_.AddStopObserver(&o); + WorkerThread::AddObserver(&o); FakeStop(); } diff --git a/content/child/worker_thread_impl.cc b/content/child/worker_thread_impl.cc index c499ccb..2f4549d 100644 --- a/content/child/worker_thread_impl.cc +++ b/content/child/worker_thread_impl.cc @@ -2,16 +2,37 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "content/public/child/worker_thread.h" +#include "content/child/worker_thread_impl.h" +#include "base/lazy_instance.h" +#include "base/logging.h" +#include "base/observer_list.h" +#include "base/threading/thread_local.h" #include "content/child/worker_task_runner.h" #include "content/common/content_export.h" +#include "content/public/child/worker_thread.h" namespace content { +namespace { + +using WorkerThreadObservers = base::ObserverList<WorkerThread::Observer>; +using ThreadLocalWorkerThreadObservers = + base::ThreadLocalPointer<WorkerThreadObservers>; + +// Stores a WorkerThreadObservers instance per thread. +base::LazyInstance<ThreadLocalWorkerThreadObservers> g_observers_tls = + LAZY_INSTANCE_INITIALIZER; + +} // namespace + +// WorkerThread: + // static int WorkerThread::GetCurrentId() { - return WorkerTaskRunner::Instance()->CurrentWorkerId(); + if (!g_observers_tls.Get().Get()) + return 0; + return base::PlatformThread::CurrentId(); } // static @@ -19,4 +40,40 @@ void WorkerThread::PostTask(int id, const base::Closure& task) { WorkerTaskRunner::Instance()->PostTask(id, task); } +// static +void WorkerThread::AddObserver(Observer* observer) { + DCHECK(GetCurrentId() > 0); + WorkerThreadObservers* observers = g_observers_tls.Get().Get(); + DCHECK(observers); + observers->AddObserver(observer); +} + +// static +void WorkerThread::RemoveObserver(Observer* observer) { + DCHECK(GetCurrentId() > 0); + WorkerThreadObservers* observers = g_observers_tls.Get().Get(); + DCHECK(observers); + observers->RemoveObserver(observer); +} + +// WorkerThreadImpl: + +// static +void WorkerThreadImpl::DidStartCurrentWorkerThread() { + ThreadLocalWorkerThreadObservers& observers_tls = g_observers_tls.Get(); + DCHECK(!observers_tls.Get()); + observers_tls.Set(new WorkerThreadObservers()); +} + +// static +void WorkerThreadImpl::WillStopCurrentWorkerThread() { + ThreadLocalWorkerThreadObservers& observers_tls = g_observers_tls.Get(); + WorkerThreadObservers* observers = observers_tls.Get(); + DCHECK(observers); + FOR_EACH_OBSERVER(WorkerThread::Observer, *observers, + WillStopCurrentWorkerThread()); + delete observers; + observers_tls.Set(nullptr); +} + } // namespace content diff --git a/content/child/worker_thread_impl.h b/content/child/worker_thread_impl.h new file mode 100644 index 0000000..55cc153 --- /dev/null +++ b/content/child/worker_thread_impl.h @@ -0,0 +1,19 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/macros.h" + +namespace content { + +// WorkerThread-related methods only exposed within the content module. +class WorkerThreadImpl { + public: + static void DidStartCurrentWorkerThread(); + static void WillStopCurrentWorkerThread(); + + private: + DISALLOW_COPY_AND_ASSIGN(WorkerThreadImpl); +}; + +} // namespace content diff --git a/content/child/worker_thread_message_filter.cc b/content/child/worker_thread_message_filter.cc index aa11b3b..77770ab 100644 --- a/content/child/worker_thread_message_filter.cc +++ b/content/child/worker_thread_message_filter.cc @@ -38,7 +38,7 @@ bool WorkerThreadMessageFilter::OnMessageReceived(const IPC::Message& msg) { // If the IPC message is received in a worker thread, but it has already been // stopped, then drop the message. if (!main_thread_task_runner_->BelongsToCurrentThread() && - !WorkerTaskRunner::Instance()->CurrentWorkerId()) { + !WorkerThread::GetCurrentId()) { return false; } OnFilteredMessageReceived(msg); diff --git a/content/content_child.gypi b/content/content_child.gypi index f08dd47..5ad5b67 100644 --- a/content/content_child.gypi +++ b/content/content_child.gypi @@ -284,6 +284,7 @@ 'child/worker_task_runner.cc', 'child/worker_task_runner.h', 'child/worker_thread_impl.cc', + 'child/worker_thread_impl.h', 'child/worker_thread_message_filter.cc', 'child/worker_thread_message_filter.h', ], diff --git a/content/public/child/worker_thread.h b/content/public/child/worker_thread.h index e8ed64c..983f09a 100644 --- a/content/public/child/worker_thread.h +++ b/content/public/child/worker_thread.h @@ -17,6 +17,25 @@ namespace content { // posting tasks back to them. class CONTENT_EXPORT WorkerThread { public: + // Observes worker thread lifetime. + class CONTENT_EXPORT Observer { + public: + virtual ~Observer() {} + + // Notifies the observer that the current worker thread is about to be + // stopped. + // + // The worker state may have already been destroyed. To observe that, use + // ContentRendererClient::WillDestroyServiceWorkerContextOnWorkerThread. + virtual void WillStopCurrentWorkerThread() {} + }; + + // Adds/removes an Observer. Observers are stored per-thread, so it is only + // valid to call these from a worker thread, and events will be dispatched on + // that worker's thread. + static void AddObserver(Observer* observer); + static void RemoveObserver(Observer* observer); + // Returns the thread ID for the current worker thread, or 0 if this is not a // worker thread (for example, the render thread). Worker thread IDs will // always be > 0. diff --git a/content/renderer/bluetooth/bluetooth_dispatcher.cc b/content/renderer/bluetooth/bluetooth_dispatcher.cc index 1a38354..1a4679e 100644 --- a/content/renderer/bluetooth/bluetooth_dispatcher.cc +++ b/content/renderer/bluetooth/bluetooth_dispatcher.cc @@ -72,7 +72,7 @@ base::LazyInstance<base::ThreadLocalPointer<void>>::Leaky g_dispatcher_tls = void* const kHasBeenDeleted = reinterpret_cast<void*>(0x1); int CurrentWorkerId() { - return WorkerTaskRunner::Instance()->CurrentWorkerId(); + return WorkerThread::GetCurrentId(); } WebBluetoothDevice::VendorIDSource GetWebVendorIdSource( @@ -111,7 +111,7 @@ BluetoothDispatcher* BluetoothDispatcher::GetOrCreateThreadSpecificInstance( BluetoothDispatcher* dispatcher = new BluetoothDispatcher(thread_safe_sender); if (CurrentWorkerId()) - WorkerTaskRunner::Instance()->AddStopObserver(dispatcher); + WorkerThread::AddObserver(dispatcher); return dispatcher; } @@ -225,7 +225,7 @@ void BluetoothDispatcher::writeValue( CurrentWorkerId(), request_id, characteristic_instance_id.utf8(), value)); } -void BluetoothDispatcher::OnWorkerRunLoopStopped() { +void BluetoothDispatcher::WillStopCurrentWorkerThread() { delete this; } diff --git a/content/renderer/bluetooth/bluetooth_dispatcher.h b/content/renderer/bluetooth/bluetooth_dispatcher.h index 1a704a7..4109414 100644 --- a/content/renderer/bluetooth/bluetooth_dispatcher.h +++ b/content/renderer/bluetooth/bluetooth_dispatcher.h @@ -7,8 +7,8 @@ #include "base/id_map.h" #include "base/memory/ref_counted.h" -#include "content/child/worker_task_runner.h" #include "content/common/bluetooth/bluetooth_device.h" +#include "content/public/child/worker_thread.h" #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetooth.h" #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothError.h" @@ -35,7 +35,7 @@ class ThreadSafeSender; // Incoming IPC messages are received by the BluetoothMessageFilter and // directed to the thread specific instance of this class. // Outgoing messages come from WebBluetoothImpl. -class BluetoothDispatcher : public WorkerTaskRunner::Observer { +class BluetoothDispatcher : public WorkerThread::Observer { public: explicit BluetoothDispatcher(ThreadSafeSender* sender); ~BluetoothDispatcher() override; @@ -70,8 +70,8 @@ class BluetoothDispatcher : public WorkerTaskRunner::Observer { const std::vector<uint8_t>& value, blink::WebBluetoothWriteValueCallbacks*); - // WorkerTaskRunner::Observer implementation. - void OnWorkerRunLoopStopped() override; + // WorkerThread::Observer implementation. + void WillStopCurrentWorkerThread() override; private: // IPC Handlers, see definitions in bluetooth_messages.h. diff --git a/content/renderer/cache_storage/cache_storage_dispatcher.cc b/content/renderer/cache_storage/cache_storage_dispatcher.cc index 95b10e3..c2612ef 100644 --- a/content/renderer/cache_storage/cache_storage_dispatcher.cc +++ b/content/renderer/cache_storage/cache_storage_dispatcher.cc @@ -220,12 +220,12 @@ CacheStorageDispatcher* CacheStorageDispatcher::ThreadSpecificInstance( CacheStorageDispatcher* dispatcher = new CacheStorageDispatcher(thread_safe_sender); - if (WorkerTaskRunner::Instance()->CurrentWorkerId()) - WorkerTaskRunner::Instance()->AddStopObserver(dispatcher); + if (WorkerThread::GetCurrentId()) + WorkerThread::AddObserver(dispatcher); return dispatcher; } -void CacheStorageDispatcher::OnWorkerRunLoopStopped() { +void CacheStorageDispatcher::WillStopCurrentWorkerThread() { delete this; } diff --git a/content/renderer/cache_storage/cache_storage_dispatcher.h b/content/renderer/cache_storage/cache_storage_dispatcher.h index 8e31b0b..f08712c 100644 --- a/content/renderer/cache_storage/cache_storage_dispatcher.h +++ b/content/renderer/cache_storage/cache_storage_dispatcher.h @@ -12,7 +12,7 @@ #include "base/memory/weak_ptr.h" #include "base/strings/string16.h" #include "base/time/time.h" -#include "content/child/worker_task_runner.h" +#include "content/public/child/worker_thread.h" #include "content/public/renderer/render_process_observer.h" #include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWorkerCache.h" #include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWorkerCacheError.h" @@ -26,7 +26,7 @@ struct ServiceWorkerResponse; // Handle the Cache Storage messaging for this context thread. The // main thread and each worker thread have their own instances. -class CacheStorageDispatcher : public WorkerTaskRunner::Observer { +class CacheStorageDispatcher : public WorkerThread::Observer { public: explicit CacheStorageDispatcher(ThreadSafeSender* thread_safe_sender); ~CacheStorageDispatcher() override; @@ -36,8 +36,8 @@ class CacheStorageDispatcher : public WorkerTaskRunner::Observer { static CacheStorageDispatcher* ThreadSpecificInstance( ThreadSafeSender* thread_safe_sender); - // WorkerTaskRunner::Observer implementation. - void OnWorkerRunLoopStopped() override; + // WorkerThread::Observer implementation. + void WillStopCurrentWorkerThread() override; bool Send(IPC::Message* msg); @@ -173,9 +173,7 @@ class CacheStorageDispatcher : public WorkerTaskRunner::Observer { using BatchCallbacksMap = IDMap<blink::WebServiceWorkerCache::CacheBatchCallbacks, IDMapOwnPointer>; - static int32 CurrentWorkerId() { - return WorkerTaskRunner::Instance()->CurrentWorkerId(); - } + static int32 CurrentWorkerId() { return WorkerThread::GetCurrentId(); } void PopulateWebResponseFromResponse( const ServiceWorkerResponse& response, diff --git a/content/renderer/service_worker/service_worker_context_client.cc b/content/renderer/service_worker/service_worker_context_client.cc index de57d4c..d3af28f 100644 --- a/content/renderer/service_worker/service_worker_context_client.cc +++ b/content/renderer/service_worker/service_worker_context_client.cc @@ -24,7 +24,6 @@ #include "content/child/service_worker/web_service_worker_registration_impl.h" #include "content/child/thread_safe_sender.h" #include "content/child/webmessageportchannel_impl.h" -#include "content/child/worker_task_runner.h" #include "content/common/devtools_messages.h" #include "content/common/message_port_messages.h" #include "content/common/mojo/service_registry_impl.h" @@ -339,7 +338,7 @@ void ServiceWorkerContextClient::workerContextFailedToStart() { void ServiceWorkerContextClient::workerContextStarted( blink::WebServiceWorkerContextProxy* proxy) { DCHECK(!worker_task_runner_.get()); - DCHECK_NE(0, WorkerTaskRunner::Instance()->CurrentWorkerId()); + DCHECK_NE(0, WorkerThread::GetCurrentId()); worker_task_runner_ = base::ThreadTaskRunnerHandle::Get(); // g_worker_client_tls.Pointer()->Get() could return NULL if this context // gets deleted before workerContextStarted() is called. @@ -363,8 +362,7 @@ void ServiceWorkerContextClient::workerContextStarted( SetRegistrationInServiceWorkerGlobalScope(); Send(new EmbeddedWorkerHostMsg_WorkerScriptLoaded( - embedded_worker_id_, - WorkerTaskRunner::Instance()->CurrentWorkerId(), + embedded_worker_id_, WorkerThread::GetCurrentId(), provider_context_->provider_id())); TRACE_EVENT_ASYNC_STEP_INTO0( @@ -389,16 +387,17 @@ void ServiceWorkerContextClient::didEvaluateWorkerScript(bool success) { void ServiceWorkerContextClient::didInitializeWorkerContext( v8::Local<v8::Context> context, const blink::WebURL& url) { - // TODO(annekao): Remove WebURL parameter from Blink (since url and script_url - // are equal). Also remove m_documentURL from ServiceWorkerGlobalScopeProxy. - DCHECK_EQ(script_url_, GURL(url)); + // TODO(annekao): Remove WebURL parameter from Blink, it's at best redundant + // given |script_url_|, and may be empty in the future. + // Also remove m_documentURL from ServiceWorkerGlobalScopeProxy. GetContentClient() ->renderer() ->DidInitializeServiceWorkerContextOnWorkerThread(context, script_url_); } -void ServiceWorkerContextClient::willDestroyWorkerContext() { - // At this point OnWorkerRunLoopStopped is already called, so +void ServiceWorkerContextClient::willDestroyWorkerContext( + v8::Local<v8::Context> context) { + // At this point WillStopCurrentWorkerThread is already called, so // worker_task_runner_->RunsTasksOnCurrentThread() returns false // (while we're still on the worker thread). proxy_ = NULL; diff --git a/content/renderer/service_worker/service_worker_context_client.h b/content/renderer/service_worker/service_worker_context_client.h index 54fd780b..339d3b0 100644 --- a/content/renderer/service_worker/service_worker_context_client.h +++ b/content/renderer/service_worker/service_worker_context_client.h @@ -103,7 +103,7 @@ class ServiceWorkerContextClient virtual void didEvaluateWorkerScript(bool success); virtual void didInitializeWorkerContext(v8::Local<v8::Context> context, const blink::WebURL& url); - virtual void willDestroyWorkerContext(); + virtual void willDestroyWorkerContext(v8::Local<v8::Context> context); virtual void workerContextDestroyed(); virtual void reportException(const blink::WebString& error_message, int line_number, |