File: service_worker_test_utils.h

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 (229 lines) | stat: -rw-r--r-- 8,813 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
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef EXTENSIONS_BROWSER_SERVICE_WORKER_SERVICE_WORKER_TEST_UTILS_H_
#define EXTENSIONS_BROWSER_SERVICE_WORKER_SERVICE_WORKER_TEST_UTILS_H_

#include <stddef.h>

#include <map>
#include <optional>
#include <set>

#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/scoped_observation.h"
#include "content/public/browser/service_worker_context_observer.h"
#include "content/public/browser/storage_partition.h"
#include "extensions/browser/process_manager.h"
#include "extensions/browser/process_manager_observer.h"
#include "extensions/browser/service_worker/service_worker_task_queue.h"
#include "extensions/common/extension_id.h"
#include "third_party/blink/public/common/service_worker/service_worker_status_code.h"
#include "url/gurl.h"

namespace content {
class ServiceWorkerContext;
class BrowserContext;
}

namespace extensions {
namespace service_worker_test_utils {

// Get the ServiceWorkerContext for the `browser_context`.
content::ServiceWorkerContext* GetServiceWorkerContext(
    content::BrowserContext* browser_context);

// A class for ServiceWorkerContextObserver events.
// Note: This class only works well when there is a *single* service worker
// being registered. We could extend this to track multiple workers.
class TestServiceWorkerContextObserver
    : public content::ServiceWorkerContextObserver,
      public content::ServiceWorkerContextObserverSynchronous {
 public:
  explicit TestServiceWorkerContextObserver(
      content::ServiceWorkerContext* context,
      std::optional<ExtensionId> extension_id = std::nullopt);
  explicit TestServiceWorkerContextObserver(
      content::BrowserContext* browser_context,
      std::optional<ExtensionId> extension_id = std::nullopt);
  ~TestServiceWorkerContextObserver() override;

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

  // Wait for the first service worker registration with an extension scheme
  // scope to be stored.
  void WaitForRegistrationStored();

  // Wait for OnStartWorkerMessageSent event is triggered, so that the observer
  // captures the version ID of the service worker that is about to be started.
  // Returns the version ID.
  int64_t WaitForStartWorkerMessageSent();

  // Wait for OnVersionStartedRunning event is triggered, so that the observer
  // captures the running service worker version ID. Returns the version ID.
  int64_t WaitForWorkerStarted();

  // Wait for OnVersionStoppedRunning event is triggered, so that the observer
  // captures the stopped service worker version ID. Returns the version ID.
  int64_t WaitForWorkerStopped();

  // Waits for the OnVersionActivated() notification from the
  // ServiceWorkerContext. Returns the version ID.
  int64_t WaitForWorkerActivated();

  // Sets the ID of an already-running worker. This is handy so this observer
  // can be instantiated after the extension has already started.
  void SetRunningId(int64_t version_id) { running_version_id_ = version_id; }

  // Returns the number of completed registrations for `scope`.
  int GetCompletedCount(const GURL& scope) const;

 private:
  // ServiceWorkerContextObserver:
  void OnRegistrationCompleted(const GURL& scope) override;
  void OnRegistrationStored(int64_t registration_id,
                            const GURL& scope,
                            const content::ServiceWorkerRegistrationInformation&
                                service_worker_info) override;
  void OnVersionStartedRunning(
      int64_t version_id,
      const content::ServiceWorkerRunningInfo& running_info) override;
  void OnVersionStoppedRunning(int64_t version_id) override;
  void OnVersionActivated(int64_t version_id, const GURL& scope) override;
  void OnDestruct(content::ServiceWorkerContext* context) override;

  // ServiceWorkerContextObserverSynchronous:
  void OnStartWorkerMessageSent(int64_t version_id, const GURL& scope) override;

  using RegistrationsMap = std::map<GURL, int>;

  RegistrationsMap registrations_completed_map_;

  // Multiple events may come in so we must wait for the specific event
  // to be triggered.
  base::OnceClosure activated_quit_closure_;
  base::OnceClosure start_message_sent_quit_closure_;
  base::OnceClosure started_quit_closure_;
  base::OnceClosure stored_quit_closure_;
  base::OnceClosure stopped_quit_closure_;

  const std::optional<GURL> extension_scope_;

  std::optional<bool> registration_stored_;
  std::optional<int64_t> activated_version_id_;
  std::optional<int64_t> start_message_sent_version_id_;
  std::optional<int64_t> running_version_id_;
  std::optional<int64_t> stopped_version_id_;

  raw_ptr<content::ServiceWorkerContext> context_ = nullptr;

  base::ScopedObservation<content::ServiceWorkerContext,
                          content::ServiceWorkerContextObserver>
      scoped_observation_{this};

  base::ScopedObservation<content::ServiceWorkerContext,
                          content::ServiceWorkerContextObserverSynchronous>
      scoped_sync_observation_{this};
};

// Observes ProcessManager::UnregisterServiceWorker.
class UnregisterWorkerObserver : public ProcessManagerObserver {
 public:
  UnregisterWorkerObserver(ProcessManager* process_manager,
                           const ExtensionId& extension_id);
  ~UnregisterWorkerObserver() override;

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

