Serenity Operating System
1/*
2 * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
3 * Copyright (c) 2021, the SerenityOS developers.
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8#pragma once
9
10#include <AK/DeprecatedString.h>
11#include <AK/URL.h>
12#include <LibWeb/Bindings/PlatformObject.h>
13#include <LibWeb/URL/URLSearchParams.h>
14#include <LibWeb/WebIDL/ExceptionOr.h>
15
16namespace Web::URL {
17
18class URL : public Bindings::PlatformObject {
19 WEB_PLATFORM_OBJECT(URL, Bindings::PlatformObject);
20
21public:
22 static WebIDL::ExceptionOr<JS::NonnullGCPtr<URL>> create(JS::Realm&, AK::URL url, JS::NonnullGCPtr<URLSearchParams> query);
23 static WebIDL::ExceptionOr<JS::NonnullGCPtr<URL>> construct_impl(JS::Realm&, String const& url, Optional<String> const& base = {});
24
25 virtual ~URL() override;
26
27 WebIDL::ExceptionOr<String> href() const;
28 WebIDL::ExceptionOr<void> set_href(String const&);
29
30 WebIDL::ExceptionOr<String> origin() const;
31
32 WebIDL::ExceptionOr<String> protocol() const;
33 WebIDL::ExceptionOr<void> set_protocol(String const&);
34
35 WebIDL::ExceptionOr<String> username() const;
36 void set_username(String const&);
37
38 WebIDL::ExceptionOr<String> password() const;
39 void set_password(String const&);
40
41 WebIDL::ExceptionOr<String> host() const;
42 void set_host(String const&);
43
44 WebIDL::ExceptionOr<String> hostname() const;
45 void set_hostname(String const&);
46
47 WebIDL::ExceptionOr<String> port() const;
48 void set_port(String const&);
49
50 WebIDL::ExceptionOr<String> pathname() const;
51 void set_pathname(String const&);
52
53 WebIDL::ExceptionOr<String> search() const;
54 WebIDL::ExceptionOr<void> set_search(String const&);
55
56 URLSearchParams const* search_params() const;
57
58 WebIDL::ExceptionOr<String> hash() const;
59 void set_hash(String const&);
60
61 WebIDL::ExceptionOr<String> to_json() const;
62
63 void set_query(Badge<URLSearchParams>, String query) { m_url.set_query(query.to_deprecated_string()); }
64
65private:
66 URL(JS::Realm&, AK::URL, JS::NonnullGCPtr<URLSearchParams> query);
67
68 virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
69 virtual void visit_edges(Cell::Visitor&) override;
70
71 AK::URL m_url;
72 JS::NonnullGCPtr<URLSearchParams> m_query;
73};
74
75HTML::Origin url_origin(AK::URL const&);
76bool host_is_domain(StringView host);
77
78}