GeneratedPagesLoader.cpp 4.2 KB

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