mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 14:02:46 +00:00 
			
		
		
		
	LibWeb: Remove unecessary dependence on Window from Fetch, XHR, FileAPI
These classes only needed Window to get at its realm. Pass a realm directly to construct Fetch, XMLHttpRequest and FileAPI classes.
This commit is contained in:
		
							parent
							
								
									6a10352712
								
							
						
					
					
						commit
						4878a18ee7
					
				
					 17 changed files with 144 additions and 169 deletions
				
			
		|  | @ -8,15 +8,15 @@ | |||
| #include <AK/StdLibExtras.h> | ||||
| #include <LibJS/Runtime/ArrayBuffer.h> | ||||
| #include <LibWeb/Bindings/BlobPrototype.h> | ||||
| #include <LibWeb/Bindings/Intrinsics.h> | ||||
| #include <LibWeb/FileAPI/Blob.h> | ||||
| #include <LibWeb/HTML/Window.h> | ||||
| #include <LibWeb/WebIDL/AbstractOperations.h> | ||||
| 
 | ||||
| namespace Web::FileAPI { | ||||
| 
 | ||||
| JS::NonnullGCPtr<Blob> Blob::create(HTML::Window& window, ByteBuffer byte_buffer, String type) | ||||
| JS::NonnullGCPtr<Blob> Blob::create(JS::Realm& realm, ByteBuffer byte_buffer, String type) | ||||
| { | ||||
|     return JS::NonnullGCPtr(*window.heap().allocate<Blob>(window.realm(), window, move(byte_buffer), move(type))); | ||||
|     return JS::NonnullGCPtr(*realm.heap().allocate<Blob>(realm, realm, move(byte_buffer), move(type))); | ||||
| } | ||||
| 
 | ||||
| // https://w3c.github.io/FileAPI/#convert-line-endings-to-native
 | ||||
|  | @ -111,40 +111,40 @@ bool is_basic_latin(StringView view) | |||
|     return true; | ||||
| } | ||||
| 
 | ||||
| Blob::Blob(HTML::Window& window) | ||||
|     : PlatformObject(window.realm()) | ||||
| Blob::Blob(JS::Realm& realm) | ||||
|     : PlatformObject(realm) | ||||
| { | ||||
|     set_prototype(&window.cached_web_prototype("Blob")); | ||||
|     set_prototype(&Bindings::cached_web_prototype(realm, "Blob")); | ||||
| } | ||||
| 
 | ||||
| Blob::Blob(HTML::Window& window, ByteBuffer byte_buffer, String type) | ||||
|     : PlatformObject(window.realm()) | ||||
| Blob::Blob(JS::Realm& realm, ByteBuffer byte_buffer, String type) | ||||
|     : PlatformObject(realm) | ||||
|     , m_byte_buffer(move(byte_buffer)) | ||||
|     , m_type(move(type)) | ||||
| { | ||||
|     set_prototype(&window.cached_web_prototype("Blob")); | ||||
|     set_prototype(&Bindings::cached_web_prototype(realm, "Blob")); | ||||
| } | ||||
| 
 | ||||
| Blob::Blob(HTML::Window& window, ByteBuffer byte_buffer) | ||||
|     : PlatformObject(window.realm()) | ||||
| Blob::Blob(JS::Realm& realm, ByteBuffer byte_buffer) | ||||
|     : PlatformObject(realm) | ||||
|     , m_byte_buffer(move(byte_buffer)) | ||||
| { | ||||
|     set_prototype(&window.cached_web_prototype("Blob")); | ||||
|     set_prototype(&Bindings::cached_web_prototype(realm, "Blob")); | ||||
| } | ||||
| 
 | ||||
| Blob::~Blob() = default; | ||||
| 
 | ||||
| // https://w3c.github.io/FileAPI/#ref-for-dom-blob-blob
 | ||||
| WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> Blob::create(HTML::Window& window, Optional<Vector<BlobPart>> const& blob_parts, Optional<BlobPropertyBag> const& options) | ||||
| WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> Blob::create(JS::Realm& realm, Optional<Vector<BlobPart>> const& blob_parts, Optional<BlobPropertyBag> const& options) | ||||
| { | ||||
|     // 1. If invoked with zero parameters, return a new Blob object consisting of 0 bytes, with size set to 0, and with type set to the empty string.
 | ||||
|     if (!blob_parts.has_value() && !options.has_value()) | ||||
|         return JS::NonnullGCPtr(*window.heap().allocate<Blob>(window.realm(), window)); | ||||
|         return JS::NonnullGCPtr(*realm.heap().allocate<Blob>(realm, realm)); | ||||
| 
 | ||||
|     ByteBuffer byte_buffer {}; | ||||
|     // 2. Let bytes be the result of processing blob parts given blobParts and options.
 | ||||
|     if (blob_parts.has_value()) { | ||||
|         byte_buffer = TRY_OR_RETURN_OOM(window, process_blob_parts(blob_parts.value(), options)); | ||||
|         byte_buffer = TRY_OR_RETURN_OOM(realm, process_blob_parts(blob_parts.value(), options)); | ||||
|     } | ||||
| 
 | ||||
|     auto type = String::empty(); | ||||
|  | @ -164,12 +164,12 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> Blob::create(HTML::Window& window, O | |||
|     } | ||||
| 
 | ||||
|     // 4. Return a Blob object referring to bytes as its associated byte sequence, with its size set to the length of bytes, and its type set to the value of t from the substeps above.
 | ||||
|     return JS::NonnullGCPtr(*window.heap().allocate<Blob>(window.realm(), window, move(byte_buffer), move(type))); | ||||
|     return JS::NonnullGCPtr(*realm.heap().allocate<Blob>(realm, realm, move(byte_buffer), move(type))); | ||||
| } | ||||
| 
 | ||||
| WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> Blob::create_with_global_object(HTML::Window& window, Optional<Vector<BlobPart>> const& blob_parts, Optional<BlobPropertyBag> const& options) | ||||
| WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> Blob::construct_impl(JS::Realm& realm, Optional<Vector<BlobPart>> const& blob_parts, Optional<BlobPropertyBag> const& options) | ||||
| { | ||||
|     return Blob::create(window, blob_parts, options); | ||||
|     return Blob::create(realm, blob_parts, options); | ||||
| } | ||||
| 
 | ||||
| // https://w3c.github.io/FileAPI/#dfn-slice
 | ||||
|  | @ -232,8 +232,8 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> Blob::slice(Optional<i64> start, Opt | |||
|     // a. S refers to span consecutive bytes from this, beginning with the byte at byte-order position relativeStart.
 | ||||
|     // b. S.size = span.
 | ||||
|     // c. S.type = relativeContentType.
 | ||||
|     auto byte_buffer = TRY_OR_RETURN_OOM(global_object(), m_byte_buffer.slice(relative_start, span)); | ||||
|     return JS::NonnullGCPtr(*heap().allocate<Blob>(realm(), global_object(), move(byte_buffer), move(relative_content_type))); | ||||
|     auto byte_buffer = TRY_OR_RETURN_OOM(realm(), m_byte_buffer.slice(relative_start, span)); | ||||
|     return JS::NonnullGCPtr(*heap().allocate<Blob>(realm(), realm(), move(byte_buffer), move(relative_content_type))); | ||||
| } | ||||
| 
 | ||||
| // https://w3c.github.io/FileAPI/#dom-blob-text
 | ||||
