1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef THIRD_PARTY_BLINK_RENDERER_BINDINGS_CORE_V8_MODULE_RECORD_H_
6 #define THIRD_PARTY_BLINK_RENDERER_BINDINGS_CORE_V8_MODULE_RECORD_H_
7 
8 #include "third_party/blink/renderer/bindings/core/v8/module_request.h"
9 #include "third_party/blink/renderer/bindings/core/v8/script_evaluation_result.h"
10 #include "third_party/blink/renderer/bindings/core/v8/script_source_location_type.h"
11 #include "third_party/blink/renderer/bindings/core/v8/v8_code_cache.h"
12 #include "third_party/blink/renderer/core/core_export.h"
13 #include "third_party/blink/renderer/platform/bindings/trace_wrapper_v8_reference.h"
14 #include "third_party/blink/renderer/platform/loader/fetch/cached_metadata_handler.h"
15 #include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
16 #include "third_party/blink/renderer/platform/wtf/text/text_position.h"
17 #include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
18 #include "third_party/blink/renderer/platform/wtf/vector.h"
19 #include "v8/include/v8.h"
20 
21 namespace blink {
22 
23 class ExceptionState;
24 class KURL;
25 class ScriptFetchOptions;
26 class ScriptState;
27 class ScriptValue;
28 
29 // ModuleRecordProduceCacheData is a parameter object for
30 // ModuleRecord::ProduceCache().
31 class CORE_EXPORT ModuleRecordProduceCacheData final
32     : public GarbageCollected<ModuleRecordProduceCacheData> {
33  public:
34   ModuleRecordProduceCacheData(v8::Isolate*,
35                                SingleCachedMetadataHandler*,
36                                V8CodeCache::ProduceCacheOptions,
37                                v8::Local<v8::Module>);
38 
39   void Trace(Visitor*) const;
40 
CacheHandler()41   SingleCachedMetadataHandler* CacheHandler() const { return cache_handler_; }
GetProduceCacheOptions()42   V8CodeCache::ProduceCacheOptions GetProduceCacheOptions() const {
43     return produce_cache_options_;
44   }
UnboundScript(v8::Isolate * isolate)45   v8::Local<v8::UnboundModuleScript> UnboundScript(v8::Isolate* isolate) const {
46     return unbound_script_.NewLocal(isolate);
47   }
48 
49  private:
50   Member<SingleCachedMetadataHandler> cache_handler_;
51   V8CodeCache::ProduceCacheOptions produce_cache_options_;
52 
53   // TODO(keishi): Visitor only defines a trace method for v8::Value so this
54   // needs to be cast.
55   GC_PLUGIN_IGNORE("757708")
56   TraceWrapperV8Reference<v8::UnboundModuleScript> unbound_script_;
57 };
58 
59 // TODO(rikaf): Add a class level comment
60 class CORE_EXPORT ModuleRecord final {
61   STATIC_ONLY(ModuleRecord);
62 
63  public:
64   static v8::Local<v8::Module> Compile(
65       v8::Isolate*,
66       const String& source,
67       const KURL& source_url,
68       const KURL& base_url,
69       const ScriptFetchOptions&,
70       const TextPosition&,
71       ExceptionState&,
72       mojom::blink::V8CacheOptions = mojom::blink::V8CacheOptions::kDefault,
73       SingleCachedMetadataHandler* = nullptr,
74       ScriptSourceLocationType source_location_type =
75           ScriptSourceLocationType::kInternal,
76       ModuleRecordProduceCacheData** out_produce_cache_data = nullptr);
77 
78   // Returns exception, if any.
79   static ScriptValue Instantiate(ScriptState*,
80                                  v8::Local<v8::Module> record,
81                                  const KURL& source_url);
82 
83   static void ReportException(ScriptState*, v8::Local<v8::Value> exception);
84 
85   static Vector<ModuleRequest> ModuleRequests(ScriptState*,
86                                               v8::Local<v8::Module> record);
87 
88   static v8::Local<v8::Value> V8Namespace(v8::Local<v8::Module> record);
89 
90  private:
91   static v8::MaybeLocal<v8::Module> ResolveModuleCallback(
92       v8::Local<v8::Context>,
93       v8::Local<v8::String> specifier,
94       v8::Local<v8::Module> referrer);
95 };
96 
97 }  // namespace blink
98 
99 #endif  // THIRD_PARTY_BLINK_RENDERER_BINDINGS_CORE_V8_MODULE_RECORD_H_
100