1/*
2 * Copyright (C) 2007, 2008 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 COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "WebKitDLL.h"
28#include "WebActionPropertyBag.h"
29
30#include "DOMCoreClasses.h"
31#include "WebElementPropertyBag.h"
32#include "WebKit.h"
33#include <WebCore/BString.h>
34#include <WebCore/COMPtr.h>
35#include <WebCore/EventHandler.h>
36#include <WebCore/HitTestResult.h>
37#include <WebCore/MouseEvent.h>
38
39using namespace WebCore;
40
41// WebActionPropertyBag ------------------------------------------------
42
43WebActionPropertyBag::WebActionPropertyBag(const NavigationAction& action, PassRefPtr<HTMLFormElement> form, PassRefPtr<Frame> frame)
44    : m_refCount(0)
45    , m_action(action)
46    , m_form(form)
47    , m_frame(frame)
48{
49    gClassCount++;
50    gClassNameCount.add("WebActionPropertyBag");
51}
52
53WebActionPropertyBag::~WebActionPropertyBag()
54{
55    gClassCount--;
56    gClassNameCount.remove("WebActionPropertyBag");
57}
58
59WebActionPropertyBag* WebActionPropertyBag::createInstance(const NavigationAction& action, PassRefPtr<HTMLFormElement> form, PassRefPtr<Frame> frame)
60{
61    WebActionPropertyBag* instance = new WebActionPropertyBag(action, form, frame);
62    instance->AddRef();
63    return instance;
64}
65
66// IUnknown -------------------------------------------------------------------
67
68HRESULT STDMETHODCALLTYPE WebActionPropertyBag::QueryInterface(REFIID riid, void** ppvObject)
69{
70    *ppvObject = 0;
71    if (IsEqualGUID(riid, IID_IUnknown))
72        *ppvObject = static_cast<IPropertyBag*>(this);
73    else if (IsEqualGUID(riid, IID_IPropertyBag))
74        *ppvObject = static_cast<IPropertyBag*>(this);
75    else
76        return E_NOINTERFACE;
77
78    AddRef();
79    return S_OK;
80}
81
82ULONG STDMETHODCALLTYPE WebActionPropertyBag::AddRef()
83{
84    return ++m_refCount;
85}
86
87ULONG STDMETHODCALLTYPE WebActionPropertyBag::Release()
88{
89    ULONG newRef = --m_refCount;
90    if (!newRef)
91        delete this;
92
93    return newRef;
94}
95
96static bool isEqual(LPCWSTR s1, LPCWSTR s2)
97{
98    return !wcscmp(s1, s2);
99}
100
101static const MouseEvent* findMouseEvent(const Event* event)
102{
103    for (const Event* e = event; e; e = e->underlyingEvent())
104        if (e->isMouseEvent())
105            return static_cast<const MouseEvent*>(e);
106    return 0;
107}
108
109HRESULT STDMETHODCALLTYPE WebActionPropertyBag::Read(LPCOLESTR pszPropName, VARIANT *pVar, IErrorLog * /*pErrorLog*/)
110{
111    if (!pszPropName)
112        return E_POINTER;
113
114    VariantClear(pVar);
115
116    if (isEqual(pszPropName, WebActionNavigationTypeKey)) {
117        V_VT(pVar) = VT_I4;
118        V_I4(pVar) = m_action.type();
119        return S_OK;
120    }
121    if (isEqual(pszPropName, WebActionElementKey)) {
122        if (const MouseEvent* mouseEvent = findMouseEvent(m_action.event())) {
123            V_VT(pVar) = VT_UNKNOWN;
124            V_UNKNOWN(pVar) = WebElementPropertyBag::createInstance(m_frame->eventHandler()->hitTestResultAtPoint(mouseEvent->absoluteLocation(), false));
125            return S_OK;
126        }
127    }
128    if (isEqual(pszPropName, WebActionButtonKey)) {
129        if (const MouseEvent* mouseEvent = findMouseEvent(m_action.event())) {
130            V_VT(pVar) = VT_I4;
131            V_I4(pVar) = mouseEvent->button();
132            return S_OK;
133        }
134    }
135    if (isEqual(pszPropName, WebActionOriginalURLKey)) {
136        V_VT(pVar) = VT_BSTR;
137        V_BSTR(pVar) = BString(m_action.url().string()).release();
138        return S_OK;
139    }
140    if (isEqual(pszPropName, WebActionModifierFlagsKey)) {
141        if (const UIEventWithKeyState* keyEvent = findEventWithKeyState(const_cast<Event*>(m_action.event()))) {
142            unsigned modifiers = 0;
143
144            if (keyEvent->ctrlKey())
145                modifiers |= MK_CONTROL;
146            if (keyEvent->shiftKey())
147                modifiers |= MK_SHIFT;
148            if (keyEvent->altKey())
149                modifiers |= MK_ALT;
150
151            V_VT(pVar) = VT_UI4;
152            V_UI4(pVar) = modifiers;
153            return S_OK;
154        }
155    }
156    if (isEqual(pszPropName, WebActionFormKey)) {
157        IDOMNode* form = DOMNode::createInstance(m_form.get());
158        V_VT(pVar) = VT_UNKNOWN;
159        V_UNKNOWN(pVar) = form;
160        return S_OK;
161    }
162    return E_INVALIDARG;
163}
164
165HRESULT STDMETHODCALLTYPE WebActionPropertyBag::Write(LPCOLESTR pszPropName, VARIANT* pVar)
166{
167    if (!pszPropName || !pVar)
168        return E_POINTER;
169
170    return E_FAIL;
171}
172