OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 "core/mojo/MojoWatcher.h" | |
6 | |
7 #include "bindings/core/v8/MojoWatchCallback.h" | |
8 #include "bindings/core/v8/ScriptState.h" | |
9 #include "public/platform/Platform.h" | |
10 #include "public/platform/WebTaskRunner.h" | |
11 | |
12 namespace blink { | |
13 | |
14 class MojoWatcher::RunReadyCallbackTask : public WebTaskRunner::Task { | |
15 public: | |
16 RunReadyCallbackTask(MojoWatcher* watcher, MojoResult result) | |
17 : m_watcher(watcher), m_result(result) {} | |
18 | |
19 void run() final { | |
20 if (m_watcher) | |
21 m_watcher->runReadyCallback(m_result); | |
22 } | |
23 | |
24 private: | |
25 CrossThreadWeakPersistent<MojoWatcher> m_watcher; | |
26 MojoResult m_result; | |
27 }; | |
28 | |
29 MojoWatcher* MojoWatcher::create(mojo::Handle handle, | |
30 MojoHandleSignals signals, | |
31 ScriptState* scriptState, | |
32 MojoWatchCallback* callback) { | |
33 MojoWatcher* watcher = new MojoWatcher( | |
34 handle, scriptState, callback, | |
35 Platform::current()->currentThread()->getWebTaskRunner()->clone()); | |
36 | |
37 MojoResult result = | |
38 MojoWatch(handle.value(), signals, &MojoWatcher::onHandleReady, | |
39 reinterpret_cast<uintptr_t>(watcher)); | |
40 // TODO(alokp): Consider raising an exception. | |
41 // Current clients expect to recieve the initial error returned by MojoWatch | |
42 // via watch callback. | |
43 if (result != MOJO_RESULT_OK) { | |
44 watcher->m_taskRunner->postTask(BLINK_FROM_HERE, | |
45 new RunReadyCallbackTask(watcher, result)); | |
46 } | |
47 return watcher; | |
48 } | |
49 | |
50 MojoWatcher::MojoWatcher(mojo::Handle handle, | |
51 ScriptState* scriptState, | |
52 MojoWatchCallback* callback, | |
53 std::unique_ptr<WebTaskRunner> taskRunner) | |
54 : m_handle(handle), | |
55 m_scriptState(scriptState), | |
56 m_callback(callback), | |
alokp
2016/10/20 18:21:38
Do I need to do anything else to manage the lifeti
| |
57 m_taskRunner(std::move(taskRunner)) {} | |
58 | |
59 MojoWatcher::~MojoWatcher() { | |
60 DCHECK(m_taskRunner->runsTasksOnCurrentThread()); | |
61 cancel(); | |
62 } | |
63 | |
64 MojoResult MojoWatcher::cancel() { | |
65 DCHECK(m_taskRunner->runsTasksOnCurrentThread()); | |
66 | |
67 MojoResult result = MOJO_RESULT_OK; | |
68 if (m_handle.is_valid()) { | |
69 result = | |
70 MojoCancelWatch(m_handle.value(), reinterpret_cast<uintptr_t>(this)); | |
71 m_handle = mojo::Handle(); | |
72 } | |
73 return result; | |
74 } | |
75 | |
76 DEFINE_TRACE(MojoWatcher) { | |
77 visitor->trace(m_callback); | |
78 } | |
79 | |
80 void MojoWatcher::onHandleReady(uintptr_t context, | |
81 MojoResult result, | |
82 MojoHandleSignalsState, | |
83 MojoWatchNotificationFlags) { | |
84 // It is safe to assume the MojoWatcher still exists because this | |
85 // callback will never be run after MojoWatcher destructor, | |
86 // which cancels the watch. | |
87 MojoWatcher* watcher = reinterpret_cast<MojoWatcher*>(context); | |
88 if (watcher->m_taskRunner->runsTasksOnCurrentThread()) { | |
89 watcher->runReadyCallback(result); | |
90 } else { | |
91 watcher->m_taskRunner->postTask(BLINK_FROM_HERE, | |
92 new RunReadyCallbackTask(watcher, result)); | |
93 } | |
94 } | |
95 | |
96 void MojoWatcher::runReadyCallback(MojoResult result) { | |
97 DCHECK(m_taskRunner->runsTasksOnCurrentThread()); | |
98 | |
99 if (result != MOJO_RESULT_OK) | |
100 cancel(); | |
101 | |
102 m_callback->call(m_scriptState.get(), nullptr, result); | |
103 } | |
104 | |
105 } // namespace blink | |
OLD | NEW |