ResourceLoader.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (c) 2018-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/SharedBuffer.h>
  27. #include <LibCore/File.h>
  28. #include <LibProtocol/Client.h>
  29. #include <LibProtocol/Download.h>
  30. #include <LibWeb/ResourceLoader.h>
  31. namespace Web {
  32. ResourceLoader& ResourceLoader::the()
  33. {
  34. static ResourceLoader* s_the;
  35. if (!s_the)
  36. s_the = &ResourceLoader::construct().leak_ref();
  37. return *s_the;
  38. }
  39. ResourceLoader::ResourceLoader()
  40. : m_protocol_client(Protocol::Client::construct())
  41. {
  42. }
  43. void ResourceLoader::load(const URL& url, Function<void(const ByteBuffer&)> success_callback, Function<void(const String&)> error_callback)
  44. {
  45. if (url.protocol() == "file") {
  46. auto f = Core::File::construct();
  47. f->set_filename(url.path());
  48. if (!f->open(Core::IODevice::OpenMode::ReadOnly)) {
  49. dbg() << "ResourceLoader::load: Error: " << f->error_string();
  50. if (error_callback)
  51. error_callback(f->error_string());
  52. return;
  53. }
  54. auto data = f->read_all();
  55. deferred_invoke([data = move(data), success_callback = move(success_callback)](auto&) {
  56. success_callback(data);
  57. });
  58. return;
  59. }
  60. if (url.protocol() == "http") {
  61. auto download = protocol_client().start_download(url.to_string());
  62. download->on_finish = [this, success_callback = move(success_callback), error_callback = move(error_callback)](bool success, const ByteBuffer& payload, auto) {
  63. --m_pending_loads;
  64. if (on_load_counter_change)
  65. on_load_counter_change();
  66. if (!success) {
  67. if (error_callback)
  68. error_callback("HTTP load failed");
  69. return;
  70. }
  71. success_callback(ByteBuffer::copy(payload.data(), payload.size()));
  72. };
  73. ++m_pending_loads;
  74. if (on_load_counter_change)
  75. on_load_counter_change();
  76. return;
  77. }
  78. if (error_callback)
  79. error_callback(String::format("Protocol not implemented: %s", url.protocol().characters()));
  80. }
  81. }