| 1 | // Copyright (c) 2013, 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 | #ifndef RUNTIME_VM_SERVICE_ISOLATE_H_ |
| 6 | #define RUNTIME_VM_SERVICE_ISOLATE_H_ |
| 7 | |
| 8 | #include "include/dart_api.h" |
| 9 | |
| 10 | #include "vm/allocation.h" |
| 11 | #include "vm/os_thread.h" |
| 12 | |
| 13 | namespace dart { |
| 14 | |
| 15 | class Isolate; |
| 16 | class ObjectPointerVisitor; |
| 17 | class SendPort; |
| 18 | |
| 19 | class ServiceIsolate : public AllStatic { |
| 20 | #if !defined(PRODUCT) |
| 21 | |
| 22 | public: |
| 23 | static const char* kName; |
| 24 | static bool NameEquals(const char* name); |
| 25 | |
| 26 | static bool Exists(); |
| 27 | static bool IsRunning(); |
| 28 | static bool IsServiceIsolate(const Isolate* isolate); |
| 29 | static bool IsServiceIsolateDescendant(Isolate* isolate); |
| 30 | static Dart_Port Port(); |
| 31 | static void WaitForServiceIsolateStartup(); |
| 32 | |
| 33 | // Returns `true` if the request was sucessfully sent. If it was, the |
| 34 | // [reply_port] will receive a Dart_TypedData_kUint8 response json. |
| 35 | // |
| 36 | // If sending the rpc failed and [error] is not `nullptr` then [error] might |
| 37 | // be set to a string containting the reason for the failure. If so, the |
| 38 | // caller is responsible for free()ing the error. |
| 39 | static bool SendServiceRpc(uint8_t* request_json, |
| 40 | intptr_t request_json_length, |
| 41 | Dart_Port reply_port, |
| 42 | char** error); |
| 43 | |
| 44 | static void Run(); |
| 45 | static bool SendIsolateStartupMessage(); |
| 46 | static bool SendIsolateShutdownMessage(); |
| 47 | static void SendServiceExitMessage(); |
| 48 | static void Shutdown(); |
| 49 | |
| 50 | static void BootVmServiceLibrary(); |
| 51 | |
| 52 | static void RequestServerInfo(const SendPort& sp); |
| 53 | static void ControlWebServer(const SendPort& sp, bool enable); |
| 54 | |
| 55 | static void SetServerAddress(const char* address); |
| 56 | |
| 57 | // Returns the server's web address or NULL if none is running. |
| 58 | static const char* server_address() { return server_address_; } |
| 59 | |
| 60 | static void VisitObjectPointers(ObjectPointerVisitor* visitor); |
| 61 | |
| 62 | private: |
| 63 | static void KillServiceIsolate(); |
| 64 | |
| 65 | protected: |
| 66 | static void SetServicePort(Dart_Port port); |
| 67 | static void SetServiceIsolate(Isolate* isolate); |
| 68 | static void FinishedExiting(); |
| 69 | static void FinishedInitializing(); |
| 70 | static void InitializingFailed(char* error); |
| 71 | static void MaybeMakeServiceIsolate(Isolate* isolate); |
| 72 | static Dart_IsolateGroupCreateCallback create_group_callback() { |
| 73 | return create_group_callback_; |
| 74 | } |
| 75 | |
| 76 | static Dart_IsolateGroupCreateCallback create_group_callback_; |
| 77 | static Monitor* monitor_; |
| 78 | enum State { |
| 79 | kStopped, |
| 80 | kStarting, |
| 81 | kStarted, |
| 82 | kStopping, |
| 83 | }; |
| 84 | static State state_; |
| 85 | static Isolate* isolate_; |
| 86 | static Dart_Port port_; |
| 87 | static Dart_Port origin_; |
| 88 | static char* server_address_; |
| 89 | |
| 90 | // If starting the service-isolate failed, this error might provide the reason |
| 91 | // for the failure. |
| 92 | static char* startup_failure_reason_; |
| 93 | #else |
| 94 | |
| 95 | public: |
| 96 | static bool NameEquals(const char* name) { return false; } |
| 97 | static bool Exists() { return false; } |
| 98 | static bool IsRunning() { return false; } |
| 99 | static bool IsServiceIsolate(const Isolate* isolate) { return false; } |
| 100 | static bool IsServiceIsolateDescendant(Isolate* isolate) { return false; } |
| 101 | static void Run() {} |
| 102 | static bool SendIsolateStartupMessage() { return false; } |
| 103 | static bool SendIsolateShutdownMessage() { return false; } |
| 104 | static void SendServiceExitMessage() {} |
| 105 | static void Shutdown() {} |
| 106 | static void VisitObjectPointers(ObjectPointerVisitor* visitor) {} |
| 107 | |
| 108 | protected: |
| 109 | static void SetServiceIsolate(Isolate* isolate) { UNREACHABLE(); } |
| 110 | #endif // !defined(PRODUCT) |
| 111 | |
| 112 | friend class Dart; |
| 113 | friend class Isolate; |
| 114 | friend class RunServiceTask; |
| 115 | friend class ServiceIsolateNatives; |
| 116 | }; |
| 117 | |
| 118 | } // namespace dart |
| 119 | |
| 120 | #endif // RUNTIME_VM_SERVICE_ISOLATE_H_ |
| 121 | |