OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "remoting/base/plugin_message_loop_proxy.h" | 5 #include "remoting/base/plugin_message_loop_proxy.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 | 8 |
9 namespace remoting { | 9 namespace remoting { |
10 | 10 |
11 PluginMessageLoopProxy::PluginMessageLoopProxy(Delegate* delegate) | 11 PluginMessageLoopProxy::PluginMessageLoopProxy(Delegate* delegate) |
12 : plugin_thread_id_(base::PlatformThread::CurrentId()), | 12 : plugin_thread_id_(base::PlatformThread::CurrentId()), |
13 delegate_(delegate) { | 13 delegate_(delegate) { |
14 } | 14 } |
15 | 15 |
16 PluginMessageLoopProxy::~PluginMessageLoopProxy() { | 16 PluginMessageLoopProxy::~PluginMessageLoopProxy() { |
17 } | 17 } |
18 | 18 |
19 void PluginMessageLoopProxy::Detach() { | 19 void PluginMessageLoopProxy::Detach() { |
20 base::AutoLock auto_lock(lock_); | 20 base::AutoLock auto_lock(lock_); |
21 if (delegate_) { | 21 if (delegate_) { |
22 DCHECK(BelongsToCurrentThread()); | 22 DCHECK(BelongsToCurrentThread()); |
23 delegate_ = NULL; | 23 delegate_ = NULL; |
24 } | 24 } |
25 } | 25 } |
26 | 26 |
27 bool PluginMessageLoopProxy::PostTask( | |
28 const tracked_objects::Location& from_here, | |
29 const base::Closure& task) { | |
30 return PostDelayedTask(from_here, task, 0); | |
31 } | |
32 | |
33 bool PluginMessageLoopProxy::PostDelayedTask( | 27 bool PluginMessageLoopProxy::PostDelayedTask( |
34 const tracked_objects::Location& from_here, | 28 const tracked_objects::Location& from_here, |
35 const base::Closure& task, | 29 const base::Closure& task, |
36 int64 delay_ms) { | 30 int64 delay_ms) { |
37 base::AutoLock auto_lock(lock_); | 31 base::AutoLock auto_lock(lock_); |
38 if (!delegate_) | 32 if (!delegate_) |
39 return false; | 33 return false; |
40 | 34 |
41 base::Closure* springpad_closure = new base::Closure(base::Bind( | 35 base::Closure* springpad_closure = new base::Closure(base::Bind( |
42 &PluginMessageLoopProxy::RunClosureIf, this, task)); | 36 &PluginMessageLoopProxy::RunClosureIf, this, task)); |
43 return delegate_->RunOnPluginThread( | 37 return delegate_->RunOnPluginThread( |
44 delay_ms, &PluginMessageLoopProxy::TaskSpringboard, springpad_closure); | 38 delay_ms, &PluginMessageLoopProxy::TaskSpringboard, springpad_closure); |
45 } | 39 } |
46 | 40 |
47 bool PluginMessageLoopProxy::PostNonNestableTask( | |
48 const tracked_objects::Location& from_here, | |
49 const base::Closure& task) { | |
50 // All tasks running on this message loop are non-nestable. | |
51 return PostTask(from_here, task); | |
52 } | |
53 | |
54 bool PluginMessageLoopProxy::PostNonNestableDelayedTask( | 41 bool PluginMessageLoopProxy::PostNonNestableDelayedTask( |
55 const tracked_objects::Location& from_here, | 42 const tracked_objects::Location& from_here, |
56 const base::Closure& task, | 43 const base::Closure& task, |
57 int64 delay_ms) { | 44 int64 delay_ms) { |
58 // All tasks running on this message loop are non-nestable. | 45 // All tasks running on this message loop are non-nestable. |
59 return PostDelayedTask(from_here, task, delay_ms); | 46 return PostDelayedTask(from_here, task, delay_ms); |
60 } | 47 } |
61 | 48 |
62 bool PluginMessageLoopProxy::BelongsToCurrentThread() { | 49 bool PluginMessageLoopProxy::RunsTasksOnCurrentThread() const { |
63 // In pepper plugins ideally we should use pp::Core::IsMainThread, | 50 // In pepper plugins ideally we should use pp::Core::IsMainThread, |
64 // but it is problematic because we would need to keep reference to | 51 // but it is problematic because we would need to keep reference to |
65 // Core somewhere, e.g. make the delegate ref-counted. | 52 // Core somewhere, e.g. make the delegate ref-counted. |
66 return base::PlatformThread::CurrentId() == plugin_thread_id_; | 53 return base::PlatformThread::CurrentId() == plugin_thread_id_; |
67 } | 54 } |
68 | 55 |
69 // static | 56 // static |
70 void PluginMessageLoopProxy::TaskSpringboard(void* data) { | 57 void PluginMessageLoopProxy::TaskSpringboard(void* data) { |
71 base::Closure* task = reinterpret_cast<base::Closure*>(data); | 58 base::Closure* task = reinterpret_cast<base::Closure*>(data); |
72 task->Run(); | 59 task->Run(); |
73 delete task; | 60 delete task; |
74 } | 61 } |
75 | 62 |
76 void PluginMessageLoopProxy::RunClosureIf(const base::Closure& task) { | 63 void PluginMessageLoopProxy::RunClosureIf(const base::Closure& task) { |
77 // |delegate_| can be changed only from our thread, so it's safe to | 64 // |delegate_| can be changed only from our thread, so it's safe to |
78 // access it without acquiring |lock_|. | 65 // access it without acquiring |lock_|. |
79 if (delegate_) | 66 if (delegate_) |
80 task.Run(); | 67 task.Run(); |
81 } | 68 } |
82 | 69 |
83 } // namespace remoting | 70 } // namespace remoting |
OLD | NEW |