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_CORE_LOADER_MODULESCRIPT_MODULE_SCRIPT_CREATION_PARAMS_H_
6 #define THIRD_PARTY_BLINK_RENDERER_CORE_LOADER_MODULESCRIPT_MODULE_SCRIPT_CREATION_PARAMS_H_
7 
8 #include "base/optional.h"
9 #include "third_party/blink/public/platform/web_url_request.h"
10 #include "third_party/blink/renderer/platform/bindings/parkable_string.h"
11 #include "third_party/blink/renderer/platform/heap/persistent.h"
12 #include "third_party/blink/renderer/platform/loader/fetch/cached_metadata_handler.h"
13 #include "third_party/blink/renderer/platform/weborigin/kurl.h"
14 #include "third_party/blink/renderer/platform/wtf/cross_thread_copier.h"
15 #include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
16 
17 namespace blink {
18 
19 // ModuleScriptCreationParams contains parameters for creating ModuleScript.
20 class ModuleScriptCreationParams {
21   DISALLOW_NEW();
22 
23   enum class ModuleType { kJavaScriptModule, kJSONModule, kCSSModule };
24 
25  public:
ModuleScriptCreationParams(const KURL & response_url,const ModuleScriptCreationParams::ModuleType module_type,const ParkableString & source_text,SingleCachedMetadataHandler * cache_handler,network::mojom::CredentialsMode credentials_mode)26   ModuleScriptCreationParams(
27       const KURL& response_url,
28       const ModuleScriptCreationParams::ModuleType module_type,
29       const ParkableString& source_text,
30       SingleCachedMetadataHandler* cache_handler,
31       network::mojom::CredentialsMode credentials_mode)
32       : response_url_(response_url),
33         module_type_(module_type),
34         is_isolated_(false),
35         source_text_(source_text),
36         isolated_source_text_(),
37         cache_handler_(cache_handler),
38         credentials_mode_(credentials_mode) {}
39 
40   ~ModuleScriptCreationParams() = default;
41 
IsolatedCopy()42   ModuleScriptCreationParams IsolatedCopy() const {
43     String isolated_source_text =
44         isolated_source_text_ ? isolated_source_text_.IsolatedCopy()
45                               : GetSourceText().ToString().IsolatedCopy();
46 
47     return ModuleScriptCreationParams(GetResponseUrl().Copy(), module_type_,
48                                       isolated_source_text,
49                                       GetFetchCredentialsMode());
50   }
51 
GetModuleType()52   ModuleScriptCreationParams::ModuleType GetModuleType() const {
53     return module_type_;
54   }
55 
GetResponseUrl()56   const KURL& GetResponseUrl() const { return response_url_; }
GetSourceText()57   const ParkableString& GetSourceText() const {
58     if (is_isolated_) {
59       source_text_ = ParkableString(isolated_source_text_.ReleaseImpl());
60       isolated_source_text_ = String();
61       is_isolated_ = false;
62     }
63     return source_text_;
64   }
CacheHandler()65   SingleCachedMetadataHandler* CacheHandler() const { return cache_handler_; }
GetFetchCredentialsMode()66   network::mojom::CredentialsMode GetFetchCredentialsMode() const {
67     return credentials_mode_;
68   }
69 
IsSafeToSendToAnotherThread()70   bool IsSafeToSendToAnotherThread() const {
71     return response_url_.IsSafeToSendToAnotherThread() && is_isolated_;
72   }
73 
74  private:
75   // Creates an isolated copy.
ModuleScriptCreationParams(const KURL & response_url,const ModuleScriptCreationParams::ModuleType & module_type,const String & isolated_source_text,network::mojom::CredentialsMode credentials_mode)76   ModuleScriptCreationParams(
77       const KURL& response_url,
78       const ModuleScriptCreationParams::ModuleType& module_type,
79       const String& isolated_source_text,
80       network::mojom::CredentialsMode credentials_mode)
81       : response_url_(response_url),
82         module_type_(module_type),
83         is_isolated_(true),
84         source_text_(),
85         isolated_source_text_(isolated_source_text),
86         credentials_mode_(credentials_mode) {}
87 
88   const KURL response_url_;
89   const ModuleType module_type_;
90 
91   // Mutable because an isolated copy can become bound to a thread when
92   // calling GetSourceText().
93   mutable bool is_isolated_;
94   mutable ParkableString source_text_;
95   mutable String isolated_source_text_;
96 
97   // |cache_handler_| is cleared when crossing thread boundaries.
98   Persistent<SingleCachedMetadataHandler> cache_handler_;
99 
100   const network::mojom::CredentialsMode credentials_mode_;
101 };
102 
103 }  // namespace blink
104 
105 namespace WTF {
106 
107 // Creates a deep copy because |response_url_| and |source_text_| are not
108 // cross-thread-transfer-safe.
109 template <>
110 struct CrossThreadCopier<blink::ModuleScriptCreationParams> {
111   static blink::ModuleScriptCreationParams Copy(
112       const blink::ModuleScriptCreationParams& params) {
113     return params.IsolatedCopy();
114   }
115 };
116 
117 }  // namespace WTF
118 
119 #endif  // THIRD_PARTY_BLINK_RENDERER_CORE_LOADER_MODULESCRIPT_MODULE_SCRIPT_CREATION_PARAMS_H_
120