File: background_fetch_test_data_manager.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 (110 lines) | stat: -rw-r--r-- 4,555 bytes parent folder | download | duplicates (4)
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
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/background_fetch/background_fetch_test_data_manager.h"

#include <utility>

#include "base/run_loop.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "components/services/storage/public/mojom/quota_client.mojom.h"
#include "content/browser/blob_storage/chrome_blob_storage_context.h"
#include "content/browser/service_worker/service_worker_context_wrapper.h"
#include "content/browser/storage_partition_impl.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "storage/browser/blob/blob_storage_context.h"
#include "storage/browser/test/mock_quota_manager.h"
#include "storage/browser/test/mock_quota_manager_proxy.h"
#include "storage/browser/test/mock_special_storage_policy.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"

namespace content {

namespace {

class MockBGFQuotaManagerProxy : public storage::MockQuotaManagerProxy {
 public:
  explicit MockBGFQuotaManagerProxy(storage::MockQuotaManager* quota_manager)
      : storage::MockQuotaManagerProxy(
            quota_manager,
            base::SingleThreadTaskRunner::GetCurrentDefault().get()) {}

  // Ignore quota client, it is irrelevant for these tests.
  void RegisterClient(mojo::PendingRemote<storage::mojom::QuotaClient> client,
                      storage::QuotaClientType client_type) override {}

  void GetUsageAndQuota(
      const blink::StorageKey& storage_key,
      scoped_refptr<base::SequencedTaskRunner> callback_task_runner,
      UsageAndQuotaCallback callback) override {
    DCHECK(callback_task_runner);

    // While this DCHECK is true, the PostTask() below isn't strictly necessary.
    // The callback could be Run() directly.
    DCHECK(callback_task_runner->RunsTasksInCurrentSequence());

    callback_task_runner->PostTask(
        FROM_HERE,
        base::BindOnce(std::move(callback), blink::mojom::QuotaStatusCode::kOk,
                       /* usage= */ 0, kBackgroundFetchMaxQuotaBytes));
  }

 protected:
  ~MockBGFQuotaManagerProxy() override = default;
};

}  // namespace

BackgroundFetchTestDataManager::BackgroundFetchTestDataManager(
    BrowserContext* browser_context,
    base::WeakPtr<StoragePartitionImpl> storage_partition,
    scoped_refptr<ServiceWorkerContextWrapper> service_worker_context)
    : BackgroundFetchDataManager(storage_partition,
                                 service_worker_context,
                                 /* quota_manager_proxy= */ nullptr),
      browser_context_(browser_context),
      storage_partition_(storage_partition) {}

void BackgroundFetchTestDataManager::Initialize() {
  // CacheStorage uses the default QuotaManager and not the mock one in this
  // class.  Set QuotaSettings appropriately so that all platforms have quota.
  // The mock one is still used for testing quota exceeded scenarios in
  // DatabaseTask.
  storage::QuotaSettings settings;
  settings.per_storage_key_quota = kBackgroundFetchMaxQuotaBytes;
  settings.pool_size = settings.per_storage_key_quota * 5;
  settings.must_remain_available = 0;
  settings.refresh_interval = base::TimeDelta::Max();
  storage_partition_->GetQuotaManager()->SetQuotaSettings(settings);

  blob_storage_context_ = ChromeBlobStorageContext::GetFor(browser_context_);
  // Wait for ChromeBlobStorageContext to finish initializing.
  base::RunLoop().RunUntilIdle();

  mock_quota_manager_ = base::MakeRefCounted<storage::MockQuotaManager>(
      storage_partition_->GetPath().empty(), storage_partition_->GetPath(),
      base::SingleThreadTaskRunner::GetCurrentDefault().get(),
      base::MakeRefCounted<storage::MockSpecialStoragePolicy>());

  quota_manager_proxy_ =
      base::MakeRefCounted<MockBGFQuotaManagerProxy>(mock_quota_manager_.get());

  mojo::PendingRemote<storage::mojom::BlobStorageContext> remote;

  base::RunLoop run_loop(base::RunLoop::Type::kNestableTasksAllowed);
  GetIOThreadTaskRunner({})->PostTaskAndReply(
      FROM_HERE,
      base::BindOnce(&ChromeBlobStorageContext::BindMojoContext,
                     blob_storage_context_,
                     remote.InitWithNewPipeAndPassReceiver()),
      run_loop.QuitClosure());
  run_loop.Run();
}

BackgroundFetchTestDataManager::~BackgroundFetchTestDataManager() = default;

}  // namespace content