Serenity Operating System
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
27#include <AK/SharedBuffer.h>
28#include <LibCore/EventLoop.h>
29#include <LibCore/File.h>
30#include <LibProtocol/Client.h>
31#include <LibProtocol/Download.h>
32#include <LibWeb/ResourceLoader.h>
33
34namespace Web {
35
36ResourceLoader& ResourceLoader::the()
37{
38 static ResourceLoader* s_the;
39 if (!s_the)
40 s_the = &ResourceLoader::construct().leak_ref();
41 return *s_the;
42}
43
44ResourceLoader::ResourceLoader()
45 : m_protocol_client(Protocol::Client::construct())
46{
47}
48
49void ResourceLoader::load_sync(const URL& url, Function<void(const ByteBuffer&)> success_callback, Function<void(const String&)> error_callback)
50{
51 Core::EventLoop loop;
52
53 load(
54 url,
55 [&](auto& data) {
56 success_callback(data);
57 loop.quit(0);
58 },
59 [&](auto& string) {
60 if (error_callback)
61 error_callback(string);
62 loop.quit(0);
63 });
64
65 loop.exec();
66}
67
68void ResourceLoader::load(const URL& url, Function<void(const ByteBuffer&)> success_callback, Function<void(const String&)> error_callback)
69{
70 if (url.protocol() == "file") {
71 auto f = Core::File::construct();
72 f->set_filename(url.path());
73 if (!f->open(Core::IODevice::OpenMode::ReadOnly)) {
74 dbg() << "ResourceLoader::load: Error: " << f->error_string();
75 if (error_callback)
76 error_callback(f->error_string());
77 return;
78 }
79
80 auto data = f->read_all();
81 deferred_invoke([data = move(data), success_callback = move(success_callback)](auto&) {
82 success_callback(data);
83 });
84 return;
85 }
86
87 if (url.protocol() == "http") {
88 auto download = protocol_client().start_download(url.to_string());
89 if (!download) {
90 if (error_callback)
91 error_callback("Failed to initiate load");
92 return;
93 }
94 download->on_finish = [this, success_callback = move(success_callback), error_callback = move(error_callback)](bool success, const ByteBuffer& payload, auto) {
95 --m_pending_loads;
96 if (on_load_counter_change)
97 on_load_counter_change();
98 if (!success) {
99 if (error_callback)
100 error_callback("HTTP load failed");
101 return;
102 }
103 success_callback(ByteBuffer::copy(payload.data(), payload.size()));
104 };
105 ++m_pending_loads;
106 if (on_load_counter_change)
107 on_load_counter_change();
108 return;
109 }
110
111 if (error_callback)
112 error_callback(String::format("Protocol not implemented: %s", url.protocol().characters()));
113}
114
115}