128040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu/*
228040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu * Copyright (C) 2010 Apple Inc. All rights reserved.
328040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu *
428040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu * Redistribution and use in source and binary forms, with or without
528040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu * modification, are permitted provided that the following conditions
628040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu * are met:
728040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu * 1. Redistributions of source code must retain the above copyright
828040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu *    notice, this list of conditions and the following disclaimer.
928040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu * 2. Redistributions in binary form must reproduce the above copyright
1028040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu *    notice, this list of conditions and the following disclaimer in the
1128040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu *    documentation and/or other materials provided with the distribution.
1228040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu *
1328040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
1428040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
1528040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1628040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
1728040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
1828040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
1928040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2028040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2128040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2228040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
2328040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu * THE POSSIBILITY OF SUCH DAMAGE.
2428040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu */
2528040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu
2628040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu#include "WindowedPluginTest.h"
2728040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu
2828040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhuusing namespace std;
2928040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu
3028040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhustatic const wchar_t instancePointerProperty[] = L"org.webkit.TestNetscapePlugin.WindowedPluginTest.InstancePointer";
3128040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu
3228040489d744e0c5d475a88663056c9040ed5320Teng-Hui ZhuWindowedPluginTest::WindowedPluginTest(NPP npp, const string& identifier)
3328040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu    : PluginTest(npp, identifier)
3428040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu    , m_window(0)
3528040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu    , m_originalWndProc(0)
3628040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu{
3728040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu}
3828040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu
3928040489d744e0c5d475a88663056c9040ed5320Teng-Hui ZhuNPError WindowedPluginTest::NPP_SetWindow(NPP instance, NPWindow* window)
4028040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu{
4128040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu    HWND newWindow = reinterpret_cast<HWND>(window->window);
4228040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu    if (newWindow == m_window)
4328040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu        return NPERR_NO_ERROR;
4428040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu
4528040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu    if (m_window) {
4628040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu        ::RemovePropW(m_window, instancePointerProperty);
4728040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu        ::SetWindowLongPtr(m_window, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(m_originalWndProc));
4828040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu        m_originalWndProc = 0;
4928040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu    }
5028040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu
5128040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu    m_window = newWindow;
5228040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu    if (!m_window)
5328040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu        return NPERR_NO_ERROR;
5428040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu
5528040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu    m_originalWndProc = reinterpret_cast<WNDPROC>(::SetWindowLongPtrW(m_window, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(staticWndProc)));
5628040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu    ::SetPropW(m_window, instancePointerProperty, this);
5728040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu
5828040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu    return NPERR_NO_ERROR;
5928040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu}
6028040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu
6128040489d744e0c5d475a88663056c9040ed5320Teng-Hui ZhuLRESULT WindowedPluginTest::staticWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
6228040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu{
6328040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu    WindowedPluginTest* instance = reinterpret_cast<WindowedPluginTest*>(::GetPropW(hwnd, instancePointerProperty));
6428040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu
6528040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu    bool handled = false;
6628040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu    LRESULT result = instance->wndProc(message, wParam, lParam, handled);
6728040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu    if (handled)
6828040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu        return result;
6928040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu
7028040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu    return ::CallWindowProcW(instance->m_originalWndProc, hwnd, message, wParam, lParam);
7128040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu}
72