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 "WebHitTestResult.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/URL.h>
29#include <WebCore/Node.h>
30#include <wtf/text/WTFString.h>
31
32using namespace WebCore;
33
34namespace WebKit {
35
36PassRefPtr<WebHitTestResult> WebHitTestResult::create(const WebHitTestResult::Data& hitTestResultData)
37{
38    return adoptRef(new WebHitTestResult(hitTestResultData));
39}
40
41WebHitTestResult::Data::Data()
42{
43}
44
45WebHitTestResult::Data::Data(const HitTestResult& hitTestResult)
46    : absoluteImageURL(hitTestResult.absoluteImageURL().string())
47    , absolutePDFURL(hitTestResult.absolutePDFURL().string())
48    , absoluteLinkURL(hitTestResult.absoluteLinkURL().string())
49    , absoluteMediaURL(hitTestResult.absoluteMediaURL().string())
50    , linkLabel(hitTestResult.textContent())
51    , linkTitle(hitTestResult.titleDisplayString())
52    , isContentEditable(hitTestResult.isContentEditable())
53    , elementBoundingBox(elementBoundingBoxInWindowCoordinates(hitTestResult))
54    , isScrollbar(hitTestResult.scrollbar())
55{
56}
57
58WebHitTestResult::Data::~Data()
59{
60}
61
62void WebHitTestResult::Data::encode(IPC::ArgumentEncoder& encoder) const
63{
64    encoder << absoluteImageURL;
65    encoder << absolutePDFURL;
66    encoder << absoluteLinkURL;
67    encoder << absoluteMediaURL;
68    encoder << linkLabel;
69    encoder << linkTitle;
70    encoder << isContentEditable;
71    encoder << elementBoundingBox;
72    encoder << isScrollbar;
73}
74
75bool WebHitTestResult::Data::decode(IPC::ArgumentDecoder& decoder, WebHitTestResult::Data& hitTestResultData)
76{
77    if (!decoder.decode(hitTestResultData.absoluteImageURL)
78        || !decoder.decode(hitTestResultData.absolutePDFURL)
79        || !decoder.decode(hitTestResultData.absoluteLinkURL)
80        || !decoder.decode(hitTestResultData.absoluteMediaURL)
81        || !decoder.decode(hitTestResultData.linkLabel)
82        || !decoder.decode(hitTestResultData.linkTitle)
83        || !decoder.decode(hitTestResultData.isContentEditable)
84        || !decoder.decode(hitTestResultData.elementBoundingBox)
85        || !decoder.decode(hitTestResultData.isScrollbar))
86        return false;
87
88    return true;
89}
90
91IntRect WebHitTestResult::Data::elementBoundingBoxInWindowCoordinates(const HitTestResult& hitTestResult)
92{
93    Node* node = hitTestResult.innerNonSharedNode();
94    if (!node)
95        return IntRect();
96
97    Frame* frame = node->document().frame();
98    if (!frame)
99        return IntRect();
100
101    FrameView* view = frame->view();
102    if (!view)
103        return IntRect();
104
105    return view->contentsToWindow(node->pixelSnappedBoundingBox());
106}
107
108} // WebKit
109