1 /* 2 * Copyright (C) 2009 Apple Inc. All Rights Reserved. 3 * Copyright (C) 2009, 2011 Google Inc. All Rights Reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 * 26 */ 27 28 #ifndef THIRD_PARTY_BLINK_RENDERER_CORE_WORKERS_WORKER_CLASSIC_SCRIPT_LOADER_H_ 29 #define THIRD_PARTY_BLINK_RENDERER_CORE_WORKERS_WORKER_CLASSIC_SCRIPT_LOADER_H_ 30 31 #include <memory> 32 #include "base/memory/scoped_refptr.h" 33 #include "mojo/public/cpp/bindings/pending_remote.h" 34 #include "services/network/public/mojom/fetch_api.mojom-blink-forward.h" 35 #include "services/network/public/mojom/ip_address_space.mojom-blink-forward.h" 36 #include "services/network/public/mojom/url_loader_factory.mojom-blink.h" 37 #include "third_party/blink/public/mojom/loader/resource_load_info_notifier.mojom-shared.h" 38 #include "third_party/blink/public/platform/cross_variant_mojo_util.h" 39 #include "third_party/blink/public/platform/web_url_request.h" 40 #include "third_party/blink/renderer/core/core_export.h" 41 #include "third_party/blink/renderer/core/loader/threadable_loader.h" 42 #include "third_party/blink/renderer/core/loader/threadable_loader_client.h" 43 #include "third_party/blink/renderer/platform/loader/allowed_by_nosniff.h" 44 #include "third_party/blink/renderer/platform/loader/fetch/resource_loader_options.h" 45 #include "third_party/blink/renderer/platform/loader/fetch/resource_request.h" 46 #include "third_party/blink/renderer/platform/loader/fetch/url_loader/worker_main_script_loader.h" 47 #include "third_party/blink/renderer/platform/loader/fetch/url_loader/worker_main_script_loader_client.h" 48 #include "third_party/blink/renderer/platform/weborigin/kurl.h" 49 #include "third_party/blink/renderer/platform/wtf/allocator/allocator.h" 50 #include "third_party/blink/renderer/platform/wtf/functional.h" 51 #include "third_party/blink/renderer/platform/wtf/ref_counted.h" 52 #include "third_party/blink/renderer/platform/wtf/text/string_builder.h" 53 54 namespace blink { 55 56 class ContentSecurityPolicy; 57 class ResourceRequest; 58 class ResourceResponse; 59 class ExecutionContext; 60 class TextResourceDecoder; 61 struct WorkerMainScriptLoadParameters; 62 63 class CORE_EXPORT WorkerClassicScriptLoader final 64 : public GarbageCollected<WorkerClassicScriptLoader>, 65 public ThreadableLoaderClient, 66 public WorkerMainScriptLoaderClient { 67 public: 68 WorkerClassicScriptLoader(); 69 70 // For importScript(). 71 void LoadSynchronously(ExecutionContext&, 72 ResourceFetcher* fetch_client_settings_object_fetcher, 73 const KURL&, 74 mojom::blink::RequestContextType, 75 network::mojom::RequestDestination); 76 77 // Note that callbacks could be invoked before 78 // LoadTopLevelScriptAsynchronously() returns. 79 // 80 // |fetch_client_settings_object_fetcher| is different from 81 // ExecutionContext::Fetcher() in off-the-main-thread fetch. 82 // TODO(crbug.com/1064920): Remove |reject_coep_unsafe_none| and 83 // |blob_url_loader_factory| when PlzDedicatedWorker ships. 84 // 85 // |worker_main_script_load_params| is valid for dedicated workers (when 86 // PlzDedicatedWorker is enabled) and shared workers. 87 void LoadTopLevelScriptAsynchronously( 88 ExecutionContext&, 89 ResourceFetcher* fetch_client_settings_object_fetcher, 90 const KURL&, 91 std::unique_ptr<WorkerMainScriptLoadParameters> 92 worker_main_script_load_params, 93 mojom::blink::RequestContextType, 94 network::mojom::RequestDestination, 95 network::mojom::RequestMode, 96 network::mojom::CredentialsMode, 97 base::OnceClosure response_callback, 98 base::OnceClosure finished_callback, 99 RejectCoepUnsafeNone reject_coep_unsafe_none = 100 RejectCoepUnsafeNone(false), 101 mojo::PendingRemote<network::mojom::blink::URLLoaderFactory> 102 blob_url_loader_factory = {}); 103 104 // This will immediately invoke |finishedCallback| if 105 // LoadTopLevelScriptAsynchronously() is in progress. 106 void Cancel(); 107 108 String SourceText(); Url()109 const KURL& Url() const { return url_; } 110 const KURL& ResponseURL() const; Failed()111 bool Failed() const { return failed_; } Canceled()112 bool Canceled() const { return canceled_; } Identifier()113 uint64_t Identifier() const { return identifier_; } AppCacheID()114 int64_t AppCacheID() const { return app_cache_id_; } 115 ReleaseCachedMetadata()116 std::unique_ptr<Vector<uint8_t>> ReleaseCachedMetadata() { 117 return std::move(cached_metadata_); 118 } 119 GetContentSecurityPolicy()120 ContentSecurityPolicy* GetContentSecurityPolicy() { 121 return content_security_policy_.Get(); 122 } 123 GetReferrerPolicy()124 const String& GetReferrerPolicy() const { return referrer_policy_; } 125 ResponseAddressSpace()126 network::mojom::IPAddressSpace ResponseAddressSpace() const { 127 return response_address_space_; 128 } 129 OriginTrialTokens()130 const Vector<String>* OriginTrialTokens() const { 131 return origin_trial_tokens_.get(); 132 } 133 134 // ThreadableLoaderClient 135 void DidReceiveResponse(uint64_t /*identifier*/, 136 const ResourceResponse&) override; 137 void DidReceiveData(const char* data, unsigned data_length) override; 138 void DidReceiveCachedMetadata(const char*, int /*dataLength*/) override; 139 void DidFinishLoading(uint64_t identifier) override; 140 void DidFail(const ResourceError&) override; 141 void DidFailRedirectCheck() override; 142 143 // WorkerMainScriptLoaderClient 144 // These will be called for dedicated workers (when PlzDedicatedWorker is 145 // enabled) and shared workers. 146 void DidReceiveData(base::span<const char> span) override; 147 void OnFinishedLoadingWorkerMainScript() override; 148 void OnFailedLoadingWorkerMainScript() override; 149 150 void Trace(Visitor*) const override; 151 152 private: 153 void NotifyError(); 154 void NotifyFinished(); 155 156 void ProcessContentSecurityPolicy(const ResourceResponse&); 157 158 // Callbacks for loadAsynchronously(). 159 base::OnceClosure response_callback_; 160 base::OnceClosure finished_callback_; 161 162 Member<ThreadableLoader> threadable_loader_; 163 164 // These are used for dedicated workers (when PlzDedicatedWorker is enabled) 165 // and shared workers. 166 Member<WorkerMainScriptLoader> worker_main_script_loader_; 167 String response_encoding_; 168 std::unique_ptr<TextResourceDecoder> decoder_; 169 StringBuilder source_text_; 170 KURL url_; 171 KURL response_url_; 172 173 // TODO(nhiroki): Consolidate these state flags for cleanup. 174 bool failed_ = false; 175 bool canceled_ = false; 176 bool need_to_cancel_ = false; 177 178 // true when LoadTopLevelScriptAsynchronously() is called, 179 // false when LoadSynchronously() is called i.e. for importScripts(). 180 bool is_top_level_script_ = false; 181 182 uint64_t identifier_ = 0; 183 int64_t app_cache_id_ = 0; 184 std::unique_ptr<Vector<uint8_t>> cached_metadata_; 185 Member<ContentSecurityPolicy> content_security_policy_; 186 network::mojom::IPAddressSpace response_address_space_; 187 std::unique_ptr<Vector<String>> origin_trial_tokens_; 188 String referrer_policy_; 189 190 Member<ResourceFetcher> fetch_client_settings_object_fetcher_; 191 }; 192 193 } // namespace blink 194 195 #endif // THIRD_PARTY_BLINK_RENDERER_CORE_WORKERS_WORKER_CLASSIC_SCRIPT_LOADER_H_ 196