1/*
2 * Copyright (C) 2012 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 "WebKitContextMenuClient.h"
22
23#include "APIContextMenuClient.h"
24#include "WebContextMenuItem.h"
25#include "WebKitWebViewPrivate.h"
26
27using namespace WebKit;
28
29class ContextMenuClient final: public API::ContextMenuClient {
30public:
31 explicit ContextMenuClient(WebKitWebView* webView)
32 : m_webView(webView)
33 {
34 }
35
36private:
37 void getContextMenuFromProposedMenu(WebPageProxy&, Vector<Ref<WebKit::WebContextMenuItem>>&& proposedMenu, WebKit::WebContextMenuListenerProxy& contextMenuListener, const WebHitTestResultData& hitTestResultData, API::Object* userData) override
38 {
39 GRefPtr<GVariant> variant;
40 if (userData) {
41 ASSERT(userData->type() == API::Object::Type::String);
42 CString userDataString = static_cast<API::String*>(userData)->string().utf8();
43 variant = adoptGRef(g_variant_parse(nullptr, userDataString.data(), userDataString.data() + userDataString.length(), nullptr, nullptr));
44 }
45
46 Vector<WebContextMenuItemData> menuItems;
47 menuItems.reserveInitialCapacity(proposedMenu.size());
48 for (auto& item : proposedMenu)
49 menuItems.uncheckedAppend(item->data());
50 webkitWebViewPopulateContextMenu(m_webView, menuItems, hitTestResultData, variant.get());
51 contextMenuListener.useContextMenuItems({ });
52 }
53
54 WebKitWebView* m_webView;
55};
56
57void attachContextMenuClientToView(WebKitWebView* webView)
58{
59 webkitWebViewGetPage(webView).setContextMenuClient(std::make_unique<ContextMenuClient>(webView));
60}
61
62