1/*
2 * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies)
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 "WebHitTestResultData.h"
22
23#include "WebCoreArgumentCoders.h"
24#include <WebCore/Document.h>
25#include <WebCore/Frame.h>
26#include <WebCore/FrameView.h>
27#include <WebCore/HitTestResult.h>
28#include <WebCore/Node.h>
29#include <WebCore/RenderObject.h>
30#include <WebCore/SharedBuffer.h>
31#include <wtf/URL.h>
32#include <wtf/text/WTFString.h>
33
34namespace WebKit {
35using namespace WebCore;
36
37WebHitTestResultData::WebHitTestResultData()
38{
39}
40
41WebHitTestResultData::WebHitTestResultData(const WebCore::HitTestResult& hitTestResult)
42 : absoluteImageURL(hitTestResult.absoluteImageURL().string())
43 , absolutePDFURL(hitTestResult.absolutePDFURL().string())
44 , absoluteLinkURL(hitTestResult.absoluteLinkURL().string())
45 , absoluteMediaURL(hitTestResult.absoluteMediaURL().string())
46 , linkLabel(hitTestResult.textContent())
47 , linkTitle(hitTestResult.titleDisplayString())
48 , linkSuggestedFilename(hitTestResult.linkSuggestedFilename())
49 , isContentEditable(hitTestResult.isContentEditable())
50 , elementBoundingBox(elementBoundingBoxInWindowCoordinates(hitTestResult))
51 , isScrollbar(hitTestResult.scrollbar())
52 , isSelected(hitTestResult.isSelected())
53 , isTextNode(hitTestResult.innerNode() && hitTestResult.innerNode()->isTextNode())
54 , isOverTextInsideFormControlElement(hitTestResult.isOverTextInsideFormControlElement())
55 , isDownloadableMedia(hitTestResult.isDownloadableMedia())
56 , imageSize(0)
57{
58}
59
60WebHitTestResultData::WebHitTestResultData(const WebCore::HitTestResult& hitTestResult, bool includeImage)
61 : absoluteImageURL(hitTestResult.absoluteImageURL().string())
62 , absolutePDFURL(hitTestResult.absolutePDFURL().string())
63 , absoluteLinkURL(hitTestResult.absoluteLinkURL().string())
64 , absoluteMediaURL(hitTestResult.absoluteMediaURL().string())
65 , linkLabel(hitTestResult.textContent())
66 , linkTitle(hitTestResult.titleDisplayString())
67 , linkSuggestedFilename(hitTestResult.linkSuggestedFilename())
68 , isContentEditable(hitTestResult.isContentEditable())
69 , elementBoundingBox(elementBoundingBoxInWindowCoordinates(hitTestResult))
70 , isScrollbar(hitTestResult.scrollbar())
71 , isSelected(hitTestResult.isSelected())
72 , isTextNode(hitTestResult.innerNode() && hitTestResult.innerNode()->isTextNode())
73 , isOverTextInsideFormControlElement(hitTestResult.isOverTextInsideFormControlElement())
74 , isDownloadableMedia(hitTestResult.isDownloadableMedia())
75 , imageSize(0)
76{
77 if (!includeImage)
78 return;
79
80 if (Image* image = hitTestResult.image()) {
81 RefPtr<SharedBuffer> buffer = image->data();
82 if (buffer) {
83 imageSharedMemory = WebKit::SharedMemory::allocate(buffer->size());
84 memcpy(imageSharedMemory->data(), buffer->data(), buffer->size());
85 imageSize = buffer->size();
86 }
87 }
88}
89
90WebHitTestResultData::~WebHitTestResultData()
91{
92}
93
94void WebHitTestResultData::encode(IPC::Encoder& encoder) const
95{
96 encoder << absoluteImageURL;
97 encoder << absolutePDFURL;
98 encoder << absoluteLinkURL;
99 encoder << absoluteMediaURL;
100 encoder << linkLabel;
101 encoder << linkTitle;
102 encoder << linkSuggestedFilename;
103 encoder << isContentEditable;
104 encoder << elementBoundingBox;
105 encoder << isScrollbar;
106 encoder << isSelected;
107 encoder << isTextNode;
108 encoder << isOverTextInsideFormControlElement;
109 encoder << isDownloadableMedia;
110 encoder << lookupText;
111 encoder << dictionaryPopupInfo;
112
113 WebKit::SharedMemory::Handle imageHandle;
114 if (imageSharedMemory && imageSharedMemory->data())
115 imageSharedMemory->createHandle(imageHandle, WebKit::SharedMemory::Protection::ReadOnly);
116 encoder << imageHandle;
117 encoder << imageSize;
118
119 bool hasLinkTextIndicator = linkTextIndicator;
120 encoder << hasLinkTextIndicator;
121 if (hasLinkTextIndicator)
122 encoder << linkTextIndicator->data();
123
124 platformEncode(encoder);
125}
126
127bool WebHitTestResultData::decode(IPC::Decoder& decoder, WebHitTestResultData& hitTestResultData)
128{
129 if (!decoder.decode(hitTestResultData.absoluteImageURL)
130 || !decoder.decode(hitTestResultData.absolutePDFURL)
131 || !decoder.decode(hitTestResultData.absoluteLinkURL)
132 || !decoder.decode(hitTestResultData.absoluteMediaURL)
133 || !decoder.decode(hitTestResultData.linkLabel)
134 || !decoder.decode(hitTestResultData.linkTitle)
135 || !decoder.decode(hitTestResultData.linkSuggestedFilename)
136 || !decoder.decode(hitTestResultData.isContentEditable)
137 || !decoder.decode(hitTestResultData.elementBoundingBox)
138 || !decoder.decode(hitTestResultData.isScrollbar)
139 || !decoder.decode(hitTestResultData.isSelected)
140 || !decoder.decode(hitTestResultData.isTextNode)
141 || !decoder.decode(hitTestResultData.isOverTextInsideFormControlElement)
142 || !decoder.decode(hitTestResultData.isDownloadableMedia)
143 || !decoder.decode(hitTestResultData.lookupText)
144 || !decoder.decode(hitTestResultData.dictionaryPopupInfo))
145 return false;
146
147 WebKit::SharedMemory::Handle imageHandle;
148 if (!decoder.decode(imageHandle))
149 return false;
150
151 if (!imageHandle.isNull())
152 hitTestResultData.imageSharedMemory = WebKit::SharedMemory::map(imageHandle, WebKit::SharedMemory::Protection::ReadOnly);
153
154 if (!decoder.decode(hitTestResultData.imageSize))
155 return false;
156
157 bool hasLinkTextIndicator;
158 if (!decoder.decode(hasLinkTextIndicator))
159 return false;
160
161 if (hasLinkTextIndicator) {
162 Optional<WebCore::TextIndicatorData> indicatorData;
163 decoder >> indicatorData;
164 if (!indicatorData)
165 return false;
166
167 hitTestResultData.linkTextIndicator = WebCore::TextIndicator::create(*indicatorData);
168 }
169
170 return platformDecode(decoder, hitTestResultData);
171}
172
173#if !PLATFORM(MAC)
174void WebHitTestResultData::platformEncode(IPC::Encoder& encoder) const
175{
176}
177
178bool WebHitTestResultData::platformDecode(IPC::Decoder& decoder, WebHitTestResultData& hitTestResultData)
179{
180 return true;
181}
182#endif // !PLATFORM(MAC)
183
184IntRect WebHitTestResultData::elementBoundingBoxInWindowCoordinates(const WebCore::HitTestResult& hitTestResult)
185{
186 Node* node = hitTestResult.innerNonSharedNode();
187 if (!node)
188 return IntRect();
189
190 Frame* frame = node->document().frame();
191 if (!frame)
192 return IntRect();
193
194 FrameView* view = frame->view();
195 if (!view)
196 return IntRect();
197
198 RenderObject* renderer = node->renderer();
199 if (!renderer)
200 return IntRect();
201
202 return view->contentsToWindow(renderer->absoluteBoundingBoxRect());
203}
204
205} // WebKit
206