Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(505)

Unified Diff: third_party/WebKit/Source/core/mojo/MojoWatcher.cpp

Issue 2405093003: [WIP] Mojo native bindings interface.
Patch Set: cleanup 2 Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/core/mojo/MojoWatcher.cpp
diff --git a/third_party/WebKit/Source/core/mojo/MojoWatcher.cpp b/third_party/WebKit/Source/core/mojo/MojoWatcher.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..ccdf7fd88bad8e657246d1a33ccc263701955856
--- /dev/null
+++ b/third_party/WebKit/Source/core/mojo/MojoWatcher.cpp
@@ -0,0 +1,105 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "core/mojo/MojoWatcher.h"
+
+#include "bindings/core/v8/MojoWatchCallback.h"
+#include "bindings/core/v8/ScriptState.h"
+#include "public/platform/Platform.h"
+#include "public/platform/WebTaskRunner.h"
+
+namespace blink {
+
+class MojoWatcher::RunReadyCallbackTask : public WebTaskRunner::Task {
+ public:
+ RunReadyCallbackTask(MojoWatcher* watcher, MojoResult result)
+ : m_watcher(watcher), m_result(result) {}
+
+ void run() final {
+ if (m_watcher)
+ m_watcher->runReadyCallback(m_result);
+ }
+
+ private:
+ CrossThreadWeakPersistent<MojoWatcher> m_watcher;
+ MojoResult m_result;
+};
+
+MojoWatcher* MojoWatcher::create(mojo::Handle handle,
+ MojoHandleSignals signals,
+ ScriptState* scriptState,
+ MojoWatchCallback* callback) {
+ MojoWatcher* watcher = new MojoWatcher(
+ handle, scriptState, callback,
+ Platform::current()->currentThread()->getWebTaskRunner()->clone());
+
+ MojoResult result =
+ MojoWatch(handle.value(), signals, &MojoWatcher::onHandleReady,
+ reinterpret_cast<uintptr_t>(watcher));
+ // TODO(alokp): Consider raising an exception.
+ // Current clients expect to recieve the initial error returned by MojoWatch
+ // via watch callback.
+ if (result != MOJO_RESULT_OK) {
+ watcher->m_taskRunner->postTask(BLINK_FROM_HERE,
+ new RunReadyCallbackTask(watcher, result));
+ }
+ return watcher;
+}
+
+MojoWatcher::MojoWatcher(mojo::Handle handle,
+ ScriptState* scriptState,
+ MojoWatchCallback* callback,
+ std::unique_ptr<WebTaskRunner> taskRunner)
+ : m_handle(handle),
+ m_scriptState(scriptState),
+ m_callback(callback),
alokp 2016/10/20 18:21:38 Do I need to do anything else to manage the lifeti
+ m_taskRunner(std::move(taskRunner)) {}
+
+MojoWatcher::~MojoWatcher() {
+ DCHECK(m_taskRunner->runsTasksOnCurrentThread());
+ cancel();
+}
+
+MojoResult MojoWatcher::cancel() {
+ DCHECK(m_taskRunner->runsTasksOnCurrentThread());
+
+ MojoResult result = MOJO_RESULT_OK;
+ if (m_handle.is_valid()) {
+ result =
+ MojoCancelWatch(m_handle.value(), reinterpret_cast<uintptr_t>(this));
+ m_handle = mojo::Handle();
+ }
+ return result;
+}
+
+DEFINE_TRACE(MojoWatcher) {
+ visitor->trace(m_callback);
+}
+
+void MojoWatcher::onHandleReady(uintptr_t context,
+ MojoResult result,
+ MojoHandleSignalsState,
+ MojoWatchNotificationFlags) {
+ // It is safe to assume the MojoWatcher still exists because this
+ // callback will never be run after MojoWatcher destructor,
+ // which cancels the watch.
+ MojoWatcher* watcher = reinterpret_cast<MojoWatcher*>(context);
+ if (watcher->m_taskRunner->runsTasksOnCurrentThread()) {
+ watcher->runReadyCallback(result);
+ } else {
+ watcher->m_taskRunner->postTask(BLINK_FROM_HERE,
+ new RunReadyCallbackTask(watcher, result));
+ }
+}
+
+void MojoWatcher::runReadyCallback(MojoResult result) {
+ DCHECK(m_taskRunner->runsTasksOnCurrentThread());
+
+ if (result != MOJO_RESULT_OK)
+ cancel();
+
+ m_callback->call(m_scriptState.get(), nullptr, result);
+}
+
+} // namespace blink
« no previous file with comments | « third_party/WebKit/Source/core/mojo/MojoWatcher.h ('k') | third_party/WebKit/Source/core/mojo/MojoWatcher.idl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698