File: paint_preview_browsertest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (594 lines) | stat: -rw-r--r-- 24,816 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <optional>
#include <string_view>

#include "base/base64.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/unguessable_token.h"
#include "build/build_config.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/paint_preview/browser/paint_preview_client.h"
#include "components/paint_preview/common/file_stream.h"
#include "components/paint_preview/common/mojom/paint_preview_recorder.mojom-shared.h"
#include "components/paint_preview/common/proto/paint_preview.pb.h"
#include "components/paint_preview/common/recording_map.h"
#include "components/paint_preview/common/serialized_recording.h"
#include "components/paint_preview/common/test_utils.h"
#include "components/ukm/test_ukm_recorder.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/fenced_frame_test_util.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
#include "third_party/skia/include/core/SkPicture.h"
#include "third_party/skia/include/core/SkStream.h"
#include "url/gurl.h"

namespace paint_preview {

class NoOpPaintPreviewRecorder : public mojom::PaintPreviewRecorder {
 public:
  NoOpPaintPreviewRecorder() = default;
  ~NoOpPaintPreviewRecorder() override = default;

  NoOpPaintPreviewRecorder(const NoOpPaintPreviewRecorder&) = delete;
  NoOpPaintPreviewRecorder& operator=(const NoOpPaintPreviewRecorder&) = delete;

  void SetRequestedClosure(base::OnceClosure requested) {
    requested_ = std::move(requested);
  }

  void CapturePaintPreview(
      mojom::PaintPreviewCaptureParamsPtr params,
      mojom::PaintPreviewRecorder::CapturePaintPreviewCallback callback)
      override {
    callback_ = std::move(callback);
    std::move(requested_).Run();
  }

  void BindRequest(mojo::ScopedInterfaceEndpointHandle handle) {
    binding_.Bind(mojo::PendingAssociatedReceiver<mojom::PaintPreviewRecorder>(
        std::move(handle)));
  }

 private:
  base::OnceClosure requested_;
  mojom::PaintPreviewRecorder::CapturePaintPreviewCallback callback_;
  mojo::AssociatedReceiver<mojom::PaintPreviewRecorder> binding_{this};
};

// Test harness for a integration test of paint previews. In this test:
// - Each RenderFrame has an instance of PaintPreviewRecorder attached.
// - Each WebContents has an instance of PaintPreviewClient attached.
// This permits end-to-end testing of the flow of paint previews.
class PaintPreviewBrowserTest
    : public InProcessBrowserTest,
      public testing::WithParamInterface<RecordingPersistence> {
 public:
  PaintPreviewBrowserTest(const PaintPreviewBrowserTest&) = delete;
  PaintPreviewBrowserTest& operator=(const PaintPreviewBrowserTest&) = delete;

 protected:
  PaintPreviewBrowserTest() = default;
  ~PaintPreviewBrowserTest() override = default;

  void SetUp() override {
    ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());

    InProcessBrowserTest::SetUp();
  }

  void SetUpOnMainThread() override {
    host_resolver()->AddRule("*", "127.0.0.1");

    http_server_.ServeFilesFromSourceDirectory("content/test/data");

    ASSERT_TRUE(http_server_.Start());
  }

  void SetUpCommandLine(base::CommandLine* command_line) override {
    content::IsolateAllSitesForTesting(command_line);
  }

  void CreateClient() {
    PaintPreviewClient::CreateForWebContents(GetWebContents());
  }

  content::WebContents* GetWebContents() {
    return browser()->tab_strip_model()->GetActiveWebContents();
  }

  void LoadPage(const GURL& url) const {
    ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
  }

  void LoadHtml(std::string_view html) const {
    std::string base64_html = base::Base64Encode(html);
    GURL url(std::string("data:text/html;base64,") + base64_html);
    ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
  }

  PaintPreviewClient::PaintPreviewParams MakeParams() const {
    PaintPreviewClient::PaintPreviewParams params(GetParam());
    params.inner.is_main_frame = true;
    params.root_dir = temp_dir_.GetPath();
    params.inner.capture_links = true;
    params.inner.max_capture_size = 0;
    return params;
  }

