Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / page / ContextMenuController.cpp
1 /*
2  * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
3  * Copyright (C) 2010 Igalia S.L
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (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 "config.h"
28 #include "core/page/ContextMenuController.h"
29
30 #include "core/dom/Document.h"
31 #include "core/dom/Node.h"
32 #include "core/events/Event.h"
33 #include "core/events/MouseEvent.h"
34 #include "core/events/RelatedEvent.h"
35 #include "core/frame/LocalFrame.h"
36 #include "core/html/HTMLMenuElement.h"
37 #include "core/page/ContextMenuClient.h"
38 #include "core/page/ContextMenuProvider.h"
39 #include "core/page/CustomContextMenuProvider.h"
40 #include "core/page/EventHandler.h"
41 #include "platform/ContextMenu.h"
42 #include "platform/ContextMenuItem.h"
43
44 namespace blink {
45
46 using namespace HTMLNames;
47
48 ContextMenuController::ContextMenuController(Page*, ContextMenuClient* client)
49     : m_client(client)
50 {
51     ASSERT_ARG(client, client);
52 }
53
54 ContextMenuController::~ContextMenuController()
55 {
56 }
57
58 PassOwnPtrWillBeRawPtr<ContextMenuController> ContextMenuController::create(Page* page, ContextMenuClient* client)
59 {
60     return adoptPtrWillBeNoop(new ContextMenuController(page, client));
61 }
62
63 void ContextMenuController::trace(Visitor* visitor)
64 {
65     visitor->trace(m_menuProvider);
66     visitor->trace(m_hitTestResult);
67 }
68
69 void ContextMenuController::clearContextMenu()
70 {
71     m_contextMenu.clear();
72     if (m_menuProvider)
73         m_menuProvider->contextMenuCleared();
74     m_menuProvider = nullptr;
75     m_client->clearContextMenu();
76     m_hitTestResult = HitTestResult();
77 }
78
79 void ContextMenuController::documentDetached(Document* document)
80 {
81     if (Node* innerNode = m_hitTestResult.innerNode()) {
82         // Invalidate the context menu info if its target document is detached.
83         if (innerNode->document() == document)
84             clearContextMenu();
85     }
86 }
87
88 void ContextMenuController::populateCustomContextMenu(const Event& event)
89 {
90     if (!RuntimeEnabledFeatures::contextMenuEnabled())
91         return;
92
93     Node* node = event.target()->toNode();
94     if (!node || !node->isHTMLElement())
95         return;
96
97     HTMLElement& element = toHTMLElement(*node);
98     RefPtrWillBeRawPtr<HTMLMenuElement> menuElement = element.contextMenu();
99     if (!menuElement || !equalIgnoringCase(menuElement->fastGetAttribute(typeAttr), "popup"))
100         return;
101     RefPtrWillBeRawPtr<RelatedEvent> relatedEvent = RelatedEvent::create(EventTypeNames::show, true, true, node);
102     if (!menuElement->dispatchEvent(relatedEvent.release()))
103         return;
104     if (menuElement != element.contextMenu())
105         return;
106     m_menuProvider = CustomContextMenuProvider::create(*menuElement, element);
107     m_menuProvider->populateContextMenu(m_contextMenu.get());
108 }
109
110 void ContextMenuController::handleContextMenuEvent(Event* event)
111 {
112     m_contextMenu = createContextMenu(event);
113     if (!m_contextMenu)
114         return;
115     populateCustomContextMenu(*event);
116     showContextMenu(event);
117 }
118
119 void ContextMenuController::showContextMenu(Event* event, PassRefPtrWillBeRawPtr<ContextMenuProvider> menuProvider)
120 {
121     m_menuProvider = menuProvider;
122
123     m_contextMenu = createContextMenu(event);
124     if (!m_contextMenu) {
125         clearContextMenu();
126         return;
127     }
128
129     m_menuProvider->populateContextMenu(m_contextMenu.get());
130     showContextMenu(event);
131 }
132
133 void ContextMenuController::showContextMenuAtPoint(LocalFrame* frame, float x, float y, PassRefPtrWillBeRawPtr<ContextMenuProvider> menuProvider)
134 {
135     m_menuProvider = menuProvider;
136
137     LayoutPoint location(x, y);
138     m_contextMenu = createContextMenu(frame, location);
139     if (!m_contextMenu) {
140         clearContextMenu();
141         return;
142     }
143
144     m_menuProvider->populateContextMenu(m_contextMenu.get());
145     showContextMenu(nullptr);
146 }
147
148 PassOwnPtr<ContextMenu> ContextMenuController::createContextMenu(Event* event)
149 {
150     ASSERT(event);
151
152     if (!event->isMouseEvent())
153         return nullptr;
154
155     MouseEvent* mouseEvent = toMouseEvent(event);
156     return createContextMenu(event->target()->toNode()->document().frame(), mouseEvent->absoluteLocation());
157 }
158
159 PassOwnPtr<ContextMenu> ContextMenuController::createContextMenu(LocalFrame* frame, const LayoutPoint& location)
160 {
161     HitTestResult result(location);
162
163     if (frame)
164         result = frame->eventHandler().hitTestResultAtPoint(location, HitTestRequest::ReadOnly | HitTestRequest::Active);
165
166     if (!result.innerNonSharedNode())
167         return nullptr;
168
169     m_hitTestResult = result;
170
171     return adoptPtr(new ContextMenu);
172 }
173
174 void ContextMenuController::showContextMenu(Event* event)
175 {
176     m_client->showContextMenu(m_contextMenu.get());
177     if (event)
178         event->setDefaultHandled();
179 }
180
181 void ContextMenuController::contextMenuItemSelected(const ContextMenuItem* item)
182 {
183     ASSERT(item->type() == ActionType || item->type() == CheckableActionType);
184
185     if (item->action() < ContextMenuItemBaseCustomTag || item->action() > ContextMenuItemLastCustomTag)
186         return;
187
188     ASSERT(m_menuProvider);
189     m_menuProvider->contextMenuItemSelected(item);
190 }
191
192 } // namespace blink