webengine/osswebengine/WebKit/win/WebActionPropertyBag.cpp
changeset 0 dd21522fd290
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2  * Copyright (C) 2007 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 
       
    29 #include "IWebView.h"
       
    30 #include "IWebPolicyDelegate.h"
       
    31 #include "WebActionPropertyBag.h"
       
    32 #include "WebElementPropertyBag.h"
       
    33 #include "COMPtr.h"
       
    34 
       
    35 #pragma warning(push, 0)
       
    36 #include <WebCore/BString.h>
       
    37 #include <WebCore/EventHandler.h>
       
    38 #include <WebCore/MouseEvent.h>
       
    39 #include <WebCore/HitTestResult.h>
       
    40 #pragma warning(pop)
       
    41 
       
    42 using namespace WebCore;
       
    43 
       
    44 // WebActionPropertyBag ------------------------------------------------
       
    45 WebActionPropertyBag::WebActionPropertyBag(const NavigationAction& action, Frame* frame)
       
    46     : m_refCount(0)
       
    47     , m_action(action) 
       
    48     , m_frame(frame)
       
    49 {
       
    50 }
       
    51 
       
    52 WebActionPropertyBag::~WebActionPropertyBag()
       
    53 {
       
    54 }
       
    55 
       
    56 WebActionPropertyBag* WebActionPropertyBag::createInstance(const NavigationAction& action, Frame* frame)
       
    57 {
       
    58     WebActionPropertyBag* instance = new WebActionPropertyBag(action, frame); 
       
    59     instance->AddRef();
       
    60 
       
    61     return instance;
       
    62 }
       
    63 
       
    64 // IUnknown -------------------------------------------------------------------
       
    65 
       
    66 HRESULT STDMETHODCALLTYPE WebActionPropertyBag::QueryInterface(REFIID riid, void** ppvObject)
       
    67 {
       
    68     *ppvObject = 0;
       
    69     if (IsEqualGUID(riid, IID_IUnknown))
       
    70         *ppvObject = static_cast<IPropertyBag*>(this);
       
    71     else if (IsEqualGUID(riid, IID_IPropertyBag))
       
    72         *ppvObject = static_cast<IPropertyBag*>(this);
       
    73     else
       
    74         return E_NOINTERFACE;
       
    75 
       
    76     AddRef();
       
    77     return S_OK;
       
    78 }
       
    79 
       
    80 ULONG STDMETHODCALLTYPE WebActionPropertyBag::AddRef(void)
       
    81 {
       
    82     return ++m_refCount;
       
    83 }
       
    84 
       
    85 ULONG STDMETHODCALLTYPE WebActionPropertyBag::Release(void)
       
    86 {
       
    87     ULONG newRef = --m_refCount;
       
    88     if (!newRef)
       
    89         delete this;
       
    90 
       
    91     return newRef;
       
    92 }
       
    93 
       
    94 static bool isEqual(LPCWSTR s1, LPCWSTR s2)
       
    95 {
       
    96     return !wcscmp(s1, s2);
       
    97 }
       
    98 
       
    99 static const MouseEvent* findMouseEvent(const Event* event)
       
   100 {
       
   101     for (const Event* e = event; e; e = e->underlyingEvent())
       
   102         if (e->isMouseEvent())
       
   103             return static_cast<const MouseEvent*>(e);
       
   104     return 0;
       
   105 }
       
   106 
       
   107 HRESULT STDMETHODCALLTYPE WebActionPropertyBag::Read(LPCOLESTR pszPropName, VARIANT *pVar, IErrorLog * /*pErrorLog*/)
       
   108 {
       
   109     if (!pszPropName)
       
   110         return E_POINTER;
       
   111 
       
   112     VariantClear(pVar);
       
   113 
       
   114     if (isEqual(pszPropName, WebActionNavigationTypeKey)) {
       
   115         V_VT(pVar) = VT_I4;
       
   116         V_I4(pVar) = m_action.type();
       
   117         return S_OK;
       
   118     } else if (isEqual(pszPropName, WebActionElementKey)) {
       
   119         if (const MouseEvent* mouseEvent = findMouseEvent(m_action.event())) {
       
   120             IntPoint point(mouseEvent->clientX(), mouseEvent->clientY());
       
   121             COMPtr<WebElementPropertyBag> elementPropertyBag;
       
   122             elementPropertyBag.adoptRef(WebElementPropertyBag::createInstance(m_frame->eventHandler()->hitTestResultAtPoint(point, false)));
       
   123 
       
   124             V_VT(pVar) = VT_UNKNOWN;
       
   125             elementPropertyBag->QueryInterface(IID_IUnknown, (void**)V_UNKNOWNREF(pVar));
       
   126             return S_OK;
       
   127         }
       
   128     } else 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     } else if (isEqual(pszPropName, WebActionOriginalURLKey)) {
       
   135         V_VT(pVar) = VT_BSTR;
       
   136         V_BSTR(pVar) = BString(m_action.URL().url()).release();
       
   137         return S_OK;
       
   138     } else if (isEqual(pszPropName, WebActionModifierFlagsKey)) {
       
   139         if (const UIEventWithKeyState* keyEvent = findEventWithKeyState(const_cast<Event*>(m_action.event()))) {
       
   140             int modifiers = 0;
       
   141 
       
   142             if (keyEvent->ctrlKey())
       
   143                 modifiers |= MK_CONTROL;
       
   144             if (keyEvent->shiftKey())
       
   145                 modifiers |= MK_SHIFT;
       
   146             if (keyEvent->altKey())
       
   147                 modifiers |= MK_ALT;
       
   148 
       
   149             V_VT(pVar) = VT_I4;
       
   150             V_I4(pVar) = modifiers;
       
   151             return S_OK;
       
   152         }
       
   153     }
       
   154     return E_INVALIDARG;
       
   155 }
       
   156 
       
   157 HRESULT STDMETHODCALLTYPE WebActionPropertyBag::Write(LPCOLESTR pszPropName, VARIANT* pVar)
       
   158 {
       
   159     if (!pszPropName || !pVar)
       
   160         return E_POINTER;
       
   161     VariantClear(pVar);
       
   162     return E_FAIL;
       
   163 }