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 <WebCore/Document.h>
33#include <WebCore/Element.h>
34#include <WebCore/Frame.h>
35#include <WebCore/FrameLoader.h>
36#include <WebCore/FrameView.h>
37#include <WebCore/URL.h>
38#include <wtf/text/WTFString.h>
39
40using namespace WebCore;
41
42namespace WebKit {
43
44PassRefPtr<InjectedBundleHitTestResult> InjectedBundleHitTestResult::create(const WebCore::HitTestResult& hitTestResult)
45{
46    return adoptRef(new InjectedBundleHitTestResult(hitTestResult));
47}
48
49PassRefPtr<InjectedBundleNodeHandle> InjectedBundleHitTestResult::nodeHandle() const
50{
51    return InjectedBundleNodeHandle::getOrCreate(m_hitTestResult.innerNonSharedNode());
52}
53
54WebFrame* InjectedBundleHitTestResult::frame() const
55{
56    Node* node = m_hitTestResult.innerNonSharedNode();
57    if (!node)
58        return nullptr;
59
60    Frame* frame = node->document().frame();
61    if (!frame)
62        return nullptr;
63
64    return WebFrame::fromCoreFrame(*frame);
65}
66
67WebFrame* InjectedBundleHitTestResult::targetFrame() const
68{
69    Frame* frame = m_hitTestResult.targetFrame();
70    if (!frame)
71        return nullptr;
72
73    return WebFrame::fromCoreFrame(*frame);
74}
75
76String InjectedBundleHitTestResult::absoluteImageURL() const
77{
78    return m_hitTestResult.absoluteImageURL().string();
79}
80
81String InjectedBundleHitTestResult::absolutePDFURL() const
82{
83    return m_hitTestResult.absolutePDFURL().string();
84}
85
86String InjectedBundleHitTestResult::absoluteLinkURL() const
87{
88    return m_hitTestResult.absoluteLinkURL().string();
89}
90
91String InjectedBundleHitTestResult::absoluteMediaURL() const
92{
93    return m_hitTestResult.absoluteMediaURL().string();
94}
95
96bool InjectedBundleHitTestResult::mediaIsInFullscreen() const
97{
98    return m_hitTestResult.mediaIsInFullscreen();
99}
100
101bool InjectedBundleHitTestResult::mediaHasAudio() const
102{
103    return m_hitTestResult.mediaHasAudio();
104}
105
106BundleHitTestResultMediaType InjectedBundleHitTestResult::mediaType() const
107{
108#if !ENABLE(VIDEO)
109    return BundleHitTestResultMediaTypeNone;
110#else
111    WebCore::Node* node = m_hitTestResult.innerNonSharedNode();
112    if (!node->isElementNode())
113        return BundleHitTestResultMediaTypeNone;
114
115    if (!toElement(node)->isMediaElement())
116        return BundleHitTestResultMediaTypeNone;
117
118    return m_hitTestResult.mediaIsVideo() ? BundleHitTestResultMediaTypeVideo : BundleHitTestResultMediaTypeAudio;
119#endif
120}
121
122String InjectedBundleHitTestResult::linkLabel() const
123{
124    return m_hitTestResult.textContent();
125}
126
127String InjectedBundleHitTestResult::linkTitle() const
128{
129    return m_hitTestResult.titleDisplayString();
130}
131
132WebCore::IntRect InjectedBundleHitTestResult::imageRect() const
133{
134    WebCore::IntRect imageRect = m_hitTestResult.imageRect();
135    if (imageRect.isEmpty())
136        return imageRect;
137
138    // The image rect in WebCore::HitTestResult is in frame coordinates, but we need it in WKView
139    // coordinates since WebKit2 clients don't have enough context to do the conversion themselves.
140    WebFrame* webFrame = frame();
141    if (!webFrame)
142        return imageRect;
143
144    WebCore::Frame* coreFrame = webFrame->coreFrame();
145    if (!coreFrame)
146        return imageRect;
147
148    WebCore::FrameView* view = coreFrame->view();
149    if (!view)
150        return imageRect;
151
152    return view->contentsToRootView(imageRect);
153}
154
155bool InjectedBundleHitTestResult::isSelected() const
156{
157    return m_hitTestResult.isSelected();
158}
159
160} // WebKit
161