  void OverrideInterface(NoOpPaintPreviewRecorder* service,
                         content::RenderFrameHost* rfh) {
    blink::AssociatedInterfaceProvider* remote_interfaces =
        rfh->GetRemoteAssociatedInterfaces();
    remote_interfaces->OverrideBinderForTesting(
        mojom::PaintPreviewRecorder::Name_,
        base::BindRepeating(&NoOpPaintPreviewRecorder::BindRequest,
                            base::Unretained(service)));
  }

  void WaitForLoadStopWithoutSuccessCheck() {
    // In many cases, the load may have finished before we get here.  Only wait
    // if the tab still has a pending navigation.
    auto* web_contents = GetWebContents();
    if (web_contents->IsLoading()) {
      content::LoadStopObserver load_stop_observer(web_contents);
      load_stop_observer.Wait();
    }
  }

  // Check that |recording_map| contains the frame |frame_proto| and is a valid
  // SkPicture. Don't bother checking the contents as this is non-trivial and
  // could change. Instead check that the SkPicture can be read correctly and
  // has a cull rect of at least |size|.
  //
  // Consumes the recording from |recording_map|.
  static void EnsureSkPictureIsValid(RecordingMap* recording_map,
                                     const PaintPreviewFrameProto& frame_proto,
                                     size_t expected_subframe_count,
                                     const gfx::Size& size = gfx::Size(1, 1)) {
    base::ScopedAllowBlockingForTesting scoped_blocking;

    auto it = recording_map->find(
        base::UnguessableToken::Deserialize(frame_proto.embedding_token_high(),
                                            frame_proto.embedding_token_low())
            .value());
    ASSERT_NE(it, recording_map->end());

    std::optional<SkpResult> result = std::move(it->second).Deserialize();
    ASSERT_TRUE(result.has_value());
    EXPECT_NE(result->skp, nullptr);
    EXPECT_GE(result->skp->cullRect().width(), 0);
    EXPECT_GE(result->skp->cullRect().height(), 0);
    EXPECT_EQ(result->ctx.size(), expected_subframe_count);

    recording_map->erase(it);
  }

  base::ScopedTempDir temp_dir_;
  net::EmbeddedTestServer http_server_;
  net::EmbeddedTestServer http_server_different_origin_;
};

IN_PROC_BROWSER_TEST_P(PaintPreviewBrowserTest, CaptureFrame) {
  LoadPage(http_server_.GetURL("a.com", "/cross_site_iframe_factory.html?a"));
  ukm::TestAutoSetUkmRecorder ukm_recorder;
  auto params = MakeParams();

  base::RunLoop loop;

  CreateClient();
  auto* client = PaintPreviewClient::FromWebContents(GetWebContents());
  WaitForLoadStopWithoutSuccessCheck();
  client->CapturePaintPreview(
      params, GetWebContents()->GetPrimaryMainFrame(),
      base::BindOnce(
          [](base::RepeatingClosure quit,
             const PaintPreviewClient::PaintPreviewParams& params,
             base::UnguessableToken guid, mojom::PaintPreviewStatus status,
             std::unique_ptr<CaptureResult> result) {
            EXPECT_EQ(guid, params.inner.document_guid);
            EXPECT_EQ(status, mojom::PaintPreviewStatus::kOk);
            EXPECT_TRUE(result->proto.has_root_frame());
            EXPECT_EQ(result->proto.subframes_size(), 0);
            EXPECT_EQ(result->proto.root_frame()
                          .content_id_to_embedding_tokens_size(),
                      0);
            EXPECT_TRUE(result->proto.root_frame().is_main_frame());
            {
              base::ScopedAllowBlockingForTesting scoped_blocking;
              auto pair = RecordingMapFromCaptureResult(std::move(*result));
              EnsureSkPictureIsValid(&pair.first, pair.second.root_frame(), 0);
            }
            quit.Run();
          },
          loop.QuitClosure(), params));
  loop.Run();

  auto entries = ukm_recorder.GetEntriesByName(
      ukm::builders::PaintPreviewCapture::kEntryName);
  EXPECT_EQ(1u, entries.size());
}

