Chromium Code Reviews| 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 "core/dom/ExecutionContext.h" | |
| 10 #include "core/dom/ExecutionContextTask.h" | |
| 11 | |
| 12 namespace blink { | |
| 13 | |
| 14 MojoWatcher* MojoWatcher::create(mojo::Handle handle, | |
| 15 MojoHandleSignals signals, | |
| 16 ScriptState* scriptState, | |
| 17 MojoWatchCallback* callback) { | |
| 18 MojoWatcher* watcher = new MojoWatcher(handle, scriptState, callback); | |
| 19 | |
| 20 MojoResult result = | |
| 21 MojoWatch(handle.value(), signals, &MojoWatcher::onHandleReady, | |
| 22 reinterpret_cast<uintptr_t>(watcher)); | |
| 23 // TODO(alokp): Consider raising an exception. | |
| 24 // Current clients expect to recieve the initial error returned by MojoWatch | |
| 25 // via watch callback. | |
| 26 if (result != MOJO_RESULT_OK) { | |
| 27 watcher->m_taskRunner->postTask( | |
| 28 BLINK_FROM_HERE, | |
| 29 createSameThreadTask(&MojoWatcher::runReadyCallback, | |
| 30 wrapWeakPersistent(watcher), result)); | |
| 31 } | |
| 32 return watcher; | |
| 33 } | |
| 34 | |
| 35 MojoWatcher::MojoWatcher(mojo::Handle handle, | |
| 36 ScriptState* scriptState, | |
| 37 MojoWatchCallback* callback) | |
| 38 : m_handle(handle), | |
| 39 m_scriptState(scriptState), | |
| 40 m_callback(callback), | |
| 41 m_taskRunner(scriptState->getExecutionContext()) {} | |
| 42 | |
| 43 MojoWatcher::~MojoWatcher() { | |
| 44 cancel(); | |
| 45 } | |
| 46 | |
| 47 MojoResult MojoWatcher::cancel() { | |
| 48 MojoResult result = MOJO_RESULT_OK; | |
| 49 if (m_handle.is_valid()) { | |
| 50 result = | |
| 51 MojoCancelWatch(m_handle.value(), reinterpret_cast<uintptr_t>(this)); | |
| 52 m_handle = mojo::Handle(); | |
| 53 } | |
| 54 return result; | |
| 55 } | |
| 56 | |
| 57 DEFINE_TRACE(MojoWatcher) { | |
| 58 visitor->trace(m_callback); | |
| 59 } | |
| 60 | |
| 61 void MojoWatcher::onHandleReady(uintptr_t context, | |
| 62 MojoResult result, | |
| 63 MojoHandleSignalsState, | |
| 64 MojoWatchNotificationFlags) { | |
| 65 // It is safe to assume the MojoWatcher still exists because this | |
| 66 // callback will never be run after MojoWatcher destructor, | |
| 67 // which cancels the watch. | |
| 68 MojoWatcher* watcher = reinterpret_cast<MojoWatcher*>(context); | |
| 69 watcher->m_taskRunner->postTask( | |
|
jbroman
2016/12/20 23:21:57
Even though this reference to ExecutionContext is
alokp
2017/01/09 23:33:10
Hmm I see multiple other instances of cross-thread
jbroman
2017/01/10 20:46:30
I believe that is probably safe, but for subtle re
alokp
2017/01/11 21:53:01
Thanks for the code snippet. It works. Done.
| |
| 70 BLINK_FROM_HERE, | |
| 71 createCrossThreadTask(&MojoWatcher::runReadyCallback, | |
| 72 wrapCrossThreadWeakPersistent(watcher), result)); | |
| 73 } | |
| 74 | |
| 75 void MojoWatcher::runReadyCallback(MojoResult result) { | |
| 76 if (!m_handle.is_valid()) | |
| 77 return; | |
| 78 | |
| 79 if (result != MOJO_RESULT_OK) | |
| 80 cancel(); | |
| 81 | |
| 82 m_callback->call(m_scriptState, this, result); | |
|
jbroman
2016/12/20 23:21:57
I don't think m_scriptState is needed for this (an
alokp
2017/01/09 23:33:10
Done.
| |
| 83 } | |
| 84 | |
| 85 } // namespace blink | |
| OLD | NEW |