• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "tooling/base/pt_params.h"
17 
18 namespace panda::ecmascript::tooling {
Create(const PtJson & params)19 std::unique_ptr<EnableParams> EnableParams::Create(const PtJson &params)
20 {
21     auto paramsObject = std::make_unique<EnableParams>();
22     std::string error;
23     Result ret;
24 
25     double maxScriptsCacheSize;
26     ret = params.GetDouble("maxScriptsCacheSize", &maxScriptsCacheSize);
27     if (ret == Result::SUCCESS) {
28         paramsObject->maxScriptsCacheSize_ = maxScriptsCacheSize;
29     } else if (ret == Result::TYPE_ERROR) {
30         error += "Wrong type of 'maxScriptsCacheSize';";
31     }
32 
33     if (!error.empty()) {
34         LOG_DEBUGGER(ERROR) << "EnableParams::Create " << error;
35         return nullptr;
36     }
37 
38     return paramsObject;
39 }
40 
Create(const PtJson & params)41 std::unique_ptr<EvaluateOnCallFrameParams> EvaluateOnCallFrameParams::Create(const PtJson &params)
42 {
43     auto paramsObject = std::make_unique<EvaluateOnCallFrameParams>();
44     std::string error;
45     Result ret;
46 
47     std::string callFrameId;
48     ret = params.GetString("callFrameId", &callFrameId);
49     if (ret == Result::SUCCESS) {
50         paramsObject->callFrameId_ = std::stoi(callFrameId);
51     } else {
52         error += "Unknown or wrong type of 'callFrameId';";
53     }
54     std::string expression;
55     ret = params.GetString("expression", &expression);
56     if (ret == Result::SUCCESS) {
57         paramsObject->expression_ = std::move(expression);
58     } else {
59         error += "Unknown or wrong type of 'expression';";
60     }
61     std::string objectGroup;
62     ret = params.GetString("objectGroup", &objectGroup);
63     if (ret == Result::SUCCESS) {
64         paramsObject->objectGroup_ = std::move(objectGroup);
65     } else if (ret == Result::TYPE_ERROR) {
66         error += "Wrong type of 'objectGroup';";
67     }
68     bool includeCommandLineAPI = false;
69     ret = params.GetBool("includeCommandLineAPI", &includeCommandLineAPI);
70     if (ret == Result::SUCCESS) {
71         paramsObject->includeCommandLineAPI_ = includeCommandLineAPI;
72     } else if (ret == Result::TYPE_ERROR) {
73         error += "Wrong type of 'includeCommandLineAPI';";
74     }
75     bool silent = false;
76     ret = params.GetBool("silent", &silent);
77     if (ret == Result::SUCCESS) {
78         paramsObject->silent_ = silent;
79     } else if (ret == Result::TYPE_ERROR) {
80         error += "Wrong type of 'silent';";
81     }
82     bool returnByValue = false;
83     ret = params.GetBool("returnByValue", &returnByValue);
84     if (ret == Result::SUCCESS) {
85         paramsObject->returnByValue_ = returnByValue;
86     } else if (ret == Result::TYPE_ERROR) {
87         error += "Wrong type of 'returnByValue';";
88     }
89     bool generatePreview = false;
90     ret = params.GetBool("generatePreview", &generatePreview);
91     if (ret == Result::SUCCESS) {
92         paramsObject->generatePreview_ = generatePreview;
93     } else if (ret == Result::TYPE_ERROR) {
94         error += "Wrong type of 'generatePreview';";
95     }
96     bool throwOnSideEffect = false;
97     ret = params.GetBool("throwOnSideEffect", &throwOnSideEffect);
98     if (ret == Result::SUCCESS) {
99         paramsObject->throwOnSideEffect_ = throwOnSideEffect;
100     } else if (ret == Result::TYPE_ERROR) {
101         error += "Wrong type of 'throwOnSideEffect';";
102     }
103 
104     if (!error.empty()) {
105         LOG_DEBUGGER(ERROR) << "EvaluateOnCallFrameParams::Create " << error;
106         return nullptr;
107     }
108     return paramsObject;
109 }
110 
Create(const PtJson & params)111 std::unique_ptr<ContinueToLocationParams> ContinueToLocationParams::Create(const PtJson &params)
112 {
113     auto paramsObject = std::make_unique<ContinueToLocationParams>();
114     std::string error;
115     Result ret;
116 
117     std::unique_ptr<PtJson> position;
118     ret = params.GetObject("location", &position);
119     if (ret == Result::SUCCESS) {
120         std::unique_ptr<Location> location = Location::Create(*position);
121         if (location == nullptr) {
122             error += "'location' is invalid;";
123         } else {
124             paramsObject->location_= std::move(location);
125         }
126     } else {
127         error += "Unknown or wrong type of 'location';";
128     }
129 
130     std::string targetCallFrames;
131     ret = params.GetString("targetCallFrames", &targetCallFrames);
132     if (ret == Result::SUCCESS) {
133         paramsObject->targetCallFrames_ = std::move(targetCallFrames);
134     } else {
135         error += "Unknown 'targetCallFrames';";
136     }
137 
138     if (!error.empty()) {
139         LOG_DEBUGGER(ERROR) << "ContinueToLocationParams::Create " << error;
140         return nullptr;
141     }
142 
143     return paramsObject;
144 }
145 
146 
Create(const PtJson & params)147 std::unique_ptr<GetPossibleBreakpointsParams> GetPossibleBreakpointsParams::Create(const PtJson &params)
148 {
149     auto paramsObject = std::make_unique<GetPossibleBreakpointsParams>();
150     std::string error;
151     Result ret;
152 
153     std::unique_ptr<PtJson> start;
154     ret = params.GetObject("start", &start);
155     if (ret == Result::SUCCESS) {
156         std::unique_ptr<Location> location = Location::Create(*start);
157         if (location == nullptr) {
158             error += "'start' is invalid;";
159         } else {
160             paramsObject->start_ = std::move(location);
161         }
162     } else {
163         error += "Unknown or wrong type of 'start';";
164     }
165     std::unique_ptr<PtJson> end;
166     ret = params.GetObject("end", &end);
167     if (ret == Result::SUCCESS) {
168         std::unique_ptr<Location> location = Location::Create(*end);
169         if (location == nullptr) {
170             error += "'end' is invalid;";
171         } else {
172             paramsObject->end_ = std::move(location);
173         }
174     } else if (ret == Result::TYPE_ERROR) {
175         error += "Wrong type of 'end';";
176     }
177     bool restrictToFunction = false;
178     ret = params.GetBool("restrictToFunction", &restrictToFunction);
179     if (ret == Result::SUCCESS) {
180         paramsObject->restrictToFunction_ = restrictToFunction;
181     } else if (ret == Result::TYPE_ERROR) {
182         error += "Wrong type of 'restrictToFunction';";
183     }
184 
185     if (!error.empty()) {
186         LOG_DEBUGGER(ERROR) << "GetPossibleBreakpointsParams::Create " << error;
187         return nullptr;
188     }
189 
190     return paramsObject;
191 }
192 
Create(const PtJson & params)193 std::unique_ptr<GetScriptSourceParams> GetScriptSourceParams::Create(const PtJson &params)
194 {
195     auto paramsObject = std::make_unique<GetScriptSourceParams>();
196     std::string error;
197     Result ret;
198 
199     std::string scriptId;
200     ret = params.GetString("scriptId", &scriptId);
201     if (ret == Result::SUCCESS) {
202         paramsObject->scriptId_ = std::stoi(scriptId);
203     } else {
204         error += "Unknown or wrong type of'scriptId';";
205     }
206 
207     if (!error.empty()) {
208         LOG_DEBUGGER(ERROR) << "GetScriptSourceParams::Create " << error;
209         return nullptr;
210     }
211 
212     return paramsObject;
213 }
214 
Create(const PtJson & params)215 std::unique_ptr<RemoveBreakpointParams> RemoveBreakpointParams::Create(const PtJson &params)
216 {
217     auto paramsObject = std::make_unique<RemoveBreakpointParams>();
218     std::string error;
219     Result ret;
220 
221     std::string breakpointId;
222     ret = params.GetString("breakpointId", &breakpointId);
223     if (ret == Result::SUCCESS) {
224         paramsObject->breakpointId_ = std::move(breakpointId);
225     } else {
226         error += "Unknown or wrong type of 'breakpointId';";
227     }
228 
229     if (!error.empty()) {
230         LOG_DEBUGGER(ERROR) << "RemoveBreakpointParams::Create " << error;
231         return nullptr;
232     }
233 
234     return paramsObject;
235 }
236 
Create(const PtJson & params)237 std::unique_ptr<ResumeParams> ResumeParams::Create(const PtJson &params)
238 {
239     auto paramsObject = std::make_unique<ResumeParams>();
240     std::string error;
241     Result ret;
242 
243     bool terminateOnResume = false;
244     ret = params.GetBool("terminateOnResume", &terminateOnResume);
245     if (ret == Result::SUCCESS) {
246         paramsObject->terminateOnResume_ = terminateOnResume;
247     } else if (ret == Result::TYPE_ERROR) {
248         error += "Wrong type of 'terminateOnResume';";
249     }
250 
251     if (!error.empty()) {
252         LOG_DEBUGGER(ERROR) << "ResumeParams::Create " << error;
253         return nullptr;
254     }
255 
256     return paramsObject;
257 }
258 
Create(const PtJson & params)259 std::unique_ptr<SetAsyncCallStackDepthParams> SetAsyncCallStackDepthParams::Create(const PtJson &params)
260 {
261     auto paramsObject = std::make_unique<SetAsyncCallStackDepthParams>();
262     std::string error;
263     Result ret;
264 
265     int32_t maxDepth;
266     ret = params.GetInt("maxDepth", &maxDepth);
267     if (ret == Result::SUCCESS) {
268         paramsObject->maxDepth_ = maxDepth;
269     } else {
270         error += "Unknown or wrong type of 'maxDepth';";
271     }
272 
273     if (!error.empty()) {
274         LOG_DEBUGGER(ERROR) << "SetAsyncCallStackDepthParams::Create " << error;
275         return nullptr;
276     }
277 
278     return paramsObject;
279 }
280 
Create(const PtJson & params)281 std::unique_ptr<SetBlackboxPatternsParams> SetBlackboxPatternsParams::Create(const PtJson &params)
282 {
283     auto paramsObject = std::make_unique<SetBlackboxPatternsParams>();
284     std::string error;
285     Result ret;
286 
287     std::unique_ptr<PtJson> patterns;
288     ret = params.GetArray("patterns", &patterns);
289     if (ret == Result::SUCCESS) {
290         int32_t len = patterns->GetSize();
291         for (int32_t i = 0; i < len; ++i) {
292             std::unique_ptr<PtJson> item = patterns->Get(i);
293             if (item->IsString()) {
294                 paramsObject->patterns_.emplace_back(item->GetString());
295             } else {
296                 error += "'patterns' items should be a String;";
297             }
298         }
299     } else {
300         error += "Unknown or wrong type of 'patterns';";
301     }
302 
303     if (!error.empty()) {
304         LOG_DEBUGGER(ERROR) << "SetBlackboxPatternsParams::Create " << error;
305         return nullptr;
306     }
307 
308     return paramsObject;
309 }
310 
Create(const PtJson & params)311 std::unique_ptr<SetBreakpointByUrlParams> SetBreakpointByUrlParams::Create(const PtJson &params)
312 {
313     auto paramsObject = std::make_unique<SetBreakpointByUrlParams>();
314     std::string error;
315     Result ret;
316 
317     int32_t lineNumber;
318     ret = params.GetInt("lineNumber", &lineNumber);
319     if (ret == Result::SUCCESS) {
320         paramsObject->lineNumber_ = lineNumber;
321     } else {
322         error += "Unknown or wrong type of 'lineNumber';";
323     }
324     std::string url;
325     ret = params.GetString("url", &url);
326     if (ret == Result::SUCCESS) {
327         paramsObject->url_ = std::move(url);
328     } else if (ret == Result::TYPE_ERROR) {
329         error += "Wrong type of 'url';";
330     }
331     std::string urlRegex;
332     ret = params.GetString("urlRegex", &urlRegex);
333     if (ret == Result::SUCCESS) {
334         paramsObject->urlRegex_ = std::move(urlRegex);
335     } else if (ret == Result::TYPE_ERROR) {
336         error += "Wrong type of 'urlRegex';";
337     }
338     std::string scriptHash;
339     ret = params.GetString("scriptHash", &scriptHash);
340     if (ret == Result::SUCCESS) {
341         paramsObject->scriptHash_ = std::move(scriptHash);
342     } else if (ret == Result::TYPE_ERROR) {
343         error += "Wrong type of 'scriptHash';";
344     }
345     int32_t columnNumber;
346     ret = params.GetInt("columnNumber", &columnNumber);
347     if (ret == Result::SUCCESS) {
348         paramsObject->columnNumber_ = columnNumber;
349     } else if (ret == Result::TYPE_ERROR) {
350         error += "Wrong type of 'columnNumber';";
351     }
352     std::string condition;
353     ret = params.GetString("condition", &condition);
354     if (ret == Result::SUCCESS) {
355         paramsObject->condition_ = std::move(condition);
356     } else if (ret == Result::TYPE_ERROR) {
357         error += "Wrong type of 'condition';";
358     }
359     if (!error.empty()) {
360         LOG_DEBUGGER(ERROR) << "SetBreakpointByUrlParams::Create " << error;
361         return nullptr;
362     }
363 
364     return paramsObject;
365 }
366 
Create(const PtJson & params)367 std::unique_ptr<SetBreakpointsActiveParams> SetBreakpointsActiveParams::Create(const PtJson &params)
368 {
369     auto paramsObject = std::make_unique<SetBreakpointsActiveParams>();
370     std::string error;
371     Result ret;
372 
373     bool breakpointsState;
374     ret = params.GetBool("active", &breakpointsState);
375     if (ret == Result::SUCCESS) {
376         paramsObject->breakpointsState_ = std::move(breakpointsState);
377     } else {
378         error += "Unknown or wrong type of 'active';";
379     }
380     if (!error.empty()) {
381         LOG_DEBUGGER(ERROR) << "SetBreakpointsActiveParams::Create " << error;
382         return nullptr;
383     }
384 
385     return paramsObject;
386 }
387 
Create(const PtJson & params)388 std::unique_ptr<SetSkipAllPausesParams> SetSkipAllPausesParams::Create(const PtJson &params)
389 {
390     auto paramsObject = std::make_unique<SetSkipAllPausesParams>();
391     std::string error;
392     Result ret;
393 
394     bool skipAllPausesState;
395     ret = params.GetBool("skip", &skipAllPausesState);
396     if (ret == Result::SUCCESS) {
397         paramsObject->skipAllPausesState_ = std::move(skipAllPausesState);
398     } else {
399         error += "Unknown or wrong type of 'skip';";
400     }
401     if (!error.empty()) {
402         LOG_DEBUGGER(ERROR) << "SetSkipAllPausesParams::Create " << error;
403         return nullptr;
404     }
405 
406     return paramsObject;
407 }
408 
Create(const PtJson & params)409 std::unique_ptr<GetPossibleAndSetBreakpointParams> GetPossibleAndSetBreakpointParams::Create(const PtJson &params)
410 {
411     auto paramsObject = std::make_unique<GetPossibleAndSetBreakpointParams>();
412     std::string error;
413     Result ret;
414 
415     std::unique_ptr<PtJson> breakpoints;
416     ret = params.GetArray("locations", &breakpoints);
417     if (ret == Result::SUCCESS) {
418         int32_t length = breakpoints->GetSize();
419         std::vector<std::unique_ptr<BreakpointInfo>> breakpointList;
420         for (int32_t i = 0; i < length; i++) {
421             std::unique_ptr<BreakpointInfo> info = BreakpointInfo::Create(*breakpoints->Get(i));
422             if (info == nullptr) {
423                 error += "'breakpoints' items BreakpointInfo is invaild;";
424                 break;
425             } else {
426                 breakpointList.emplace_back(std::move(info));
427             }
428         }
429         if (!breakpointList.empty()) {
430             paramsObject->breakpointsList_ = std::move(breakpointList);
431         }
432     } else if (ret == Result::TYPE_ERROR) {
433         error += "Wrong type of 'breakpoints';";
434     }
435 
436     if (!error.empty()) {
437         LOG_DEBUGGER(ERROR) << "GetPossibleAndSetBreakpointParams::Create " << error;
438         return nullptr;
439     }
440 
441     return paramsObject;
442 }
443 
Create(const PtJson & params)444 std::unique_ptr<SetPauseOnExceptionsParams> SetPauseOnExceptionsParams::Create(const PtJson &params)
445 {
446     auto paramsObject = std::make_unique<SetPauseOnExceptionsParams>();
447     std::string error;
448     Result ret;
449 
450     std::string state;
451     ret = params.GetString("state", &state);
452     if (ret == Result::SUCCESS) {
453         paramsObject->StoreState(state);
454     } else {
455         error += "Unknown or wrong type of'state';";
456     }
457 
458     if (!error.empty()) {
459         LOG_DEBUGGER(ERROR) << "SetPauseOnExceptionsParams::Create " << error;
460         return nullptr;
461     }
462 
463     return paramsObject;
464 }
465 
Create(const PtJson & params)466 std::unique_ptr<StepIntoParams> StepIntoParams::Create(const PtJson &params)
467 {
468     auto paramsObject = std::make_unique<StepIntoParams>();
469     std::string error;
470     Result ret;
471 
472     bool breakOnAsyncCall = false;
473     ret = params.GetBool("breakOnAsyncCall", &breakOnAsyncCall);
474     if (ret == Result::SUCCESS) {
475         paramsObject->breakOnAsyncCall_ = breakOnAsyncCall;
476     } else if (ret == Result::TYPE_ERROR) {
477         error += "Wrong type of 'breakOnAsyncCall';";
478     }
479     std::unique_ptr<PtJson> skipList;
480     ret = params.GetArray("skipList", &skipList);
481     if (ret == Result::SUCCESS) {
482         int32_t len = skipList->GetSize();
483         std::list<std::unique_ptr<LocationRange>> listLocation;
484         for (int32_t i = 0; i < len; ++i) {
485             std::unique_ptr<LocationRange> obj = LocationRange::Create(*skipList->Get(i));
486             if (obj == nullptr) {
487                 error += "'skipList' items LocationRange is invalid;";
488                 break;
489             } else {
490                 listLocation.emplace_back(std::move(obj));
491             }
492         }
493         if (listLocation.size()) {
494             paramsObject->skipList_ = std::move(listLocation);
495         }
496     } else if (ret == Result::TYPE_ERROR) {
497         error += "Wrong type of 'skipList';";
498     }
499 
500     if (!error.empty()) {
501         LOG_DEBUGGER(ERROR) << "StepIntoParams::Create " << error;
502         return nullptr;
503     }
504 
505     return paramsObject;
506 }
507 
Create(const PtJson & params)508 std::unique_ptr<StepOverParams> StepOverParams::Create(const PtJson &params)
509 {
510     auto paramsObject = std::make_unique<StepOverParams>();
511     std::string error;
512     Result ret;
513 
514     std::unique_ptr<PtJson> skipList;
515     ret = params.GetArray("skipList", &skipList);
516     if (ret == Result::SUCCESS) {
517         int32_t len = skipList->GetSize();
518         std::list<std::unique_ptr<LocationRange>> listLocation;
519         for (int32_t i = 0; i < len; ++i) {
520             std::unique_ptr<LocationRange> obj = LocationRange::Create(*skipList->Get(i));
521             if (obj == nullptr) {
522                 error += "'skipList' items LocationRange is invalid;";
523                 break;
524             } else {
525                 listLocation.emplace_back(std::move(obj));
526             }
527         }
528         if (listLocation.size()) {
529             paramsObject->skipList_ = std::move(listLocation);
530         }
531     } else if (ret == Result::TYPE_ERROR) {
532         error += "Wrong type of 'skipList';";
533     }
534 
535     if (!error.empty()) {
536         LOG_DEBUGGER(ERROR) << "StepOverParams::Create " << error;
537         return nullptr;
538     }
539 
540     return paramsObject;
541 }
542 
Create(const PtJson & params)543 std::unique_ptr<DropFrameParams> DropFrameParams::Create(const PtJson &params)
544 {
545     auto paramsObject = std::make_unique<DropFrameParams>();
546     std::string error;
547     Result ret;
548 
549     uint32_t droppedDepth = 0;
550     ret = params.GetUInt("droppedDepth", &droppedDepth);
551     if (ret == Result::SUCCESS) {
552         paramsObject->droppedDepth_ = droppedDepth;
553     } else if (ret == Result::TYPE_ERROR) {
554         error += "Wrong type of 'droppedDepth';";
555     }
556 
557     if (!error.empty()) {
558         LOG_DEBUGGER(ERROR) << "DropFrameParams::Create " << error;
559         return nullptr;
560     }
561 
562     return paramsObject;
563 }
564 
Create(const PtJson & params)565 std::unique_ptr<SetNativeRangeParams> SetNativeRangeParams::Create(const PtJson &params)
566 {
567     auto paramsObject = std::make_unique<SetNativeRangeParams>();
568     std::string error;
569     Result ret;
570 
571     std::unique_ptr<PtJson> nativeRange;
572     ret = params.GetArray("nativeRange", &nativeRange);
573     if (ret == Result::SUCCESS) {
574         int32_t len = nativeRange->GetSize();
575         std::vector<NativeRange> vectorNativeRange;
576         for (int32_t i = 0; i < len; ++i) {
577             std::unique_ptr<NativeRange> obj = NativeRange::Create(*nativeRange->Get(i));
578             if (obj == nullptr) {
579                 error += "'nativeRange' is invalid;";
580                 break;
581             } else {
582                 vectorNativeRange.emplace_back(std::move(*obj));
583             }
584         }
585         if (vectorNativeRange.size()) {
586             paramsObject->nativeRange_ = std::move(vectorNativeRange);
587         }
588     } else if (ret == Result::TYPE_ERROR) {
589         error += "Unknown 'nativeRange';";
590     }
591 
592     if (!error.empty()) {
593         LOG_DEBUGGER(ERROR) << "SetNativeRangeParams::Create " << error;
594         return nullptr;
595     }
596 
597     return paramsObject;
598 }
599 
Create(const PtJson & params)600 std::unique_ptr<SetMixedDebugParams> SetMixedDebugParams::Create(const PtJson &params)
601 {
602     auto paramsObject = std::make_unique<SetMixedDebugParams>();
603     std::string error;
604     Result ret;
605 
606     bool enabled = false;
607     ret = params.GetBool("enabled", &enabled);
608     if (ret == Result::SUCCESS) {
609         paramsObject->enabled_ = enabled;
610     } else if (ret == Result::TYPE_ERROR) {
611         error += "Wrong type of 'enabled';";
612     }
613 
614     bool mixedStackEnabled = false;
615     ret = params.GetBool("mixedStackEnabled", &mixedStackEnabled);
616     if (ret == Result::SUCCESS) {
617         paramsObject->mixedStackEnabled_ = mixedStackEnabled;
618     } else if (ret == Result::TYPE_ERROR) {
619         error += "Wrong type of 'enabled';";
620     }
621 
622     if (!error.empty()) {
623         LOG_DEBUGGER(ERROR) << "SetMixedDebugParams::Create " << error;
624         return nullptr;
625     }
626 
627     return paramsObject;
628 }
629 
Create(const PtJson & params)630 std::unique_ptr<ResetSingleStepperParams> ResetSingleStepperParams::Create(const PtJson &params)
631 {
632     auto paramsObject = std::make_unique<ResetSingleStepperParams>();
633     std::string error;
634     Result ret;
635 
636     bool resetSingleStepper = false;
637     ret = params.GetBool("resetSingleStepper", &resetSingleStepper);
638     if (ret == Result::SUCCESS) {
639         paramsObject->resetSingleStepper_ = resetSingleStepper;
640     } else if (ret == Result::TYPE_ERROR) {
641         error += "Wrong type of 'resetSingleStepper';";
642     }
643 
644     if (!error.empty()) {
645         LOG_DEBUGGER(ERROR) << "ResetSingleStepperParams::Create " << error;
646         return nullptr;
647     }
648 
649     return paramsObject;
650 }
651 
Create(const PtJson & params)652 std::unique_ptr<ReplyNativeCallingParams> ReplyNativeCallingParams::Create(const PtJson &params)
653 {
654     auto paramsObject = std::make_unique<ReplyNativeCallingParams>();
655     std::string error;
656     Result ret;
657 
658     bool userCode = false;
659     ret = params.GetBool("userCode", &userCode);
660     if (ret == Result::SUCCESS) {
661         paramsObject->userCode_ = userCode;
662     } else if (ret == Result::TYPE_ERROR) {
663         error += "Wrong type of 'userCode';";
664     }
665 
666     if (!error.empty()) {
667         LOG_DEBUGGER(ERROR) << "ReplyNativeCallingParams::Create " << error;
668         return nullptr;
669     }
670 
671     return paramsObject;
672 }
673 
Create(const PtJson & params)674 std::unique_ptr<GetPropertiesParams> GetPropertiesParams::Create(const PtJson &params)
675 {
676     auto paramsObject = std::make_unique<GetPropertiesParams>();
677     std::string error;
678     Result ret;
679 
680     std::string objectId;
681     ret = params.GetString("objectId", &objectId);
682     if (ret == Result::SUCCESS) {
683         paramsObject->objectId_ = std::stoi(objectId);
684     } else {
685         error += "Unknown or wrong type of 'objectId';";
686     }
687     bool ownProperties = false;
688     ret = params.GetBool("ownProperties", &ownProperties);
689     if (ret == Result::SUCCESS) {
690         paramsObject->ownProperties_ = ownProperties;
691     } else if (ret == Result::TYPE_ERROR) {
692         error += "Wrong type of 'ownProperties';";
693     }
694     bool accessorPropertiesOnly = false;
695     ret = params.GetBool("accessorPropertiesOnly", &accessorPropertiesOnly);
696     if (ret == Result::SUCCESS) {
697         paramsObject->accessorPropertiesOnly_ = accessorPropertiesOnly;
698     } else if (ret == Result::TYPE_ERROR) {
699         error += "Wrong type of 'accessorPropertiesOnly';";
700     }
701     bool generatePreview = false;
702     ret = params.GetBool("generatePreview", &generatePreview);
703     if (ret == Result::SUCCESS) {
704         paramsObject->generatePreview_ = generatePreview;
705     } else if (ret == Result::TYPE_ERROR) {
706         error += "Wrong type of 'generatePreview';";
707     }
708     if (!error.empty()) {
709         LOG_DEBUGGER(ERROR) << "GetPropertiesParams::Create " << error;
710         return nullptr;
711     }
712 
713     return paramsObject;
714 }
715 
Create(const PtJson & params)716 std::unique_ptr<CallFunctionOnParams> CallFunctionOnParams::Create(const PtJson &params)
717 {
718     auto paramsObject = std::make_unique<CallFunctionOnParams>();
719     std::string error;
720     Result ret;
721 
722     // paramsObject->callFrameId_
723     std::string callFrameId;
724     ret = params.GetString("callFrameId", &callFrameId);
725     if (ret == Result::SUCCESS) {
726         paramsObject->callFrameId_ = std::stoi(callFrameId);
727     } else {
728         error += "Unknown or wrong type of 'callFrameId';";
729     }
730     // paramsObject->functionDeclaration_
731     std::string functionDeclaration;
732     ret = params.GetString("functionDeclaration", &functionDeclaration);
733     if (ret == Result::SUCCESS) {
734         paramsObject->functionDeclaration_ = std::move(functionDeclaration);
735     } else {
736         error += "Unknown or wrong type of 'functionDeclaration';";
737     }
738     // paramsObject->objectId_
739     std::string objectId;
740     ret = params.GetString("objectId", &objectId);
741     if (ret == Result::SUCCESS) {
742         paramsObject->objectId_ = std::stoi(objectId);
743     } else if (ret == Result::TYPE_ERROR) {
744         error += "Wrong type of 'objectId';";
745     }
746     // paramsObject->arguments_
747     std::unique_ptr<PtJson> arguments;
748     ret = params.GetArray("arguments", &arguments);
749     if (ret == Result::SUCCESS) {
750         int32_t len = arguments->GetSize();
751         std::vector<std::unique_ptr<CallArgument>> callArgument;
752         for (int32_t i = 0; i < len; ++i) {
753             std::unique_ptr<CallArgument> obj = CallArgument::Create(*arguments->Get(i));
754             if (obj == nullptr) {
755                 error += "'arguments' items CallArgument is invaild;";
756                 break;
757             } else {
758                 callArgument.emplace_back(std::move(obj));
759             }
760         }
761         if (callArgument.size()) {
762             paramsObject->arguments_ = std::move(callArgument);
763         }
764     } else if (ret == Result::TYPE_ERROR) {
765         error += "Wrong type of 'arguments';";
766     }
767     // paramsObject->silent_
768     bool silent = false;
769     ret = params.GetBool("silent", &silent);
770     if (ret == Result::SUCCESS) {
771         paramsObject->silent_ = silent;
772     } else if (ret == Result::TYPE_ERROR) {
773         error += "Wrong type of 'silent';";
774     }
775     // paramsObject->returnByValue_
776     bool returnByValue = false;
777     ret = params.GetBool("returnByValue", &returnByValue);
778     if (ret == Result::SUCCESS) {
779         paramsObject->returnByValue_ = returnByValue;
780     } else if (ret == Result::TYPE_ERROR) {
781         error += "Wrong type of 'returnByValue';";
782     }
783     // paramsObject->generatePreview_
784     bool generatePreview = false;
785     ret = params.GetBool("generatePreview", &generatePreview);
786     if (ret == Result::SUCCESS) {
787         paramsObject->generatePreview_ = generatePreview;
788     } else if (ret == Result::TYPE_ERROR) {
789         error += "Wrong type of 'generatePreview';";
790     }
791     // paramsObject->userGesture_
792     bool userGesture = false;
793     ret = params.GetBool("userGesture", &userGesture);
794     if (ret == Result::SUCCESS) {
795         paramsObject->userGesture_ = userGesture;
796     } else if (ret == Result::TYPE_ERROR) {
797         error += "Wrong type of 'userGesture';";
798     }
799     // paramsObject->awaitPromise_
800     bool awaitPromise = false;
801     ret = params.GetBool("awaitPromise", &awaitPromise);
802     if (ret == Result::SUCCESS) {
803         paramsObject->awaitPromise_ = awaitPromise;
804     } else if (ret == Result::TYPE_ERROR) {
805         error += "Wrong type of 'awaitPromise';";
806     }
807     // paramsObject->executionContextId_
808     int32_t executionContextId;
809     ret = params.GetInt("executionContextId", &executionContextId);
810     if (ret == Result::SUCCESS) {
811         paramsObject->executionContextId_ = executionContextId;
812     } else if (ret == Result::TYPE_ERROR) {
813         error += "Wrong type of 'executionContextId';";
814     }
815     // paramsObject->objectGroup_
816     std::string objectGroup;
817     ret = params.GetString("objectGroup", &objectGroup);
818     if (ret == Result::SUCCESS) {
819         paramsObject->objectGroup_ = std::move(objectGroup);
820     } else if (ret == Result::TYPE_ERROR) {
821         error += "Wrong type of 'objectGroup';";
822     }
823     // paramsObject->throwOnSideEffect_
824     bool throwOnSideEffect = false;
825     ret = params.GetBool("throwOnSideEffect", &throwOnSideEffect);
826     if (ret == Result::SUCCESS) {
827         paramsObject->throwOnSideEffect_ = throwOnSideEffect;
828     } else if (ret == Result::TYPE_ERROR) {
829         error += "Wrong type of 'throwOnSideEffect';";
830     }
831 
832     // Check whether the error is empty.
833     if (!error.empty()) {
834         LOG_DEBUGGER(ERROR) << "CallFunctionOnParams::Create " << error;
835         return nullptr;
836     }
837 
838     return paramsObject;
839 }
840 
Create(const PtJson & params)841 std::unique_ptr<StartSamplingParams> StartSamplingParams::Create(const PtJson &params)
842 {
843     auto paramsObject = std::make_unique<StartSamplingParams>();
844     std::string error;
845     Result ret;
846 
847     double samplingInterval;
848     ret = params.GetDouble("samplingInterval", &samplingInterval);
849     if (ret == Result::SUCCESS) {
850         if (samplingInterval <= 0) {
851             error += "Invalid SamplingInterval";
852         } else {
853             paramsObject->samplingInterval_ = samplingInterval;
854         }
855     } else if (ret == Result::TYPE_ERROR) {
856         error += "Wrong type of 'samplingInterval';";
857     }
858 
859     if (!error.empty()) {
860         LOG_DEBUGGER(ERROR) << "StartSamplingParams::Create " << error;
861         return nullptr;
862     }
863     return paramsObject;
864 }
865 
Create(const PtJson & params)866 std::unique_ptr<StartTrackingHeapObjectsParams> StartTrackingHeapObjectsParams::Create(const PtJson &params)
867 {
868     auto paramsObject = std::make_unique<StartTrackingHeapObjectsParams>();
869     std::string error;
870     Result ret;
871 
872     bool trackAllocations = false;
873     ret = params.GetBool("trackAllocations", &trackAllocations);
874     if (ret == Result::SUCCESS) {
875         paramsObject->trackAllocations_ = trackAllocations;
876     } else if (ret == Result::TYPE_ERROR) {
877         error += "Wrong type of 'trackAllocations';";
878     }
879 
880     if (!error.empty()) {
881         LOG_DEBUGGER(ERROR) << "StartTrackingHeapObjectsParams::Create " << error;
882         return nullptr;
883     }
884     return paramsObject;
885 }
886 
Create(const PtJson & params)887 std::unique_ptr<StopTrackingHeapObjectsParams> StopTrackingHeapObjectsParams::Create(const PtJson &params)
888 {
889     auto paramsObject = std::make_unique<StopTrackingHeapObjectsParams>();
890     std::string error;
891     Result ret;
892 
893     bool reportProgress = false;
894     ret = params.GetBool("reportProgress", &reportProgress);
895     if (ret == Result::SUCCESS) {
896         paramsObject->reportProgress_ = reportProgress;
897     } else if (ret == Result::TYPE_ERROR) {
898         error += "Wrong type of 'reportProgress';";
899     }
900 
901     bool treatGlobalObjectsAsRoots = false;
902     ret = params.GetBool("treatGlobalObjectsAsRoots", &treatGlobalObjectsAsRoots);
903     if (ret == Result::SUCCESS) {
904         paramsObject->treatGlobalObjectsAsRoots_ = treatGlobalObjectsAsRoots;
905     } else if (ret == Result::TYPE_ERROR) {
906         error += "Wrong type of 'treatGlobalObjectsAsRoots';";
907     }
908 
909     bool captureNumericValue = false;
910     ret = params.GetBool("captureNumericValue", &captureNumericValue);
911     if (ret == Result::SUCCESS) {
912         paramsObject->captureNumericValue_ = captureNumericValue;
913     } else if (ret == Result::TYPE_ERROR) {
914         error += "Wrong type of 'captureNumericValue';";
915     }
916 
917     if (!error.empty()) {
918         LOG_DEBUGGER(ERROR) << "StopTrackingHeapObjectsParams::Create " << error;
919         return nullptr;
920     }
921     return paramsObject;
922 }
923 
Create(const PtJson & params)924 std::unique_ptr<AddInspectedHeapObjectParams> AddInspectedHeapObjectParams::Create(const PtJson &params)
925 {
926     auto paramsObject = std::make_unique<AddInspectedHeapObjectParams>();
927     std::string error;
928     Result ret;
929 
930     std::string heapObjectId;
931     ret = params.GetString("heapObjectId", &heapObjectId);
932     if (ret == Result::SUCCESS) {
933         paramsObject->heapObjectId_ = std::stoi(heapObjectId);
934     } else {
935         error += "Unknown or wrong type of 'heapObjectId';";
936     }
937 
938     if (!error.empty()) {
939         LOG_DEBUGGER(ERROR) << "AddInspectedHeapObjectParams::Create " << error;
940         return nullptr;
941     }
942     return paramsObject;
943 }
944 
Create(const PtJson & params)945 std::unique_ptr<GetHeapObjectIdParams> GetHeapObjectIdParams::Create(const PtJson &params)
946 {
947     auto paramsObject = std::make_unique<GetHeapObjectIdParams>();
948     std::string error;
949     Result ret;
950 
951     std::string objectId;
952     ret = params.GetString("objectId", &objectId);
953     if (ret == Result::SUCCESS) {
954         paramsObject->objectId_ = std::stoi(objectId);
955     } else if (ret == Result::TYPE_ERROR) {
956         error += "Wrong type of 'objectId';";
957     }
958 
959     if (!error.empty()) {
960         LOG_DEBUGGER(ERROR) << "GetHeapObjectIdParams::Create " << error;
961         return nullptr;
962     }
963     return paramsObject;
964 }
965 
Create(const PtJson & params)966 std::unique_ptr<GetObjectByHeapObjectIdParams> GetObjectByHeapObjectIdParams::Create(const PtJson &params)
967 {
968     auto paramsObject = std::make_unique<GetObjectByHeapObjectIdParams>();
969     std::string error;
970     Result ret;
971 
972     std::string objectId;
973     ret = params.GetString("objectId", &objectId);
974     if (ret == Result::SUCCESS) {
975         paramsObject->objectId_ = std::stoi(objectId);
976     } else if (ret == Result::TYPE_ERROR) {
977         error += "Wrong type of 'objectId';";
978     }
979 
980     std::string objectGroup;
981     ret = params.GetString("objectGroup", &objectGroup);
982     if (ret == Result::SUCCESS) {
983         paramsObject->objectGroup_ = std::move(objectGroup);
984     } else if (ret == Result::TYPE_ERROR) {
985         error += "Wrong type of 'objectGroup';";
986     }
987 
988     if (!error.empty()) {
989         LOG_DEBUGGER(ERROR) << "GetObjectByHeapObjectIdParams::Create " << error;
990         return nullptr;
991     }
992     return paramsObject;
993 }
994 
Create(const PtJson & params)995 std::unique_ptr<StartPreciseCoverageParams> StartPreciseCoverageParams::Create(const PtJson &params)
996 {
997     auto paramsObject = std::make_unique<StartPreciseCoverageParams>();
998     std::string error;
999     Result ret;
1000 
1001     bool callCount = false;
1002     ret = params.GetBool("callCount", &callCount);
1003     if (ret == Result::SUCCESS) {
1004         paramsObject->callCount_ = callCount;
1005     } else if (ret == Result::TYPE_ERROR) {
1006         error += "Wrong type of 'callCount';";
1007     }
1008 
1009     bool detailed = false;
1010     ret = params.GetBool("detailed", &detailed);
1011     if (ret == Result::SUCCESS) {
1012         paramsObject->detailed_ = detailed;
1013     } else if (ret == Result::TYPE_ERROR) {
1014         error += "Wrong type of 'detailed';";
1015     }
1016 
1017     bool allowTriggeredUpdates = false;
1018     ret = params.GetBool("allowTriggeredUpdates", &allowTriggeredUpdates);
1019     if (ret == Result::SUCCESS) {
1020         paramsObject->allowTriggeredUpdates_ = allowTriggeredUpdates;
1021     } else if (ret == Result::TYPE_ERROR) {
1022         error += "Wrong type of 'allowTriggeredUpdates';";
1023     }
1024 
1025     if (!error.empty()) {
1026         LOG_DEBUGGER(ERROR) << "StartPreciseCoverageParams::Create " << error;
1027         return nullptr;
1028     }
1029     return paramsObject;
1030 }
1031 
Create(const PtJson & params)1032 std::unique_ptr<SetSamplingIntervalParams> SetSamplingIntervalParams::Create(const PtJson &params)
1033 {
1034     auto paramsObject = std::make_unique<SetSamplingIntervalParams>();
1035     std::string error;
1036     Result ret;
1037 
1038     int32_t interval = 0;
1039     ret = params.GetInt("interval", &interval);
1040     if (ret == Result::SUCCESS) {
1041         paramsObject->interval_ = interval;
1042     } else {
1043         error += "Unknown or wrong type of 'interval';";
1044     }
1045 
1046     if (!error.empty()) {
1047         LOG_DEBUGGER(ERROR) << "SetSamplingIntervalParams::Create " << error;
1048         return nullptr;
1049     }
1050     return paramsObject;
1051 }
1052 
Create(const PtJson & params)1053 std::unique_ptr<RecordClockSyncMarkerParams> RecordClockSyncMarkerParams::Create(const PtJson &params)
1054 {
1055     std::string error;
1056     auto recordClockSyncMarkerParams = std::make_unique<RecordClockSyncMarkerParams>();
1057     Result ret;
1058 
1059     std::string syncId;
1060     ret = params.GetString("syncId", &syncId);
1061     if (ret == Result::SUCCESS) {
1062         recordClockSyncMarkerParams->syncId_ = syncId;
1063     } else {
1064         error += "Unknown or wrong type of 'syncId';";
1065     }
1066 
1067     if (!error.empty()) {
1068         LOG_DEBUGGER(ERROR) << "RecordClockSyncMarkerParams::Create " << error;
1069         return nullptr;
1070     }
1071 
1072     return recordClockSyncMarkerParams;
1073 }
1074 
Create(const PtJson & params)1075 std::unique_ptr<RequestMemoryDumpParams> RequestMemoryDumpParams::Create(const PtJson &params)
1076 {
1077     std::string error;
1078     auto requestMemoryDumpParams = std::make_unique<RequestMemoryDumpParams>();
1079     Result ret;
1080 
1081     bool deterministic = false;
1082     ret = params.GetBool("deterministic", &deterministic);
1083     if (ret == Result::SUCCESS) {
1084         requestMemoryDumpParams->deterministic_ = deterministic;
1085     } else if (ret == Result::TYPE_ERROR) {
1086         error += "Wrong type of 'deterministic';";
1087     }
1088 
1089     std::string levelOfDetail;
1090     ret = params.GetString("levelOfDetail", &levelOfDetail);
1091     if (ret == Result::SUCCESS) {
1092         if (MemoryDumpLevelOfDetailValues::Valid(levelOfDetail)) {
1093             requestMemoryDumpParams->levelOfDetail_ = std::move(levelOfDetail);
1094         } else {
1095             error += "'levelOfDetail' is invalid;";
1096         }
1097     } else if (ret == Result::TYPE_ERROR) {
1098         error += "Wrong type of 'levelOfDetail';";
1099     }
1100 
1101     if (!error.empty()) {
1102         LOG_DEBUGGER(ERROR) << "RequestMemoryDumpParams::Create " << error;
1103         return nullptr;
1104     }
1105 
1106     return requestMemoryDumpParams;
1107 }
1108 
Create(const PtJson & params)1109 std::unique_ptr<StartParams> StartParams::Create(const PtJson &params)
1110 {
1111     std::string error;
1112     auto startParams = std::make_unique<StartParams>();
1113     Result ret;
1114 
1115     std::string categories;
1116     ret = params.GetString("categories", &categories);
1117     if (ret == Result::SUCCESS) {
1118         startParams->categories_ = std::move(categories);
1119     } else if (ret == Result::TYPE_ERROR) {
1120         error += "Wrong type of 'categories';";
1121     }
1122 
1123     std::string options;
1124     ret = params.GetString("options", &options);
1125     if (ret == Result::SUCCESS) {
1126         startParams->options_ = std::move(options);
1127     } else if (ret == Result::TYPE_ERROR) {
1128         error += "Wrong type of 'options';";
1129     }
1130 
1131     int32_t bufferUsageReportingInterval = 0;
1132     ret = params.GetInt("bufferUsageReportingInterval", &bufferUsageReportingInterval);
1133     if (ret == Result::SUCCESS) {
1134         startParams->bufferUsageReportingInterval_ = bufferUsageReportingInterval;
1135     } else if (ret == Result::TYPE_ERROR) {
1136         error += "Wrong type of 'bufferUsageReportingInterval';";
1137     }
1138 
1139     std::string transferMode;
1140     ret = params.GetString("transferMode", &transferMode);
1141     if (ret == Result::SUCCESS) {
1142         if (StartParams::TransferModeValues::Valid(transferMode)) {
1143             startParams->transferMode_ = std::move(transferMode);
1144         } else {
1145             error += "'transferMode' is invalid;";
1146         }
1147     } else if (ret == Result::TYPE_ERROR) {
1148         error += "Wrong type of 'transferMode';";
1149     }
1150 
1151     std::string streamFormat;
1152     ret = params.GetString("streamFormat", &streamFormat);
1153     if (ret == Result::SUCCESS) {
1154         if (StreamFormatValues::Valid(streamFormat)) {
1155             startParams->streamFormat_ = std::move(streamFormat);
1156         } else {
1157             error += "'streamFormat' is invalid;";
1158         }
1159     } else if (ret == Result::TYPE_ERROR) {
1160         error += "Wrong type of 'streamFormat';";
1161     }
1162 
1163     std::string streamCompression;
1164     ret = params.GetString("streamCompression", &streamCompression);
1165     if (ret == Result::SUCCESS) {
1166         if (StreamCompressionValues::Valid(streamCompression)) {
1167             startParams->streamCompression_ = std::move(streamCompression);
1168         } else {
1169             error += "'streamCompression' is invalid;";
1170         }
1171     } else if (ret == Result::TYPE_ERROR) {
1172         error += "Wrong type of 'streamCompression';";
1173     }
1174 
1175     std::unique_ptr<PtJson> traceConfig;
1176     ret = params.GetObject("traceConfig", &traceConfig);
1177     if (ret == Result::SUCCESS) {
1178         std::unique_ptr<TraceConfig> pTraceConfig = TraceConfig::Create(*traceConfig);
1179         if (pTraceConfig == nullptr) {
1180             error += "'traceConfig' format is invalid;";
1181         } else {
1182             startParams->traceConfig_ = std::move(pTraceConfig);
1183         }
1184     } else if (ret == Result::TYPE_ERROR) {
1185         error += "Wrong type of 'traceConfig';";
1186     }
1187 
1188     std::string perfettoConfig;
1189     ret = params.GetString("perfettoConfig", &perfettoConfig);
1190     if (ret == Result::SUCCESS) {
1191         startParams->perfettoConfig_ = std::move(perfettoConfig);
1192     } else if (ret == Result::TYPE_ERROR) {
1193         error += "Wrong type of 'perfettoConfig';";
1194     }
1195 
1196     std::string tracingBackend;
1197     ret = params.GetString("tracingBackend", &tracingBackend);
1198     if (ret == Result::SUCCESS) {
1199         if (TracingBackendValues::Valid(tracingBackend)) {
1200             startParams->tracingBackend_ = std::move(tracingBackend);
1201         } else {
1202             error += "'tracingBackend' is invalid;";
1203         }
1204     } else if (ret == Result::TYPE_ERROR) {
1205         error += "Wrong type of 'tracingBackend';";
1206     }
1207 
1208     if (!error.empty()) {
1209         LOG_DEBUGGER(ERROR) << "StartParams::Create " << error;
1210         return nullptr;
1211     }
1212 
1213     return startParams;
1214 }
1215 }  // namespace panda::ecmascript::tooling
1216