File: incognito_browsing_data_browsertest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (539 lines) | stat: -rw-r--r-- 22,012 bytes parent folder | download | duplicates (6)
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
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stddef.h>
#include <memory>
#include <string>

#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/test_future.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "chrome/browser/browsing_data/browsing_data_remover_browsertest_base.h"
#include "chrome/browser/browsing_data/chrome_browsing_data_remover_constants.h"
#include "chrome/browser/browsing_data/counters/cache_counter.h"
#include "chrome/browser/browsing_data/counters/site_data_counting_helper.h"
#include "chrome/browser/external_protocol/external_protocol_handler.h"
#include "chrome/browser/media/clear_key_cdm_test_helper.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/browsing_data/content/browsing_data_model.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/storage_usage_info.h"
#include "content/public/common/content_client.h"
#include "content/public/common/content_paths.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "media/base/media_switches.h"
#include "media/mojo/mojom/media_types.mojom.h"
#include "media/mojo/services/video_decode_perf_history.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

#if BUILDFLAG(ENABLE_LIBRARY_CDMS)
#if BUILDFLAG(IS_MAC)
#include "base/threading/platform_thread.h"
#endif
#include "base/memory/scoped_refptr.h"
#endif  // BUILDFLAG(ENABLE_LIBRARY_CDMS)

#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/browser_process.h"
#include "chrome/browser/browser_process_platform_part.h"
#endif  // BUILDFLAG(IS_CHROMEOS)

using content::BrowserThread;
using content::BrowsingDataFilterBuilder;

namespace {
static const char* kExampleHost = "example.com";
static const char* kLocalHost = "localhost";
}  // namespace

