| 129 | } |
| 130 | |
| 131 | void JavaScriptANRDetector::cancelNextTick() { |
| 132 | if (_nextTickTask != 0) { |
| 133 | _dispatchQueue->cancel(_nextTickTask); |
| 134 | _nextTickTask = 0; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | void JavaScriptANRDetector::setTickInterval(std::chrono::steady_clock::duration tickInterval) { |
| 139 | std::lock_guard<Mutex> lock(_mutex); |
| 140 | _tickInterval = tickInterval; |
| 141 | } |
| 142 | |
| 143 | void JavaScriptANRDetector::onNextTick(DispatchFunction nextTickCallback) { |
| 144 | std::lock_guard<Mutex> lock(_mutex); |
| 145 | _nextTickCallbacks.emplace_back(std::move(nextTickCallback)); |
| 146 | } |
| 147 | |
| 148 | void JavaScriptANRDetector::scheduleNextTick() { |
| 149 | if (_nextTickTask != 0 || !_started || !_isInForeground) { |
| 150 | return; |
| 151 | } |
| 152 | _nextTickTask = _dispatchQueue->asyncAfter( |
| 153 | [self = Valdi::weakRef(this)]() { |
| 154 | auto strongSelf = self.lock(); |
| 155 | if (strongSelf != nullptr) { |
| 156 | strongSelf->checkForANRs(); |
| 157 | } |
| 158 | }, |
| 159 | _tickInterval); |
| 160 | } |
| 161 | |
| 162 | // Must be called with _mutex held, for an entry whose syn is still waiting at a tick. |
| 163 | void JavaScriptANRDetector::startSampling(Entry& entry, std::chrono::steady_clock::time_point now) { |
| 164 | if (!_samplingConfig.enabled || entry.sampler != nullptr) { |
| 165 | return; |
| 166 | } |
| 167 | entry.sampler = std::make_unique<JavaScriptANRSampler>(_samplingConfig.maxSamples, _samplingConfig.interval); |
| 168 | entry.sampler->sample(*entry.taskScheduler, now); |
| 169 | } |
| 170 | |
| 171 | void JavaScriptANRDetector::sampleWaitingEntries() { |
| 172 | std::lock_guard<Mutex> lock(_mutex); |
| 173 | _nextSampleTask = 0; |
| 174 | auto now = std::chrono::steady_clock::now(); |
| 175 | for (const auto& entry : _entries) { |
| 176 | if (isSampling(*entry)) { |
| 177 | entry->sampler->sample(*entry->taskScheduler, now); |
| 178 | } |
| 179 | } |
| 180 | scheduleNextSample(); |
| 181 | } |
| 182 | |
| 183 | // Must be called with _mutex held. |
| 184 | void JavaScriptANRDetector::scheduleNextSample() { |
| 185 | if (_nextSampleTask != 0 || !_started || !_isInForeground || |
| 186 | std::none_of(_entries.begin(), _entries.end(), [](const auto& entry) { return isSampling(*entry); })) { |
| 187 | return; |
| 188 | } |
nothing calls this directly
no test coverage detected