1/*
2 * Copyright (C) 2010 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 INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "InjectedBundleHitTestResult.h"
28
29#include "InjectedBundleNodeHandle.h"
30#include "WebFrame.h"
31#include "WebFrameLoaderClient.h"
32#include "WebImage.h"
33#include <WebCore/BitmapImage.h>
34#include <WebCore/Document.h>
35#include <WebCore/Element.h>
36#include <WebCore/Frame.h>
37#include <WebCore/FrameLoader.h>
38#include <WebCore/FrameView.h>
39#include <WebCore/GraphicsContext.h>
40#include <wtf/URL.h>
41#include <wtf/text/WTFString.h>
42
43namespace WebKit {
44using namespace WebCore;
45
46Ref<InjectedBundleHitTestResult> InjectedBundleHitTestResult::create(const HitTestResult& hitTestResult)
47{
48 return adoptRef(*new InjectedBundleHitTestResult(hitTestResult));
49}
50
51RefPtr<InjectedBundleNodeHandle> InjectedBundleHitTestResult::nodeHandle() const
52{
53 return InjectedBundleNodeHandle::getOrCreate(m_hitTestResult.innerNonSharedNode());
54}
55
56RefPtr<InjectedBundleNodeHandle> InjectedBundleHitTestResult::urlElementHandle() const
57{
58 return InjectedBundleNodeHandle::getOrCreate(m_hitTestResult.URLElement());
59}
60
61WebFrame* InjectedBundleHitTestResult::frame() const
62{
63 Node* node = m_hitTestResult.innerNonSharedNode();
64 if (!node)
65 return nullptr;
66
67 Frame* frame = node->document().frame();
68 if (!frame)
69 return nullptr;
70
71 return WebFrame::fromCoreFrame(*frame);
72}
73
74WebFrame* InjectedBundleHitTestResult::targetFrame() const
75{
76 Frame* frame = m_hitTestResult.targetFrame();
77 if (!frame)
78 return nullptr;
79
80 return WebFrame::fromCoreFrame(*frame);
81}
82
83String InjectedBundleHitTestResult::absoluteImageURL() const
84{
85 return m_hitTestResult.absoluteImageURL().string();
86}
87
88String InjectedBundleHitTestResult::absolutePDFURL() const
89{
90 return m_hitTestResult.absolutePDFURL().string();
91}
92
93String InjectedBundleHitTestResult::absoluteLinkURL() const
94{
95 return m_hitTestResult.absoluteLinkURL().string();
96}
97
98String InjectedBundleHitTestResult::absoluteMediaURL() const
99{
100 return m_hitTestResult.absoluteMediaURL().string();
101}
102
103bool InjectedBundleHitTestResult::mediaIsInFullscreen() const
104{
105 return m_hitTestResult.mediaIsInFullscreen();
106}
107
108bool InjectedBundleHitTestResult::mediaHasAudio() const
109{
110 return m_hitTestResult.mediaHasAudio();
111}
112
113bool InjectedBundleHitTestResult::isDownloadableMedia() const
114{
115 return m_hitTestResult.isDownloadableMedia();
116}
117
118BundleHitTestResultMediaType InjectedBundleHitTestResult::mediaType() const
119{
120#if !ENABLE(VIDEO)
121 return BundleHitTestResultMediaTypeNone;
122#else
123 Node* node = m_hitTestResult.innerNonSharedNode();
124 if (!is<Element>(*node))
125 return BundleHitTestResultMediaTypeNone;
126
127 if (!downcast<Element>(*node).isMediaElement())
128 return BundleHitTestResultMediaTypeNone;
129
130 return m_hitTestResult.mediaIsVideo() ? BundleHitTestResultMediaTypeVideo : BundleHitTestResultMediaTypeAudio;
131#endif
132}
133
134String InjectedBundleHitTestResult::linkLabel() const
135{
136 return m_hitTestResult.textContent();
137}
138
139String InjectedBundleHitTestResult::linkTitle() const
140{
141 return m_hitTestResult.titleDisplayString();
142}
143
144String InjectedBundleHitTestResult::linkSuggestedFilename() const
145{
146 return m_hitTestResult.linkSuggestedFilename();
147}
148
149IntRect InjectedBundleHitTestResult::imageRect() const
150{
151 IntRect imageRect = m_hitTestResult.imageRect();
152 if (imageRect.isEmpty())
153 return imageRect;
154
155 // The image rect in HitTestResult is in frame coordinates, but we need it in WKView
156 // coordinates since WebKit2 clients don't have enough context to do the conversion themselves.
157 WebFrame* webFrame = frame();
158 if (!webFrame)
159 return imageRect;
160
161 Frame* coreFrame = webFrame->coreFrame();
162 if (!coreFrame)
163 return imageRect;
164
165 FrameView* view = coreFrame->view();
166 if (!view)
167 return imageRect;
168
169 return view->contentsToRootView(imageRect);
170}
171
172RefPtr<WebImage> InjectedBundleHitTestResult::image() const
173{
174 Image* image = m_hitTestResult.image();
175 // For now, we only handle bitmap images.
176 if (!is<BitmapImage>(image))
177 return nullptr;
178
179 BitmapImage& bitmapImage = downcast<BitmapImage>(*image);
180 IntSize size(bitmapImage.size());
181 auto webImage = WebImage::create(size, static_cast<ImageOptions>(0));
182
183 // FIXME: need to handle EXIF rotation.
184 auto graphicsContext = webImage->bitmap().createGraphicsContext();
185 graphicsContext->drawImage(bitmapImage, {{ }, size});
186
187 return webImage;
188}
189
190bool InjectedBundleHitTestResult::isSelected() const
191{
192 return m_hitTestResult.isSelected();
193}
194
195} // WebKit
196