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

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

Issue 2732163002: Implements JS bindings for mojo data pipe. (Closed)
Patch Set: optional args Created 3 years, 9 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/MojoHandle.cpp
diff --git a/third_party/WebKit/Source/core/mojo/MojoHandle.cpp b/third_party/WebKit/Source/core/mojo/MojoHandle.cpp
index d2441055265f982ca4f212811d946c9ef5815aff..aa8031099de2ac14dc5af9b242ddaec08337387c 100644
--- a/third_party/WebKit/Source/core/mojo/MojoHandle.cpp
+++ b/third_party/WebKit/Source/core/mojo/MojoHandle.cpp
@@ -11,9 +11,13 @@
#include "core/mojo/MojoCreateSharedBufferResult.h"
#include "core/mojo/MojoDuplicateBufferHandleOptions.h"
#include "core/mojo/MojoMapBufferResult.h"
+#include "core/mojo/MojoReadDataFlags.h"
+#include "core/mojo/MojoReadDataResult.h"
#include "core/mojo/MojoReadMessageFlags.h"
#include "core/mojo/MojoReadMessageResult.h"
#include "core/mojo/MojoWatcher.h"
+#include "core/mojo/MojoWriteDataFlags.h"
+#include "core/mojo/MojoWriteDataResult.h"
// Mojo messages typically do not contain many handles. In fact most
// messages do not contain any handle. An inline capacity of 4 should avoid
@@ -95,6 +99,97 @@ void MojoHandle::readMessage(const MojoReadMessageFlags& flagsDict,
resultDict.setHandles(handles);
}
+void MojoHandle::writeData(ArrayBufferOrArrayBufferView& buffer,
+ const MojoWriteDataFlags& flagsDict,
+ MojoWriteDataResult& resultDict) {
+ ::MojoWriteDataFlags flags = MOJO_WRITE_DATA_FLAG_NONE;
+ if (flagsDict.allOrNone())
+ flags |= MOJO_WRITE_DATA_FLAG_ALL_OR_NONE;
+
+ const void* elements = nullptr;
+ uint32_t numBytes = 0;
+ if (buffer.isArrayBuffer()) {
+ DOMArrayBuffer* array = buffer.getAsArrayBuffer();
+ elements = array->data();
+ numBytes = array->byteLength();
+ } else {
+ DOMArrayBufferView* view = buffer.getAsArrayBufferView();
+ elements = view->baseAddress();
+ numBytes = view->byteLength();
+ }
+
+ MojoResult result =
+ MojoWriteData(m_handle->value(), elements, &numBytes, flags);
+ resultDict.setResult(result);
+ resultDict.setNumBytes(numBytes);
+}
+
+void MojoHandle::readData(const MojoReadDataFlags& flagsDict,
+ MojoReadDataResult& resultDict) {
+ // This version of readData can only be used for querying.
+ if (!flagsDict.query()) {
+ resultDict.setResult(MOJO_RESULT_INVALID_ARGUMENT);
+ return;
+ }
+
+ uint32_t numBytes = 0;
+ MojoResult result = MojoReadData(m_handle->value(), nullptr, &numBytes,
+ MOJO_READ_DATA_FLAG_QUERY);
+ resultDict.setResult(result);
+ resultDict.setNumBytes(numBytes);
+}
+
+void MojoHandle::readData(const MojoReadDataFlags& flagsDict,
+ unsigned numBytes,
+ MojoReadDataResult& resultDict) {
+ // This version of readData can only be used for discarding.
+ if (!flagsDict.discard()) {
+ resultDict.setResult(MOJO_RESULT_INVALID_ARGUMENT);
+ return;
+ }
+
+ ::MojoReadDataFlags flags = MOJO_READ_DATA_FLAG_DISCARD;
+ if (flagsDict.allOrNone())
+ flags |= MOJO_READ_DATA_FLAG_ALL_OR_NONE;
+
+ MojoResult result =
+ MojoReadData(m_handle->value(), nullptr, &numBytes, flags);
+ resultDict.setResult(result);
+ resultDict.setNumBytes(numBytes);
+}
+
+void MojoHandle::readData(const MojoReadDataFlags& flagsDict,
+ unsigned numBytes,
jbroman 2017/03/07 16:01:44 Any reason for the disparity between read/write, w
alokp 2017/03/07 19:34:20 My reasoning for not having numBytes on write was
jbroman 2017/03/07 20:31:47 Mild preference for that (for simplicity, and cons
alokp 2017/03/07 20:49:31 Done.
+ ArrayBufferOrArrayBufferView& buffer,
yzshen1 2017/03/07 17:56:39 [just to double check] Is it because having |buffe
alokp 2017/03/07 19:34:20 Right. I think a common usage pattern would be to
+ MojoReadDataResult& resultDict) {
+ // TODO(alokp): Validate options.
+
+ // This version of readData can only be used for reading and peeking.
+ ::MojoReadDataFlags flags = MOJO_READ_DATA_FLAG_NONE;
+ if (flagsDict.allOrNone())
+ flags |= MOJO_READ_DATA_FLAG_ALL_OR_NONE;
+ if (flagsDict.peek())
+ flags |= MOJO_READ_DATA_FLAG_PEEK;
+
+ void* elements = nullptr;
+ unsigned bufferLength = 0;
+ if (buffer.isArrayBuffer()) {
+ DOMArrayBuffer* array = buffer.getAsArrayBuffer();
+ elements = array->data();
+ bufferLength = array->byteLength();
+ } else {
+ DOMArrayBufferView* view = buffer.getAsArrayBufferView();
+ elements = view->baseAddress();
+ bufferLength = view->byteLength();
+ }
+ numBytes = std::min(bufferLength, numBytes);
jbroman 2017/03/07 16:01:44 Isn't it always an error on the part of the progra
yzshen1 2017/03/07 17:56:39 We could return INVALID_ARGUMENT in this case, con
alokp 2017/03/07 19:34:20 Done.
+
+ MojoResult result =
+ MojoReadData(m_handle->value(), elements, &numBytes, flags);
+ resultDict.setResult(result);
+ resultDict.setResult(numBytes);
+}
+
void MojoHandle::mapBuffer(unsigned offset,
unsigned numBytes,
MojoMapBufferResult& resultDict) {

Powered by Google App Engine
This is Rietveld 408576698