Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Public Member Functions | Friends | List of all members
dart::JSONStream Class Reference

#include <json_stream.h>

Inheritance diagram for dart::JSONStream:
dart::ValueObject

Public Member Functions

 JSONStream (intptr_t buf_size=256)
 
void Setup (Zone *zone, Dart_Port reply_port, const Instance &seq, const String &method, const Array &param_keys, const Array &param_values, bool parameters_are_dart_objects=false)
 
void SetupError ()
 
void PrintError (intptr_t code, const char *details_format,...) PRINTF_ATTRIBUTE(3
 
void void PostReply ()
 
void set_id_zone (ServiceIdZone *id_zone)
 
ServiceIdZoneid_zone ()
 
TextBufferbuffer ()
 
const char * ToCString ()
 
void Steal (char **buffer, intptr_t *buffer_length)
 
void set_reply_port (Dart_Port port)
 
bool include_private_members () const
 
void set_include_private_members (bool include_private_members)
 
bool IsAllowableKey (const char *key)
 
void SetParams (const char **param_keys, const char **param_values, intptr_t num_params)
 
Dart_Port reply_port () const
 
intptr_t NumObjectParameters () const
 
ObjectPtr GetObjectParameterKey (intptr_t i) const
 
ObjectPtr GetObjectParameterValue (intptr_t i) const
 
ObjectPtr LookupObjectParam (const char *key) const
 
intptr_t num_params () const
 
const char * GetParamKey (intptr_t i) const
 
const char * GetParamValue (intptr_t i) const
 
const char * LookupParam (const char *key) const
 
bool HasParam (const char *key) const
 
bool ParamIs (const char *key, const char *value) const
 
const char * method () const
 
const char ** param_keys () const
 
const char ** param_values () const
 
void set_offset (intptr_t value)
 
void set_count (intptr_t value)
 
void ComputeOffsetAndCount (intptr_t length, intptr_t *offset, intptr_t *count)
 
void AppendBytes (const uint8_t *buffer, intptr_t buffer_length)
 
void AppendSerializedObject (const char *serialized_object)
 
void AppendSerializedObject (const char *property_name, const char *serialized_object)
 
void PrintCommaIfNeeded ()
 
JSONWriterwriter ()
 

Friends

class JSONObject
 
class JSONArray
 
class JSONBase64String
 
class TimelineEvent
 

Detailed Description

Definition at line 76 of file json_stream.h.

Constructor & Destructor Documentation

◆ JSONStream()

dart::JSONStream::JSONStream ( intptr_t  buf_size = 256)
explicit

Definition at line 26 of file json_stream.cc.

27 : writer_(buf_size),
28 default_id_zone_(),
29 id_zone_(&default_id_zone_),
30 reply_port_(ILLEGAL_PORT),
31 seq_(nullptr),
32 parameter_keys_(nullptr),
33 parameter_values_(nullptr),
34 method_(""),
35 param_keys_(nullptr),
36 param_values_(nullptr),
37 num_params_(0),
38 offset_(0),
39 count_(-1),
40 include_private_members_(true),
41 ignore_object_depth_(0) {
42 ObjectIdRing* ring = nullptr;
43 Isolate* isolate = Isolate::Current();
44 if (isolate != nullptr) {
45 ring = isolate->EnsureObjectIdRing();
46 }
47 default_id_zone_.Init(ring, ObjectIdRing::kAllocateId);
48}
static Isolate * Current()
Definition isolate.h:939
void Init(ObjectIdRing *ring, ObjectIdRing::IdPolicy policy)
Definition service.cc:374
#define ILLEGAL_PORT
Definition dart_api.h:1530

Member Function Documentation

◆ AppendBytes()

void dart::JSONStream::AppendBytes ( const uint8_t *  buffer,
intptr_t  buffer_length 
)
inline

Definition at line 160 of file json_stream.h.

160 {
161 writer_.AppendBytes(buffer, buffer_length);
162 }
TextBuffer * buffer()
Definition json_stream.h:97
void AppendBytes(const uint8_t *buffer, intptr_t buffer_length)

◆ AppendSerializedObject() [1/2]

void dart::JSONStream::AppendSerializedObject ( const char *  property_name,
const char *  serialized_object 
)
inline

Definition at line 170 of file json_stream.h.

171 {
172 writer_.AppendSerializedObject(property_name, serialized_object);
173 }
void AppendSerializedObject(const char *serialized_object)

◆ AppendSerializedObject() [2/2]

void dart::JSONStream::AppendSerializedObject ( const char *  serialized_object)
inline

Definition at line 165 of file json_stream.h.

165 {
166 writer_.AppendSerializedObject(serialized_object);
167 }

◆ buffer()

TextBuffer * dart::JSONStream::buffer ( )
inline

Definition at line 97 of file json_stream.h.

97{ return writer_.buffer(); }
TextBuffer * buffer()
Definition json_writer.h:19

◆ ComputeOffsetAndCount()

void dart::JSONStream::ComputeOffsetAndCount ( intptr_t  length,
intptr_t *  offset,
intptr_t *  count 
)

Definition at line 294 of file json_stream.cc.

296 {
297 // This function is written to avoid adding (count + offset) in case
298 // that triggers an integer overflow.
299 *offset = offset_;
300 if (*offset > length) {
301 *offset = length;
302 }
303 intptr_t remaining = length - *offset;
304 *count = count_;
305 if (*count < 0 || *count > remaining) {
306 *count = remaining;
307 }
308}
int count
size_t length
Point offset

◆ GetObjectParameterKey()

ObjectPtr dart::JSONStream::GetObjectParameterKey ( intptr_t  i) const

Definition at line 456 of file json_stream.cc.

456 {
457 ASSERT((i >= 0) && (i < NumObjectParameters()));
458 return parameter_keys_->At(i);
459}
ObjectPtr At(intptr_t index) const
Definition object.h:10854
intptr_t NumObjectParameters() const
#define ASSERT(E)

◆ GetObjectParameterValue()

ObjectPtr dart::JSONStream::GetObjectParameterValue ( intptr_t  i) const

Definition at line 461 of file json_stream.cc.

461 {
462 ASSERT((i >= 0) && (i < NumObjectParameters()));
463 return parameter_values_->At(i);
464}

◆ GetParamKey()

const char * dart::JSONStream::GetParamKey ( intptr_t  i) const
inline

Definition at line 130 of file json_stream.h.

130{ return param_keys_[i]; }

◆ GetParamValue()

const char * dart::JSONStream::GetParamValue ( intptr_t  i) const
inline

Definition at line 131 of file json_stream.h.

131{ return param_values_[i]; }

◆ HasParam()

bool dart::JSONStream::HasParam ( const char *  key) const

Definition at line 282 of file json_stream.cc.

282 {
283 ASSERT(key);
284 return LookupParam(key) != nullptr;
285}
const char * LookupParam(const char *key) const

◆ id_zone()

ServiceIdZone * dart::JSONStream::id_zone ( )
inline

Definition at line 95 of file json_stream.h.

95{ return id_zone_; }

◆ include_private_members()

bool dart::JSONStream::include_private_members ( ) const
inline

Definition at line 106 of file json_stream.h.

106{ return include_private_members_; }

◆ IsAllowableKey()

bool dart::JSONStream::IsAllowableKey ( const char *  key)
inline

Definition at line 111 of file json_stream.h.

111 {
112 if (include_private_members_) {
113 return true;
114 }
115 return *key != '_';
116 }

◆ LookupObjectParam()

ObjectPtr dart::JSONStream::LookupObjectParam ( const char *  key) const

Definition at line 466 of file json_stream.cc.

466 {
467 const String& key = String::Handle(String::New(c_key));
468 Object& test = Object::Handle();
469 const intptr_t num_object_parameters = NumObjectParameters();
470 for (intptr_t i = 0; i < num_object_parameters; i++) {
472 if (test.IsString() && String::Cast(test).Equals(key)) {
473 return GetObjectParameterValue(i);
474 }
475 }
476 return Object::null();
477}
ObjectPtr GetObjectParameterKey(intptr_t i) const
ObjectPtr GetObjectParameterValue(intptr_t i) const
static ObjectPtr null()
Definition object.h:433
static Object & Handle()
Definition object.h:407
static StringPtr New(const char *cstr, Heap::Space space=Heap::kNew)
Definition object.cc:23777

◆ LookupParam()

const char * dart::JSONStream::LookupParam ( const char *  key) const

Definition at line 273 of file json_stream.cc.

273 {
274 for (int i = 0; i < num_params(); i++) {
275 if (strcmp(key, param_keys_[i]) == 0) {
276 return param_values_[i];
277 }
278 }
279 return nullptr;
280}
intptr_t num_params() const

◆ method()

const char * dart::JSONStream::method ( ) const
inline

Definition at line 141 of file json_stream.h.

141{ return method_; }

◆ num_params()

intptr_t dart::JSONStream::num_params ( ) const
inline

Definition at line 129 of file json_stream.h.

129{ return num_params_; }

◆ NumObjectParameters()

intptr_t dart::JSONStream::NumObjectParameters ( ) const

Definition at line 447 of file json_stream.cc.

447 {
448 if (parameter_keys_ == nullptr) {
449 return 0;
450 }
451 ASSERT(parameter_keys_ != nullptr);
452 ASSERT(parameter_values_ != nullptr);
453 return parameter_keys_->Length();
454}
intptr_t Length() const
Definition object.h:10808

◆ param_keys()

const char ** dart::JSONStream::param_keys ( ) const
inline

Definition at line 142 of file json_stream.h.

142{ return param_keys_; }

◆ param_values()

const char ** dart::JSONStream::param_values ( ) const
inline

Definition at line 143 of file json_stream.h.

143{ return param_values_; }

◆ ParamIs()

bool dart::JSONStream::ParamIs ( const char *  key,
const char *  value 
) const

Definition at line 287 of file json_stream.cc.

287 {
288 ASSERT(key);
289 ASSERT(value);
290 const char* key_value = LookupParam(key);
291 return (key_value != nullptr) && (strcmp(key_value, value) == 0);
292}

◆ PostReply()

void dart::JSONStream::PostReply ( )

Definition at line 196 of file json_stream.cc.

196 {
197 ASSERT(seq_ != nullptr);
199 set_reply_port(ILLEGAL_PORT); // Prevent double replies.
200 if (seq_->IsString()) {
201 const String& str = String::Cast(*seq_);
202 PrintProperty("id", str.ToCString());
203 } else if (seq_->IsInteger()) {
204 const Integer& integer = Integer::Cast(*seq_);
205 PrintProperty64("id", integer.AsInt64Value());
206 } else if (seq_->IsDouble()) {
207 const Double& dbl = Double::Cast(*seq_);
208 PrintProperty("id", dbl.value());
209 } else if (seq_->IsNull()) {
210 if (port == ILLEGAL_PORT) {
211 // This path is only used in tests.
212 buffer()->AddChar('}'); // Finish our message.
213 char* cstr;
214 intptr_t length;
215 Steal(&cstr, &length);
216 OS::PrintErr("-----\nDropping reply:\n%s\n-----\n", cstr);
217 free(cstr);
218 }
219 // JSON-RPC 2.0 says that a request with a null ID shouldn't get a reply.
220 PostNullReply(port);
221 return;
222 }
223 ASSERT(port != ILLEGAL_PORT);
224
225 buffer()->AddChar('}'); // Finish our message.
226 char* cstr;
227 intptr_t length;
228 Steal(&cstr, &length);
229
230 bool result;
231 {
232 TransitionVMToNative transition(Thread::Current());
233 Dart_CObject bytes;
237 bytes.value.as_external_typed_data.data = reinterpret_cast<uint8_t*>(cstr);
238 bytes.value.as_external_typed_data.peer = cstr;
240 Dart_CObject* elements[1];
241 elements[0] = &bytes;
244 message.value.as_array.length = 1;
245 message.value.as_array.values = elements;
247 }
248
249 if (!result) {
250 free(cstr);
251 }
252
253 if (FLAG_trace_service) {
254 Isolate* isolate = Isolate::Current();
255 ASSERT(isolate != nullptr);
256 int64_t main_port = static_cast<int64_t>(isolate->main_port());
257 const char* isolate_name = isolate->name();
258 int64_t total_time = OS::GetCurrentTimeMicros() - setup_time_micros_;
259 if (result) {
260 OS::PrintErr("[+%" Pd64 "ms] Isolate (%" Pd64
261 ") %s processed service request %s (%" Pd64 "us)\n",
262 Dart::UptimeMillis(), main_port, isolate_name, method_,
263 total_time);
264 } else {
265 OS::PrintErr("[+%" Pd64 "ms] Isolate (%" Pd64
266 ") %s processed service request %s (%" Pd64 "us) FAILED\n",
267 Dart::UptimeMillis(), main_port, isolate_name, method_,
268 total_time);
269 }
270 }
271}
void AddChar(char ch)
static int64_t UptimeMillis()
Definition dart.h:76
Dart_Port reply_port() const
void set_reply_port(Dart_Port port)
void Steal(char **buffer, intptr_t *buffer_length)
static void static void PrintErr(const char *format,...) PRINTF_ATTRIBUTE(1
static int64_t GetCurrentTimeMicros()
bool IsNull() const
Definition object.h:363
static Thread * Current()
Definition thread.h:361
int64_t Dart_Port
Definition dart_api.h:1524
@ Dart_TypedData_kUint8
Definition dart_api.h:2606
@ Dart_CObject_kArray
@ Dart_CObject_kExternalTypedData
GAsyncResult * result
Win32Message message
static void Finalizer(void *isolate_callback_data, void *buffer)
DART_EXPORT bool Dart_PostCObject(Dart_Port port_id, Dart_CObject *message)
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service port
Definition switches.h:87
#define Pd64
Definition globals.h:416
Dart_HandleFinalizer callback
union _Dart_CObject::@86 value
Dart_CObject_Type type
struct _Dart_CObject::@86::@91 as_external_typed_data

◆ PrintCommaIfNeeded()

void dart::JSONStream::PrintCommaIfNeeded ( )
inline

Definition at line 175 of file json_stream.h.

175{ writer_.PrintCommaIfNeeded(); }
void PrintCommaIfNeeded()

◆ PrintError()

void dart::JSONStream::PrintError ( intptr_t  code,
const char *  details_format,
  ... 
)

Definition at line 163 of file json_stream.cc.

163 {
164 SetupError();
165 JSONObject jsobj(this);
166 jsobj.AddProperty("code", code);
167 jsobj.AddProperty("message", GetJSONRpcErrorMessage(code));
168 {
169 JSONObject data(&jsobj, "data");
170 PrintRequest(&data, this);
171 if (details_format != nullptr) {
172 va_list measure_args;
173 va_start(measure_args, details_format);
174 intptr_t len = Utils::VSNPrint(nullptr, 0, details_format, measure_args);
175 va_end(measure_args);
176
177 char* buffer = Thread::Current()->zone()->Alloc<char>(len + 1);
178 va_list print_args;
179 va_start(print_args, details_format);
180 Utils::VSNPrint(buffer, (len + 1), details_format, print_args);
181 va_end(print_args);
182 data.AddProperty("details", buffer);
183 }
184 }
185}
friend class JSONObject
Zone * zone() const
static int static int VSNPrint(char *str, size_t size, const char *format, va_list args)
ElementType * Alloc(intptr_t length)
va_start(args, format)
va_end(args)
static const char * GetJSONRpcErrorMessage(intptr_t code)
static int8_t data[kExtLength]
static void PrintRequest(JSONObject *obj, JSONStream *js)

◆ reply_port()

Dart_Port dart::JSONStream::reply_port ( ) const
inline

Definition at line 122 of file json_stream.h.

122{ return reply_port_; }

◆ set_count()

void dart::JSONStream::set_count ( intptr_t  value)
inline

Definition at line 150 of file json_stream.h.

150 {
151 ASSERT(value > 0);
152 count_ = value;
153 }
uint8_t value

◆ set_id_zone()

void dart::JSONStream::set_id_zone ( ServiceIdZone id_zone)
inline

Definition at line 94 of file json_stream.h.

94{ id_zone_ = id_zone; }
ServiceIdZone * id_zone()
Definition json_stream.h:95

◆ set_include_private_members()

void dart::JSONStream::set_include_private_members ( bool  include_private_members)
inline

Definition at line 107 of file json_stream.h.

107 {
108 include_private_members_ = include_private_members;
109 }
bool include_private_members() const

◆ set_offset()

void dart::JSONStream::set_offset ( intptr_t  value)
inline

Definition at line 145 of file json_stream.h.

145 {
146 ASSERT(value > 0);
147 offset_ = value;
148 }

◆ set_reply_port()

void dart::JSONStream::set_reply_port ( Dart_Port  port)

Definition at line 443 of file json_stream.cc.

443 {
444 reply_port_ = port;
445}

◆ SetParams()

void dart::JSONStream::SetParams ( const char **  param_keys,
const char **  param_values,
intptr_t  num_params 
)

Definition at line 479 of file json_stream.cc.

481 {
482 param_keys_ = param_keys;
483 param_values_ = param_values;
484 num_params_ = num_params;
485}
const char ** param_values() const
const char ** param_keys() const

◆ Setup()

void dart::JSONStream::Setup ( Zone zone,
Dart_Port  reply_port,
const Instance seq,
const String method,
const Array param_keys,
const Array param_values,
bool  parameters_are_dart_objects = false 
)

Definition at line 50 of file json_stream.cc.

56 {
58 seq_ = &Instance::ZoneHandle(seq.ptr());
59 method_ = method.ToCString();
60
61 if (parameters_are_dart_objects) {
62 parameter_keys_ = &Array::ZoneHandle(param_keys.ptr());
63 parameter_values_ = &Array::ZoneHandle(param_values.ptr());
64 ASSERT(parameter_keys_->Length() == parameter_values_->Length());
65 } else if (param_keys.Length() > 0) {
66 String& string_iterator = String::Handle();
67 ASSERT(param_keys.Length() == param_values.Length());
68 const char** param_keys_native =
69 zone->Alloc<const char*>(param_keys.Length());
70 const char** param_values_native =
71 zone->Alloc<const char*>(param_keys.Length());
72 for (intptr_t i = 0; i < param_keys.Length(); i++) {
73 string_iterator ^= param_keys.At(i);
74 param_keys_native[i] =
75 zone->MakeCopyOfString(string_iterator.ToCString());
76 string_iterator ^= param_values.At(i);
77 param_values_native[i] =
78 zone->MakeCopyOfString(string_iterator.ToCString());
79 }
80 SetParams(param_keys_native, param_values_native, param_keys.Length());
81 }
82
83 if (FLAG_trace_service) {
84 Isolate* isolate = Isolate::Current();
85 ASSERT(isolate != nullptr);
86 int64_t main_port = static_cast<int64_t>(isolate->main_port());
87 const char* isolate_name = isolate->name();
88 setup_time_micros_ = OS::GetCurrentTimeMicros();
89 OS::PrintErr("[+%" Pd64 "ms] Isolate (%" Pd64
90 ") %s processing service "
91 "request %s\n",
92 Dart::UptimeMillis(), main_port, isolate_name, method_);
93 }
94 const char* kIncludePrivateMembersKey = "_includePrivateMembers";
95 if (HasParam(kIncludePrivateMembersKey)) {
96 include_private_members_ = ParamIs(kIncludePrivateMembersKey, "true");
97 }
98 buffer()->Printf("{\"jsonrpc\":\"2.0\", \"result\":");
99}
intptr_t Printf(const char *format,...) PRINTF_ATTRIBUTE(2
bool ParamIs(const char *key, const char *value) const
bool HasParam(const char *key) const
const char * method() const
void SetParams(const char **param_keys, const char **param_values, intptr_t num_params)
static Object & ZoneHandle()
Definition object.h:419

◆ SetupError()

void dart::JSONStream::SetupError ( )

Definition at line 101 of file json_stream.cc.

101 {
102 Clear();
103 buffer()->Printf("{\"jsonrpc\":\"2.0\", \"error\":");
104}

◆ Steal()

void dart::JSONStream::Steal ( char **  buffer,
intptr_t *  buffer_length 
)
inline

Definition at line 100 of file json_stream.h.

100 {
101 writer_.Steal(buffer, buffer_length);
102 }
void Steal(char **buffer, intptr_t *buffer_length)

◆ ToCString()

const char * dart::JSONStream::ToCString ( )
inline

Definition at line 98 of file json_stream.h.

98{ return writer_.ToCString(); }
const char * ToCString()
Definition json_writer.h:20

◆ writer()

JSONWriter * dart::JSONStream::writer ( )
inline

Definition at line 176 of file json_stream.h.

176{ return &writer_; }

Friends And Related Symbol Documentation

◆ JSONArray

friend class JSONArray
friend

Definition at line 363 of file json_stream.h.

◆ JSONBase64String

friend class JSONBase64String
friend

Definition at line 364 of file json_stream.h.

◆ JSONObject

friend class JSONObject
friend

Definition at line 362 of file json_stream.h.

◆ TimelineEvent

friend class TimelineEvent
friend

Definition at line 365 of file json_stream.h.


The documentation for this class was generated from the following files: