1/*
2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All Rights Reserved.
3 * Copyright (C) 2008 Collabora, Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "config.h"
28#include "PluginMessageThrottlerWin.h"
29
30#include "PluginView.h"
31#include <wtf/ASCIICType.h>
32
33using namespace WTF;
34
35namespace WebCore {
36
37static const double MessageThrottleTimeInterval = 0.001;
38
39PluginMessageThrottlerWin::PluginMessageThrottlerWin(PluginView* pluginView)
40    : m_back(0), m_front(0)
41    , m_pluginView(pluginView)
42    , m_messageThrottleTimer(this, &PluginMessageThrottlerWin::messageThrottleTimerFired)
43{
44    // Initialize the free list with our inline messages
45    for (unsigned i = 0; i < NumInlineMessages - 1; i++)
46        m_inlineMessages[i].next = &m_inlineMessages[i + 1];
47    m_inlineMessages[NumInlineMessages - 1].next = 0;
48    m_freeInlineMessages = &m_inlineMessages[0];
49}
50
51PluginMessageThrottlerWin::~PluginMessageThrottlerWin()
52{
53    PluginMessage* next;
54
55    for (PluginMessage* message = m_front; message; message = next) {
56        next = message->next;
57        freeMessage(message);
58    }
59}
60
61void PluginMessageThrottlerWin::appendMessage(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
62{
63    PluginMessage* message = allocateMessage();
64
65    message->hWnd = hWnd;
66    message->msg = msg;
67    message->wParam = wParam;
68    message->lParam = lParam;
69    message->next = 0;
70
71    if (m_back)
72        m_back->next = message;
73    m_back = message;
74    if (!m_front)
75        m_front = message;
76
77    if (!m_messageThrottleTimer.isActive())
78        m_messageThrottleTimer.startOneShot(MessageThrottleTimeInterval);
79}
80
81void PluginMessageThrottlerWin::messageThrottleTimerFired(Timer<PluginMessageThrottlerWin>*)
82{
83    PluginMessage* message = m_front;
84    m_front = m_front->next;
85    if (message == m_back)
86        m_back = 0;
87
88    // Protect the PluginView from destruction while calling its window proc.
89    // <rdar://problem/6930280>
90    RefPtr<PluginView> protect(m_pluginView);
91    ::CallWindowProc(m_pluginView->pluginWndProc(), message->hWnd, message->msg, message->wParam, message->lParam);
92
93    freeMessage(message);
94
95    if (m_front)
96        m_messageThrottleTimer.startOneShot(MessageThrottleTimeInterval);
97}
98
99PluginMessage* PluginMessageThrottlerWin::allocateMessage()
100{
101    PluginMessage *message;
102
103    if (m_freeInlineMessages) {
104        message = m_freeInlineMessages;
105        m_freeInlineMessages = message->next;
106    } else
107        message = new PluginMessage;
108
109    return message;
110}
111
112bool PluginMessageThrottlerWin::isInlineMessage(PluginMessage* message)
113{
114    return message >= &m_inlineMessages[0] && message <= &m_inlineMessages[NumInlineMessages - 1];
115}
116
117void PluginMessageThrottlerWin::freeMessage(PluginMessage* message)
118{
119    if (isInlineMessage(message)) {
120        message->next = m_freeInlineMessages;
121        m_freeInlineMessages = message;
122    } else
123        delete message;
124}
125
126} // namespace WebCore
127