1/*
2 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
3 * Copyright (C) 2010 Apple Inc. All rights reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB.  If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 *
20 */
21
22#include "config.h"
23
24#if ENABLE(CONTEXT_MENUS)
25
26#include "WebContextMenu.h"
27
28#include "ContextMenuContextData.h"
29#include "InjectedBundleHitTestResult.h"
30#include "InjectedBundleUserMessageCoders.h"
31#include "WebCoreArgumentCoders.h"
32#include "WebHitTestResult.h"
33#include "WebPage.h"
34#include "WebPageProxyMessages.h"
35#include <WebCore/ContextMenu.h>
36#include <WebCore/ContextMenuController.h>
37#include <WebCore/Frame.h>
38#include <WebCore/FrameView.h>
39#include <WebCore/Page.h>
40
41using namespace WebCore;
42
43namespace WebKit {
44
45WebContextMenu::WebContextMenu(WebPage* page)
46    : m_page(page)
47{
48}
49
50WebContextMenu::~WebContextMenu()
51{
52}
53
54void WebContextMenu::show()
55{
56    ContextMenuController& controller = m_page->corePage()->contextMenuController();
57    Frame* frame = controller.hitTestResult().innerNodeFrame();
58    if (!frame)
59        return;
60    FrameView* view = frame->view();
61    if (!view)
62        return;
63
64    Vector<WebContextMenuItemData> menuItems;
65    RefPtr<API::Object> userData;
66    menuItemsWithUserData(menuItems, userData);
67    ContextMenuContextData contextMenuContextData(controller.context());
68
69    // Mark the WebPage has having a shown context menu then notify the UIProcess.
70    m_page->contextMenuShowing();
71    m_page->send(Messages::WebPageProxy::ShowContextMenu(view->contentsToWindow(controller.hitTestResult().roundedPointInInnerNodeFrame()), contextMenuContextData, menuItems, InjectedBundleUserMessageEncoder(userData.get())));
72}
73
74void WebContextMenu::itemSelected(const WebContextMenuItemData& item)
75{
76    ContextMenuItem coreItem(ActionType, static_cast<ContextMenuAction>(item.action()), item.title());
77    m_page->corePage()->contextMenuController().contextMenuItemSelected(&coreItem);
78}
79
80void WebContextMenu::menuItemsWithUserData(Vector<WebContextMenuItemData> &menuItems, RefPtr<API::Object>& userData) const
81{
82    ContextMenuController& controller = m_page->corePage()->contextMenuController();
83
84    ContextMenu* menu = controller.contextMenu();
85    if (!menu)
86        return;
87
88    // Give the bundle client a chance to process the menu.
89#if USE(CROSS_PLATFORM_CONTEXT_MENUS)
90    const Vector<ContextMenuItem>& coreItems = menu->items();
91#else
92    Vector<ContextMenuItem> coreItems = contextMenuItemVector(menu->platformDescription());
93#endif
94
95    Vector<WebContextMenuItemData> proposedMenu = kitItems(coreItems);
96    Vector<WebContextMenuItemData> newMenu;
97    RefPtr<InjectedBundleHitTestResult> hitTestResult = InjectedBundleHitTestResult::create(controller.hitTestResult());
98    if (m_page->injectedBundleContextMenuClient().getCustomMenuFromDefaultItems(m_page, hitTestResult.get(), proposedMenu, newMenu, userData))
99        proposedMenu = newMenu;
100    menuItems = proposedMenu;
101}
102
103Vector<WebContextMenuItemData> WebContextMenu::items() const
104{
105    Vector<WebContextMenuItemData> menuItems;
106    RefPtr<API::Object> userData;
107    menuItemsWithUserData(menuItems, userData);
108    return menuItems;
109}
110
111} // namespace WebKit
112
113#endif // ENABLE(CONTEXT_MENUS)
114