FrameLoader.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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/LexicalPath.h>
  27. #include <LibGemini/Document.h>
  28. #include <LibGfx/ImageDecoder.h>
  29. #include <LibMarkdown/Document.h>
  30. #include <LibWeb/DOM/Document.h>
  31. #include <LibWeb/DOM/ElementFactory.h>
  32. #include <LibWeb/DOM/Text.h>
  33. #include <LibWeb/HTML/HTMLIFrameElement.h>
  34. #include <LibWeb/HTML/Parser/HTMLDocumentParser.h>
  35. #include <LibWeb/Loader/FrameLoader.h>
  36. #include <LibWeb/Loader/ResourceLoader.h>
  37. #include <LibWeb/Namespace.h>
  38. #include <LibWeb/Page/Frame.h>
  39. #include <LibWeb/Page/Page.h>
  40. //#define GEMINI_DEBUG 1
  41. namespace Web {
  42. FrameLoader::FrameLoader(Frame& frame)
  43. : m_frame(frame)
  44. {
  45. }
  46. FrameLoader::~FrameLoader()
  47. {
  48. }
  49. static RefPtr<DOM::Document> create_markdown_document(const ByteBuffer& data, const URL& url)
  50. {
  51. auto markdown_document = Markdown::Document::parse(data);
  52. if (!markdown_document)
  53. return nullptr;
  54. return HTML::parse_html_document(markdown_document->render_to_html(), url, "utf-8");
  55. }
  56. static RefPtr<DOM::Document> create_text_document(const ByteBuffer& data, const URL& url)
  57. {
  58. auto document = DOM::Document::create(url);
  59. auto html_element = document->create_element("html");
  60. document->append_child(html_element);
  61. auto head_element = document->create_element("head");
  62. html_element->append_child(head_element);
  63. auto title_element = document->create_element("title");
  64. head_element->append_child(title_element);
  65. auto title_text = document->create_text_node(url.basename());
  66. title_element->append_child(title_text);
  67. auto body_element = document->create_element("body");
  68. html_element->append_child(body_element);
  69. auto pre_element = document->create_element("pre");
  70. body_element->append_child(pre_element);
  71. pre_element->append_child(document->create_text_node(String::copy(data)));
  72. return document;
  73. }
  74. static RefPtr<DOM::Document> create_image_document(const ByteBuffer& data, const URL& url)
  75. {
  76. auto document = DOM::Document::create(url);
  77. auto image_decoder = Gfx::ImageDecoder::create(data.data(), data.size());
  78. auto bitmap = image_decoder->bitmap();
  79. ASSERT(bitmap);
  80. auto html_element = document->create_element("html");
  81. document->append_child(html_element);
  82. auto head_element = document->create_element("head");
  83. html_element->append_child(head_element);
  84. auto title_element = document->create_element("title");
  85. head_element->append_child(title_element);
  86. auto basename = LexicalPath(url.path()).basename();
  87. auto title_text = adopt(*new DOM::Text(document, String::format("%s [%dx%d]", basename.characters(), bitmap->width(), bitmap->height())));
  88. title_element->append_child(title_text);
  89. auto body_element = document->create_element("body");
  90. html_element->append_child(body_element);
  91. auto image_element = document->create_element("img");
  92. image_element->set_attribute(HTML::AttributeNames::src, url.to_string());
  93. body_element->append_child(image_element);
  94. return document;
  95. }
  96. static RefPtr<DOM::Document> create_gemini_document(const ByteBuffer& data, const URL& url)
  97. {
  98. StringView gemini_data { data };
  99. auto gemini_document = Gemini::Document::parse(gemini_data, url);
  100. String html_data = gemini_document->render_to_html();
  101. #ifdef GEMINI_DEBUG
  102. dbgln("Gemini data:\n\"\"\"{}\"\"\"", gemini_data);
  103. dbgln("Converted to HTML:\n\"\"\"{}\"\"\"", html_data);
  104. #endif
  105. return HTML::parse_html_document(move(html_data), url, "utf-8");
  106. }
  107. RefPtr<DOM::Document> FrameLoader::create_document_from_mime_type(const ByteBuffer& data, const URL& url, const String& mime_type, const String& encoding)
  108. {
  109. RefPtr<DOM::Document> document;
  110. if (mime_type == "text/html" || mime_type == "image/svg+xml") {
  111. document = HTML::parse_html_document(data, url, encoding);
  112. } else if (mime_type.starts_with("image/")) {
  113. document = create_image_document(data, url);
  114. } else if (mime_type == "text/plain") {
  115. document = create_text_document(data, url);
  116. } else if (mime_type == "text/markdown") {
  117. document = create_markdown_document(data, url);
  118. } else if (mime_type == "text/gemini") {
  119. document = create_gemini_document(data, url);
  120. }
  121. if (document)
  122. document->set_content_type(mime_type);
  123. return document;
  124. }
  125. bool FrameLoader::load(const LoadRequest& request, Type type)
  126. {
  127. if (!request.is_valid()) {
  128. load_error_page(request.url(), "Invalid request");
  129. return false;
  130. }
  131. auto& url = request.url();
  132. set_resource(ResourceLoader::the().load_resource(Resource::Type::Generic, request));
  133. if (type == Type::Navigation) {
  134. if (auto* page = frame().page())
  135. page->client().page_did_start_loading(url);
  136. }
  137. if (type == Type::IFrame)
  138. return true;
  139. if (url.protocol() == "http" || url.protocol() == "https") {
  140. URL favicon_url;
  141. favicon_url.set_protocol(url.protocol());
  142. favicon_url.set_host(url.host());
  143. favicon_url.set_port(url.port());
  144. favicon_url.set_path("/favicon.ico");
  145. ResourceLoader::the().load(
  146. favicon_url,
  147. [this, favicon_url](auto data, auto&) {
  148. dbg() << "Favicon downloaded, " << data.size() << " bytes from " << favicon_url;
  149. auto decoder = Gfx::ImageDecoder::create(data.data(), data.size());
  150. auto bitmap = decoder->bitmap();
  151. if (!bitmap) {
  152. dbg() << "Could not decode favicon " << favicon_url;
  153. return;
  154. }
  155. dbg() << "Decoded favicon, " << bitmap->size();
  156. if (auto* page = frame().page())
  157. page->client().page_did_change_favicon(*bitmap);
  158. });
  159. }
  160. return true;
  161. }
  162. bool FrameLoader::load(const URL& url, Type type)
  163. {
  164. dbg() << "FrameLoader::load: " << url;
  165. if (!url.is_valid()) {
  166. load_error_page(url, "Invalid URL");
  167. return false;
  168. }
  169. LoadRequest request;
  170. request.set_url(url);
  171. return load(request, type);
  172. }
  173. void FrameLoader::load_html(const StringView& html, const URL& url)
  174. {
  175. HTML::HTMLDocumentParser parser(html, "utf-8");
  176. parser.run(url);
  177. frame().set_document(&parser.document());
  178. }
  179. void FrameLoader::load_error_page(const URL& failed_url, const String& error)
  180. {
  181. auto error_page_url = "file:///res/html/error.html";
  182. ResourceLoader::the().load(
  183. error_page_url,
  184. [this, failed_url, error](auto data, auto&) {
  185. ASSERT(!data.is_null());
  186. auto html = String::format(
  187. String::copy(data).characters(),
  188. escape_html_entities(failed_url.to_string()).characters(),
  189. escape_html_entities(error).characters());
  190. auto document = HTML::parse_html_document(html, failed_url, "utf-8");
  191. ASSERT(document);
  192. frame().set_document(document);
  193. },
  194. [](auto error) {
  195. dbg() << "Failed to load error page: " << error;
  196. ASSERT_NOT_REACHED();
  197. });
  198. }
  199. void FrameLoader::resource_did_load()
  200. {
  201. auto url = resource()->url();
  202. if (!resource()->has_encoded_data()) {
  203. load_error_page(url, "No data");
  204. return;
  205. }
  206. // FIXME: Also check HTTP status code before redirecting
  207. auto location = resource()->response_headers().get("Location");
  208. if (location.has_value()) {
  209. load(url.complete_url(location.value()), FrameLoader::Type::Navigation);
  210. return;
  211. }
  212. dbg() << "I believe this content has MIME type '" << resource()->mime_type() << "', encoding '" << resource()->encoding() << "'";
  213. auto document = create_document_from_mime_type(resource()->encoded_data(), url, resource()->mime_type(), resource()->encoding());
  214. if (!document) {
  215. load_error_page(url, "Failed to parse content.");
  216. return;
  217. }
  218. frame().set_document(document);
  219. if (!url.fragment().is_empty())
  220. frame().scroll_to_anchor(url.fragment());
  221. if (auto* host_element = frame().host_element()) {
  222. // FIXME: Perhaps in the future we'll have a better common base class for <frame> and <iframe>
  223. ASSERT(is<HTML::HTMLIFrameElement>(*host_element));
  224. downcast<HTML::HTMLIFrameElement>(*host_element).content_frame_did_load({});
  225. }
  226. }
  227. void FrameLoader::resource_did_fail()
  228. {
  229. load_error_page(resource()->url(), resource()->error());
  230. }
  231. }