File: service_worker_device_delegate_observer.h

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 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 (116 lines) | stat: -rw-r--r-- 4,774 bytes parent folder | download | duplicates (9)
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
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DEVICE_DELEGATE_OBSERVER_H_
#define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DEVICE_DELEGATE_OBSERVER_H_

#include "base/containers/flat_map.h"
#include "base/memory/weak_ptr.h"
#include "base/scoped_observation.h"
#include "content/browser/service_worker/service_worker_context_core_observer.h"
#include "content/browser/service_worker/service_worker_context_wrapper.h"
#include "content/browser/service_worker/service_worker_registration.h"
#include "content/browser/service_worker/service_worker_version.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"

namespace content {

class ServiceWorkerContextCore;

class CONTENT_EXPORT ServiceWorkerDeviceDelegateObserver
    : public ServiceWorkerContextCoreObserver {
 public:
  using ServiceWorkerStartedCallback =
      base::OnceCallback<void(scoped_refptr<ServiceWorkerVersion>,
                              blink::ServiceWorkerStatusCode)>;
  struct RegistrationInfo {
    blink::StorageKey key;
    bool has_event_handlers;

    bool operator==(const RegistrationInfo& other) const {
      return key == other.key && has_event_handlers == other.has_event_handlers;
    }
  };
  using RegistrationIdMap = base::flat_map<int64_t, RegistrationInfo>;

  explicit ServiceWorkerDeviceDelegateObserver(
      ServiceWorkerContextCore* context);
  ServiceWorkerDeviceDelegateObserver(
      const ServiceWorkerDeviceDelegateObserver&) = delete;
  ServiceWorkerDeviceDelegateObserver& operator=(
      const ServiceWorkerDeviceDelegateObserver&) = delete;
  ~ServiceWorkerDeviceDelegateObserver() override;

  // ServiceWorkerContextCoreObserver:
  void OnRegistrationDeleted(int64_t registration_id,
                             const GURL& scope,
                             const blink::StorageKey& key) override;
  void OnStopped(int64_t version_id) override;

  // Register the ServiceWorkerRegistration with `registration_id` to respond to
  // device delegate observer.
  void Register(int64_t registration_id);

  // Start the worker with `registration_id` and run `callback` on the worker.
  void DispatchEventToWorker(int64_t registration_id,
                             ServiceWorkerStartedCallback callback);

  // Set `registration_id_map_[registration_id].has_event_handlers` to
  // `has_event_handlers`. If `registration_id` isn't in
  // `registration_id_map_`. It will call `Register(registration_id)` to
  // register it.
  void UpdateHasEventHandlers(int64_t registration_id, bool has_event_handlers);

  // Process pending callbacked stored in
  // `pending_callbacks_[version->version_id()]`.
  void ProcessPendingCallbacks(ServiceWorkerVersion* version);

  // Add `callback` to `pending_callbacks_[version->version_id()]`.
  void AddPendingCallback(ServiceWorkerVersion* version,
                          base::OnceClosure callback);

  BrowserContext* GetBrowserContext();

  const RegistrationIdMap& registration_id_map() {
    return registration_id_map_;
  }

  const base::flat_map<int64_t, std::vector<base::OnceClosure>>&
  GetPendingCallbacksForTesting() {
    return pending_callbacks_;
  }

 private:
  // It is called when `registration_id` is added to `registration_id_map_`.
  virtual void RegistrationAdded(int64_t registration_id) = 0;

  // It is called when `registration_id` is removed to `registration_id_map_`.
  virtual void RegistrationRemoved(int64_t registration_id) = 0;

  // The map stores ids of service worker registrations that need to respond to
  // device delegate observer.
  RegistrationIdMap registration_id_map_;

  // `pending_callbacks_` is a buffer for storing callbacks when some states are
  // not yet reached even when the worker is running.
  // For example, when the script is evaluated and the service worker is
  // running, the inter-process request that registers to HidService for
  // renderer client might not be done. Callbacks `pending_callbacks_` here can
  // be used as a buffer to hold these callbacks until the client registration
  // completes.
  // `pending_callbacks_[version_id]` will be cleared when the worker with
  // `version_id` stopped.
  base::flat_map<int64_t, std::vector<base::OnceClosure>> pending_callbacks_;

  // ServiceWorkerDeviceDelegateObserver is owned by ServiceWorkerContextCore.
  const raw_ref<ServiceWorkerContextCore> context_;

  base::ScopedObservation<ServiceWorkerContextWrapper,
                          ServiceWorkerContextCoreObserver>
      observation_{this};
};

}  // namespace content

#endif  // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DEVICE_DELEGATE_OBSERVER_H_