1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #include <memory>
9 
10 #include "ABI49_0_0NativePerformanceObserver.h"
11 #include "ABI49_0_0PerformanceEntryReporter.h"
12 
13 #include "ABI49_0_0Plugins.h"
14 
15 std::shared_ptr<ABI49_0_0facebook::ABI49_0_0React::TurboModule>
NativePerformanceObserverModuleProvider(std::shared_ptr<ABI49_0_0facebook::ABI49_0_0React::CallInvoker> jsInvoker)16 NativePerformanceObserverModuleProvider(
17     std::shared_ptr<ABI49_0_0facebook::ABI49_0_0React::CallInvoker> jsInvoker) {
18   return std::make_shared<ABI49_0_0facebook::ABI49_0_0React::NativePerformanceObserver>(
19       std::move(jsInvoker));
20 }
21 
22 namespace ABI49_0_0facebook::ABI49_0_0React {
23 
NativePerformanceObserver(std::shared_ptr<CallInvoker> jsInvoker)24 NativePerformanceObserver::NativePerformanceObserver(
25     std::shared_ptr<CallInvoker> jsInvoker)
26     : ABI49_0_0NativePerformanceObserverCxxSpec(std::move(jsInvoker)) {
27   setEventLogger(&PerformanceEntryReporter::getInstance());
28 }
29 
~NativePerformanceObserver()30 NativePerformanceObserver::~NativePerformanceObserver() {
31   setEventLogger(nullptr);
32 }
33 
startReporting(jsi::Runtime & rt,int32_t entryType)34 void NativePerformanceObserver::startReporting(
35     jsi::Runtime &rt,
36     int32_t entryType) {
37   PerformanceEntryReporter::getInstance().startReporting(
38       static_cast<PerformanceEntryType>(entryType));
39 }
40 
stopReporting(jsi::Runtime & rt,int32_t entryType)41 void NativePerformanceObserver::stopReporting(
42     jsi::Runtime &rt,
43     int32_t entryType) {
44   PerformanceEntryReporter::getInstance().stopReporting(
45       static_cast<PerformanceEntryType>(entryType));
46 }
47 
popPendingEntries(jsi::Runtime & rt)48 GetPendingEntriesResult NativePerformanceObserver::popPendingEntries(
49     jsi::Runtime &rt) {
50   return PerformanceEntryReporter::getInstance().popPendingEntries();
51 }
52 
setOnPerformanceEntryCallback(jsi::Runtime & rt,std::optional<AsyncCallback<>> callback)53 void NativePerformanceObserver::setOnPerformanceEntryCallback(
54     jsi::Runtime &rt,
55     std::optional<AsyncCallback<>> callback) {
56   PerformanceEntryReporter::getInstance().setReportingCallback(callback);
57 }
58 
logRawEntry(jsi::Runtime & rt,RawPerformanceEntry entry)59 void NativePerformanceObserver::logRawEntry(
60     jsi::Runtime &rt,
61     RawPerformanceEntry entry) {
62   PerformanceEntryReporter::getInstance().logEntry(entry);
63 }
64 
65 std::vector<std::pair<std::string, uint32_t>>
getEventCounts(jsi::Runtime & rt)66 NativePerformanceObserver::getEventCounts(jsi::Runtime &rt) {
67   const auto &eventCounts =
68       PerformanceEntryReporter::getInstance().getEventCounts();
69   return std::vector<std::pair<std::string, uint32_t>>(
70       eventCounts.begin(), eventCounts.end());
71 }
72 
setDurationThreshold(jsi::Runtime & rt,int32_t entryType,double durationThreshold)73 void NativePerformanceObserver::setDurationThreshold(
74     jsi::Runtime &rt,
75     int32_t entryType,
76     double durationThreshold) {
77   PerformanceEntryReporter::getInstance().setDurationThreshold(
78       static_cast<PerformanceEntryType>(entryType), durationThreshold);
79 }
80 
clearEntries(jsi::Runtime & rt,int32_t entryType,std::optional<std::string> entryName)81 void NativePerformanceObserver::clearEntries(
82     jsi::Runtime &rt,
83     int32_t entryType,
84     std::optional<std::string> entryName) {
85   PerformanceEntryReporter::getInstance().clearEntries(
86       static_cast<PerformanceEntryType>(entryType),
87       entryName ? entryName->c_str() : nullptr);
88 }
89 
getEntries(jsi::Runtime & rt,std::optional<int32_t> entryType,std::optional<std::string> entryName)90 std::vector<RawPerformanceEntry> NativePerformanceObserver::getEntries(
91     jsi::Runtime &rt,
92     std::optional<int32_t> entryType,
93     std::optional<std::string> entryName) {
94   return PerformanceEntryReporter::getInstance().getEntries(
95       entryType ? static_cast<PerformanceEntryType>(*entryType)
96                 : PerformanceEntryType::UNDEFINED,
97       entryName ? entryName->c_str() : nullptr);
98 }
99 
100 } // namespace ABI49_0_0facebook::ABI49_0_0React
101