|  |  | |||
|  | @ -33,9 +33,9 @@ class Blob : public Bindings::PlatformObject { | |||
| public: | ||||
|     virtual ~Blob() override; | ||||
| 
 | ||||
|     static JS::NonnullGCPtr<Blob> create(HTML::Window&, ByteBuffer, String type); | ||||
|     static WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> create(HTML::Window&, Optional<Vector<BlobPart>> const& blob_parts = {}, Optional<BlobPropertyBag> const& options = {}); | ||||
|     static WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> create_with_global_object(HTML::Window&, Optional<Vector<BlobPart>> const& blob_parts = {}, Optional<BlobPropertyBag> const& options = {}); | ||||
|     static JS::NonnullGCPtr<Blob> create(JS::Realm&, ByteBuffer, String type); | ||||
|     static WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> create(JS::Realm&, Optional<Vector<BlobPart>> const& blob_parts = {}, Optional<BlobPropertyBag> const& options = {}); | ||||
|     static WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> construct_impl(JS::Realm&, Optional<Vector<BlobPart>> const& blob_parts = {}, Optional<BlobPropertyBag> const& options = {}); | ||||
| 
 | ||||
|     // https://w3c.github.io/FileAPI/#dfn-size
 | ||||
|     u64 size() const { return m_byte_buffer.size(); } | ||||
|  | @ -50,11 +50,11 @@ public: | |||
|     ReadonlyBytes bytes() const { return m_byte_buffer.bytes(); } | ||||
| 
 | ||||
| protected: | ||||
|     Blob(HTML::Window&, ByteBuffer, String type); | ||||
|     Blob(HTML::Window&, ByteBuffer); | ||||
|     Blob(JS::Realm&, ByteBuffer, String type); | ||||
|     Blob(JS::Realm&, ByteBuffer); | ||||
| 
 | ||||
| private: | ||||
|     explicit Blob(HTML::Window&); | ||||
|     explicit Blob(JS::Realm&); | ||||
| 
 | ||||
|     ByteBuffer m_byte_buffer {}; | ||||
|     String m_type {}; | ||||
|  |  | |||
|  | @ -4,26 +4,27 @@ | |||
|  * SPDX-License-Identifier: BSD-2-Clause | ||||
|  */ | ||||
| 
 | ||||
| #include <AK/Time.h> | ||||
| #include <LibWeb/Bindings/Intrinsics.h> | ||||
| #include <LibWeb/FileAPI/File.h> | ||||
| #include <LibWeb/HTML/Window.h> | ||||
| 
 | ||||
| namespace Web::FileAPI { | ||||
| 
 | ||||
| File::File(HTML::Window& window, ByteBuffer byte_buffer, String file_name, String type, i64 last_modified) | ||||
|     : Blob(window, move(byte_buffer), move(type)) | ||||
| File::File(JS::Realm& realm, ByteBuffer byte_buffer, String file_name, String type, i64 last_modified) | ||||
|     : Blob(realm, move(byte_buffer), move(type)) | ||||
|     , m_name(move(file_name)) | ||||
|     , m_last_modified(last_modified) | ||||
| { | ||||
|     set_prototype(&window.cached_web_prototype("File")); | ||||
|     set_prototype(&Bindings::cached_web_prototype(realm, "File")); | ||||
| } | ||||
| 
 | ||||
| File::~File() = default; | ||||
| 
 | ||||
| // https://w3c.github.io/FileAPI/#ref-for-dom-file-file
 | ||||
| WebIDL::ExceptionOr<JS::NonnullGCPtr<File>> File::create(HTML::Window& window, Vector<BlobPart> const& file_bits, String const& file_name, Optional<FilePropertyBag> const& options) | ||||
| WebIDL::ExceptionOr<JS::NonnullGCPtr<File>> File::create(JS::Realm& realm, Vector<BlobPart> const& file_bits, String const& file_name, Optional<FilePropertyBag> const& options) | ||||
| { | ||||
|     // 1. Let bytes be the result of processing blob parts given fileBits and options.
 | ||||
|     auto bytes = TRY_OR_RETURN_OOM(window, process_blob_parts(file_bits, static_cast<Optional<BlobPropertyBag> const&>(*options))); | ||||
|     auto bytes = TRY_OR_RETURN_OOM(realm, process_blob_parts(file_bits, static_cast<Optional<BlobPropertyBag> const&>(*options))); | ||||
| 
 | ||||
|     // 2. Let n be the fileName argument to the constructor.
 | ||||
|     //    NOTE: Underlying OS filesystems use differing conventions for file name; with constructed files, mandating UTF-16 lessens ambiquity when file names are converted to byte sequences.
 | ||||
|  | @ -57,12 +58,12 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<File>> File::create(HTML::Window& window, V | |||
|     //    4. F.name is set to n.
 | ||||
|     //    5. F.type is set to t.
 | ||||
|     //    6. F.lastModified is set to d.
 | ||||
|     return JS::NonnullGCPtr(*window.heap().allocate<File>(window.realm(), window, move(bytes), move(name), move(type), last_modified)); | ||||
|     return JS::NonnullGCPtr(*realm.heap().allocate<File>(realm, realm, move(bytes), move(name), move(type), last_modified)); | ||||
| } | ||||
| 
 | ||||
| WebIDL::ExceptionOr<JS::NonnullGCPtr<File>> File::create_with_global_object(HTML::Window& window, Vector<BlobPart> const& file_bits, String const& file_name, Optional<FilePropertyBag> const& options) | ||||
| WebIDL::ExceptionOr<JS::NonnullGCPtr<File>> File::construct_impl(JS::Realm& realm, Vector<BlobPart> const& file_bits, String const& file_name, Optional<FilePropertyBag> const& options) | ||||
| { | ||||
|     return create(window, file_bits, file_name, options); | ||||
|     return create(realm, file_bits, file_name, options); | ||||
| } | ||||
| 
 | ||||
| } | ||||
|  |  | |||
|  | @ -6,7 +6,6 @@ | |||
| 
 | ||||
| #pragma once | ||||
| 
 | ||||
| #include <AK/Time.h> | ||||
| #include <LibWeb/FileAPI/Blob.h> | ||||
| 
 | ||||
| namespace Web::FileAPI { | ||||
|  | @ -19,8 +18,8 @@ class File : public Blob { | |||
|     WEB_PLATFORM_OBJECT(File, Blob); | ||||
| 
 | ||||
| public: | ||||
|     static WebIDL::ExceptionOr<JS::NonnullGCPtr<File>> create(HTML::Window&, Vector<BlobPart> const& file_bits, String const& file_name, Optional<FilePropertyBag> const& options = {}); | ||||
|     static WebIDL::ExceptionOr<JS::NonnullGCPtr<File>> create_with_global_object(HTML::Window&, Vector<BlobPart> const& file_bits, String const& file_name, Optional<FilePropertyBag> const& options = {}); | ||||
|     static WebIDL::ExceptionOr<JS::NonnullGCPtr<File>> create(JS::Realm&, Vector<BlobPart> const& file_bits, String const& file_name, Optional<FilePropertyBag> const& options = {}); | ||||
|     static WebIDL::ExceptionOr<JS::NonnullGCPtr<File>> construct_impl(JS::Realm&, Vector<BlobPart> const& file_bits, String const& file_name, Optional<FilePropertyBag> const& options = {}); | ||||
| 
 | ||||
|     virtual ~File() override; | ||||
| 
 | ||||
|  | @ -30,7 +29,7 @@ public: | |||
|     i64 last_modified() const { return m_last_modified; } | ||||
| 
 | ||||
| private: | ||||
|     File(HTML::Window&, ByteBuffer, String file_name, String type, i64 last_modified); | ||||
|     File(JS::Realm&, ByteBuffer, String file_name, String type, i64 last_modified); | ||||
| 
 | ||||
|     String m_name; | ||||
|     i64 m_last_modified { 0 }; | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Andrew Kaster
						Andrew Kaster