FrameLoader.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Debug.h>
  27. #include <AK/LexicalPath.h>
  28. #include <LibGemini/Document.h>
  29. #include <LibGfx/ImageDecoder.h>
  30. #include <LibMarkdown/Document.h>
  31. #include <LibWeb/DOM/Document.h>
  32. #include <LibWeb/DOM/ElementFactory.h>
  33. #include <LibWeb/DOM/Text.h>
  34. #include <LibWeb/HTML/HTMLIFrameElement.h>
  35. #include <LibWeb/HTML/Parser/HTMLDocumentParser.h>
  36. #include <LibWeb/Loader/FrameLoader.h>
  37. #include <LibWeb/Loader/ResourceLoader.h>
  38. #include <LibWeb/Namespace.h>
  39. #include <LibWeb/Page/Frame.h>
  40. #include <LibWeb/Page/Page.h>
  41. namespace Web {
  42. FrameLoader::FrameLoader(Frame& frame)
  43. : m_frame(frame)
  44. {
  45. }
  46. FrameLoader::~FrameLoader()
  47. {
  48. }
  49. static bool build_markdown_document(DOM::Document& document, const ByteBuffer& data)
  50. {
  51. auto markdown_document = Markdown::Document::parse(data);
  52. if (!markdown_document)
  53. return false;
  54. HTML::HTMLDocumentParser parser(document, markdown_document->render_to_html(), "utf-8");
  55. parser.run(document.url());
  56. return true;
  57. }
  58. static bool build_text_document(DOM::Document& document, const ByteBuffer& data)
  59. {
  60. auto html_element = document.create_element("html");
  61. document.append_child(html_element);
  62. auto head_element = document.create_element("head");
  63. html_element->append_child(head_element);
  64. auto title_element = document.create_element("title");
  65. head_element->append_child(title_element);
  66. auto title_text = document.create_text_node(document.url().basename());
  67. title_element->append_child(title_text);
  68. auto body_element = document.create_element("body");
  69. html_element->append_child(body_element);
  70. auto pre_element = document.create_element("pre");
  71. body_element->append_child(pre_element);
  72. pre_element->append_child(document.create_text_node(String::copy(data)));
  73. return true;
  74. }
  75. static bool build_image_document(DOM::Document& document, const ByteBuffer& data)
  76. {
  77. auto image_decoder = Gfx::ImageDecoder::create(data.data(), data.size());
  78. auto bitmap = image_decoder->bitmap();
  79. if (!bitmap)
  80. return false;
  81. auto html_element = document.create_element("html");
  82. document.append_child(html_element);
  83. auto head_element = document.create_element("head");
  84. html_element->append_child(head_element);
  85. auto title_element = document.create_element("title");
  86. head_element->append_child(title_element);
  87. auto basename = LexicalPath(document.url().path()).basename();
  88. auto title_text = adopt(*new DOM::Text(document, String::formatted("{} [{}x{}]", basename, bitmap->width(), bitmap->height())));
  89. title_element->append_child(title_text);
  90. auto body_element = document.create_element("body");
  91. html_element->append_child(body_element);
  92. auto image_element = document.create_element("img");
  93. image_element->set_attribute(HTML::AttributeNames::src, document.url().to_string());
  94. body_element->append_child(image_element);
  95. return true;
  96. }
  97. static bool build_gemini_document(DOM::Document& document, const ByteBuffer& data)
  98. {
  99. StringView gemini_data { data };
  100. auto gemini_document = Gemini::Document::parse(gemini_data, document.url());
  101. String html_data = gemini_document->render_to_html();
  102. #ifdef GEMINI_DEBUG
  103. dbgln("Gemini data:\n\"\"\"{}\"\"\"", gemini_data);
  104. dbgln("Converted to HTML:\n\"\"\"{}\"\"\"", html_data);
  105. #endif
  106. HTML::HTMLDocumentParser parser(document, html_data, "utf-8");
  107. parser.run(document.url());
  108. return true;
  109. }
  110. bool FrameLoader::parse_document(DOM::Document& document, const ByteBuffer& data)
  111. {
  112. auto& mime_type = document.content_type();
  113. if (mime_type == "text/html" || mime_type == "image/svg+xml") {
  114. HTML::HTMLDocumentParser parser(document, data, document.encoding());
  115. parser.run(document.url());
  116. return true;
  117. }
  118. if (mime_type.starts_with("image/"))
  119. return build_image_document(document, data);
  120. if (mime_type == "text/plain")
  121. return build_text_document(document, data);
  122. if (mime_type == "text/markdown")
  123. return build_markdown_document(document, data);
  124. if (mime_type == "text/gemini")
  125. return build_gemini_document(document, data);
  126. return false;
  127. }
  128. bool FrameLoader::load(const LoadRequest& request, Type type)
  129. {
  130. if (!request.is_valid()) {
  131. load_error_page(request.url(), "Invalid request");
  132. return false;
  133. }
  134. auto& url = request.url();
  135. set_resource(ResourceLoader::the().load_resource(Resource::Type::Generic, request));
  136. if (type == Type::Navigation) {
  137. if (auto* page = frame().page())
  138. page->client().page_did_start_loading(url);
  139. }
  140. if (type == Type::IFrame)
  141. return true;
  142. if (url.protocol() == "http" || url.protocol() == "https") {
  143. URL favicon_url;
  144. favicon_url.set_protocol(url.protocol());
  145. favicon_url.set_host(url.host());
  146. favicon_url.set_port(url.port());
  147. favicon_url.set_path("/favicon.ico");
  148. ResourceLoader::the().load(
  149. favicon_url,
  150. [this, favicon_url](auto data, auto&) {
  151. dbgln("Favicon downloaded, {} bytes from {}", data.size(), favicon_url);
  152. auto decoder = Gfx::ImageDecoder::create(data.data(), data.size());
  153. auto bitmap = decoder->bitmap();
  154. if (!bitmap) {
  155. dbgln("Could not decode favicon {}", favicon_url);
  156. return;
  157. }
  158. dbgln("Decoded favicon, {}", bitmap->size());
  159. if (auto* page = frame().page())
  160. page->client().page_did_change_favicon(*bitmap);
  161. });
  162. }
  163. return true;
  164. }
  165. bool FrameLoader::load(const URL& url, Type type)
  166. {
  167. dbgln("FrameLoader::load: {}", url);
  168. if (!url.is_valid()) {
  169. load_error_page(url, "Invalid URL");
  170. return false;
  171. }
  172. LoadRequest request;
  173. request.set_url(url);
  174. return load(request, type);
  175. }
  176. void FrameLoader::load_html(const StringView& html, const URL& url)
  177. {
  178. auto document = DOM::Document::create(url);
  179. HTML::HTMLDocumentParser parser(document, html, "utf-8");
  180. parser.run(url);
  181. frame().set_document(&parser.document());
  182. }
  183. // FIXME: Use an actual templating engine (our own one when it's built, preferably
  184. // with a way to check these usages at compile time)
  185. void FrameLoader::load_error_page(const URL& failed_url, const String& error)
  186. {
  187. auto error_page_url = "file:///res/html/error.html";
  188. ResourceLoader::the().load(
  189. error_page_url,
  190. [this, failed_url, error](auto data, auto&) {
  191. ASSERT(!data.is_null());
  192. #pragma GCC diagnostic ignored "-Wformat-nonliteral"
  193. auto html = String::format(
  194. String::copy(data).characters(),
  195. escape_html_entities(failed_url.to_string()).characters(),
  196. escape_html_entities(error).characters());
  197. #pragma GCC diagnostic pop
  198. auto document = HTML::parse_html_document(html, failed_url, "utf-8");
  199. ASSERT(document);
  200. frame().set_document(document);
  201. },
  202. [](auto error) {
  203. dbgln("Failed to load error page: {}", error);
  204. ASSERT_NOT_REACHED();
  205. });
  206. }
  207. void FrameLoader::resource_did_load()
  208. {
  209. auto url = resource()->url();
  210. if (!resource()->has_encoded_data()) {
  211. load_error_page(url, "No data");
  212. return;
  213. }
  214. // FIXME: Also check HTTP status code before redirecting
  215. auto location = resource()->response_headers().get("Location");
  216. if (location.has_value()) {
  217. load(url.complete_url(location.value()), FrameLoader::Type::Navigation);
  218. return;
  219. }
  220. dbgln("I believe this content has MIME type '{}', , encoding '{}'", resource()->mime_type(), resource()->encoding());
  221. auto document = DOM::Document::create();
  222. document->set_url(url);
  223. document->set_encoding(resource()->encoding());
  224. document->set_content_type(resource()->mime_type());
  225. frame().set_document(document);
  226. if (!parse_document(*document, resource()->encoded_data())) {
  227. load_error_page(url, "Failed to parse content.");
  228. return;
  229. }
  230. if (!url.fragment().is_empty())
  231. frame().scroll_to_anchor(url.fragment());
  232. if (auto* host_element = frame().host_element()) {
  233. // FIXME: Perhaps in the future we'll have a better common base class for <frame> and <iframe>
  234. ASSERT(is<HTML::HTMLIFrameElement>(*host_element));
  235. downcast<HTML::HTMLIFrameElement>(*host_element).content_frame_did_load({});
  236. }
  237. if (auto* page = frame().page())
  238. page->client().page_did_finish_loading(url);
  239. }
  240. void FrameLoader::resource_did_fail()
  241. {
  242. load_error_page(resource()->url(), resource()->error());
  243. }
  244. }