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