| 1 | // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
|---|---|
| 2 | // for details. All rights reserved. Use of this source code is governed by a |
| 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | |
| 5 | #include "vm/native_message_handler.h" |
| 6 | |
| 7 | #include <memory> |
| 8 | |
| 9 | #include "vm/dart_api_message.h" |
| 10 | #include "vm/isolate.h" |
| 11 | #include "vm/message.h" |
| 12 | #include "vm/snapshot.h" |
| 13 | |
| 14 | namespace dart { |
| 15 | |
| 16 | NativeMessageHandler::NativeMessageHandler(const char* name, |
| 17 | Dart_NativeMessageHandler func) |
| 18 | : name_(Utils::StrDup(name)), func_(func) {} |
| 19 | |
| 20 | NativeMessageHandler::~NativeMessageHandler() { |
| 21 | free(name_); |
| 22 | } |
| 23 | |
| 24 | #if defined(DEBUG) |
| 25 | void NativeMessageHandler::CheckAccess() { |
| 26 | ASSERT(Isolate::Current() == NULL); |
| 27 | } |
| 28 | #endif |
| 29 | |
| 30 | MessageHandler::MessageStatus NativeMessageHandler::HandleMessage( |
| 31 | std::unique_ptr<Message> message) { |
| 32 | if (message->IsOOB()) { |
| 33 | // We currently do not use OOB messages for native ports. |
| 34 | UNREACHABLE(); |
| 35 | } |
| 36 | // We create a native scope for handling the message. |
| 37 | // All allocation of objects for decoding the message is done in the |
| 38 | // zone associated with this scope. |
| 39 | ApiNativeScope scope; |
| 40 | Dart_CObject* object; |
| 41 | ApiMessageReader reader(message.get()); |
| 42 | object = reader.ReadMessage(); |
| 43 | (*func())(message->dest_port(), object); |
| 44 | return kOK; |
| 45 | } |
| 46 | |
| 47 | } // namespace dart |
| 48 |