| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 // MessageLoopProxy interface implementation. | |
| 28 bool PluginMessageLoopProxy::PostTask( | |
| 29 const tracked_objects::Location& from_here, | |
| 30 Task* task) { | |
| 31 return PostDelayedTask(from_here, task, 0); | |
| 32 } | |
| 33 | |
| 34 bool PluginMessageLoopProxy::PostDelayedTask( | |
| 35 const tracked_objects::Location& from_here, | |
| 36 Task* task, | |
| 37 int64 delay_ms) { | |
| 38 base::AutoLock auto_lock(lock_); | |
| 39 if (!delegate_) | |
| 40 return false; | |
| 41 | |
| 42 base::Closure* springpad_closure = new base::Closure(base::Bind( | |
| 43 &PluginMessageLoopProxy::RunTaskIf, this, task)); | |
| 44 return delegate_->RunOnPluginThread( | |
| 45 delay_ms, &PluginMessageLoopProxy::TaskSpringboard, springpad_closure); | |
| 46 } | |
| 47 | |
| 48 bool PluginMessageLoopProxy::PostNonNestableTask( | |
| 49 const tracked_objects::Location& from_here, | |
| 50 Task* task) { | |
| 51 // All tasks running on this message loop are non-nestable. | |
| 52 return PostTask(from_here, task); | |
| 53 } | |
| 54 | |
| 55 bool PluginMessageLoopProxy::PostNonNestableDelayedTask( | |
| 56 const tracked_objects::Location& from_here, | |
| 57 Task* task, | |
| 58 int64 delay_ms) { | |
| 59 // All tasks running on this message loop are non-nestable. | |
| 60 return PostDelayedTask(from_here, task, delay_ms); | |
| 61 } | |
| 62 | |
| 63 bool PluginMessageLoopProxy::PostTask( | 27 bool PluginMessageLoopProxy::PostTask( |
| 64 const tracked_objects::Location& from_here, | 28 const tracked_objects::Location& from_here, |
| 65 const base::Closure& task) { | 29 const base::Closure& task) { |
| 66 return PostDelayedTask(from_here, task, 0); | 30 return PostDelayedTask(from_here, task, 0); |
| 67 } | 31 } |
| 68 | 32 |
| 69 bool PluginMessageLoopProxy::PostDelayedTask( | 33 bool PluginMessageLoopProxy::PostDelayedTask( |
| 70 const tracked_objects::Location& from_here, | 34 const tracked_objects::Location& from_here, |
| 71 const base::Closure& task, | 35 const base::Closure& task, |
| 72 int64 delay_ms) { | 36 int64 delay_ms) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 90 bool PluginMessageLoopProxy::PostNonNestableDelayedTask( | 54 bool PluginMessageLoopProxy::PostNonNestableDelayedTask( |
| 91 const tracked_objects::Location& from_here, | 55 const tracked_objects::Location& from_here, |
| 92 const base::Closure& task, | 56 const base::Closure& task, |
| 93 int64 delay_ms) { | 57 int64 delay_ms) { |
| 94 // All tasks running on this message loop are non-nestable. | 58 // All tasks running on this message loop are non-nestable. |
| 95 return PostDelayedTask(from_here, task, delay_ms); | 59 return PostDelayedTask(from_here, task, delay_ms); |
| 96 } | 60 } |
| 97 | 61 |
| 98 bool PluginMessageLoopProxy::BelongsToCurrentThread() { | 62 bool PluginMessageLoopProxy::BelongsToCurrentThread() { |
| 99 // In pepper plugins ideally we should use pp::Core::IsMainThread, | 63 // In pepper plugins ideally we should use pp::Core::IsMainThread, |
| 100 // but it is problematic becase we would need to keep reference to | 64 // but it is problematic because we would need to keep reference to |
| 101 // Core somewhere, e.g. make the delegate ref-counted. | 65 // Core somewhere, e.g. make the delegate ref-counted. |
| 102 return base::PlatformThread::CurrentId() == plugin_thread_id_; | 66 return base::PlatformThread::CurrentId() == plugin_thread_id_; |
| 103 } | 67 } |
| 104 | 68 |
| 105 // static | 69 // static |
| 106 void PluginMessageLoopProxy::TaskSpringboard(void* data) { | 70 void PluginMessageLoopProxy::TaskSpringboard(void* data) { |
| 107 base::Closure* task = reinterpret_cast<base::Closure*>(data); | 71 base::Closure* task = reinterpret_cast<base::Closure*>(data); |
| 108 task->Run(); | 72 task->Run(); |
| 109 delete task; | 73 delete task; |
| 110 } | 74 } |
| 111 | 75 |
| 112 void PluginMessageLoopProxy::RunTaskIf(Task* task) { | |
| 113 DCHECK(BelongsToCurrentThread()); | |
| 114 // |delegate_| can be changed only from our thread, so it's safe to | |
| 115 // access it without acquiring |lock_|. | |
| 116 if (delegate_) | |
| 117 task->Run(); | |
| 118 delete task; | |
| 119 } | |
| 120 | |
| 121 void PluginMessageLoopProxy::RunClosureIf(const base::Closure& task) { | 76 void PluginMessageLoopProxy::RunClosureIf(const base::Closure& task) { |
| 122 // |delegate_| can be changed only from our thread, so it's safe to | 77 // |delegate_| can be changed only from our thread, so it's safe to |
| 123 // access it without acquiring |lock_|. | 78 // access it without acquiring |lock_|. |
| 124 if (delegate_) | 79 if (delegate_) |
| 125 task.Run(); | 80 task.Run(); |
| 126 } | 81 } |
| 127 | 82 |
| 128 } // namespace remoting | 83 } // namespace remoting |
| OLD | NEW |