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