  // ProcessManagerObserver:
  void OnStoppedTrackingServiceWorkerInstance(
      const WorkerId& worker_id) override;

  // Waits for ProcessManager::UnregisterServiceWorker for `extension_id_`.
  void WaitForUnregister();

 private:
  ExtensionId extension_id_;
  base::ScopedObservation<ProcessManager, ProcessManagerObserver> observation_{
      this};
  base::RunLoop run_loop_;
};

class TestServiceWorkerTaskQueueObserver
    : public ServiceWorkerTaskQueue::TestObserver {
 public:
  TestServiceWorkerTaskQueueObserver();
  ~TestServiceWorkerTaskQueueObserver() override;

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

  struct WorkerStartFailedData {
    size_t num_pending_tasks = 0;
    blink::ServiceWorkerStatusCode status_code =
        blink::ServiceWorkerStatusCode::kOk;
  };

  void WaitForWorkerStarted(const ExtensionId& extension_id);
  void WaitForWorkerStopped(const ExtensionId& extension_id);
  void WaitForWorkerContextInitialized(const ExtensionId& extension_id);
  WorkerStartFailedData WaitForDidStartWorkerFail(
      const ExtensionId& extension_id);
  void WaitForOnActivateExtension(const ExtensionId& extension_id);
  bool WaitForRegistrationMismatchMitigation(const ExtensionId& extension_id);
  void WaitForUntrackServiceWorkerState(const GURL& scope);

  std::optional<bool> WillRegisterServiceWorker(
      const ExtensionId& extension_id) const;

  int GetRequestedWorkerStartedCount(const ExtensionId& extension_id) const;

  // ServiceWorkerTaskQueue::TestObserver
  void DidStartWorker(const ExtensionId& extension_id) override;
  void DidInitializeServiceWorkerContext(
      const ExtensionId& extension_id) override;
  void DidStartWorkerFail(const ExtensionId& extension_id,
                          size_t num_pending_tasks,
                          blink::ServiceWorkerStatusCode status_code) override;
  void OnActivateExtension(const ExtensionId& extension_id,
                           bool will_register_service_worker) override;
  void RegistrationMismatchMitigated(const ExtensionId& extension_id,
                                     bool success) override;
  void RequestedWorkerStart(const ExtensionId& extension_id) override;
  void DidStopServiceWorkerContext(const ExtensionId& extension_id) override;
  void UntrackServiceWorkerState(const GURL& scope) override;

 private:
  std::map<ExtensionId, bool> activated_map_;

  std::map<ExtensionId, WorkerStartFailedData> failed_map_;

  std::set<ExtensionId> inited_set_;

  std::map<ExtensionId, bool> mitigated_map_;

  std::map<ExtensionId, int> requested_worker_started_map_;

  std::set<ExtensionId> started_set_;

  std::set<ExtensionId> stopped_set_;

  std::set<GURL> untracked_set_;

  base::OnceClosure quit_closure_;

  base::OnceClosure untrack_quit_closure_;
};

}  // namespace service_worker_test_utils
}  // namespace extensions

#endif  // EXTENSIONS_BROWSER_SERVICE_WORKER_SERVICE_WORKER_TEST_UTILS_H_