• 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 "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) {  // optional value
30         error += "Unknown '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 '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 '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) {  // optional value
66         error += "Unknown '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) {  // optional value
73         error += "Unknown '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) {  // optional value
80         error += "Unknown '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) {  // optional value
87         error += "Unknown '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) {  // optional value
94         error += "Unknown '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) {  // optional value
101         error += "Unknown '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<GetPossibleBreakpointsParams> GetPossibleBreakpointsParams::Create(const PtJson &params)
112 {
113     auto paramsObject = std::make_unique<GetPossibleBreakpointsParams>();
114     std::string error;
115     Result ret;
116 
117     std::unique_ptr<PtJson> start;
118     ret = params.GetObject("start", &start);
119     if (ret == Result::SUCCESS) {
120         std::unique_ptr<Location> location = Location::Create(*start);
121         if (location == nullptr) {
122             error += "Unknown 'start';";
123         } else {
124             paramsObject->start_ = std::move(location);
125         }
126     } else {
127         error += "Unknown 'start';";
128     }
129     std::unique_ptr<PtJson> end;
130     ret = params.GetObject("end", &end);
131     if (ret == Result::SUCCESS) {
132         std::unique_ptr<Location> location = Location::Create(*end);
133         if (location == nullptr) {
134             error += "Unknown 'end';";
135         } else {
136             paramsObject->end_ = std::move(location);
137         }
138     } else if (ret == Result::TYPE_ERROR) {  // optional value
139         error += "Unknown 'end';";
140     }
141     bool restrictToFunction = false;
142     ret = params.GetBool("restrictToFunction", &restrictToFunction);
143     if (ret == Result::SUCCESS) {
144         paramsObject->restrictToFunction_ = restrictToFunction;
145     } else if (ret == Result::TYPE_ERROR) {  // optional value
146         error += "Unknown 'restrictToFunction';";
147     }
148 
149     if (!error.empty()) {
150         LOG_DEBUGGER(ERROR) << "GetPossibleBreakpointsParams::Create " << error;
151         return nullptr;
152     }
153 
154     return paramsObject;
155 }
156 
Create(const PtJson & params)157 std::unique_ptr<GetScriptSourceParams> GetScriptSourceParams::Create(const PtJson &params)
158 {
159     auto paramsObject = std::make_unique<GetScriptSourceParams>();
160     std::string error;
161     Result ret;
162 
163     std::string scriptId;
164     ret = params.GetString("scriptId", &scriptId);
165     if (ret == Result::SUCCESS) {
166         paramsObject->scriptId_ = std::stoi(scriptId);
167     } else {
168         error += "Unknown 'scriptId';";
169     }
170 
171     if (!error.empty()) {
172         LOG_DEBUGGER(ERROR) << "GetScriptSourceParams::Create " << error;
173         return nullptr;
174     }
175 
176     return paramsObject;
177 }
178 
Create(const PtJson & params)179 std::unique_ptr<RemoveBreakpointParams> RemoveBreakpointParams::Create(const PtJson &params)
180 {
181     auto paramsObject = std::make_unique<RemoveBreakpointParams>();
182     std::string error;
183     Result ret;
184 
185     std::string breakpointId;
186     ret = params.GetString("breakpointId", &breakpointId);
187     if (ret == Result::SUCCESS) {
188         paramsObject->breakpointId_ = std::move(breakpointId);
189     } else {
190         error += "Unknown 'breakpointId';";
191     }
192 
193     if (!error.empty()) {
194         LOG_DEBUGGER(ERROR) << "RemoveBreakpointParams::Create " << error;
195         return nullptr;
196     }
197 
198     return paramsObject;
199 }
200 
Create(const PtJson & params)201 std::unique_ptr<ResumeParams> ResumeParams::Create(const PtJson &params)
202 {
203     auto paramsObject = std::make_unique<ResumeParams>();
204     std::string error;
205     Result ret;
206 
207     bool terminateOnResume = false;
208     ret = params.GetBool("terminateOnResume", &terminateOnResume);
209     if (ret == Result::SUCCESS) {
210         paramsObject->terminateOnResume_ = terminateOnResume;
211     } else if (ret == Result::TYPE_ERROR) {  // optional value
212         error += "Unknown 'terminateOnResume';";
213     }
214 
215     if (!error.empty()) {
216         LOG_DEBUGGER(ERROR) << "ResumeParams::Create " << error;
217         return nullptr;
218     }
219 
220     return paramsObject;
221 }
222 
Create(const PtJson & params)223 std::unique_ptr<SetAsyncCallStackDepthParams> SetAsyncCallStackDepthParams::Create(const PtJson &params)
224 {
225     auto paramsObject = std::make_unique<SetAsyncCallStackDepthParams>();
226     std::string error;
227     Result ret;
228 
229     int32_t maxDepth;
230     ret = params.GetInt("maxDepth", &maxDepth);
231     if (ret == Result::SUCCESS) {
232         paramsObject->maxDepth_ = maxDepth;
233     } else {
234         error += "Unknown 'maxDepth';";
235     }
236 
237     if (!error.empty()) {
238         LOG_DEBUGGER(ERROR) << "SetAsyncCallStackDepthParams::Create " << error;
239         return nullptr;
240     }
241 
242     return paramsObject;
243 }
244 
Create(const PtJson & params)245 std::unique_ptr<SetBlackboxPatternsParams> SetBlackboxPatternsParams::Create(const PtJson &params)
246 {
247     auto paramsObject = std::make_unique<SetBlackboxPatternsParams>();
248     std::string error;
249     Result ret;
250 
251     std::unique_ptr<PtJson> patterns;
252     ret = params.GetArray("patterns", &patterns);
253     if (ret == Result::SUCCESS) {
254         int32_t len = patterns->GetSize();
255         for (int32_t i = 0; i < len; ++i) {
256             std::unique_ptr<PtJson> item = patterns->Get(i);
257             if (item->IsString()) {
258                 paramsObject->patterns_.emplace_back(item->GetString());
259             } else {
260                 error += "'patterns' items should be a String;";
261             }
262         }
263     } else {
264         error += "Unknown 'patterns';";
265     }
266 
267     if (!error.empty()) {
268         LOG_DEBUGGER(ERROR) << "SetBlackboxPatternsParams::Create " << error;
269         return nullptr;
270     }
271 
272     return paramsObject;
273 }
274 
Create(const PtJson & params)275 std::unique_ptr<SetBreakpointByUrlParams> SetBreakpointByUrlParams::Create(const PtJson &params)
276 {
277     auto paramsObject = std::make_unique<SetBreakpointByUrlParams>();
278     std::string error;
279     Result ret;
280 
281     int32_t lineNumber;
282     ret = params.GetInt("lineNumber", &lineNumber);
283     if (ret == Result::SUCCESS) {
284         paramsObject->lineNumber_ = lineNumber;
285     } else {
286         error += "Unknown 'lineNumber';";
287     }
288     std::string url;
289     ret = params.GetString("url", &url);
290     if (ret == Result::SUCCESS) {
291         paramsObject->url_ = std::move(url);
292     } else if (ret == Result::TYPE_ERROR) {  // optional value
293         error += "Unknown 'url';";
294     }
295     std::string urlRegex;
296     ret = params.GetString("urlRegex", &urlRegex);
297     if (ret == Result::SUCCESS) {
298         paramsObject->urlRegex_ = std::move(urlRegex);
299     } else if (ret == Result::TYPE_ERROR) {  // optional value
300         error += "Unknown 'urlRegex';";
301     }
302     std::string scriptHash;
303     ret = params.GetString("scriptHash", &scriptHash);
304     if (ret == Result::SUCCESS) {
305         paramsObject->scriptHash_ = std::move(scriptHash);
306     } else if (ret == Result::TYPE_ERROR) {  // optional value
307         error += "Unknown 'scriptHash';";
308     }
309     int32_t columnNumber;
310     ret = params.GetInt("columnNumber", &columnNumber);
311     if (ret == Result::SUCCESS) {
312         paramsObject->columnNumber_ = columnNumber;
313     } else if (ret == Result::TYPE_ERROR) {  // optional value
314         error += "Unknown 'columnNumber';";
315     }
316     std::string condition;
317     ret = params.GetString("condition", &condition);
318     if (ret == Result::SUCCESS) {
319         paramsObject->condition_ = std::move(condition);
320     } else if (ret == Result::TYPE_ERROR) {  // optional value
321         error += "Unknown 'condition';";
322     }
323     if (!error.empty()) {
324         LOG_DEBUGGER(ERROR) << "SetBreakpointByUrlParams::Create " << error;
325         return nullptr;
326     }
327 
328     return paramsObject;
329 }
330 
Create(const PtJson & params)331 std::unique_ptr<GetPossibleAndSetBreakpointParams> GetPossibleAndSetBreakpointParams::Create(const PtJson &params)
332 {
333     auto paramsObject = std::make_unique<GetPossibleAndSetBreakpointParams>();
334     std::string error;
335     Result ret;
336 
337     std::unique_ptr<PtJson> breakpoints;
338     ret = params.GetArray("locations", &breakpoints);
339     if (ret == Result::SUCCESS) {
340         int32_t length = breakpoints->GetSize();
341         std::vector<std::unique_ptr<BreakpointInfo>> breakpointList;
342         for (int32_t i = 0; i < length; i++) {
343             std::unique_ptr<BreakpointInfo> info = BreakpointInfo::Create(*breakpoints->Get(i));
344             if (info == nullptr) {
345                 error += "'breakpoints' items BreakpointInfo is invaild;";
346                 break;
347             } else {
348                 breakpointList.emplace_back(std::move(info));
349             }
350         }
351         if (!breakpointList.empty()) {
352             paramsObject->breakpointsList_ = std::move(breakpointList);
353         }
354     } else if (ret == Result::TYPE_ERROR) {
355         error += "Unknown 'breakpoints';";
356     }
357 
358     if (!error.empty()) {
359         LOG_DEBUGGER(ERROR) << "GetPossibleAndSetBreakpointParams::Create " << error;
360         return nullptr;
361     }
362 
363     return paramsObject;
364 }
365 
Create(const PtJson & params)366 std::unique_ptr<SetPauseOnExceptionsParams> SetPauseOnExceptionsParams::Create(const PtJson &params)
367 {
368     auto paramsObject = std::make_unique<SetPauseOnExceptionsParams>();
369     std::string error;
370     Result ret;
371 
372     std::string state;
373     ret = params.GetString("state", &state);
374     if (ret == Result::SUCCESS) {
375         paramsObject->StoreState(state);
376     } else {
377         error += "Unknown 'state';";
378     }
379 
380     if (!error.empty()) {
381         LOG_DEBUGGER(ERROR) << "SetPauseOnExceptionsParams::Create " << error;
382         return nullptr;
383     }
384 
385     return paramsObject;
386 }
387 
Create(const PtJson & params)388 std::unique_ptr<StepIntoParams> StepIntoParams::Create(const PtJson &params)
389 {
390     auto paramsObject = std::make_unique<StepIntoParams>();
391     std::string error;
392     Result ret;
393 
394     bool breakOnAsyncCall = false;
395     ret = params.GetBool("breakOnAsyncCall", &breakOnAsyncCall);
396     if (ret == Result::SUCCESS) {
397         paramsObject->breakOnAsyncCall_ = breakOnAsyncCall;
398     } else if (ret == Result::TYPE_ERROR) {  // optional value
399         error += "Unknown 'breakOnAsyncCall';";
400     }
401     std::unique_ptr<PtJson> skipList;
402     ret = params.GetArray("skipList", &skipList);
403     if (ret == Result::SUCCESS) {
404         int32_t len = skipList->GetSize();
405         std::list<std::unique_ptr<LocationRange>> listLocation;
406         for (int32_t i = 0; i < len; ++i) {
407             std::unique_ptr<LocationRange> obj = LocationRange::Create(*skipList->Get(i));
408             if (obj == nullptr) {
409                 error += "'skipList' items LocationRange is invalid;";
410                 break;
411             } else {
412                 listLocation.emplace_back(std::move(obj));
413             }
414         }
415         if (listLocation.size()) {
416             paramsObject->skipList_ = std::move(listLocation);
417         }
418     } else if (ret == Result::TYPE_ERROR) {  // optional value
419         error += "Unknown 'skipList';";
420     }
421 
422     if (!error.empty()) {
423         LOG_DEBUGGER(ERROR) << "StepIntoParams::Create " << error;
424         return nullptr;
425     }
426 
427     return paramsObject;
428 }
429 
Create(const PtJson & params)430 std::unique_ptr<StepOverParams> StepOverParams::Create(const PtJson &params)
431 {
432     auto paramsObject = std::make_unique<StepOverParams>();
433     std::string error;
434     Result ret;
435 
436     std::unique_ptr<PtJson> skipList;
437     ret = params.GetArray("skipList", &skipList);
438     if (ret == Result::SUCCESS) {
439         int32_t len = skipList->GetSize();
440         std::list<std::unique_ptr<LocationRange>> listLocation;
441         for (int32_t i = 0; i < len; ++i) {
442             std::unique_ptr<LocationRange> obj = LocationRange::Create(*skipList->Get(i));
443             if (obj == nullptr) {
444                 error += "'skipList' items LocationRange is invalid;";
445                 break;
446             } else {
447                 listLocation.emplace_back(std::move(obj));
448             }
449         }
450         if (listLocation.size()) {
451             paramsObject->skipList_ = std::move(listLocation);
452         }
453     } else if (ret == Result::TYPE_ERROR) {  // optional value
454         error += "Unknown 'skipList';";
455     }
456 
457     if (!error.empty()) {
458         LOG_DEBUGGER(ERROR) << "StepOverParams::Create " << error;
459         return nullptr;
460     }
461 
462     return paramsObject;
463 }
464 
Create(const PtJson & params)465 std::unique_ptr<DropFrameParams> DropFrameParams::Create(const PtJson &params)
466 {
467     auto paramsObject = std::make_unique<DropFrameParams>();
468     std::string error;
469     Result ret;
470 
471     uint32_t droppedDepth = 0;
472     ret = params.GetUInt("droppedDepth", &droppedDepth);
473     if (ret == Result::SUCCESS) {
474         paramsObject->droppedDepth_ = droppedDepth;
475     } else if (ret == Result::TYPE_ERROR) {  // optional value
476         error += "Unknown 'droppedDepth';";
477     }
478 
479     if (!error.empty()) {
480         LOG_DEBUGGER(ERROR) << "DropFrameParams::Create " << error;
481         return nullptr;
482     }
483 
484     return paramsObject;
485 }
486 
Create(const PtJson & params)487 std::unique_ptr<SetMixedDebugParams> SetMixedDebugParams::Create(const PtJson &params)
488 {
489     auto paramsObject = std::make_unique<SetMixedDebugParams>();
490     std::string error;
491     Result ret;
492 
493     bool enabled = false;
494     ret = params.GetBool("enabled", &enabled);
495     if (ret == Result::SUCCESS) {
496         paramsObject->enabled_ = enabled;
497     } else if (ret == Result::TYPE_ERROR) {  // optional value
498         error += "Unknown 'enabled';";
499     }
500 
501     if (!error.empty()) {
502         LOG_DEBUGGER(ERROR) << "SetMixedDebugParams::Create " << error;
503         return nullptr;
504     }
505 
506     return paramsObject;
507 }
508 
Create(const PtJson & params)509 std::unique_ptr<ReplyNativeCallingParams> ReplyNativeCallingParams::Create(const PtJson &params)
510 {
511     auto paramsObject = std::make_unique<ReplyNativeCallingParams>();
512     std::string error;
513     Result ret;
514 
515     bool userCode = false;
516     ret = params.GetBool("userCode", &userCode);
517     if (ret == Result::SUCCESS) {
518         paramsObject->userCode_ = userCode;
519     } else if (ret == Result::TYPE_ERROR) {  // optional value
520         error += "Unknown 'userCode';";
521     }
522 
523     if (!error.empty()) {
524         LOG_DEBUGGER(ERROR) << "ReplyNativeCallingParams::Create " << error;
525         return nullptr;
526     }
527 
528     return paramsObject;
529 }
530 
Create(const PtJson & params)531 std::unique_ptr<GetPropertiesParams> GetPropertiesParams::Create(const PtJson &params)
532 {
533     auto paramsObject = std::make_unique<GetPropertiesParams>();
534     std::string error;
535     Result ret;
536 
537     std::string objectId;
538     ret = params.GetString("objectId", &objectId);
539     if (ret == Result::SUCCESS) {
540         paramsObject->objectId_ = std::stoi(objectId);
541     } else {
542         error += "Unknown 'objectId';";
543     }
544     bool ownProperties = false;
545     ret = params.GetBool("ownProperties", &ownProperties);
546     if (ret == Result::SUCCESS) {
547         paramsObject->ownProperties_ = ownProperties;
548     } else if (ret == Result::TYPE_ERROR) {  // optional value
549         error += "Unknown 'ownProperties';";
550     }
551     bool accessorPropertiesOnly = false;
552     ret = params.GetBool("accessorPropertiesOnly", &accessorPropertiesOnly);
553     if (ret == Result::SUCCESS) {
554         paramsObject->accessorPropertiesOnly_ = accessorPropertiesOnly;
555     } else if (ret == Result::TYPE_ERROR) {  // optional value
556         error += "Unknown 'accessorPropertiesOnly';";
557     }
558     bool generatePreview = false;
559     ret = params.GetBool("generatePreview", &generatePreview);
560     if (ret == Result::SUCCESS) {
561         paramsObject->generatePreview_ = generatePreview;
562     } else if (ret == Result::TYPE_ERROR) {  // optional value
563         error += "Unknown 'generatePreview';";
564     }
565     if (!error.empty()) {
566         LOG_DEBUGGER(ERROR) << "GetPropertiesParams::Create " << error;
567         return nullptr;
568     }
569 
570     return paramsObject;
571 }
572 
Create(const PtJson & params)573 std::unique_ptr<CallFunctionOnParams> CallFunctionOnParams::Create(const PtJson &params)
574 {
575     auto paramsObject = std::make_unique<CallFunctionOnParams>();
576     std::string error;
577     Result ret;
578 
579     // paramsObject->functionDeclaration_
580     std::string functionDeclaration;
581     ret = params.GetString("functionDeclaration", &functionDeclaration);
582     if (ret == Result::SUCCESS) {
583         paramsObject->functionDeclaration_ = std::move(functionDeclaration);
584     } else {
585         error += "Unknown 'functionDeclaration';";
586     }
587     // paramsObject->objectId_
588     std::string objectId;
589     ret = params.GetString("objectId", &objectId);
590     if (ret == Result::SUCCESS) {
591         paramsObject->objectId_ = std::stoi(objectId);
592     } else if (ret == Result::TYPE_ERROR) {  // optional value
593         error += "Unknown 'objectId';";
594     }
595     // paramsObject->arguments_
596     std::unique_ptr<PtJson> arguments;
597     ret = params.GetArray("arguments", &arguments);
598     if (ret == Result::SUCCESS) {
599         int32_t len = arguments->GetSize();
600         std::vector<std::unique_ptr<CallArgument>> callArgument;
601         for (int32_t i = 0; i < len; ++i) {
602             std::unique_ptr<CallArgument> obj = CallArgument::Create(*arguments->Get(i));
603             if (obj == nullptr) {
604                 error += "'arguments' items CallArgument is invaild;";
605                 break;
606             } else {
607                 callArgument.emplace_back(std::move(obj));
608             }
609         }
610         if (callArgument.size()) {
611             paramsObject->arguments_ = std::move(callArgument);
612         }
613     } else if (ret == Result::TYPE_ERROR) {  // optional value
614         error += "Unknown 'arguments';";
615     }
616     // paramsObject->silent_
617     bool silent = false;
618     ret = params.GetBool("silent", &silent);
619     if (ret == Result::SUCCESS) {
620         paramsObject->silent_ = silent;
621     } else if (ret == Result::TYPE_ERROR) {  // optional value
622         error += "Unknown 'silent';";
623     }
624     // paramsObject->returnByValue_
625     bool returnByValue = false;
626     ret = params.GetBool("returnByValue", &returnByValue);
627     if (ret == Result::SUCCESS) {
628         paramsObject->returnByValue_ = returnByValue;
629     } else if (ret == Result::TYPE_ERROR) {  // optional value
630         error += "Unknown 'returnByValue';";
631     }
632     // paramsObject->generatePreview_
633     bool generatePreview = false;
634     ret = params.GetBool("generatePreview", &generatePreview);
635     if (ret == Result::SUCCESS) {
636         paramsObject->generatePreview_ = generatePreview;
637     } else if (ret == Result::TYPE_ERROR) {  // optional value
638         error += "Unknown 'generatePreview';";
639     }
640     // paramsObject->userGesture_
641     bool userGesture = false;
642     ret = params.GetBool("userGesture", &userGesture);
643     if (ret == Result::SUCCESS) {
644         paramsObject->userGesture_ = userGesture;
645     } else if (ret == Result::TYPE_ERROR) {  // optional value
646         error += "Unknown 'userGesture';";
647     }
648     // paramsObject->awaitPromise_
649     bool awaitPromise = false;
650     ret = params.GetBool("awaitPromise", &awaitPromise);
651     if (ret == Result::SUCCESS) {
652         paramsObject->awaitPromise_ = awaitPromise;
653     } else if (ret == Result::TYPE_ERROR) {  // optional value
654         error += "Unknown 'awaitPromise';";
655     }
656     // paramsObject->executionContextId_
657     int32_t executionContextId;
658     ret = params.GetInt("executionContextId", &executionContextId);
659     if (ret == Result::SUCCESS) {
660         paramsObject->executionContextId_ = executionContextId;
661     } else if (ret == Result::TYPE_ERROR) {  // optional value
662         error += "Unknown 'executionContextId';";
663     }
664     // paramsObject->objectGroup_
665     std::string objectGroup;
666     ret = params.GetString("objectGroup", &objectGroup);
667     if (ret == Result::SUCCESS) {
668         paramsObject->objectGroup_ = std::move(objectGroup);
669     } else if (ret == Result::TYPE_ERROR) {  // optional value
670         error += "Unknown 'objectGroup';";
671     }
672     // paramsObject->throwOnSideEffect_
673     bool throwOnSideEffect = false;
674     ret = params.GetBool("throwOnSideEffect", &throwOnSideEffect);
675     if (ret == Result::SUCCESS) {
676         paramsObject->throwOnSideEffect_ = throwOnSideEffect;
677     } else if (ret == Result::TYPE_ERROR) {  // optional value
678         error += "Unknown 'throwOnSideEffect';";
679     }
680 
681     // Check whether the error is empty.
682     if (!error.empty()) {
683         LOG_DEBUGGER(ERROR) << "CallFunctionOnParams::Create " << error;
684         return nullptr;
685     }
686 
687     return paramsObject;
688 }
689 
Create(const PtJson & params)690 std::unique_ptr<StartSamplingParams> StartSamplingParams::Create(const PtJson &params)
691 {
692     auto paramsObject = std::make_unique<StartSamplingParams>();
693     std::string error;
694     Result ret;
695 
696     double samplingInterval;
697     ret = params.GetDouble("samplingInterval", &samplingInterval);
698     if (ret == Result::SUCCESS) {
699         if (samplingInterval <= 0) {
700             error += "Invalid SamplingInterval";
701         } else {
702             paramsObject->samplingInterval_ = samplingInterval;
703         }
704     } else if (ret == Result::TYPE_ERROR) {  // optional value
705         error += "Unknown 'samplingInterval';";
706     }
707 
708     if (!error.empty()) {
709         LOG_DEBUGGER(ERROR) << "StartSamplingParams::Create " << error;
710         return nullptr;
711     }
712     return paramsObject;
713 }
714 
Create(const PtJson & params)715 std::unique_ptr<StartTrackingHeapObjectsParams> StartTrackingHeapObjectsParams::Create(const PtJson &params)
716 {
717     auto paramsObject = std::make_unique<StartTrackingHeapObjectsParams>();
718     std::string error;
719     Result ret;
720 
721     bool trackAllocations = false;
722     ret = params.GetBool("trackAllocations", &trackAllocations);
723     if (ret == Result::SUCCESS) {
724         paramsObject->trackAllocations_ = trackAllocations;
725     } else if (ret == Result::TYPE_ERROR) {  // optional value
726         error += "Unknown 'trackAllocations';";
727     }
728 
729     if (!error.empty()) {
730         LOG_DEBUGGER(ERROR) << "StartTrackingHeapObjectsParams::Create " << error;
731         return nullptr;
732     }
733     return paramsObject;
734 }
735 
Create(const PtJson & params)736 std::unique_ptr<StopTrackingHeapObjectsParams> StopTrackingHeapObjectsParams::Create(const PtJson &params)
737 {
738     auto paramsObject = std::make_unique<StopTrackingHeapObjectsParams>();
739     std::string error;
740     Result ret;
741 
742     bool reportProgress = false;
743     ret = params.GetBool("reportProgress", &reportProgress);
744     if (ret == Result::SUCCESS) {
745         paramsObject->reportProgress_ = reportProgress;
746     } else if (ret == Result::TYPE_ERROR) {  // optional value
747         error += "Unknown 'reportProgress';";
748     }
749 
750     bool treatGlobalObjectsAsRoots = false;
751     ret = params.GetBool("treatGlobalObjectsAsRoots", &treatGlobalObjectsAsRoots);
752     if (ret == Result::SUCCESS) {
753         paramsObject->treatGlobalObjectsAsRoots_ = treatGlobalObjectsAsRoots;
754     } else if (ret == Result::TYPE_ERROR) {  // optional value
755         error += "Unknown 'treatGlobalObjectsAsRoots';";
756     }
757 
758     bool captureNumericValue = false;
759     ret = params.GetBool("captureNumericValue", &captureNumericValue);
760     if (ret == Result::SUCCESS) {
761         paramsObject->captureNumericValue_ = captureNumericValue;
762     } else if (ret == Result::TYPE_ERROR) {  // optional value
763         error += "Unknown 'captureNumericValue';";
764     }
765 
766     if (!error.empty()) {
767         LOG_DEBUGGER(ERROR) << "StopTrackingHeapObjectsParams::Create " << error;
768         return nullptr;
769     }
770     return paramsObject;
771 }
772 
Create(const PtJson & params)773 std::unique_ptr<AddInspectedHeapObjectParams> AddInspectedHeapObjectParams::Create(const PtJson &params)
774 {
775     auto paramsObject = std::make_unique<AddInspectedHeapObjectParams>();
776     std::string error;
777     Result ret;
778 
779     std::string heapObjectId;
780     ret = params.GetString("heapObjectId", &heapObjectId);
781     if (ret == Result::SUCCESS) {
782         paramsObject->heapObjectId_ = std::stoi(heapObjectId);
783     } else {
784         error += "Unknown 'heapObjectId';";
785     }
786 
787     if (!error.empty()) {
788         LOG_DEBUGGER(ERROR) << "AddInspectedHeapObjectParams::Create " << error;
789         return nullptr;
790     }
791     return paramsObject;
792 }
793 
Create(const PtJson & params)794 std::unique_ptr<GetHeapObjectIdParams> GetHeapObjectIdParams::Create(const PtJson &params)
795 {
796     auto paramsObject = std::make_unique<GetHeapObjectIdParams>();
797     std::string error;
798     Result ret;
799 
800     std::string objectId;
801     ret = params.GetString("objectId", &objectId);
802     if (ret == Result::SUCCESS) {
803         paramsObject->objectId_ = std::stoi(objectId);
804     } else if (ret == Result::TYPE_ERROR) {  // optional value
805         error += "Unknown 'objectId';";
806     }
807 
808     if (!error.empty()) {
809         LOG_DEBUGGER(ERROR) << "GetHeapObjectIdParams::Create " << error;
810         return nullptr;
811     }
812     return paramsObject;
813 }
814 
Create(const PtJson & params)815 std::unique_ptr<GetObjectByHeapObjectIdParams> GetObjectByHeapObjectIdParams::Create(const PtJson &params)
816 {
817     auto paramsObject = std::make_unique<GetObjectByHeapObjectIdParams>();
818     std::string error;
819     Result ret;
820 
821     std::string objectId;
822     ret = params.GetString("objectId", &objectId);
823     if (ret == Result::SUCCESS) {
824         paramsObject->objectId_ = std::stoi(objectId);
825     } else if (ret == Result::TYPE_ERROR) {  // optional value
826         error += "Unknown 'objectId';";
827     }
828 
829     std::string objectGroup;
830     ret = params.GetString("objectGroup", &objectGroup);
831     if (ret == Result::SUCCESS) {
832         paramsObject->objectGroup_ = std::move(objectGroup);
833     } else if (ret == Result::TYPE_ERROR) {  // optional value
834         error += "Unknown 'objectGroup';";
835     }
836 
837     if (!error.empty()) {
838         LOG_DEBUGGER(ERROR) << "GetObjectByHeapObjectIdParams::Create " << error;
839         return nullptr;
840     }
841     return paramsObject;
842 }
843 
Create(const PtJson & params)844 std::unique_ptr<StartPreciseCoverageParams> StartPreciseCoverageParams::Create(const PtJson &params)
845 {
846     auto paramsObject = std::make_unique<StartPreciseCoverageParams>();
847     std::string error;
848     Result ret;
849 
850     bool callCount = false;
851     ret = params.GetBool("callCount", &callCount);
852     if (ret == Result::SUCCESS) {
853         paramsObject->callCount_ = callCount;
854     } else if (ret == Result::TYPE_ERROR) {  // optional value
855         error += "Unknown 'callCount';";
856     }
857 
858     bool detailed = false;
859     ret = params.GetBool("detailed", &detailed);
860     if (ret == Result::SUCCESS) {
861         paramsObject->detailed_ = detailed;
862     } else if (ret == Result::TYPE_ERROR) {  // optional value
863         error += "Unknown 'detailed';";
864     }
865 
866     bool allowTriggeredUpdates = false;
867     ret = params.GetBool("allowTriggeredUpdates", &allowTriggeredUpdates);
868     if (ret == Result::SUCCESS) {
869         paramsObject->allowTriggeredUpdates_ = allowTriggeredUpdates;
870     } else if (ret == Result::TYPE_ERROR) {  // optional value
871         error += "Unknown 'allowTriggeredUpdates';";
872     }
873 
874     if (!error.empty()) {
875         LOG_DEBUGGER(ERROR) << "StartPreciseCoverageParams::Create " << error;
876         return nullptr;
877     }
878     return paramsObject;
879 }
880 
Create(const PtJson & params)881 std::unique_ptr<SetSamplingIntervalParams> SetSamplingIntervalParams::Create(const PtJson &params)
882 {
883     auto paramsObject = std::make_unique<SetSamplingIntervalParams>();
884     std::string error;
885     Result ret;
886 
887     int32_t interval = 0;
888     ret = params.GetInt("interval", &interval);
889     if (ret == Result::SUCCESS) {
890         paramsObject->interval_ = interval;
891     } else {
892         error += "Unknown 'interval';";
893     }
894 
895     if (!error.empty()) {
896         LOG_DEBUGGER(ERROR) << "SetSamplingIntervalParams::Create " << error;
897         return nullptr;
898     }
899     return paramsObject;
900 }
901 
Create(const PtJson & params)902 std::unique_ptr<RecordClockSyncMarkerParams> RecordClockSyncMarkerParams::Create(const PtJson &params)
903 {
904     std::string error;
905     auto recordClockSyncMarkerParams = std::make_unique<RecordClockSyncMarkerParams>();
906     Result ret;
907 
908     std::string syncId;
909     ret = params.GetString("syncId", &syncId);
910     if (ret == Result::SUCCESS) {
911         recordClockSyncMarkerParams->syncId_ = syncId;
912     } else {
913         error += "Unknown 'syncId';";
914     }
915 
916     if (!error.empty()) {
917         LOG_DEBUGGER(ERROR) << "RecordClockSyncMarkerParams::Create " << error;
918         return nullptr;
919     }
920 
921     return recordClockSyncMarkerParams;
922 }
923 
Create(const PtJson & params)924 std::unique_ptr<RequestMemoryDumpParams> RequestMemoryDumpParams::Create(const PtJson &params)
925 {
926     std::string error;
927     auto requestMemoryDumpParams = std::make_unique<RequestMemoryDumpParams>();
928     Result ret;
929 
930     bool deterministic = false;
931     ret = params.GetBool("deterministic", &deterministic);
932     if (ret == Result::SUCCESS) {
933         requestMemoryDumpParams->deterministic_ = deterministic;
934     } else if (ret == Result::TYPE_ERROR) {
935         error += "Unknown 'deterministic';";
936     }
937 
938     std::string levelOfDetail;
939     ret = params.GetString("levelOfDetail", &levelOfDetail);
940     if (ret == Result::SUCCESS) {
941         if (MemoryDumpLevelOfDetailValues::Valid(levelOfDetail)) {
942             requestMemoryDumpParams->levelOfDetail_ = std::move(levelOfDetail);
943         } else {
944             error += "'levelOfDetail' is invalid;";
945         }
946     } else if (ret == Result::TYPE_ERROR) {
947         error += "Unknown 'levelOfDetail';";
948     }
949 
950     if (!error.empty()) {
951         LOG_DEBUGGER(ERROR) << "RequestMemoryDumpParams::Create " << error;
952         return nullptr;
953     }
954 
955     return requestMemoryDumpParams;
956 }
957 
Create(const PtJson & params)958 std::unique_ptr<StartParams> StartParams::Create(const PtJson &params)
959 {
960     std::string error;
961     auto startParams = std::make_unique<StartParams>();
962     Result ret;
963 
964     std::string categories;
965     ret = params.GetString("categories", &categories);
966     if (ret == Result::SUCCESS) {
967         startParams->categories_ = std::move(categories);
968     } else if (ret == Result::TYPE_ERROR) {
969         error += "Unknown 'categories';";
970     }
971 
972     std::string options;
973     ret = params.GetString("options", &options);
974     if (ret == Result::SUCCESS) {
975         startParams->options_ = std::move(options);
976     } else if (ret == Result::TYPE_ERROR) {
977         error += "Unknown 'options';";
978     }
979 
980     int32_t bufferUsageReportingInterval = 0;
981     ret = params.GetInt("bufferUsageReportingInterval", &bufferUsageReportingInterval);
982     if (ret == Result::SUCCESS) {
983         startParams->bufferUsageReportingInterval_ = bufferUsageReportingInterval;
984     } else if (ret == Result::TYPE_ERROR) {
985         error += "Unknown 'bufferUsageReportingInterval';";
986     }
987 
988     std::string transferMode;
989     ret = params.GetString("transferMode", &transferMode);
990     if (ret == Result::SUCCESS) {
991         if (StartParams::TransferModeValues::Valid(transferMode)) {
992             startParams->transferMode_ = std::move(transferMode);
993         } else {
994             error += "'transferMode' is invalid;";
995         }
996     } else if (ret == Result::TYPE_ERROR) {
997         error += "Unknown 'transferMode';";
998     }
999 
1000     std::string streamFormat;
1001     ret = params.GetString("streamFormat", &streamFormat);
1002     if (ret == Result::SUCCESS) {
1003         if (StreamFormatValues::Valid(streamFormat)) {
1004             startParams->streamFormat_ = std::move(streamFormat);
1005         } else {
1006             error += "'streamFormat' is invalid;";
1007         }
1008     } else if (ret == Result::TYPE_ERROR) {
1009         error += "Unknown 'streamFormat';";
1010     }
1011 
1012     std::string streamCompression;
1013     ret = params.GetString("streamCompression", &streamCompression);
1014     if (ret == Result::SUCCESS) {
1015         if (StreamCompressionValues::Valid(streamCompression)) {
1016             startParams->streamCompression_ = std::move(streamCompression);
1017         } else {
1018             error += "'streamCompression' is invalid;";
1019         }
1020     } else if (ret == Result::TYPE_ERROR) {
1021         error += "Unknown 'streamCompression';";
1022     }
1023 
1024     std::unique_ptr<PtJson> traceConfig;
1025     ret = params.GetObject("traceConfig", &traceConfig);
1026     if (ret == Result::SUCCESS) {
1027         std::unique_ptr<TraceConfig> pTraceConfig = TraceConfig::Create(*traceConfig);
1028         if (pTraceConfig == nullptr) {
1029             error += "'traceConfig' format invalid;";
1030         } else {
1031             startParams->traceConfig_ = std::move(pTraceConfig);
1032         }
1033     } else if (ret == Result::TYPE_ERROR) {
1034         error += "Unknown 'traceConfig';";
1035     }
1036 
1037     std::string perfettoConfig;
1038     ret = params.GetString("perfettoConfig", &perfettoConfig);
1039     if (ret == Result::SUCCESS) {
1040         startParams->perfettoConfig_ = std::move(perfettoConfig);
1041     } else if (ret == Result::TYPE_ERROR) {
1042         error += "Unknown 'perfettoConfig';";
1043     }
1044 
1045     std::string tracingBackend;
1046     ret = params.GetString("tracingBackend", &tracingBackend);
1047     if (ret == Result::SUCCESS) {
1048         if (TracingBackendValues::Valid(tracingBackend)) {
1049             startParams->tracingBackend_ = std::move(tracingBackend);
1050         } else {
1051             error += "'tracingBackend' is invalid;";
1052         }
1053     } else if (ret == Result::TYPE_ERROR) {
1054         error += "Unknown 'tracingBackend';";
1055     }
1056 
1057     if (!error.empty()) {
1058         LOG_DEBUGGER(ERROR) << "StartParams::Create " << error;
1059         return nullptr;
1060     }
1061 
1062     return startParams;
1063 }
1064 }  // namespace panda::ecmascript::tooling
1065