IN_PROC_BROWSER_TEST_P(PaintPreviewBrowserTest,
                       CaptureMainFrameWithCrossProcessSubframe) {
  LoadPage(
      http_server_.GetURL("a.com", "/cross_site_iframe_factory.html?a(b)"));
  auto params = MakeParams();

  base::RunLoop loop;

  CreateClient();
  auto* client = PaintPreviewClient::FromWebContents(GetWebContents());
  WaitForLoadStopWithoutSuccessCheck();
  client->CapturePaintPreview(
      params, GetWebContents()->GetPrimaryMainFrame(),
      base::BindOnce(
          [](base::RepeatingClosure quit,
             const PaintPreviewClient::PaintPreviewParams& params,
             base::UnguessableToken guid, mojom::PaintPreviewStatus status,
             std::unique_ptr<CaptureResult> result) {
            EXPECT_EQ(guid, params.inner.document_guid);
            EXPECT_EQ(status, mojom::PaintPreviewStatus::kOk);
            EXPECT_TRUE(result->proto.has_root_frame());
            EXPECT_EQ(result->proto.subframes_size(), 1);
            EXPECT_EQ(result->proto.root_frame()
                          .content_id_to_embedding_tokens_size(),
                      1);
            EXPECT_TRUE(result->proto.root_frame().is_main_frame());
            EXPECT_EQ(result->proto.subframes(0)
                          .content_id_to_embedding_tokens_size(),
                      0);
            EXPECT_FALSE(result->proto.subframes(0).is_main_frame());
            {
              base::ScopedAllowBlockingForTesting scoped_blocking;
              auto pair = RecordingMapFromCaptureResult(std::move(*result));
              EnsureSkPictureIsValid(&pair.first, pair.second.root_frame(), 1);
              EnsureSkPictureIsValid(&pair.first, pair.second.subframes(0), 0);
            }
            quit.Run();
          },
          loop.QuitClosure(), params));
  loop.Run();
}

class PaintPreviewFencedFrameBrowserTest : public PaintPreviewBrowserTest {
 public:
  PaintPreviewFencedFrameBrowserTest() = default;
  ~PaintPreviewFencedFrameBrowserTest() override = default;

  content::test::FencedFrameTestHelper& fenced_frame_test_helper() {
    return fenced_frame_test_helper_;
  }

 private:
  content::test::FencedFrameTestHelper fenced_frame_test_helper_;
};

IN_PROC_BROWSER_TEST_P(PaintPreviewFencedFrameBrowserTest,
                       CaptureMainFrameWithCrossProcessFencedFrames) {
  LoadPage(http_server_.GetURL("a.com", "/title1.html"));
  content::RenderFrameHost* primary_main_rfh =
      GetWebContents()->GetPrimaryMainFrame();

  // Create two fenced frames.
  fenced_frame_test_helper().CreateFencedFrame(
      primary_main_rfh,
      http_server_.GetURL("b.com", "/fenced_frames/title1.html"));
  fenced_frame_test_helper().CreateFencedFrame(
      primary_main_rfh,
      http_server_.GetURL("c.com", "/fenced_frames/title1.html"));

  base::RunLoop finished_loop;
  CreateClient();
  auto* client = PaintPreviewClient::FromWebContents(GetWebContents());
  auto params = MakeParams();

  client->CapturePaintPreview(
      params, primary_main_rfh,
      base::BindOnce(
          [](const PaintPreviewClient::PaintPreviewParams& params,
             base::UnguessableToken guid, mojom::PaintPreviewStatus status,
             std::unique_ptr<CaptureResult> result) {
            // This callback should have a success result without any DCHECK
            // error.
            EXPECT_EQ(guid, params.inner.document_guid);
            EXPECT_EQ(status, mojom::PaintPreviewStatus::kOk);
            EXPECT_TRUE(result->proto.has_root_frame());
            EXPECT_EQ(result->proto.subframes_size(), 2);
            EXPECT_EQ(result->proto.root_frame()
                          .content_id_to_embedding_tokens_size(),
                      2);
            EXPECT_TRUE(result->proto.root_frame().is_main_frame());
            EXPECT_FALSE(result->proto.subframes(0).is_main_frame());
            EXPECT_FALSE(result->proto.subframes(1).is_main_frame());
            {
              base::ScopedAllowBlockingForTesting scoped_blocking;
              auto pair = RecordingMapFromCaptureResult(std::move(*result));
              EnsureSkPictureIsValid(&pair.first, pair.second.root_frame(), 2);
              EnsureSkPictureIsValid(&pair.first, pair.second.subframes(0), 0);
              EnsureSkPictureIsValid(&pair.first, pair.second.subframes(1), 0);
            }
          },
          params)
          .Then(finished_loop.QuitClosure()));
  finished_loop.Run();
}

