File: service_worker_lifetime_manager.cc

package info (click to toggle)
chromium 140.0.7339.185-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,193,740 kB
  • sloc: cpp: 35,093,945; ansic: 7,161,670; javascript: 4,199,694; python: 1,441,797; asm: 949,904; xml: 747,515; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,114; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (149 lines) | stat: -rw-r--r-- 5,183 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
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/file_system_provider/service_worker_lifetime_manager.h"

#include <tuple>
#include <utility>

#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "extensions/browser/process_manager.h"
#include "extensions/browser/process_manager_factory.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_database.mojom.h"

namespace ash::file_system_provider {

bool RequestKey::operator<(const RequestKey& other) const {
  return std::tie(extension_id, file_system_id, request_id) <
         std::tie(other.extension_id, other.file_system_id, other.request_id);
}

ServiceWorkerLifetimeManager::ServiceWorkerLifetimeManager(
    content::BrowserContext* context)
    // Context can be null in tests.
    : process_manager_(context ? extensions::ProcessManager::Get(context)
                               : nullptr) {}

ServiceWorkerLifetimeManager::~ServiceWorkerLifetimeManager() = default;

ServiceWorkerLifetimeManager* ServiceWorkerLifetimeManager::Get(
    content::BrowserContext* context) {
  return ServiceWorkerLifetimeManagerFactory::GetForBrowserContext(context);
}

void ServiceWorkerLifetimeManager::StartRequest(const RequestKey& key) {
  DCHECK(!base::Contains(requests_, key));
  requests_[key] = {};
}

void ServiceWorkerLifetimeManager::FinishRequest(
    const RequestKey& request_key) {
  auto it = requests_.find(request_key);
  if (it == requests_.end()) {
    return;
  }
  std::set<KeepaliveKey> keepalive_keys = std::move(it->second);
  requests_.erase(it);
  for (const KeepaliveKey& keepalive_key : keepalive_keys) {
    DecrementKeepalive(keepalive_key);
  }
}

void ServiceWorkerLifetimeManager::RequestDispatched(
    const RequestKey& key,
    const extensions::EventTarget& target) {
  if (target.service_worker_version_id ==
      blink::mojom::kInvalidServiceWorkerVersionId) {
    return;
  }
  auto it = requests_.find(key);
  if (it == requests_.end()) {
    return;
  }
  std::set<KeepaliveKey>& keepalive_keys = it->second;
  extensions::WorkerId worker_id{
      target.extension_id,
      target.render_process_id,
      target.service_worker_version_id,
      target.worker_thread_id,
  };
  std::string uuid = IncrementKeepalive(worker_id);
  keepalive_keys.insert(KeepaliveKey{worker_id, uuid});
}

void ServiceWorkerLifetimeManager::Shutdown() {
  for (const auto& [_, keys] : requests_) {
    for (const KeepaliveKey& key : keys) {
      DecrementKeepalive(key);
    }
  }
}

extensions::Event::DidDispatchCallback
ServiceWorkerLifetimeManager::CreateDispatchCallbackForRequest(
    const RequestKey& request_key) {
  return base::BindRepeating(&ServiceWorkerLifetimeManager::RequestDispatched,
                             weak_ptr_factory_.GetWeakPtr(), request_key);
}

bool ServiceWorkerLifetimeManager::KeepaliveKey::operator<(
    const KeepaliveKey& other) const {
  return std::tie(worker_id, request_uuid) <
         std::tie(other.worker_id, other.request_uuid);
}

std::string ServiceWorkerLifetimeManager::IncrementKeepalive(
    const extensions::WorkerId& worker_id) {
  return process_manager_
      ->IncrementServiceWorkerKeepaliveCount(
          worker_id,
          content::ServiceWorkerExternalRequestTimeoutType::kDoesNotTimeout,
          extensions::Activity::Type::EVENT, /*extra_data=*/"")
      .AsLowercaseString();
}

void ServiceWorkerLifetimeManager::DecrementKeepalive(const KeepaliveKey& key) {
  base::Uuid uuid = base::Uuid::ParseLowercase(key.request_uuid);
  process_manager_->DecrementServiceWorkerKeepaliveCount(
      key.worker_id, uuid, extensions::Activity::Type::EVENT,
      /*extra_data=*/"");
}

// static
ServiceWorkerLifetimeManager*
ServiceWorkerLifetimeManagerFactory::GetForBrowserContext(
    content::BrowserContext* context) {
  return static_cast<ServiceWorkerLifetimeManager*>(
      GetInstance()->GetServiceForBrowserContext(context, true));
}

// static
ServiceWorkerLifetimeManagerFactory*
ServiceWorkerLifetimeManagerFactory::GetInstance() {
  return base::Singleton<ServiceWorkerLifetimeManagerFactory>::get();
}

ServiceWorkerLifetimeManagerFactory::ServiceWorkerLifetimeManagerFactory()
    : ProfileKeyedServiceFactory(
          "ServiceWorkerLifetimeManagerFactory",
          ProfileSelections::Builder()
              .WithRegular(ProfileSelection::kOriginalOnly)
              .WithGuest(ProfileSelection::kOriginalOnly)
              // TODO(crbug.com/41488885): Check if this service is needed for
              // Ash Internals.
              .WithAshInternals(ProfileSelection::kOriginalOnly)
              .Build()) {
  DependsOn(extensions::ProcessManagerFactory::GetInstance());
}

ServiceWorkerLifetimeManagerFactory::~ServiceWorkerLifetimeManagerFactory() =
    default;

std::unique_ptr<KeyedService>
ServiceWorkerLifetimeManagerFactory::BuildServiceInstanceForBrowserContext(
    content::BrowserContext* context) const {
  return std::make_unique<ServiceWorkerLifetimeManager>(context);
}

}  // namespace ash::file_system_provider