1 | /* |
2 | * Copyright (C) 2017 Igalia S.L. |
3 | * |
4 | * This library is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU Library General Public |
6 | * License as published by the Free Software Foundation; either |
7 | * version 2 of the License, or (at your option) any later version. |
8 | * |
9 | * This library is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | * Library General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU Library General Public License |
15 | * along with this library; see the file COPYING.LIB. If not, write to |
16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
17 | * Boston, MA 02110-1301, USA. |
18 | */ |
19 | |
20 | #include "config.h" |
21 | #include "WebKitIconLoadingClient.h" |
22 | |
23 | #include "APIIconLoadingClient.h" |
24 | #include "WebKitWebViewPrivate.h" |
25 | |
26 | using namespace WebKit; |
27 | |
28 | class IconLoadingClient : public API::IconLoadingClient { |
29 | public: |
30 | explicit IconLoadingClient(WebKitWebView* webView) |
31 | : m_webView(webView) |
32 | { |
33 | } |
34 | |
35 | private: |
36 | void getLoadDecisionForIcon(const WebCore::LinkIcon& icon, CompletionHandler<void(Function<void(API::Data*, CallbackBase::Error)>&&)>&& completionHandler) override |
37 | { |
38 | // WebCore can send non HTTP icons. |
39 | if (!icon.url.protocolIsInHTTPFamily()) { |
40 | completionHandler(nullptr); |
41 | return; |
42 | } |
43 | |
44 | WebCore::LinkIcon copiedIcon = icon; |
45 | webkitWebViewGetLoadDecisionForIcon(m_webView, icon, [protectedWebView = GRefPtr<WebKitWebView>(m_webView), icon = WTFMove(copiedIcon), completionHandler = WTFMove(completionHandler)] (bool loadIcon) mutable { |
46 | if (!loadIcon) { |
47 | completionHandler(nullptr); |
48 | return; |
49 | } |
50 | |
51 | completionHandler([protectedWebView = WTFMove(protectedWebView), icon = WTFMove(icon)] (API::Data* iconData, CallbackBase::Error error) { |
52 | if (error != CallbackBase::Error::None || !iconData) |
53 | return; |
54 | webkitWebViewSetIcon(protectedWebView.get(), icon, *iconData); |
55 | }); |
56 | }); |
57 | } |
58 | |
59 | WebKitWebView* m_webView; |
60 | }; |
61 | |
62 | void attachIconLoadingClientToView(WebKitWebView* webView) |
63 | { |
64 | webkitWebViewGetPage(webView).setIconLoadingClient(std::make_unique<IconLoadingClient>(webView)); |
65 | } |
66 | |