Chromium Code Reviews| Index: content/common/worker_task_runner.h |
| diff --git a/content/common/worker_task_runner.h b/content/common/worker_task_runner.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..be1193c026cb3335284d7e5bf6995f67f707515f |
| --- /dev/null |
| +++ b/content/common/worker_task_runner.h |
| @@ -0,0 +1,83 @@ |
| +// Copyright (c) 2011 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. |
| + |
| +#ifndef CONTENT_COMMON_WORKER_TASK_RUNNER_H_ |
| +#define CONTENT_COMMON_WORKER_TASK_RUNNER_H_ |
| +#pragma once |
| + |
| +#include "base/memory/ref_counted.h" |
| +#include "base/observer_list.h" |
| +#include "base/synchronization/lock.h" |
| +#include "base/threading/thread_local.h" |
| +#include "content/renderer/renderer_webkitplatformsupport_impl.h" |
| +#include "third_party/WebKit/Source/WebKit/chromium/public/WebWorkerRunLoop.h" |
| + |
| +#include <map> |
| + |
| +using WebKit::WebWorkerRunLoop; |
| + |
| +class WorkerTaskRunner : public base::RefCountedThreadSafe<WorkerTaskRunner> { |
|
michaeln
2011/12/01 21:42:05
why refcounted?
dgrogan
2011/12/01 22:49:00
I was imagining that ChildThread would be handing
michaeln
2011/12/02 00:15:03
Take a look a renderer_main.cc, worker_main.cc, Ch
dgrogan
2011/12/02 11:57:44
I interpret this to mean that the ref counting is
|
| + public: |
| + // Change this to a real closure. |
| + void PostTask(int id, WebKit::WebWorkerRunLoop::Task* closure) { |
| + DCHECK(id > 0); |
| + base::AutoLock locker_(lock_); |
| + IDToLoopMap::iterator found = loop_map_.find(id); |
| + if (found != loop_map_.end()) |
| + found->second.postTask(closure); |
| + else |
| + delete closure; |
| + } |
| + int CurrentWorkerId() { |
| + if (!current_tls_.Get()) |
| + return 0; |
| + return current_tls_.Get()->first; |
| + } |
| + class Observer { |
| + public: |
| + virtual ~Observer() { } |
| + virtual void onLoopRegistered() { } |
| + virtual void onLoopUnregistered() { } |
| + }; |
| + void AddObserver(Observer* obs) { |
| + observer_list_.AddObserver(obs); |
| + } |
| + void RemoveObserver(Observer* obs) { |
| + observer_list_.RemoveObserver(obs); |
| + } |
| + |
| + private: |
| + friend RendererWebKitPlatformSupportImpl; |
| + |
| + void RegisterCurrentWorkerLoop(const WebWorkerRunLoop& loop) { |
| + DCHECK(!current_tls_.Get()); |
| + { |
| + base::AutoLock locker_(lock_); |
| + int id = ++id_sequence_; |
| + current_tls_.Set(new std::pair<int, WebWorkerRunLoop>(id, loop)); |
| + loop_map_[id] = loop; |
| + } |
| + FOR_EACH_OBSERVER(Observer, observer_list_, onLoopRegistered()); |
| + } |
| + void UnRegisterCurrentWorkerLoop(const WebWorkerRunLoop& loop) { |
|
michaeln
2011/12/01 21:42:05
there's probably enough code in here to warrant a
dgrogan
2011/12/01 22:49:00
Agreed.
|
| + DCHECK(CurrentWorkerId() > 0); |
| + DCHECK(current_tls_.Get()); |
| + FOR_EACH_OBSERVER(Observer, observer_list_, onLoopUnregistered()); |
| + base::AutoLock locker_(lock_); |
| + DCHECK(loop_map_[CurrentWorkerId()] == loop); |
| + loop_map_.erase(CurrentWorkerId()); |
| + delete current_tls_.Get(); |
| + current_tls_.Set(NULL); |
| + } |
| + |
| + typedef std::map<int, WebWorkerRunLoop> IDToLoopMap; |
| + base::ThreadLocalPointer<std::pair<int, WebWorkerRunLoop> > current_tls_; |
| + base::Lock lock_; |
| + int id_sequence_ = 0; |
| + std::map<int, WebWorkerRunLoop> loop_map_; |
| + |
| + ObserverList<Observer> observer_list_; |
|
michaeln
2011/12/01 21:42:05
I think ObserverList is not thread-safe.
dgrogan
2011/12/01 22:49:00
Argh, quite right. ObserverListThreadSafe doesn't
|
| +}; |
| + |
| +#endif // CONTENT_COMMON_SOCKET_STREAM_H_ |