Merge "[Release] Webkit2-efl-123997_0.11.75" into tizen_2.2
[framework/web/webkit-efl.git] / Source / WebKit2 / WebProcess / WebPage / WebContextMenu.cpp
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 "InjectedBundleHitTestResult.h"
29 #include "InjectedBundleUserMessageCoders.h"
30 #include "WebCoreArgumentCoders.h"
31 #include "WebHitTestResult.h"
32 #include "WebPage.h"
33 #include "WebPageProxyMessages.h"
34 #include "WebProcess.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
41 using namespace WebCore;
42
43 namespace WebKit {
44
45 WebContextMenu::WebContextMenu(WebPage* page)
46     : m_page(page)
47 {
48 }
49
50 WebContextMenu::~WebContextMenu()
51 {
52 }
53
54 void WebContextMenu::show()
55 {
56 #if ENABLE(CONTEXT_MENUS)
57     ContextMenuController* controller = m_page->corePage()->contextMenuController();
58     if (!controller)
59         return;
60     ContextMenu* menu = controller->contextMenu();
61     if (!menu)
62         return;
63     Node* node = controller->hitTestResult().innerNonSharedNode();
64     if (!node)
65         return;
66     Frame* frame = node->document()->frame();
67     if (!frame)
68         return;
69     FrameView* view = frame->view();
70     if (!view)
71         return;
72
73     // Give the bundle client a chance to process the menu.
74 #if USE(CROSS_PLATFORM_CONTEXT_MENUS)
75     const Vector<ContextMenuItem>& coreItems = menu->items();
76 #else
77     Vector<ContextMenuItem> coreItems = contextMenuItemVector(menu->platformDescription());
78 #endif
79     Vector<WebContextMenuItemData> proposedMenu = kitItems(coreItems, menu);
80     Vector<WebContextMenuItemData> newMenu;
81     RefPtr<APIObject> userData;
82     RefPtr<InjectedBundleHitTestResult> hitTestResult = InjectedBundleHitTestResult::create(controller->hitTestResult());
83     if (m_page->injectedBundleContextMenuClient().getCustomMenuFromDefaultItems(m_page, hitTestResult.get(), proposedMenu, newMenu, userData))
84         proposedMenu = newMenu;
85
86     WebHitTestResult::Data webHitTestResultData(controller->hitTestResult());
87
88     // Mark the WebPage has having a shown context menu then notify the UIProcess.
89     m_page->contextMenuShowing();
90     m_page->send(Messages::WebPageProxy::ShowContextMenu(view->contentsToWindow(controller->hitTestResult().roundedPoint()), webHitTestResultData, proposedMenu, InjectedBundleUserMessageEncoder(userData.get())));
91 #endif
92 }
93
94 void WebContextMenu::itemSelected(const WebContextMenuItemData& item)
95 {
96 #if !PLATFORM(EFL) || ENABLE(TIZEN_CONTEXT_MENU_WEBKIT_2)
97     ContextMenuItem coreItem(ActionType, static_cast<ContextMenuAction>(item.action()), item.title());
98     m_page->corePage()->contextMenuController()->contextMenuItemSelected(&coreItem);
99 #endif
100 }
101
102 #if ENABLE(TIZEN_CONTEXT_CLICK)
103 Vector<WebContextMenuItemData> WebContextMenu::items() const
104 {
105     Vector<WebContextMenuItemData> proposedMenu;
106 #if ENABLE(CONTEXT_MENUS)
107     ContextMenuController* controller = m_page->corePage()->contextMenuController();
108     if (!controller)
109         return proposedMenu;
110     ContextMenu* menu = controller->contextMenu();
111     if (!menu)
112         return proposedMenu;
113
114     // Give the bundle client a chance to process the menu.
115 #if USE(CROSS_PLATFORM_CONTEXT_MENUS)
116     const Vector<ContextMenuItem>& coreItems = menu->items();
117 #else
118     Vector<ContextMenuItem> coreItems = contextMenuItemVector(menu->platformDescription());
119 #endif
120     proposedMenu = kitItems(coreItems, menu);
121     Vector<WebContextMenuItemData> newMenu;
122     RefPtr<APIObject> userData;
123     RefPtr<InjectedBundleHitTestResult> hitTestResult = InjectedBundleHitTestResult::create(controller->hitTestResult());
124     if (m_page->injectedBundleContextMenuClient().getCustomMenuFromDefaultItems(m_page, hitTestResult.get(), proposedMenu, newMenu, userData))
125         proposedMenu = newMenu;
126 #endif
127     return proposedMenu;
128 }
129 #endif
130
131 } // namespace WebKit
132
133 #endif // ENABLE(CONTEXT_MENUS)