|
@@ -26,6 +26,25 @@
|
|
|
|
|
|
namespace electron {
|
|
|
|
|
|
+namespace {
|
|
|
+
|
|
|
+bool IsValidWrappable(const v8::Local<v8::Value>& obj) {
|
|
|
+ v8::Local<v8::Object> port = v8::Local<v8::Object>::Cast(obj);
|
|
|
+
|
|
|
+ if (!port->IsObject())
|
|
|
+ return false;
|
|
|
+
|
|
|
+ if (port->InternalFieldCount() != gin::kNumberOfInternalFields)
|
|
|
+ return false;
|
|
|
+
|
|
|
+ const auto* info = static_cast<gin::WrapperInfo*>(
|
|
|
+ port->GetAlignedPointerFromInternalField(gin::kWrapperInfoIndex));
|
|
|
+
|
|
|
+ return info && info->embedder == gin::kEmbedderNativeGin;
|
|
|
+}
|
|
|
+
|
|
|
+} // namespace
|
|
|
+
|
|
|
gin::WrapperInfo MessagePort::kWrapperInfo = {gin::kEmbedderNativeGin};
|
|
|
|
|
|
MessagePort::MessagePort() = default;
|
|
@@ -48,10 +67,11 @@ void MessagePort::PostMessage(gin::Arguments* args) {
|
|
|
DCHECK(!IsNeutered());
|
|
|
|
|
|
blink::TransferableMessage transferable_message;
|
|
|
+ gin_helper::ErrorThrower thrower(args->isolate());
|
|
|
|
|
|
v8::Local<v8::Value> message_value;
|
|
|
if (!args->GetNext(&message_value)) {
|
|
|
- args->ThrowTypeError("Expected at least one argument to postMessage");
|
|
|
+ thrower.ThrowTypeError("Expected at least one argument to postMessage");
|
|
|
return;
|
|
|
}
|
|
|
|
|
@@ -61,8 +81,23 @@ void MessagePort::PostMessage(gin::Arguments* args) {
|
|
|
v8::Local<v8::Value> transferables;
|
|
|
std::vector<gin::Handle<MessagePort>> wrapped_ports;
|
|
|
if (args->GetNext(&transferables)) {
|
|
|
+ std::vector<v8::Local<v8::Value>> wrapped_port_values;
|
|
|
+ if (!gin::ConvertFromV8(args->isolate(), transferables,
|
|
|
+ &wrapped_port_values)) {
|
|
|
+ thrower.ThrowTypeError("transferables must be an array of MessagePorts");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ for (unsigned i = 0; i < wrapped_port_values.size(); ++i) {
|
|
|
+ if (!IsValidWrappable(wrapped_port_values[i])) {
|
|
|
+ thrower.ThrowTypeError("Port at index " + base::NumberToString(i) +
|
|
|
+ " is not a valid port");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
if (!gin::ConvertFromV8(args->isolate(), transferables, &wrapped_ports)) {
|
|
|
- args->ThrowError();
|
|
|
+ thrower.ThrowTypeError("Passed an invalid MessagePort");
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
@@ -70,9 +105,8 @@ void MessagePort::PostMessage(gin::Arguments* args) {
|
|
|
// Make sure we aren't connected to any of the passed-in ports.
|
|
|
for (unsigned i = 0; i < wrapped_ports.size(); ++i) {
|
|
|
if (wrapped_ports[i].get() == this) {
|
|
|
- gin_helper::ErrorThrower(args->isolate())
|
|
|
- .ThrowError("Port at index " + base::NumberToString(i) +
|
|
|
- " contains the source port.");
|
|
|
+ thrower.ThrowError("Port at index " + base::NumberToString(i) +
|
|
|
+ " contains the source port.");
|
|
|
return;
|
|
|
}
|
|
|
}
|