FrameLoader.cpp 8.3 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/ElementFactory.h>
  31. #include <LibWeb/DOM/Text.h>
  32. #include <LibWeb/Frame/Frame.h>
  33. #include <LibWeb/Loader/FrameLoader.h>
  34. #include <LibWeb/Loader/ResourceLoader.h>
  35. #include <LibWeb/Page.h>
  36. #include <LibWeb/Parser/HTMLDocumentParser.h>
  37. #include <LibWeb/Parser/HTMLParser.h>
  38. namespace Web {
  39. FrameLoader::FrameLoader(Frame& frame)
  40. : m_frame(frame)
  41. {
  42. }
  43. FrameLoader::~FrameLoader()
  44. {
  45. }
  46. static RefPtr<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 parse_html_document(markdown_document->render_to_html(), url);
  52. }
  53. static RefPtr<Document> create_text_document(const ByteBuffer& data, const URL& url)
  54. {
  55. auto document = adopt(*new 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<Document> create_image_document(const ByteBuffer& data, const URL& url)
  72. {
  73. auto document = adopt(*new 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 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<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 parse_html_document(markdown_document->render_to_html(), url);
  97. }
  98. RefPtr<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.starts_with("image/"))
  101. return create_image_document(data, url);
  102. if (mime_type == "text/plain")
  103. return create_text_document(data, url);
  104. if (mime_type == "text/markdown")
  105. return create_markdown_document(data, url);
  106. if (mime_type == "text/gemini")
  107. return create_gemini_document(data, url);
  108. if (mime_type == "text/html") {
  109. if (m_use_old_parser)
  110. return parse_html_document(data, url, encoding);
  111. HTMLDocumentParser parser(data, encoding);
  112. parser.run(url);
  113. return parser.document();
  114. }
  115. return nullptr;
  116. }
  117. bool FrameLoader::load(const URL& url)
  118. {
  119. dbg() << "FrameLoader::load: " << url;
  120. if (!url.is_valid()) {
  121. load_error_page(url, "Invalid URL");
  122. return false;
  123. }
  124. LoadRequest request;
  125. request.set_url(url);
  126. set_resource(ResourceLoader::the().load_resource(Resource::Type::Generic, request));
  127. frame().page().client().page_did_start_loading(url);
  128. if (url.protocol() != "file" && url.protocol() != "about") {
  129. URL favicon_url;
  130. favicon_url.set_protocol(url.protocol());
  131. favicon_url.set_host(url.host());
  132. favicon_url.set_port(url.port());
  133. favicon_url.set_path("/favicon.ico");
  134. ResourceLoader::the().load(
  135. favicon_url,
  136. [this, favicon_url](auto data, auto&) {
  137. dbg() << "Favicon downloaded, " << data.size() << " bytes from " << favicon_url;
  138. auto decoder = Gfx::ImageDecoder::create(data.data(), data.size());
  139. auto bitmap = decoder->bitmap();
  140. if (!bitmap) {
  141. dbg() << "Could not decode favicon " << favicon_url;
  142. return;
  143. }
  144. dbg() << "Decoded favicon, " << bitmap->size();
  145. frame().page().client().page_did_change_favicon(*bitmap);
  146. });
  147. }
  148. return true;
  149. }
  150. void FrameLoader::load_error_page(const URL& failed_url, const String& error)
  151. {
  152. auto error_page_url = "file:///res/html/error.html";
  153. ResourceLoader::the().load(
  154. error_page_url,
  155. [this, failed_url, error](auto data, auto&) {
  156. ASSERT(!data.is_null());
  157. auto html = String::format(
  158. String::copy(data).characters(),
  159. escape_html_entities(failed_url.to_string()).characters(),
  160. escape_html_entities(error).characters());
  161. auto document = parse_html_document(html, failed_url);
  162. ASSERT(document);
  163. frame().set_document(document);
  164. frame().page().client().page_did_change_title(document->title());
  165. },
  166. [](auto error) {
  167. dbg() << "Failed to load error page: " << error;
  168. ASSERT_NOT_REACHED();
  169. });
  170. }
  171. void FrameLoader::resource_did_load()
  172. {
  173. auto url = resource()->url();
  174. if (!resource()->has_encoded_data()) {
  175. load_error_page(url, "No data");
  176. return;
  177. }
  178. // FIXME: Also check HTTP status code before redirecting
  179. auto location = resource()->response_headers().get("Location");
  180. if (location.has_value()) {
  181. load(location.value());
  182. return;
  183. }
  184. dbg() << "I believe this content has MIME type '" << resource()->mime_type() << "', encoding '" << resource()->encoding() << "'";
  185. auto document = create_document_from_mime_type(resource()->encoded_data(), url, resource()->mime_type(), resource()->encoding());
  186. if (!document) {
  187. load_error_page(url, "Failed to parse content.");
  188. return;
  189. }
  190. frame().set_document(document);
  191. frame().page().client().page_did_change_title(document->title());
  192. if (!url.fragment().is_empty())
  193. frame().scroll_to_anchor(url.fragment());
  194. }
  195. void FrameLoader::resource_did_fail()
  196. {
  197. load_error_page(resource()->url(), resource()->error());
  198. }
  199. }