class IncognitoBrowsingDataBrowserTest
    : public BrowsingDataRemoverBrowserTestBase {
 public:
  IncognitoBrowsingDataBrowserTest() {
    std::vector<base::test::FeatureRef> enabled_features = {};
#if BUILDFLAG(ENABLE_LIBRARY_CDMS)
    enabled_features.push_back(media::kExternalClearKeyForTesting);
#endif
    InitFeatureLists(std::move(enabled_features), {});
  }

  void SetUpOnMainThread() override {
    BrowsingDataRemoverBrowserTestBase::SetUpOnMainThread();
    host_resolver()->AddRule(kExampleHost, "127.0.0.1");
    UseIncognitoBrowser();
  }

  Browser* GetRegularBrowser() { return browser(); }

  Browser* GetIncognitoBrowser() { return GetBrowser(); }

  // Test a data type by creating a value in Incognito mode and checking it is
  // counted by the Incognito cookie counter and not by the regular mode one.
  // Then closing Incognito mode and ensuring it is not affecting a new
  // Incognito profile.
  void TestSiteData(const std::string& type) {
    Browser* regular_browser = GetRegularBrowser();
    Browser* incognito_browser = GetIncognitoBrowser();

    EXPECT_TRUE(regular_browser->profile()->IsRegularProfile());
    EXPECT_TRUE(incognito_browser->profile()->IsIncognitoProfile());

    // Ensure there is no prior data.
    EXPECT_EQ(0, GetSiteDataCount(GetActiveWebContents(regular_browser)));
    EXPECT_EQ(0, GetSiteDataCount(GetActiveWebContents(incognito_browser)));
    GURL url = embedded_test_server()->GetURL("/browsing_data/site_data.html");
    ASSERT_TRUE(ui_test_utils::NavigateToURL(incognito_browser, url));
    ASSERT_TRUE(ui_test_utils::NavigateToURL(regular_browser, url));

    // Even after navigation.
    EXPECT_EQ(0, GetSiteDataCount(GetActiveWebContents(incognito_browser)));
    ExpectTotalModelCount(incognito_browser, 0);
    EXPECT_FALSE(HasDataForType(type, GetActiveWebContents(incognito_browser)));

    // Set data type in Incognito mode, ensure only Incognito mode is affected.
    SetDataForType(type, GetActiveWebContents(incognito_browser));
    EXPECT_EQ(0, GetSiteDataCount(GetActiveWebContents(regular_browser)));
    EXPECT_EQ(1, GetSiteDataCount(GetActiveWebContents(incognito_browser)));
    ExpectTotalModelCount(regular_browser, 0);
    // TODO(crbug.com/40218898): Use a different approach to determine presence
    // of data that does not depend on UI code and has a better resolution when
    // 3PSP is fully enabled. ExpectTotalModelCount(incognito_browser, 1);
    // is not always true here.

    EXPECT_FALSE(HasDataForType(type, GetActiveWebContents(regular_browser)));
    EXPECT_TRUE(HasDataForType(type, GetActiveWebContents(incognito_browser)));

    // Restart Incognito and ensure it is empty.
    RestartIncognitoBrowser();
    incognito_browser = GetIncognitoBrowser();
    ASSERT_TRUE(ui_test_utils::NavigateToURL(incognito_browser, url));

    EXPECT_EQ(0, GetSiteDataCount(GetActiveWebContents(incognito_browser)));
    ExpectTotalModelCount(incognito_browser, 0);
    EXPECT_FALSE(HasDataForType(type, GetActiveWebContents(incognito_browser)));
  }

  // Test that storage systems like filesystem, where just an access creates an
  // empty store, are counted and deleted correctly.
  void TestEmptySiteData(const std::string& type) {
    Browser* regular_browser = GetRegularBrowser();
    Browser* incognito_browser = GetIncognitoBrowser();

    EXPECT_EQ(0, GetSiteDataCount(GetActiveWebContents(regular_browser)));
    EXPECT_EQ(0, GetSiteDataCount(GetActiveWebContents(incognito_browser)));
    ExpectTotalModelCount(regular_browser, 0);
    ExpectTotalModelCount(incognito_browser, 0);

    GURL url = embedded_test_server()->GetURL("/browsing_data/site_data.html");
    ASSERT_TRUE(ui_test_utils::NavigateToURL(incognito_browser, url));
    EXPECT_EQ(0, GetSiteDataCount(GetActiveWebContents(incognito_browser)));
    ExpectTotalModelCount(incognito_browser, 0);
    // Opening a store of this type creates a site data entry in Incognito only.
    EXPECT_FALSE(HasDataForType(type, GetActiveWebContents(incognito_browser)));
    EXPECT_EQ(0, GetSiteDataCount(GetActiveWebContents(regular_browser)));
    EXPECT_EQ(1, GetSiteDataCount(GetActiveWebContents(incognito_browser)));
    ExpectTotalModelCount(regular_browser, 0);
    // TODO(crbug.com/40218898): Use a different approach to determine presence
    // of data that does not depend on UI code and has a better resolution when
    // 3PSP is fully enabled. ExpectTotalModelCount(incognito_browser, 1);
    // is not always true here.

    // Restart Incognito, ensure there is no residue from previous one.
    RestartIncognitoBrowser();
    incognito_browser = GetIncognitoBrowser();
    EXPECT_EQ(0, GetSiteDataCount(GetActiveWebContents(incognito_browser)));
    ExpectTotalModelCount(incognito_browser, 0);
  }

  inline void ExpectTotalModelCount(Browser* browser, size_t expected) {
    std::unique_ptr<BrowsingDataModel> browsing_data_model =
        GetBrowsingDataModel(browser->profile());

    EXPECT_EQ(expected, browsing_data_model->size());
  }

  void OnVideoDecodePerfInfo(base::RunLoop* run_loop,
                             bool* out_is_smooth,
                             bool* out_is_power_efficient,
                             bool is_smooth,
                             bool is_power_efficient) {
    *out_is_smooth = is_smooth;
    *out_is_power_efficient = is_power_efficient;
    run_loop->QuitWhenIdle();
  }

  network::mojom::NetworkContext* network_context() const {
    return GetBrowser()
        ->profile()
        ->GetDefaultStoragePartition()
        ->GetNetworkContext();
  }

 private:
  void OnCacheSizeResult(
      base::RunLoop* run_loop,
      browsing_data::BrowsingDataCounter::ResultInt* out_size,
      std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result) {
    if (!result->Finished())
      return;

    *out_size =
        static_cast<browsing_data::BrowsingDataCounter::FinishedResult*>(
            result.get())
            ->Value();
    run_loop->Quit();
  }

  void SetUpCommandLine(base::CommandLine* command_line) override {
#if BUILDFLAG(ENABLE_LIBRARY_CDMS)
    // Testing MediaLicenses requires additional command line parameters as
    // it uses the External Clear Key CDM.
    RegisterClearKeyCdm(command_line);
#endif
  }
};