IN_PROC_BROWSER_TEST_P(PaintPreviewFencedFrameBrowserTest,
                       DoNotAffectAnotherFrameWhenRemovingFencedFrame) {
  base::ScopedAllowBlockingForTesting scope;

  LoadPage(http_server_.GetURL("a.com", "/title1.html"));
  content::RenderFrameHost* primary_main_rfh =
      GetWebContents()->GetPrimaryMainFrame();

  // Create two fenced frames.
  content::RenderFrameHostWrapper fenced_rfh_wrapper(
      fenced_frame_test_helper().CreateFencedFrame(
          primary_main_rfh,
          http_server_.GetURL("b.com", "/fenced_frames/title1.html")));
  fenced_frame_test_helper().CreateFencedFrame(
      primary_main_rfh,
      http_server_.GetURL("c.com", "/fenced_frames/title1.html"));

  // Override remote interfaces of the fenced frame with a no-op.
  base::RunLoop started_loop;
  NoOpPaintPreviewRecorder noop_recorder;
  noop_recorder.SetRequestedClosure(started_loop.QuitClosure());

  OverrideInterface(&noop_recorder, fenced_rfh_wrapper.get());

  base::RunLoop finished_loop;
  CreateClient();
  auto* client = PaintPreviewClient::FromWebContents(GetWebContents());
  auto params = MakeParams();

  client->CapturePaintPreview(
      params, primary_main_rfh,
      base::BindOnce(
          [](const PaintPreviewClient::PaintPreviewParams& params,
             base::UnguessableToken guid, mojom::PaintPreviewStatus status,
             std::unique_ptr<CaptureResult> result) {
            // This callback should have a partial success result since the
            // fenced frame has been removed during running the capture.
            EXPECT_EQ(guid, params.inner.document_guid);
            EXPECT_EQ(status, mojom::PaintPreviewStatus::kPartialSuccess);
            EXPECT_TRUE(result->proto.has_root_frame());
            EXPECT_EQ(result->proto.subframes_size(), 1);
            EXPECT_EQ(result->proto.root_frame()
                          .content_id_to_embedding_tokens_size(),
                      2);
            EXPECT_TRUE(result->proto.root_frame().is_main_frame());
            EXPECT_EQ(result->proto.subframes(0)
                          .content_id_to_embedding_tokens_size(),
                      0);
            EXPECT_FALSE(result->proto.subframes(0).is_main_frame());
            {
              base::ScopedAllowBlockingForTesting scoped_blocking;
              auto pair = RecordingMapFromCaptureResult(std::move(*result));
              EnsureSkPictureIsValid(&pair.first, pair.second.root_frame(), 2);
              EnsureSkPictureIsValid(&pair.first, pair.second.subframes(0), 0);
            }
          },
          params)
          .Then(finished_loop.QuitClosure()));

  // Wait for the request to execute before removing the fenced frame.
  started_loop.Run();

  // Remove the fenced frame.
  EXPECT_TRUE(
      ExecJs(primary_main_rfh,
             "const ff = document.querySelector('fencedframe'); ff.remove();"));
  ASSERT_TRUE(fenced_rfh_wrapper.WaitUntilRenderFrameDeleted());

  finished_loop.Run();
}

