-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
service_worker_lifetime_manager.h
120 lines (95 loc) · 4.17 KB
/
service_worker_lifetime_manager.h
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
// 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.
#ifndef CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_SYSTEM_PROVIDER_SERVICE_WORKER_LIFETIME_MANAGER_H_
#define CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_SYSTEM_PROVIDER_SERVICE_WORKER_LIFETIME_MANAGER_H_
#include <map>
#include <set>
#include <string>
#include "base/memory/singleton.h"
#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
#include "components/keyed_service/core/keyed_service.h"
#include "extensions/browser/service_worker/worker_id.h"
#include "extensions/common/extension_id.h"
namespace content {
class BrowserContext;
}
namespace extensions {
class ProcessManager;
struct EventTarget;
namespace file_system_provider {
// Identifies a unique fileSystemProvider request: request ID sequence of
// integers tracked per a filesystem instance, or per provider (extension) for
// requests that aren't specific to a filesystem instance.
struct RequestKey {
extensions::ExtensionId extension_id;
std::string file_system_id;
int request_id;
bool operator<(const RequestKey& other) const;
};
// Tracks fileSystemProvider requests that have been dispatched to service
// workers but not replied to yet, and keeps service workers alive while there
// are requests in progress.
class ServiceWorkerLifetimeManager : public KeyedService {
public:
ServiceWorkerLifetimeManager(const ServiceWorkerLifetimeManager&) = delete;
ServiceWorkerLifetimeManager& operator=(const ServiceWorkerLifetimeManager&) =
delete;
~ServiceWorkerLifetimeManager() override;
static ServiceWorkerLifetimeManager* Get(content::BrowserContext*);
// Signals that a request has been sent to a fileSystemProvider. Called when
// the request is about to be dispatched (the actual targets that received the
// request aren't known yet).
void StartRequest(const RequestKey&);
// Signals that a request previously sent to a fileSystemProvider has
// finished. Called either when a request has been replied to (the first
// response finishes the request), or is cancelled, due to timeout or being
// aborted.
void FinishRequest(const RequestKey&);
// Signals that a request has been dispatched to a service worker with
// registered fileSystemProvider listeners. Called for each service worker the
// request has been dispatched to.
void RequestDispatched(const RequestKey&, const EventTarget&);
// KeyedService:
void Shutdown() override;
protected:
struct KeepaliveKey {
WorkerId worker_id;
std::string request_uuid;
bool operator==(const KeepaliveKey& other) const;
bool operator<(const KeepaliveKey& other) const;
};
explicit ServiceWorkerLifetimeManager(content::BrowserContext*);
// Virtual for tests.
virtual std::string IncrementKeepalive(const WorkerId&);
virtual void DecrementKeepalive(const KeepaliveKey&);
private:
friend class ServiceWorkerLifetimeManagerFactory;
FRIEND_TEST_ALL_PREFIXES(ServiceWorkerLifetimeManagerTest,
TestDispatchMultipleEvents);
raw_ptr<ProcessManager> process_manager_;
std::map<RequestKey, std::set<KeepaliveKey>> requests_;
};
// KeyedService factory for ServiceWorkerLifetimeManager.
class ServiceWorkerLifetimeManagerFactory
: public BrowserContextKeyedServiceFactory {
public:
ServiceWorkerLifetimeManagerFactory(
const ServiceWorkerLifetimeManagerFactory&) = delete;
ServiceWorkerLifetimeManagerFactory& operator=(
const ServiceWorkerLifetimeManagerFactory&) = delete;
static ServiceWorkerLifetimeManager* GetForBrowserContext(
content::BrowserContext*);
static ServiceWorkerLifetimeManagerFactory* GetInstance();
private:
friend struct base::DefaultSingletonTraits<
ServiceWorkerLifetimeManagerFactory>;
ServiceWorkerLifetimeManagerFactory();
~ServiceWorkerLifetimeManagerFactory() override;
// BrowserContextKeyedServiceFactory:
KeyedService* BuildServiceInstanceFor(
content::BrowserContext* context) const override;
};
} // namespace file_system_provider
} // namespace extensions
#endif // CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_SYSTEM_PROVIDER_SERVICE_WORKER_LIFETIME_MANAGER_H_