// Test BrowsingDataRemover for downloads.
IN_PROC_BROWSER_TEST_F(IncognitoBrowsingDataBrowserTest, Download) {
  DownloadAnItem();
  VerifyDownloadCount(0u, GetRegularBrowser()->profile());

  // Restart Incognito, ensure no residue.
  RestartIncognitoBrowser();
  VerifyDownloadCount(0u, GetIncognitoBrowser()->profile());
}

// Test that the salt for media device IDs is reset between Incognito sessions.
IN_PROC_BROWSER_TEST_F(IncognitoBrowsingDataBrowserTest, MediaDeviceIdSalt) {
  auto get_salt = [&]() {
    content::RenderFrameHost* frame_host = GetBrowser()
                                               ->tab_strip_model()
                                               ->GetActiveWebContents()
                                               ->GetPrimaryMainFrame();
    url::Origin origin = frame_host->GetLastCommittedOrigin();
    net::SiteForCookies site_for_cookies =
        net::SiteForCookies::FromOrigin(origin);
    blink::StorageKey storage_key = blink::StorageKey::CreateFirstParty(origin);
    base::test::TestFuture<bool, const std::string&> future;
    content::GetContentClientForTesting()->browser()->GetMediaDeviceIDSalt(
        frame_host, site_for_cookies, storage_key, future.GetCallback());
    return future.Get<1>();
  };
  std::string original_salt = get_salt();
  RestartIncognitoBrowser();
  std::string new_salt = get_salt();
  EXPECT_NE(original_salt, new_salt);
}