IN_PROC_BROWSER_TEST_P(PaintPreviewBrowserTest,
                       CaptureMainFrameWithScrollableSameProcessSubframe) {
  std::string html = R"(<html>
                          <iframe
                            srcdoc="<div
                                      style='width: 300px;
                                             height: 300px;
                                             background-color: #ff0000'>
                                      &nbsp;
                                    </div>"
                            title="subframe"
                            width="100px"
                            height="100px">
                          </iframe>
                        </html>)";
  LoadHtml(html);
  auto params = MakeParams();

  base::RunLoop loop;
  CreateClient();
  auto* client = PaintPreviewClient::FromWebContents(GetWebContents());
  WaitForLoadStopWithoutSuccessCheck();
  client->CapturePaintPreview(
      params, GetWebContents()->GetPrimaryMainFrame(),
      base::BindOnce(
          [](base::RepeatingClosure quit,
             const PaintPreviewClient::PaintPreviewParams& params,
             base::UnguessableToken guid, mojom::PaintPreviewStatus status,
             std::unique_ptr<CaptureResult> result) {
            EXPECT_EQ(guid, params.inner.document_guid);
            EXPECT_EQ(status, mojom::PaintPreviewStatus::kOk);
            EXPECT_TRUE(result->proto.has_root_frame());
            EXPECT_EQ(result->proto.subframes_size(), 1);
            EXPECT_EQ(result->proto.root_frame()
                          .content_id_to_embedding_tokens_size(),
                      1);
            EXPECT_TRUE(result->proto.root_frame().is_main_frame());
            EXPECT_EQ(result->proto.subframes(0)
                          .content_id_to_embedding_tokens_size(),
                      0);
            EXPECT_FALSE(result->proto.subframes(0).is_main_frame());
            {
              base::ScopedAllowBlockingForTesting scoped_blocking;
              auto pair = RecordingMapFromCaptureResult(std::move(*result));
              EnsureSkPictureIsValid(&pair.first, pair.second.root_frame(), 1);
              EnsureSkPictureIsValid(&pair.first, pair.second.subframes(0), 0,
                                     gfx::Size(300, 300));
            }
            quit.Run();
          },
          loop.QuitClosure(), params));
  loop.Run();
}

IN_PROC_BROWSER_TEST_P(PaintPreviewBrowserTest,
                       CaptureMainFrameWithNonScrollableSameProcessSubframe) {
  std::string html = R"(<html>
                          <iframe
                            srcdoc="<div
                                      style='width: 50px;
                                             height: 50px;
                                             background-color: #ff0000'>
                                      &nbsp;
                                    </div>"
                            title="subframe"
                            width="100px"
                            height="100px">
                          </iframe>
                        </html>)";
  LoadHtml(html);
  auto params = MakeParams();

  base::RunLoop loop;
  CreateClient();
  auto* client = PaintPreviewClient::FromWebContents(GetWebContents());
  WaitForLoadStopWithoutSuccessCheck();
  client->CapturePaintPreview(
      params, GetWebContents()->GetPrimaryMainFrame(),
      base::BindOnce(
          [](base::RepeatingClosure quit,
             const PaintPreviewClient::PaintPreviewParams& params,
             base::UnguessableToken guid, mojom::PaintPreviewStatus status,
             std::unique_ptr<CaptureResult> result) {
            EXPECT_EQ(guid, params.inner.document_guid);
            EXPECT_EQ(status, mojom::PaintPreviewStatus::kOk);
            EXPECT_TRUE(result->proto.has_root_frame());
            EXPECT_EQ(result->proto.subframes_size(), 0);
            EXPECT_EQ(result->proto.root_frame()
                          .content_id_to_embedding_tokens_size(),
                      0);
            EXPECT_TRUE(result->proto.root_frame().is_main_frame());
            {
              base::ScopedAllowBlockingForTesting scoped_blocking;
              auto pair = RecordingMapFromCaptureResult(std::move(*result));
              EnsureSkPictureIsValid(&pair.first, pair.second.root_frame(), 0);
            }
            quit.Run();
          },
          loop.QuitClosure(), params));
  loop.Run();
}

