1 /*
2 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "config.h"
27 #include "WebContextMenuClient.h"
28
29 #include "WebElementPropertyBag.h"
30 #include "WebLocalizableStrings.h"
31 #include "WebView.h"
32
33 #pragma warning(push, 0)
34 #include <WebCore/ContextMenu.h>
35 #include <WebCore/Event.h>
36 #include <WebCore/FrameLoader.h>
37 #include <WebCore/FrameLoadRequest.h>
38 #include <WebCore/Page.h>
39 #include <WebCore/ResourceRequest.h>
40 #include <WebCore/NotImplemented.h>
41 #pragma warning(pop)
42
43 #include <tchar.h>
44
45 using namespace WebCore;
46
WebContextMenuClient(WebView * webView)47 WebContextMenuClient::WebContextMenuClient(WebView* webView)
48 : m_webView(webView)
49 {
50 }
51
contextMenuDestroyed()52 void WebContextMenuClient::contextMenuDestroyed()
53 {
54 delete this;
55 }
56
isPreInspectElementTagSafari(IWebUIDelegate * uiDelegate)57 static bool isPreInspectElementTagSafari(IWebUIDelegate* uiDelegate)
58 {
59 if (!uiDelegate)
60 return false;
61
62 TCHAR modulePath[MAX_PATH];
63 DWORD length = ::GetModuleFileName(0, modulePath, _countof(modulePath));
64 if (!length)
65 return false;
66
67 return String(modulePath, length).endsWith("Safari.exe", false);
68 }
69
fixMenuReceivedFromOldSafari(IWebUIDelegate * uiDelegate,ContextMenu * originalMenu,HMENU menuFromClient)70 static HMENU fixMenuReceivedFromOldSafari(IWebUIDelegate* uiDelegate, ContextMenu* originalMenu, HMENU menuFromClient)
71 {
72 ASSERT_ARG(originalMenu, originalMenu);
73 if (!isPreInspectElementTagSafari(uiDelegate))
74 return menuFromClient;
75
76 int count = ::GetMenuItemCount(originalMenu->platformDescription());
77 if (count < 1)
78 return menuFromClient;
79
80 if (::GetMenuItemID(originalMenu->platformDescription(), count - 1) != WebMenuItemTagInspectElement)
81 return menuFromClient;
82
83 count = ::GetMenuItemCount(menuFromClient);
84 if (count < 1)
85 return menuFromClient;
86
87 if (::GetMenuItemID(menuFromClient, count - 1) == WebMenuItemTagInspectElement)
88 return menuFromClient;
89
90 originalMenu->setPlatformDescription(menuFromClient);
91 originalMenu->addInspectElementItem();
92 return originalMenu->platformDescription();
93 }
94
getCustomMenuFromDefaultItems(ContextMenu * menu)95 HMENU WebContextMenuClient::getCustomMenuFromDefaultItems(ContextMenu* menu)
96 {
97 COMPtr<IWebUIDelegate> uiDelegate;
98 if (FAILED(m_webView->uiDelegate(&uiDelegate)))
99 return menu->platformDescription();
100
101 ASSERT(uiDelegate);
102
103 HMENU newMenu = 0;
104 COMPtr<WebElementPropertyBag> propertyBag;
105 propertyBag.adoptRef(WebElementPropertyBag::createInstance(menu->hitTestResult()));
106 // FIXME: We need to decide whether to do the default before calling this delegate method
107 if (FAILED(uiDelegate->contextMenuItemsForElement(m_webView, propertyBag.get(), (OLE_HANDLE)(ULONG64)menu->platformDescription(), (OLE_HANDLE*)&newMenu)))
108 return menu->platformDescription();
109 return fixMenuReceivedFromOldSafari(uiDelegate.get(), menu, newMenu);
110 }
111
contextMenuItemSelected(ContextMenuItem * item,const ContextMenu * parentMenu)112 void WebContextMenuClient::contextMenuItemSelected(ContextMenuItem* item, const ContextMenu* parentMenu)
113 {
114 ASSERT(item->type() == ActionType || item->type() == CheckableActionType);
115
116 COMPtr<IWebUIDelegate> uiDelegate;
117 if (FAILED(m_webView->uiDelegate(&uiDelegate)))
118 return;
119
120 ASSERT(uiDelegate);
121
122 COMPtr<WebElementPropertyBag> propertyBag;
123 propertyBag.adoptRef(WebElementPropertyBag::createInstance(parentMenu->hitTestResult()));
124
125 uiDelegate->contextMenuItemSelected(m_webView, item->releasePlatformDescription(), propertyBag.get());
126 }
127
downloadURL(const KURL & url)128 void WebContextMenuClient::downloadURL(const KURL& url)
129 {
130 m_webView->downloadURL(url);
131 }
132
searchWithGoogle(const Frame * frame)133 void WebContextMenuClient::searchWithGoogle(const Frame* frame)
134 {
135 String searchString = frame->selectedText();
136 searchString.stripWhiteSpace();
137 String encoded = encodeWithURLEscapeSequences(searchString);
138 encoded.replace("%20", "+");
139
140 String url("http://www.google.com/search?q=");
141 url.append(encoded);
142 url.append("&ie=UTF-8&oe=UTF-8");
143
144 ResourceRequest request = ResourceRequest(url);
145 if (Page* page = frame->page())
146 page->mainFrame()->loader()->urlSelected(request, String(), 0, false, false, true);
147 }
148
lookUpInDictionary(Frame *)149 void WebContextMenuClient::lookUpInDictionary(Frame*)
150 {
151 notImplemented();
152 }
153
speak(const String &)154 void WebContextMenuClient::speak(const String&)
155 {
156 notImplemented();
157 }
158
stopSpeaking()159 void WebContextMenuClient::stopSpeaking()
160 {
161 notImplemented();
162 }
163
isSpeaking()164 bool WebContextMenuClient::isSpeaking()
165 {
166 notImplemented();
167 return false;
168 }
169