| File: | Volumes/Data/worker/macOS-Safer-CPP-Checks-EWS/build/Source/WebCore/inspector/agents/page/PageTimelineAgent.cpp |
| Warning: | line 265, column 31 Call argument for 'this' parameter is unchecked and unsafe |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | /* |
| 2 | * Copyright (C) 2012 Google Inc. All rights reserved. |
| 3 | * Copyright (C) 2014 University of Washington. |
| 4 | * Copyright (C) 2015-2016 Apple Inc. All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 17 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 18 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 19 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 25 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include "config.h" |
| 29 | #include "PageTimelineAgent.h" |
| 30 | |
| 31 | #include "DocumentView.h" |
| 32 | #include "FrameSnapshotting.h" |
| 33 | #include "ImageBuffer.h" |
| 34 | #include "ImageUtilities.h" |
| 35 | #include "InspectorBackendClient.h" |
| 36 | #include "InstrumentingAgents.h" |
| 37 | #include "Page.h" |
| 38 | #include "PageInspectorController.h" |
| 39 | #include "RenderObjectInlines.h" |
| 40 | #include "RenderView.h" |
| 41 | #include "TimelineRecordFactory.h" |
| 42 | #include "WebDebuggerAgent.h" |
| 43 | |
| 44 | #if PLATFORM(IOS_FAMILY)(defined WTF_PLATFORM_IOS_FAMILY && WTF_PLATFORM_IOS_FAMILY ) |
| 45 | #include "WebCoreThreadInternal.h" |
| 46 | #include <wtf/RuntimeApplicationChecks.h> |
| 47 | #endif |
| 48 | |
| 49 | #if PLATFORM(COCOA)(defined 1 && 1) |
| 50 | #include "RunLoopObserver.h" |
| 51 | #endif |
| 52 | |
| 53 | namespace WebCore { |
| 54 | |
| 55 | using namespace Inspector; |
| 56 | |
| 57 | WTF_MAKE_TZONE_ALLOCATED_IMPL(PageTimelineAgent)::bmalloc::api::HeapRef PageTimelineAgent::s_heapRef; const TZoneSpecification PageTimelineAgent::s_heapSpec = { &PageTimelineAgent::s_heapRef , TZoneSpecification::encodeSize<PageTimelineAgent>(), TZoneSpecification ::encodeAlignment<PageTimelineAgent>(), TZoneSpecification ::encodeCategory<PageTimelineAgent>(), ::bmalloc::api:: compactAllocationMode<PageTimelineAgent>(), TZoneSpecification ::encodeDescriptor<PageTimelineAgent>(), }; void* PageTimelineAgent ::operatorNewSlow(size_t size) { if constexpr (::bmalloc::api ::compactAllocationMode<PageTimelineAgent>() == CompactAllocationMode ::Compact) return ::bmalloc::api::tzoneAllocateCompactSlow(size , s_heapSpec); return ::bmalloc::api::tzoneAllocateNonCompactSlow (size, s_heapSpec); } using __makeBtzoneMallocedInlineMacroSemicolonifier __attribute__((unused)) = int; |
| 58 | |
| 59 | #if PLATFORM(COCOA)(defined 1 && 1) |
| 60 | static CFRunLoopRef currentRunLoop() |
| 61 | { |
| 62 | #if PLATFORM(IOS_FAMILY)(defined WTF_PLATFORM_IOS_FAMILY && WTF_PLATFORM_IOS_FAMILY ) |
| 63 | // A race condition during WebView deallocation can lead to a crash if the layer sync run loop |
| 64 | // observer is added to the main run loop <rdar://problem/9798550>. However, for responsiveness, |
| 65 | // we still allow this, see <rdar://problem/7403328>. Since the race condition and subsequent |
| 66 | // crash are especially troublesome for Apple Books, we never allow the observer to be added to the |
| 67 | // main run loop in Apple Books. |
| 68 | if (WTF::CocoaApplication::isAppleBooks()) |
| 69 | return WebThreadRunLoop(); |
| 70 | #endif |
| 71 | return CFRunLoopGetCurrent(); |
| 72 | } |
| 73 | #endif |
| 74 | |
| 75 | PageTimelineAgent::PageTimelineAgent(PageAgentContext& context) |
| 76 | : InspectorTimelineAgent(context) |
| 77 | , m_inspectedPage(context.inspectedPage) |
| 78 | { |
| 79 | } |
| 80 | |
| 81 | PageTimelineAgent::~PageTimelineAgent() = default; |
| 82 | |
| 83 | bool PageTimelineAgent::enabled() const |
| 84 | { |
| 85 | return Ref { m_instrumentingAgents.get() }->enabledPageTimelineAgent() == this && InspectorTimelineAgent::enabled(); |
| 86 | } |
| 87 | |
| 88 | void PageTimelineAgent::internalEnable() |
| 89 | { |
| 90 | Ref { m_instrumentingAgents.get() }->setEnabledPageTimelineAgent(this); |
| 91 | |
| 92 | InspectorTimelineAgent::internalEnable(); |
| 93 | } |
| 94 | |
| 95 | void PageTimelineAgent::internalDisable() |
| 96 | { |
| 97 | Ref { m_instrumentingAgents.get() }->setEnabledPageTimelineAgent(nullptr); |
| 98 | |
| 99 | m_autoCaptureEnabled = false; |
| 100 | |
| 101 | InspectorTimelineAgent::internalDisable(); |
| 102 | } |
| 103 | |
| 104 | bool PageTimelineAgent::tracking() const |
| 105 | { |
| 106 | return Ref { m_instrumentingAgents.get() }->trackingPageTimelineAgent() == this && InspectorTimelineAgent::tracking(); |
| 107 | } |
| 108 | |
| 109 | void PageTimelineAgent::internalStart(std::optional<int>&& maxCallStackDepth) |
| 110 | { |
| 111 | Ref { m_instrumentingAgents.get() }->setTrackingPageTimelineAgent(this); |
| 112 | |
| 113 | // FIXME: Abstract away platform-specific code once https://bugs.webkit.org/show_bug.cgi?id=142748 is fixed. |
| 114 | |
| 115 | #if PLATFORM(COCOA)(defined 1 && 1) |
| 116 | m_frameStartObserver = makeUnique<RunLoopObserver>(RunLoopObserver::WellKnownOrder::InspectorFrameBegin, [weakThis = WeakPtr { *this }] { |
| 117 | CheckedPtr checkedThis = weakThis.get(); |
| 118 | if (!checkedThis) |
| 119 | return; |
| 120 | if (!checkedThis->tracking() || protect(checkedThis->environment())->debugger()->isPaused()) |
| 121 | return; |
| 122 | if (!checkedThis->m_runLoopNestingLevel) { |
| 123 | checkedThis->pushCurrentRecord(JSON::Object::create(), TimelineRecordType::RenderingFrame, false); |
| 124 | checkedThis->m_runLoopNestingLevel++; |
| 125 | } |
| 126 | }); |
| 127 | |
| 128 | m_frameStartObserver->schedule(currentRunLoop(), { RunLoopObserver::Activity::Entry, RunLoopObserver::Activity::AfterWaiting }); |
| 129 | |
| 130 | // Create a runloop record and increment the runloop nesting level, to capture the current turn of the main runloop |
| 131 | // (which is the outer runloop if recording started while paused in the debugger). |
| 132 | pushCurrentRecord(JSON::Object::create(), TimelineRecordType::RenderingFrame, false); |
| 133 | |
| 134 | m_runLoopNestingLevel = 1; |
| 135 | #elif USE(GLIB_EVENT_LOOP)(defined USE_GLIB_EVENT_LOOP && USE_GLIB_EVENT_LOOP) |
| 136 | m_runLoopObserver = RunLoop::EventObserver::create([weakThis = WeakPtr { *this }](RunLoop::Event event, const String& name) { |
| 137 | CheckedPtr checkedThis = weakThis.get(); |
| 138 | if (!checkedThis) |
| 139 | return; |
| 140 | if (!checkedThis->tracking() || protect(checkedThis->environment())->debugger()->isPaused()) |
| 141 | return; |
| 142 | |
| 143 | switch (event) { |
| 144 | case RunLoop::Event::WillDispatch: |
| 145 | checkedThis->pushCurrentRecord(TimelineRecordFactory::createRenderingFrameData(name), TimelineRecordType::RenderingFrame, false); |
| 146 | break; |
| 147 | case RunLoop::Event::DidDispatch: |
| 148 | if (checkedThis->m_startedComposite) |
| 149 | checkedThis->didComposite(); |
| 150 | checkedThis->didCompleteCurrentRecord(TimelineRecordType::RenderingFrame); |
| 151 | break; |
| 152 | } |
| 153 | }); |
| 154 | RunLoop::currentSingleton().observeEvent(*m_runLoopObserver); |
| 155 | #endif |
| 156 | |
| 157 | InspectorTimelineAgent::internalStart(WTF::move(maxCallStackDepth)); |
| 158 | |
| 159 | if (auto* client = m_inspectedPage->inspectorController().inspectorBackendClient()) |
| 160 | client->timelineRecordingChanged(true); |
| 161 | } |
| 162 | |
| 163 | void PageTimelineAgent::internalStop() |
| 164 | { |
| 165 | Ref { m_instrumentingAgents.get() }->setTrackingPageTimelineAgent(nullptr); |
| 166 | |
| 167 | m_autoCapturePhase = AutoCapturePhase::None; |
| 168 | |
| 169 | #if PLATFORM(COCOA)(defined 1 && 1) |
| 170 | m_frameStartObserver = nullptr; |
| 171 | m_runLoopNestingLevel = 0; |
| 172 | #elif USE(GLIB_EVENT_LOOP)(defined USE_GLIB_EVENT_LOOP && USE_GLIB_EVENT_LOOP) |
| 173 | m_runLoopObserver = nullptr; |
| 174 | #endif |
| 175 | m_startedComposite = false; |
| 176 | |
| 177 | InspectorTimelineAgent::internalStop(); |
| 178 | |
| 179 | if (auto* client = m_inspectedPage->inspectorController().inspectorBackendClient()) |
| 180 | client->timelineRecordingChanged(false); |
| 181 | } |
| 182 | |
| 183 | Inspector::Protocol::ErrorStringOr<void> PageTimelineAgent::setAutoCaptureEnabled(bool enabled) |
| 184 | { |
| 185 | m_autoCaptureEnabled = enabled; |
| 186 | |
| 187 | return { }; |
| 188 | } |
| 189 | |
| 190 | void PageTimelineAgent::didInvalidateLayout() |
| 191 | { |
| 192 | appendRecord(JSON::Object::create(), TimelineRecordType::InvalidateLayout, true); |
| 193 | } |
| 194 | |
| 195 | void PageTimelineAgent::willLayout() |
| 196 | { |
| 197 | pushCurrentRecord(JSON::Object::create(), TimelineRecordType::Layout, true); |
| 198 | } |
| 199 | |
| 200 | void PageTimelineAgent::didLayout(const Vector<FloatQuad>& layoutAreas) |
| 201 | { |
| 202 | auto* entry = lastRecordEntry(); |
| 203 | if (!entry) |
| 204 | return; |
| 205 | |
| 206 | ASSERT(entry->type == TimelineRecordType::Layout)((void)0); |
| 207 | ASSERT(!layoutAreas.isEmpty())((void)0); |
| 208 | if (!layoutAreas.isEmpty()) |
| 209 | TimelineRecordFactory::appendLayoutRoot(entry->data.get(), layoutAreas[0]); |
| 210 | |
| 211 | didCompleteCurrentRecord(TimelineRecordType::Layout); |
| 212 | } |
| 213 | |
| 214 | void PageTimelineAgent::didScheduleStyleRecalculation() |
| 215 | { |
| 216 | appendRecord(JSON::Object::create(), TimelineRecordType::ScheduleStyleRecalculation, true); |
| 217 | } |
| 218 | |
| 219 | void PageTimelineAgent::willRecalculateStyle() |
| 220 | { |
| 221 | pushCurrentRecord(JSON::Object::create(), TimelineRecordType::RecalculateStyles, true); |
| 222 | } |
| 223 | |
| 224 | void PageTimelineAgent::didRecalculateStyle() |
| 225 | { |
| 226 | didCompleteCurrentRecord(TimelineRecordType::RecalculateStyles); |
| 227 | } |
| 228 | |
| 229 | void PageTimelineAgent::willComposite() |
| 230 | { |
| 231 | ASSERT(!m_startedComposite)((void)0); |
| 232 | pushCurrentRecord(JSON::Object::create(), TimelineRecordType::Composite, true); |
| 233 | m_startedComposite = true; |
| 234 | } |
| 235 | |
| 236 | void PageTimelineAgent::didComposite() |
| 237 | { |
| 238 | if (m_startedComposite) |
| 239 | didCompleteCurrentRecord(TimelineRecordType::Composite); |
| 240 | m_startedComposite = false; |
| 241 | |
| 242 | if (instruments().contains(Inspector::Protocol::Timeline::Instrument::Screenshot)) |
| 243 | captureScreenshot(); |
| 244 | } |
| 245 | |
| 246 | void PageTimelineAgent::willPaint() |
| 247 | { |
| 248 | if (m_isCapturingScreenshot) |
| 249 | return; |
| 250 | |
| 251 | pushCurrentRecord(JSON::Object::create(), TimelineRecordType::Paint, true); |
| 252 | } |
| 253 | |
| 254 | void PageTimelineAgent::didPaint(RenderObject& renderer, const LayoutRect& clipRect) |
| 255 | { |
| 256 | if (m_isCapturingScreenshot) |
| 257 | return; |
| 258 | |
| 259 | auto* entry = lastRecordEntry(); |
| 260 | if (!entry) |
| 261 | return; |
| 262 | |
| 263 | ASSERT(entry->type == TimelineRecordType::Paint)((void)0); |
| 264 | |
| 265 | auto clipQuadInRootView = renderer.view().frameView().contentsToRootView(renderer.localToAbsoluteQuad({ clipRect })); |
Call argument for 'this' parameter is unchecked and unsafe | |
| 266 | entry->data = TimelineRecordFactory::createPaintData(clipQuadInRootView); |
| 267 | |
| 268 | didCompleteCurrentRecord(TimelineRecordType::Paint); |
| 269 | } |
| 270 | |
| 271 | void PageTimelineAgent::mainFrameStartedLoading() |
| 272 | { |
| 273 | if (!m_autoCaptureEnabled) |
| 274 | return; |
| 275 | |
| 276 | if (instruments().isEmpty()) |
| 277 | return; |
| 278 | |
| 279 | m_autoCapturePhase = AutoCapturePhase::BeforeLoad; |
| 280 | |
| 281 | // Pre-emptively disable breakpoints. The frontend must re-enable them. |
| 282 | if (auto* webDebuggerAgent = Ref { m_instrumentingAgents.get() }->enabledWebDebuggerAgent()) |
| 283 | std::ignore = webDebuggerAgent->setBreakpointsActive(false); |
| 284 | |
| 285 | // Inform the frontend we started an auto capture. The frontend must stop capture. |
| 286 | autoCaptureStarted(); |
| 287 | |
| 288 | toggleInstruments(InstrumentState::Start); |
| 289 | } |
| 290 | |
| 291 | void PageTimelineAgent::mainFrameNavigated() |
| 292 | { |
| 293 | if (m_autoCapturePhase == AutoCapturePhase::BeforeLoad) { |
| 294 | m_autoCapturePhase = AutoCapturePhase::FirstNavigation; |
| 295 | toggleInstruments(InstrumentState::Start); |
| 296 | m_autoCapturePhase = AutoCapturePhase::AfterFirstNavigation; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | void PageTimelineAgent::didCompleteRenderingFrame() |
| 301 | { |
| 302 | #if PLATFORM(COCOA)(defined 1 && 1) |
| 303 | if (!tracking() || protect(environment())->debugger()->isPaused()) |
| 304 | return; |
| 305 | |
| 306 | ASSERT(m_runLoopNestingLevel > 0)((void)0); |
| 307 | m_runLoopNestingLevel--; |
| 308 | if (m_runLoopNestingLevel) |
| 309 | return; |
| 310 | |
| 311 | if (m_startedComposite) |
| 312 | didComposite(); |
| 313 | |
| 314 | didCompleteCurrentRecord(TimelineRecordType::RenderingFrame); |
| 315 | #endif |
| 316 | } |
| 317 | |
| 318 | bool PageTimelineAgent::shouldStartHeapInstrument() const |
| 319 | { |
| 320 | if (m_autoCapturePhase == AutoCapturePhase::BeforeLoad || m_autoCapturePhase == AutoCapturePhase::AfterFirstNavigation) |
| 321 | return false; |
| 322 | return InspectorTimelineAgent::shouldStartHeapInstrument(); |
| 323 | } |
| 324 | |
| 325 | void PageTimelineAgent::captureScreenshot() |
| 326 | { |
| 327 | SetForScope isTakingScreenshot(m_isCapturingScreenshot, true); |
| 328 | |
| 329 | auto snapshotStartTime = timestamp(); |
| 330 | |
| 331 | Ref inspectedPage = m_inspectedPage.get(); |
| 332 | RefPtr localMainFrame = inspectedPage->localMainFrame(); |
| 333 | if (!localMainFrame) |
| 334 | return; |
| 335 | |
| 336 | RefPtr localMainFrameView = localMainFrame->view(); |
| 337 | if (!localMainFrameView) |
| 338 | return; |
| 339 | |
| 340 | if (RefPtr snapshot = snapshotFrameRect(*localMainFrame, localMainFrameView->unobscuredContentRect(), { { }, PixelFormat::BGRA8, DestinationColorSpace::SRGB() })) { |
| 341 | Ref snapshotRecord = TimelineRecordFactory::createScreenshotData(encodeDataURL(WTF::move(snapshot), "image/png"_s)); |
| 342 | pushCurrentRecord(WTF::move(snapshotRecord), TimelineRecordType::Screenshot, false, snapshotStartTime); |
| 343 | didCompleteCurrentRecord(TimelineRecordType::Screenshot); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | } // namespace WebCore |