// https://crbug.com/1146573 reproduction. If a renderer crashes,
// WebContentsObserver::RenderFrameDeleted. Paint preview implements this in an
// observer which in turn releases the capture handle which can cause the
// WebContents to be reloaded on Android where we have auto-reload. This reload
// occurs *during* crash handling, leaving the frame in an invalid state and
// leading to a crash when it subsequently unloaded.
// This is fixed by deferring it to a PostTask.
IN_PROC_BROWSER_TEST_P(PaintPreviewBrowserTest, DontReloadInRenderProcessExit) {
  // In the FileSystem variant of this test, blocking needs to be permitted to
  // allow cleanup to work during the crash.
  base::ScopedAllowBlockingForTesting scope;
  LoadPage(http_server_.GetURL("a.com", "/title1.html"));

  content::WebContents* web_contents = GetWebContents();

  // Override remote interfaces with a no-op.
  base::RunLoop started_loop;
  NoOpPaintPreviewRecorder noop_recorder;
  noop_recorder.SetRequestedClosure(started_loop.QuitClosure());
  OverrideInterface(&noop_recorder, GetWebContents()->GetPrimaryMainFrame());

  CreateClient();
  auto* client = PaintPreviewClient::FromWebContents(web_contents);
  // Do this twice to simulate conditions for crash.
  auto handle1 = web_contents->IncrementCapturerCount(
      gfx::Size(), /*stay_hidden=*/true,
      /*stay_awake=*/true, /*is_activity=*/true);
  auto handle2 = web_contents->IncrementCapturerCount(
      gfx::Size(), /*stay_hidden=*/true,
      /*stay_awake=*/true, /*is_activity=*/true);

  // A callback that causes the frame to reload and end up in an invalid state
  // if it is allowed to run during crash handling.
  base::RunLoop finished_loop;
  auto params = MakeParams();
  bool did_run = false;
  client->CapturePaintPreview(
      params, web_contents->GetPrimaryMainFrame(),
      // This callback is now posted so it shouldn't cause a crash.
      base::BindOnce(
          [](content::WebContents* web_contents, bool* did_run_ptr,
             base::ScopedClosureRunner handle1,
             base::ScopedClosureRunner handle2, base::UnguessableToken guid,
             mojom::PaintPreviewStatus status,
             std::unique_ptr<CaptureResult> result) {
            EXPECT_EQ(status, mojom::PaintPreviewStatus::kFailed);
            EXPECT_EQ(result, nullptr);
            // On Android crashed frames are marked as needing reload.
            web_contents->GetController().SetNeedsReload();
            handle1.RunAndReset();
            handle2.RunAndReset();
            *did_run_ptr = true;
          },
          web_contents, &did_run, std::move(handle1), std::move(handle2))
          .Then(finished_loop.QuitClosure()));
  // Wait for the request to execute before crashing the renderer. Otherwise in
  // the FileSystem variant it is possible there will be a race during creation
  // of the file with the renderer crash. If this happens the callback for
  // `finished_loop` will not be run as no request to capture succeeded leading
  // to a timeout.
  started_loop.Run();

  // Crash the renderer.
  content::RenderProcessHost* process =
      GetWebContents()->GetPrimaryMainFrame()->GetProcess();
  content::RenderProcessHostWatcher crash_observer(
      process, content::RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT);
  process->Shutdown(0);
  crash_observer.Wait();

  // The browser would have crashed before the loop exited if the callback was
  // not posted.
  if (!did_run)
    finished_loop.Run();

  // Now navigate away and ensure that the frame unloads successfully.
  LoadPage(http_server_.GetURL("a.com", "/title2.html"));
}

INSTANTIATE_TEST_SUITE_P(All,
                         PaintPreviewBrowserTest,
                         testing::Values(RecordingPersistence::kFileSystem,
                                         RecordingPersistence::kMemoryBuffer),
                         PersistenceParamToString);
INSTANTIATE_TEST_SUITE_P(All,
                         PaintPreviewFencedFrameBrowserTest,
                         testing::Values(RecordingPersistence::kFileSystem,
                                         RecordingPersistence::kMemoryBuffer),
                         PersistenceParamToString);
}  // namespace paint_preview