FrameLoader.cpp 8.5 KB

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