OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CONTENT_COMMON_WORKER_TASK_RUNNER_H_ | |
6 #define CONTENT_COMMON_WORKER_TASK_RUNNER_H_ | |
7 #pragma once | |
8 | |
9 #include "base/memory/ref_counted.h" | |
10 #include "base/observer_list.h" | |
11 #include "base/synchronization/lock.h" | |
12 #include "base/threading/thread_local.h" | |
13 #include "content/renderer/renderer_webkitplatformsupport_impl.h" | |
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebWorkerRunLoop.h" | |
15 | |
16 #include <map> | |
17 | |
18 using WebKit::WebWorkerRunLoop; | |
19 | |
20 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
| |
21 public: | |
22 // Change this to a real closure. | |
23 void PostTask(int id, WebKit::WebWorkerRunLoop::Task* closure) { | |
24 DCHECK(id > 0); | |
25 base::AutoLock locker_(lock_); | |
26 IDToLoopMap::iterator found = loop_map_.find(id); | |
27 if (found != loop_map_.end()) | |
28 found->second.postTask(closure); | |
29 else | |
30 delete closure; | |
31 } | |
32 int CurrentWorkerId() { | |
33 if (!current_tls_.Get()) | |
34 return 0; | |
35 return current_tls_.Get()->first; | |
36 } | |
37 class Observer { | |
38 public: | |
39 virtual ~Observer() { } | |
40 virtual void onLoopRegistered() { } | |
41 virtual void onLoopUnregistered() { } | |
42 }; | |
43 void AddObserver(Observer* obs) { | |
44 observer_list_.AddObserver(obs); | |
45 } | |
46 void RemoveObserver(Observer* obs) { | |
47 observer_list_.RemoveObserver(obs); | |
48 } | |
49 | |
50 private: | |
51 friend RendererWebKitPlatformSupportImpl; | |
52 | |
53 void RegisterCurrentWorkerLoop(const WebWorkerRunLoop& loop) { | |
54 DCHECK(!current_tls_.Get()); | |
55 { | |
56 base::AutoLock locker_(lock_); | |
57 int id = ++id_sequence_; | |
58 current_tls_.Set(new std::pair<int, WebWorkerRunLoop>(id, loop)); | |
59 loop_map_[id] = loop; | |
60 } | |
61 FOR_EACH_OBSERVER(Observer, observer_list_, onLoopRegistered()); | |
62 } | |
63 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.
| |
64 DCHECK(CurrentWorkerId() > 0); | |
65 DCHECK(current_tls_.Get()); | |
66 FOR_EACH_OBSERVER(Observer, observer_list_, onLoopUnregistered()); | |
67 base::AutoLock locker_(lock_); | |
68 DCHECK(loop_map_[CurrentWorkerId()] == loop); | |
69 loop_map_.erase(CurrentWorkerId()); | |
70 delete current_tls_.Get(); | |
71 current_tls_.Set(NULL); | |
72 } | |
73 | |
74 typedef std::map<int, WebWorkerRunLoop> IDToLoopMap; | |
75 base::ThreadLocalPointer<std::pair<int, WebWorkerRunLoop> > current_tls_; | |
76 base::Lock lock_; | |
77 int id_sequence_ = 0; | |
78 std::map<int, WebWorkerRunLoop> loop_map_; | |
79 | |
80 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
| |
81 }; | |
82 | |
83 #endif // CONTENT_COMMON_SOCKET_STREAM_H_ | |
OLD | NEW |