/src/serenity/Userland/Libraries/LibWeb/Loader/GeneratedPagesLoader.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2023, Bastiaan van der Plaat <bastiaan.v.d.plaat@gmail.com> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #include <AK/NumberFormat.h> |
8 | | #include <AK/QuickSort.h> |
9 | | #include <AK/SourceGenerator.h> |
10 | | #include <LibCore/DateTime.h> |
11 | | #include <LibCore/Directory.h> |
12 | | #include <LibCore/Resource.h> |
13 | | #include <LibCore/System.h> |
14 | | #include <LibWeb/Loader/GeneratedPagesLoader.h> |
15 | | #include <LibWeb/Loader/UserAgent.h> |
16 | | |
17 | | namespace Web { |
18 | | |
19 | | void set_chrome_process_command_line(StringView command_line) |
20 | 0 | { |
21 | 0 | s_chrome_process_command_line = MUST(String::from_utf8(command_line)); |
22 | 0 | } |
23 | | |
24 | | void set_chrome_process_executable_path(StringView executable_path) |
25 | 0 | { |
26 | 0 | s_chrome_process_executable_path = MUST(String::from_utf8(executable_path)); |
27 | 0 | } |
28 | | |
29 | | ErrorOr<String> load_error_page(URL::URL const& url, StringView error_message) |
30 | 0 | { |
31 | | // Generate HTML error page from error template file |
32 | | // FIXME: Use an actual templating engine (our own one when it's built, preferably with a way to check these usages at compile time) |
33 | 0 | auto template_file = TRY(Core::Resource::load_from_uri("resource://ladybird/templates/error.html"sv)); |
34 | 0 | StringBuilder builder; |
35 | 0 | SourceGenerator generator { builder }; |
36 | 0 | generator.set("failed_url", url.to_byte_string()); |
37 | 0 | generator.set("error_message", escape_html_entities(error_message)); |
38 | 0 | generator.append(template_file->data()); |
39 | 0 | return TRY(String::from_utf8(generator.as_string_view())); |
40 | 0 | } |
41 | | |
42 | | ErrorOr<String> load_file_directory_page(URL::URL const& url) |
43 | 0 | { |
44 | | // Generate HTML contents entries table |
45 | 0 | auto lexical_path = LexicalPath(URL::percent_decode(url.serialize_path())); |
46 | 0 | Core::DirIterator dt(lexical_path.string(), Core::DirIterator::Flags::SkipParentAndBaseDir); |
47 | 0 | Vector<ByteString> names; |
48 | 0 | while (dt.has_next()) |
49 | 0 | names.append(dt.next_path()); |
50 | 0 | quick_sort(names); |
51 | |
|
52 | 0 | StringBuilder contents; |
53 | 0 | contents.append("<table>"sv); |
54 | 0 | for (auto& name : names) { |
55 | 0 | auto path = lexical_path.append(name); |
56 | 0 | auto maybe_st = Core::System::stat(path.string()); |
57 | 0 | if (!maybe_st.is_error()) { |
58 | 0 | auto st = maybe_st.release_value(); |
59 | 0 | auto is_directory = S_ISDIR(st.st_mode); |
60 | |
|
61 | 0 | contents.append("<tr>"sv); |
62 | 0 | contents.appendff("<td><span class=\"{}\"></span></td>", is_directory ? "folder" : "file"); |
63 | 0 | contents.appendff("<td><a href=\"file://{}\">{}</a></td><td> </td>"sv, path, name); |
64 | 0 | contents.appendff("<td>{:10}</td><td> </td>", is_directory ? "-"_string : human_readable_size(st.st_size)); |
65 | 0 | contents.appendff("<td>{}</td>"sv, Core::DateTime::from_timestamp(st.st_mtime).to_byte_string()); |
66 | 0 | contents.append("</tr>\n"sv); |
67 | 0 | } |
68 | 0 | } |
69 | 0 | contents.append("</table>"sv); |
70 | | |
71 | | // Generate HTML directory page from directory template file |
72 | | // FIXME: Use an actual templating engine (our own one when it's built, preferably with a way to check these usages at compile time) |
73 | 0 | auto template_file = TRY(Core::Resource::load_from_uri("resource://ladybird/templates/directory.html"sv)); |
74 | 0 | StringBuilder builder; |
75 | 0 | SourceGenerator generator { builder }; |
76 | 0 | generator.set("path", escape_html_entities(lexical_path.string())); |
77 | 0 | generator.set("parent_url", TRY(String::formatted("file://{}", escape_html_entities(lexical_path.parent().string())))); |
78 | 0 | generator.set("contents", contents.to_byte_string()); |
79 | 0 | generator.append(template_file->data()); |
80 | 0 | return TRY(String::from_utf8(generator.as_string_view())); |
81 | 0 | } |
82 | | |
83 | | ErrorOr<String> load_about_version_page() |
84 | 0 | { |
85 | | // Generate HTML about version page from template file |
86 | | // FIXME: Use an actual templating engine (our own one when it's built, preferably with a way to check these usages at compile time) |
87 | 0 | auto template_file = TRY(Core::Resource::load_from_uri("resource://ladybird/templates/version.html"sv)); |
88 | 0 | StringBuilder builder; |
89 | 0 | SourceGenerator generator { builder }; |
90 | 0 | generator.set("browser_name", BROWSER_NAME); |
91 | 0 | generator.set("browser_version", BROWSER_VERSION); |
92 | 0 | generator.set("arch_name", CPU_STRING); |
93 | 0 | generator.set("os_name", OS_STRING); |
94 | 0 | generator.set("user_agent", default_user_agent); |
95 | 0 | generator.set("command_line", s_chrome_process_command_line); |
96 | 0 | generator.set("executable_path", s_chrome_process_executable_path); |
97 | 0 | generator.append(template_file->data()); |
98 | 0 | return TRY(String::from_utf8(generator.as_string_view())); |
99 | 0 | } |
100 | | |
101 | | } |