1 // Copyright 2020 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_PLATFORM_LOADER_FETCH_URL_LOADER_WORKER_MAIN_SCRIPT_LOADER_H_ 6 #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_LOADER_FETCH_URL_LOADER_WORKER_MAIN_SCRIPT_LOADER_H_ 7 8 #include <memory> 9 10 #include "base/memory/scoped_refptr.h" 11 #include "mojo/public/cpp/bindings/pending_receiver.h" 12 #include "mojo/public/cpp/bindings/pending_remote.h" 13 #include "mojo/public/cpp/bindings/receiver.h" 14 #include "mojo/public/cpp/bindings/remote.h" 15 #include "services/network/public/mojom/url_loader.mojom.h" 16 #include "services/network/public/mojom/url_response_head.mojom-forward.h" 17 #include "third_party/blink/public/common/loader/worker_main_script_load_parameters.h" 18 #include "third_party/blink/public/mojom/fetch/fetch_api_request.mojom-shared.h" 19 #include "third_party/blink/public/mojom/loader/resource_load_info.mojom.h" 20 #include "third_party/blink/public/platform/cross_variant_mojo_util.h" 21 #include "third_party/blink/renderer/platform/heap/member.h" 22 #include "third_party/blink/renderer/platform/loader/fetch/resource_load_observer.h" 23 #include "third_party/blink/renderer/platform/loader/fetch/resource_response.h" 24 #include "third_party/blink/renderer/platform/loader/fetch/response_body_loader_client.h" 25 #include "third_party/blink/renderer/platform/platform_export.h" 26 #include "third_party/blink/renderer/platform/weborigin/kurl.h" 27 #include "third_party/blink/renderer/platform/wtf/shared_buffer.h" 28 #include "third_party/blink/renderer/platform/wtf/text/text_encoding.h" 29 30 namespace blink { 31 32 class FetchContext; 33 class FetchParameters; 34 class ResourceLoadInfoNotifierWrapper; 35 class SingleCachedMetadataHandler; 36 class WorkerMainScriptLoaderClient; 37 struct ResourceLoaderOptions; 38 39 // For dedicated workers (PlzDedicatedWorker) and shared workers, the main 40 // script is pre-requested by the browser process. This class is used for 41 // receiving the response in the renderer process. 42 class PLATFORM_EXPORT WorkerMainScriptLoader final 43 : public GarbageCollected<WorkerMainScriptLoader>, 44 public network::mojom::URLLoaderClient { 45 public: 46 WorkerMainScriptLoader(); 47 ~WorkerMainScriptLoader() override; 48 49 // Starts to load the main script. 50 void Start(FetchParameters& fetch_params, 51 std::unique_ptr<WorkerMainScriptLoadParameters> 52 worker_main_script_load_params, 53 FetchContext* fetch_context, 54 ResourceLoadObserver* resource_load_observer, 55 WorkerMainScriptLoaderClient* client); 56 57 // This will immediately cancel the ongoing loading of the main script and any 58 // method of the WorkerMainScriptLoaderClient will not be invoked. 59 void Cancel(); 60 61 // Implements network::mojom::URLLoaderClient. 62 void OnReceiveResponse( 63 network::mojom::URLResponseHeadPtr response_head) override; 64 void OnReceiveRedirect( 65 const net::RedirectInfo& redirect_info, 66 network::mojom::URLResponseHeadPtr response_head) override; 67 void OnUploadProgress(int64_t current_position, 68 int64_t total_size, 69 OnUploadProgressCallback callback) override; 70 void OnReceiveCachedMetadata(mojo_base::BigBuffer data) override; 71 void OnTransferSizeUpdated(int32_t transfer_size_diff) override; 72 void OnStartLoadingResponseBody( 73 mojo::ScopedDataPipeConsumerHandle handle) override; 74 void OnComplete(const network::URLLoaderCompletionStatus& status) override; 75 GetRequestURL()76 const KURL& GetRequestURL() const { return initial_request_url_; } GetResponse()77 const ResourceResponse& GetResponse() const { return resource_response_; } 78 // Gets the raw data of the main script. Data()79 SharedBuffer* Data() const { return data_.get(); } GetScriptEncoding()80 WTF::TextEncoding GetScriptEncoding() { return script_encoding_; } 81 SingleCachedMetadataHandler* CreateCachedMetadataHandler(); 82 83 virtual void Trace(Visitor*) const; 84 85 private: 86 void StartLoadingBody(); 87 void OnReadable(MojoResult); 88 void NotifyCompletionIfAppropriate(); 89 void OnConnectionClosed(); 90 void HandleRedirections( 91 std::vector<net::RedirectInfo>& redirect_infos, 92 std::vector<network::mojom::URLResponseHeadPtr>& redirect_responses); 93 94 std::unique_ptr<mojo::SimpleWatcher> watcher_; 95 mojo::ScopedDataPipeConsumerHandle data_pipe_; 96 97 Member<FetchContext> fetch_context_; 98 Member<WorkerMainScriptLoaderClient> client_; 99 Member<ResourceLoadObserver> resource_load_observer_; 100 101 ResourceRequest initial_request_; 102 ResourceLoaderOptions resource_loader_options_{nullptr /* world */}; 103 KURL initial_request_url_; 104 KURL last_request_url_; 105 ResourceResponse resource_response_; 106 scoped_refptr<SharedBuffer> data_; 107 WTF::TextEncoding script_encoding_; 108 109 // The final status received from network. 110 network::URLLoaderCompletionStatus status_; 111 // Whether we got the final status. 112 bool has_received_completion_ = false; 113 // Whether we got all the body data. 114 bool has_seen_end_of_data_ = false; 115 // Whether we need to cancel the loading of the main script. 116 bool has_cancelled_ = false; 117 118 mojo::Remote<network::mojom::URLLoader> url_loader_remote_; 119 mojo::Receiver<network::mojom::URLLoaderClient> receiver_{this}; 120 121 // Used to notify the loading stats of main script when PlzDedicatedWorker. 122 std::unique_ptr<ResourceLoadInfoNotifierWrapper> 123 resource_load_info_notifier_wrapper_; 124 }; 125 126 } // namespace blink 127 128 #endif // THIRD_PARTY_BLINK_RENDERER_PLATFORM_LOADER_FETCH_URL_LOADER_WORKER_MAIN_SCRIPT_LOADER_H_ 129