OLD | NEW |
(Empty) | |
| 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 "vm/isolate.h" |
| 8 #include "vm/message.h" |
| 9 #include "vm/thread.h" |
| 10 |
| 11 namespace dart { |
| 12 |
| 13 NativeMessageHandler::NativeMessageHandler(const char* name, |
| 14 Dart_NativeMessageHandler func) |
| 15 : name_(strdup(name)), |
| 16 func_(func) { |
| 17 // A NativeMessageHandler always has one live port. |
| 18 increment_live_ports(); |
| 19 } |
| 20 |
| 21 |
| 22 NativeMessageHandler::~NativeMessageHandler() { |
| 23 free(name_); |
| 24 } |
| 25 |
| 26 |
| 27 #if defined(DEBUG) |
| 28 void NativeMessageHandler::CheckAccess() { |
| 29 ASSERT(Isolate::Current() == NULL); |
| 30 } |
| 31 #endif |
| 32 |
| 33 |
| 34 static void RunWorker(uword parameter) { |
| 35 NativeMessageHandler* handler = |
| 36 reinterpret_cast<NativeMessageHandler*>(parameter); |
| 37 #if defined(DEBUG) |
| 38 handler->CheckAccess(); |
| 39 #endif |
| 40 |
| 41 while (handler->HasLivePorts()) { |
| 42 Message* message = handler->queue()->Dequeue(0); |
| 43 if (message != NULL) { |
| 44 if (message->priority() >= Message::kOOBPriority) { |
| 45 // TODO(turnidge): Out of band messages will not go through |
| 46 // the regular message handler. Instead they will be |
| 47 // dispatched to special vm code. Implement. |
| 48 UNIMPLEMENTED(); |
| 49 } |
| 50 // TODO(sgjesse): Once CMessageReader::ReadObject is committed, |
| 51 // use that here and pass the resulting data object to the |
| 52 // handler instead. |
| 53 (*handler->func())(message->dest_port(), |
| 54 message->reply_port(), |
| 55 message->data()); |
| 56 delete message; |
| 57 } |
| 58 } |
| 59 } |
| 60 |
| 61 |
| 62 void NativeMessageHandler::StartWorker() { |
| 63 new Thread(RunWorker, reinterpret_cast<uword>(this)); |
| 64 } |
| 65 |
| 66 |
| 67 } // namespace dart |
OLD | NEW |