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/KURL.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 0;
59
60    Document* document = node->document();
61    if (!document)
62        return 0;
63
64    Frame* frame = document->frame();
65    if (!frame)
66        return 0;
67
68    WebFrameLoaderClient* webFrameLoaderClient = toWebFrameLoaderClient(frame->loader()->client());
69    return webFrameLoaderClient ? webFrameLoaderClient->webFrame() : 0;
70}
71
72WebFrame* InjectedBundleHitTestResult::targetFrame() const
73{
74    Frame* frame = m_hitTestResult.targetFrame();
75    if (!frame)
76        return 0;
77
78    WebFrameLoaderClient* webFrameLoaderClient = toWebFrameLoaderClient(frame->loader()->client());
79    return webFrameLoaderClient ? webFrameLoaderClient->webFrame() : 0;
80}
81
82String InjectedBundleHitTestResult::absoluteImageURL() const
83{
84    return m_hitTestResult.absoluteImageURL().string();
85}
86
87String InjectedBundleHitTestResult::absolutePDFURL() const
88{
89    return m_hitTestResult.absolutePDFURL().string();
90}
91
92String InjectedBundleHitTestResult::absoluteLinkURL() const
93{
94    return m_hitTestResult.absoluteLinkURL().string();
95}
96
97String InjectedBundleHitTestResult::absoluteMediaURL() const
98{
99    return m_hitTestResult.absoluteMediaURL().string();
100}
101
102bool InjectedBundleHitTestResult::mediaIsInFullscreen() const
103{
104    return m_hitTestResult.mediaIsInFullscreen();
105}
106
107bool InjectedBundleHitTestResult::mediaHasAudio() const
108{
109    return m_hitTestResult.mediaHasAudio();
110}
111
112BundleHitTestResultMediaType InjectedBundleHitTestResult::mediaType() const
113{
114#if !ENABLE(VIDEO)
115    return BundleHitTestResultMediaTypeNone;
116#else
117    WebCore::Node* node = m_hitTestResult.innerNonSharedNode();
118    if (!node->isElementNode())
119        return BundleHitTestResultMediaTypeNone;
120
121    if (!toElement(node)->isMediaElement())
122        return BundleHitTestResultMediaTypeNone;
123
124    return m_hitTestResult.mediaIsVideo() ? BundleHitTestResultMediaTypeVideo : BundleHitTestResultMediaTypeAudio;
125#endif
126}
127
128String InjectedBundleHitTestResult::linkLabel() const
129{
130    return m_hitTestResult.textContent();
131}
132
133String InjectedBundleHitTestResult::linkTitle() const
134{
135    return m_hitTestResult.titleDisplayString();
136}
137
138WebCore::IntRect InjectedBundleHitTestResult::imageRect() const
139{
140    WebCore::IntRect imageRect = m_hitTestResult.imageRect();
141    if (imageRect.isEmpty())
142        return imageRect;
143
144    // The image rect in WebCore::HitTestResult is in frame coordinates, but we need it in WKView
145    // coordinates since WebKit2 clients don't have enough context to do the conversion themselves.
146    WebFrame* webFrame = frame();
147    if (!webFrame)
148        return imageRect;
149
150    WebCore::Frame* coreFrame = webFrame->coreFrame();
151    if (!coreFrame)
152        return imageRect;
153
154    WebCore::FrameView* view = coreFrame->view();
155    if (!view)
156        return imageRect;
157
158    return view->contentsToRootView(imageRect);
159}
160
161bool InjectedBundleHitTestResult::isSelected() const
162{
163    return m_hitTestResult.isSelected();
164}
165
166} // WebKit
167