FrameLoader.cpp 8.3 KB

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