// Verify VideoDecodePerfHistory is cleared after restarting Incognito session
// and is not affecting regular mode.
IN_PROC_BROWSER_TEST_F(IncognitoBrowsingDataBrowserTest,
                       VideoDecodePerfHistory) {
  media::VideoDecodePerfHistory* video_decode_perf_history =
      GetBrowser()->profile()->GetVideoDecodePerfHistory();

  // Save a video decode record. Note: we avoid using a web page to generate the
  // stats as this takes at least 5 seconds and even then is not a guarantee
  // depending on scheduler. Manual injection is quick and non-flaky.
  const media::VideoCodecProfile kProfile = media::VP9PROFILE_PROFILE0;
  const gfx::Size kSize(100, 200);
  const int kFrameRate = 30;
  const int kFramesDecoded = 1000;
  const int kFramesDropped = .9 * kFramesDecoded;
  const int kFramesPowerEfficient = 0;
  const url::Origin kOrigin = url::Origin::Create(GURL("http://example.com"));
  const bool kIsTopFrame = true;
  const uint64_t kPlayerId = 1234u;

  media::mojom::PredictionFeatures prediction_features;
  prediction_features.profile = kProfile;
  prediction_features.video_size = kSize;
  prediction_features.frames_per_sec = kFrameRate;

  media::mojom::PredictionTargets prediction_targets;
  prediction_targets.frames_decoded = kFramesDecoded;
  prediction_targets.frames_dropped = kFramesDropped;
  prediction_targets.frames_power_efficient = kFramesPowerEfficient;

  {
    base::RunLoop run_loop;
    video_decode_perf_history->GetSaveCallback().Run(
        ukm::kInvalidSourceId, media::learning::FeatureValue(0), kIsTopFrame,
        prediction_features, prediction_targets, kPlayerId,
        run_loop.QuitWhenIdleClosure());
    run_loop.Run();
  }

  // Verify history exists.
  // Expect |is_smooth| = false and |is_power_efficient| = false given that 90%
  // of recorded frames were dropped and 0 were power efficient.
  bool is_smooth = true;
  bool is_power_efficient = true;
  {
    base::RunLoop run_loop;
    video_decode_perf_history->GetPerfInfo(
        media::mojom::PredictionFeatures::New(prediction_features),
        base::BindOnce(&IncognitoBrowsingDataBrowserTest::OnVideoDecodePerfInfo,
                       base::Unretained(this), &run_loop, &is_smooth,
                       &is_power_efficient));
    run_loop.Run();
  }
  EXPECT_FALSE(is_smooth);
  EXPECT_FALSE(is_power_efficient);

  // Verify it has not affected regular mode. Both |is_smooth| and
  // |is_power_efficient| should report true because the VideoDecodePerfHistory
  // optimistically returns true when it has no data.
  media::VideoDecodePerfHistory* regular_mode_video_decode_perf_history =
      GetRegularBrowser()->profile()->GetVideoDecodePerfHistory();
  {
    base::RunLoop run_loop;
    regular_mode_video_decode_perf_history->GetPerfInfo(
        media::mojom::PredictionFeatures::New(prediction_features),
        base::BindOnce(&IncognitoBrowsingDataBrowserTest::OnVideoDecodePerfInfo,
                       base::Unretained(this), &run_loop, &is_smooth,
                       &is_power_efficient));
    run_loop.Run();
  }
  EXPECT_TRUE(is_smooth);
  EXPECT_TRUE(is_power_efficient);

  // Restart Incognito.
  RestartIncognitoBrowser();
  video_decode_perf_history =
      GetBrowser()->profile()->GetVideoDecodePerfHistory();

  // Verify history no longer exists. Both |is_smooth| and |is_power_efficient|
  // should now report true because the VideoDecodePerfHistory optimistically
  // returns true when it has no data.
  {
    base::RunLoop run_loop;
    video_decode_perf_history->GetPerfInfo(
        media::mojom::PredictionFeatures::New(prediction_features),
        base::BindOnce(&IncognitoBrowsingDataBrowserTest::OnVideoDecodePerfInfo,
                       base::Unretained(this), &run_loop, &is_smooth,
                       &is_power_efficient));
    run_loop.Run();
  }
  EXPECT_TRUE(is_smooth);
  EXPECT_TRUE(is_power_efficient);
}

// Verifies that cache is reset after restarting Incognito.
IN_PROC_BROWSER_TEST_F(IncognitoBrowsingDataBrowserTest, Cache) {
  // Load several resources.
  GURL url1 = embedded_test_server()->GetURL("/cachetime");
  GURL url2 = embedded_test_server()->GetURL(kExampleHost, "/cachetime");
  ASSERT_FALSE(url::IsSameOriginWith(url1, url2));

  EXPECT_EQ(net::OK, content::LoadBasicRequest(network_context(), url1));
  EXPECT_EQ(net::OK, content::LoadBasicRequest(network_context(), url2));

  // Check that the cache has been populated by revisiting these pages with the
  // server stopped.
  ASSERT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete());
  EXPECT_EQ(net::OK, content::LoadBasicRequest(network_context(), url1));
  EXPECT_EQ(net::OK, content::LoadBasicRequest(network_context(), url2));

  RestartIncognitoBrowser();

  // The cache should be empty.
  EXPECT_NE(net::OK, content::LoadBasicRequest(network_context(), url1));
  EXPECT_NE(net::OK, content::LoadBasicRequest(network_context(), url2));
}

