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
|
// 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_ASH_FILE_SYSTEM_PROVIDER_SERVICE_WORKER_LIFETIME_MANAGER_H_
#define CHROME_BROWSER_ASH_FILE_SYSTEM_PROVIDER_SERVICE_WORKER_LIFETIME_MANAGER_H_
#include <map>
#include <set>
#include <string>
#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/singleton.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/profiles/profile_keyed_service_factory.h"
#include "components/keyed_service/core/keyed_service.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/service_worker/worker_id.h"
#include "extensions/common/extension_id.h"
namespace content {
class BrowserContext;
} // namespace content
namespace extensions {
class ProcessManager;
struct EventTarget;
} // namespace extensions
namespace ash::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;
int64_t 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:
explicit ServiceWorkerLifetimeManager(content::BrowserContext*);
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 extensions::EventTarget&);
// KeyedService:
void Shutdown() override;
// Helper to create a callback for when an event is dispatched. The callback
// is safe as it handles this object's lifetime.
extensions::Event::DidDispatchCallback CreateDispatchCallbackForRequest(
const RequestKey&);
protected:
struct KeepaliveKey {
extensions::WorkerId worker_id;
std::string request_uuid;
bool operator==(const KeepaliveKey& other) const;
bool operator<(const KeepaliveKey& other) const;
};
// Virtual for tests.
virtual std::string IncrementKeepalive(const extensions::WorkerId&);
virtual void DecrementKeepalive(const KeepaliveKey&);
private:
friend class ServiceWorkerLifetimeManagerFactory;
FRIEND_TEST_ALL_PREFIXES(ServiceWorkerLifetimeManagerTest,
TestDispatchMultipleEvents);
raw_ptr<extensions::ProcessManager> process_manager_;
std::map<RequestKey, std::set<KeepaliveKey>> requests_;
base::WeakPtrFactory<ServiceWorkerLifetimeManager> weak_ptr_factory_{this};
};
// KeyedService factory for ServiceWorkerLifetimeManager.
class ServiceWorkerLifetimeManagerFactory : public ProfileKeyedServiceFactory {
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:
std::unique_ptr<KeyedService> BuildServiceInstanceForBrowserContext(
content::BrowserContext* context) const override;
};
} // namespace ash::file_system_provider
#endif // CHROME_BROWSER_ASH_FILE_SYSTEM_PROVIDER_SERVICE_WORKER_LIFETIME_MANAGER_H_
|