File: per_profile_worker_task_tracker.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 (156 lines) | stat: -rw-r--r-- 6,149 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
// 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 CHROME_BROWSER_TASK_MANAGER_PROVIDERS_PER_PROFILE_WORKER_TASK_TRACKER_H_
#define CHROME_BROWSER_TASK_MANAGER_PROVIDERS_PER_PROFILE_WORKER_TASK_TRACKER_H_

#include <memory>

#include "base/containers/flat_map.h"
#include "base/containers/flat_set.h"
#include "base/memory/raw_ptr.h"
#include "base/scoped_observation.h"
#include "chrome/browser/task_manager/providers/task.h"
#include "content/public/browser/dedicated_worker_service.h"
#include "content/public/browser/service_worker_context.h"
#include "content/public/browser/service_worker_context_observer.h"
#include "content/public/browser/shared_worker_service.h"
#include "third_party/blink/public/common/tokens/tokens.h"

class Profile;

namespace content {
class RenderProcessHost;
}

namespace url {
class Origin;
}

namespace task_manager {

class WorkerTask;
class WorkerTaskProvider;

// This is a helper class owned by WorkerTaskProvider to track all workers
// associated with a single profile. It manages the WorkerTasks and sends
// lifetime notifications to the WorkerTaskProvider.
class PerProfileWorkerTaskTracker
    : public content::DedicatedWorkerService::Observer,
      public content::SharedWorkerService::Observer,
      public content::ServiceWorkerContextObserver {
 public:
  PerProfileWorkerTaskTracker(WorkerTaskProvider* worker_task_provider,
                              Profile* profile);

  ~PerProfileWorkerTaskTracker() override;

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

  // content::DedicatedWorkerService::Observer:
  void OnWorkerCreated(const blink::DedicatedWorkerToken& worker_token,
                       int worker_process_id,
                       const url::Origin& security_origin,
                       content::DedicatedWorkerCreator creator) override;
  void OnBeforeWorkerDestroyed(
      const blink::DedicatedWorkerToken& worker_token,
      content::DedicatedWorkerCreator creator) override;
  void OnFinalResponseURLDetermined(
      const blink::DedicatedWorkerToken& worker_token,
      const GURL& url) override;

  // content::SharedWorkerService::Observer:
  void OnWorkerCreated(const blink::SharedWorkerToken& shared_worker_token,
                       int worker_process_id,
                       const url::Origin& security_origin,
                       const base::UnguessableToken& dev_tools_token) override;
  void OnBeforeWorkerDestroyed(
      const blink::SharedWorkerToken& shared_worker_token) override;
  void OnFinalResponseURLDetermined(
      const blink::SharedWorkerToken& shared_worker_token,
      const GURL& url) override;
  void OnClientAdded(
      const blink::SharedWorkerToken& shared_worker_token,
      content::GlobalRenderFrameHostId render_frame_host_id) override {}
  void OnClientRemoved(
      const blink::SharedWorkerToken& shared_worker_token,
      content::GlobalRenderFrameHostId render_frame_host_id) override {}

  // content::ServiceWorkerContextObserver:
  void OnVersionStartedRunning(
      int64_t version_id,
      const content::ServiceWorkerRunningInfo& running_info) override;
  void OnVersionStoppedRunning(int64_t version_id) override;

 private:
  // Creates a WorkerTask and inserts it into |out_worker_tasks|. Then it
  // notifies the |worker_task_provider_| about the new Task.
  //
  // Note that this function is templated because each worker type uses a
  // different type as its ID.
  template <typename WorkerId>
  void CreateWorkerTask(
      const WorkerId& worker_id,
      Task::Type task_type,
      content::RenderProcessHost* worker_process_host,
      base::flat_map<WorkerId, std::unique_ptr<WorkerTask>>* out_worker_tasks);

  // Deletes an existing WorkerTask from |out_worker_tasks| and notifies
  // |worker_task_provider_| about the deletion of the task.
  //
  // Note that this function is templated because each worker type uses a
  // different type as its ID.
  template <typename WorkerId>
  void DeleteWorkerTask(
      const WorkerId& worker_id,
      base::flat_map<WorkerId, std::unique_ptr<WorkerTask>>* out_worker_tasks);

  // Sets the script URL of an existing WorkerTask.
  //
  // Note that this function is templated because each worker type uses a
  // different type as its ID.
  template <typename WorkerId>
  void SetWorkerTaskScriptUrl(
      const WorkerId& worker_id,
      const GURL& script_url,
      base::flat_map<WorkerId, std::unique_ptr<WorkerTask>>* out_worker_tasks);

  // The provider that gets notified when a WorkerTask is created/deleted.
  const raw_ptr<WorkerTaskProvider> worker_task_provider_;  // Owner.

  // For dedicated workers:
  base::ScopedObservation<content::DedicatedWorkerService,
                          content::DedicatedWorkerService::Observer>
      scoped_dedicated_worker_service_observation_{this};

  base::flat_map<blink::DedicatedWorkerToken, std::unique_ptr<WorkerTask>>
      dedicated_worker_tasks_;

  // For shared workers:
  base::ScopedObservation<content::SharedWorkerService,
                          content::SharedWorkerService::Observer>
      scoped_shared_worker_service_observation_{this};

  base::flat_map<blink::SharedWorkerToken, std::unique_ptr<WorkerTask>>
      shared_worker_tasks_;

  // For service workers:
  base::ScopedObservation<content::ServiceWorkerContext,
                          content::ServiceWorkerContextObserver>
      scoped_service_worker_context_observation_{this};

  base::flat_map<int64_t /*version_id*/, std::unique_ptr<WorkerTask>>
      service_worker_tasks_;

  // Because service worker notifications are asynchronous, it is possible to
  // be notified of the creation of a service worker after its render process
  // was deleted. Those workers are ignored.
  base::flat_set<int64_t /*version_id*/> ignored_service_worker_;
};

}  // namespace task_manager

#endif  // CHROME_BROWSER_TASK_MANAGER_PROVIDERS_PER_PROFILE_WORKER_TASK_TRACKER_H_