IN_PROC_BROWSER_TEST_F(IncognitoBrowsingDataBrowserTest,
                       ExternalProtocolHandlerPerOriginPrefs) {
  Profile* profile = GetBrowser()->profile();
  url::Origin test_origin = url::Origin::Create(GURL("https://example.test/"));
  const std::string serialized_test_origin = test_origin.Serialize();
  base::Value::Dict allowed_protocols_for_origin;
  allowed_protocols_for_origin.Set("tel", true);
  base::Value::Dict origin_pref;
  origin_pref.Set(serialized_test_origin,
                  std::move(allowed_protocols_for_origin));
  profile->GetPrefs()->SetDict(prefs::kProtocolHandlerPerOriginAllowedProtocols,
                               std::move(origin_pref));
  ExternalProtocolHandler::BlockState block_state =
      ExternalProtocolHandler::GetBlockState("tel", &test_origin, profile);
  ASSERT_EQ(ExternalProtocolHandler::DONT_BLOCK, block_state);

  // Regular profile should be unaffected.
  block_state = ExternalProtocolHandler::GetBlockState(
      "tel", &test_origin, GetRegularBrowser()->profile());
  ASSERT_EQ(ExternalProtocolHandler::UNKNOWN, block_state);

  RestartIncognitoBrowser();
  profile = GetBrowser()->profile();

  block_state =
      ExternalProtocolHandler::GetBlockState("tel", &test_origin, profile);
  ASSERT_EQ(ExternalProtocolHandler::UNKNOWN, block_state);
}

IN_PROC_BROWSER_TEST_F(IncognitoBrowsingDataBrowserTest, CookieDeletion) {
  TestSiteData("Cookie");
}

IN_PROC_BROWSER_TEST_F(IncognitoBrowsingDataBrowserTest,
                       SessionCookieDeletion) {
  TestSiteData("SessionCookie");
}

IN_PROC_BROWSER_TEST_F(IncognitoBrowsingDataBrowserTest, LocalStorageDeletion) {
  TestSiteData("LocalStorage");
}

// TODO(crbug.com/41348517): DISABLED until session storage is working
// correctly.
IN_PROC_BROWSER_TEST_F(IncognitoBrowsingDataBrowserTest,
                       DISABLED_SessionStorageDeletion) {
  TestSiteData("SessionStorage");
}

// SessionStorage is not supported by site data counting and the cookie tree
// model but we can test the web visible behavior.
IN_PROC_BROWSER_TEST_F(IncognitoBrowsingDataBrowserTest,
                       SessionStorageDeletionWebOnly) {
  GURL url = embedded_test_server()->GetURL("/browsing_data/site_data.html");
  ASSERT_TRUE(ui_test_utils::NavigateToURL(GetBrowser(), url));
  const std::string type = "SessionStorage";
  EXPECT_FALSE(HasDataForType(type));
  SetDataForType(type);
  EXPECT_TRUE(HasDataForType(type));

  // No residue in regular mode.
  ASSERT_TRUE(ui_test_utils::NavigateToURL(GetRegularBrowser(), url));
  EXPECT_FALSE(HasDataForType(type, GetActiveWebContents(GetRegularBrowser())));

  RestartIncognitoBrowser();
  ASSERT_TRUE(ui_test_utils::NavigateToURL(GetBrowser(), url));
  EXPECT_FALSE(HasDataForType(type));
}

IN_PROC_BROWSER_TEST_F(IncognitoBrowsingDataBrowserTest,
                       ServiceWorkerDeletion) {
  TestSiteData("ServiceWorker");
}

IN_PROC_BROWSER_TEST_F(IncognitoBrowsingDataBrowserTest, CacheStorageDeletion) {
  TestSiteData("CacheStorage");
}

IN_PROC_BROWSER_TEST_F(IncognitoBrowsingDataBrowserTest, FileSystemDeletion) {
  TestSiteData("FileSystem");
}

// Test that empty filesystems are deleted correctly.
IN_PROC_BROWSER_TEST_F(IncognitoBrowsingDataBrowserTest,
                       EmptyFileSystemDeletion) {
  TestEmptySiteData("FileSystem");
}

IN_PROC_BROWSER_TEST_F(IncognitoBrowsingDataBrowserTest, IndexedDbDeletion) {
  TestSiteData("IndexedDb");
}

// Test that empty indexed dbs are deleted correctly.
IN_PROC_BROWSER_TEST_F(IncognitoBrowsingDataBrowserTest, EmptyIndexedDb) {
  TestEmptySiteData("IndexedDb");
}

