| // Copyright 2017 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #ifndef WorkletModuleResponsesMap_h |
| #define WorkletModuleResponsesMap_h |
| |
| #include "core/CoreExport.h" |
| #include "core/loader/modulescript/ModuleScriptCreationParams.h" |
| #include "core/workers/WorkerOrWorkletModuleFetchCoordinator.h" |
| #include "platform/heap/Heap.h" |
| #include "platform/heap/HeapAllocator.h" |
| #include "platform/loader/fetch/FetchParameters.h" |
| #include "platform/loader/fetch/ResourceFetcher.h" |
| #include "platform/weborigin/KURL.h" |
| #include "platform/weborigin/KURLHash.h" |
| |
| namespace blink { |
| |
| // WorkletModuleResponsesMap implements the module responses map concept and the |
| // "fetch a worklet script" algorithm: |
| // https://drafts.css-houdini.org/worklets/#module-responses-map |
| // https://drafts.css-houdini.org/worklets/#fetch-a-worklet-script |
| // |
| // This acts as a cache for creation params (including source code) of module |
| // scripts, but also performs fetch when needed. The creation params are added |
| // and retrieved using Fetch(). If a module script for a given URL has already |
| // been fetched, Fetch() returns the cached creation params. Otherwise, Fetch() |
| // internally creates DocumentModuleScriptFetcher with thie ResourceFetcher that |
| // is given to its ctor. Once the module script is fetched, its creation params |
| // are cached and Fetch() returns it. |
| // |
| // TODO(nhiroki): Rename this to WorkletModuleFetchCoordinator, and revise the |
| // class-level comment. |
| class CORE_EXPORT WorkletModuleResponsesMap |
| : public GarbageCollectedFinalized<WorkletModuleResponsesMap>, |
| public WorkerOrWorkletModuleFetchCoordinator { |
| USING_GARBAGE_COLLECTED_MIXIN(WorkletModuleResponsesMap); |
| |
| public: |
| explicit WorkletModuleResponsesMap(ResourceFetcher*); |
| |
| // Fetches a module script. If the script is already fetched, synchronously |
| // calls Client::OnFetched(). Otherwise, it's called on the completion of the |
| // fetch. See also the class-level comment. |
| void Fetch(FetchParameters&, Client*); |
| |
| // Invalidates an inflight module script fetch, and calls OnFailed() for |
| // waiting clients. |
| void Invalidate(const KURL&); |
| |
| // Called when the associated document is destroyed. Aborts all waiting |
| // clients and clears the map. Following Fetch() calls are simply ignored. |
| void Dispose(); |
| |
| void Trace(blink::Visitor*); |
| |
| private: |
| class Entry; |
| |
| bool is_available_ = true; |
| |
| Member<ResourceFetcher> fetcher_; |
| |
| // TODO(nhiroki): Keep the insertion order of top-level modules to replay |
| // addModule() calls for a newly created global scope. |
| // See https://drafts.css-houdini.org/worklets/#creating-a-workletglobalscope |
| HeapHashMap<KURL, Member<Entry>> entries_; |
| }; |
| |
| } // namespace blink |
| |
| #endif // WorkletModuleResponsesMap_h |