#if BUILDFLAG(ENABLE_LIBRARY_CDMS)
// Test Media Licenses by creating one and checking it is counted by the
// cookie counter. Then delete it and check that the cookie counter is back
// to zero.
IN_PROC_BROWSER_TEST_F(IncognitoBrowsingDataBrowserTest, MediaLicenseDeletion) {
  const std::string kMediaLicenseType = "MediaLicense";

  EXPECT_EQ(0, GetSiteDataCount());
  GURL url =
      embedded_test_server()->GetURL("/browsing_data/media_license.html");
  ASSERT_TRUE(ui_test_utils::NavigateToURL(GetBrowser(), url));

  EXPECT_EQ(0, GetSiteDataCount());
  ExpectTotalModelCount(GetBrowser(), 0);
  EXPECT_FALSE(HasDataForType(kMediaLicenseType));

  // The new media license backend will not store media licenses explicitly
  // within CookieTreeModel, but the data will still be tracked through the
  // quota system.
  SetDataForType(kMediaLicenseType);
  EXPECT_EQ(1, GetSiteDataCount());
  ExpectTotalModelCount(GetBrowser(), 1);
  EXPECT_TRUE(HasDataForType(kMediaLicenseType));

  // No residue in regular mode.
  ASSERT_TRUE(ui_test_utils::NavigateToURL(GetRegularBrowser(), url));
  EXPECT_FALSE(HasDataForType(kMediaLicenseType,
                              GetActiveWebContents(GetRegularBrowser())));

  // Restart Incognito.
  RestartIncognitoBrowser();
  ASSERT_TRUE(ui_test_utils::NavigateToURL(GetBrowser(), url));

  EXPECT_EQ(0, GetSiteDataCount());
  ExpectTotalModelCount(GetBrowser(), 0);
  EXPECT_FALSE(HasDataForType(kMediaLicenseType));
}
#endif  // BUILDFLAG(ENABLE_LIBRARY_CDMS)

const std::vector<std::string> kStorageTypes{
    "Cookie",    "LocalStorage",  "FileSystem",   "SessionStorage",
    "IndexedDb", "ServiceWorker", "CacheStorage", "MediaLicense"};

// Test that storage doesn't leave any traces on disk.
IN_PROC_BROWSER_TEST_F(IncognitoBrowsingDataBrowserTest,
                       StorageDoesntWriteToDisk) {
  // Checking leveldb content fails in most cases. See https://crbug.com/1238325
  ASSERT_EQ(0, CheckUserDirectoryForString(kLocalHost, {},
                                           /*check_leveldb_content=*/false));
  ASSERT_EQ(0, GetSiteDataCount());
  ExpectTotalModelCount(GetBrowser(), 0);

  // To use secure-only features on a host name, we need an https server.
  net::EmbeddedTestServer https_server(net::EmbeddedTestServer::TYPE_HTTPS);
  https_server.SetSSLConfig(
      net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN);
  base::FilePath path;
  base::PathService::Get(content::DIR_TEST_DATA, &path);
  https_server.ServeFilesFromDirectory(path);
  ASSERT_TRUE(https_server.Start());

  GURL url = https_server.GetURL(kLocalHost, "/browsing_data/site_data.html");
  ASSERT_TRUE(ui_test_utils::NavigateToURL(GetBrowser(), url));

  for (const std::string& type : kStorageTypes) {
    SetDataForType(type);
    EXPECT_TRUE(HasDataForType(type));
  }
  // TODO(crbug.com/40577815): Add more datatypes for testing. E.g.
  // notifications, payment handler, content settings, autofill, ...?

  int found = CheckUserDirectoryForString(kLocalHost, {},
                                          /*check_leveldb_content=*/false);
  EXPECT_EQ(0, found) << "A file contains the hostname.";

  EXPECT_EQ(0, GetSiteDataCount(GetActiveWebContents(GetRegularBrowser())));
  ExpectTotalModelCount(GetRegularBrowser(), 0);

  RestartIncognitoBrowser();

  EXPECT_EQ(0, GetSiteDataCount());
  ExpectTotalModelCount(GetBrowser(), 0);
}