1/*
2 * Copyright 2009, The Android Open Source Project
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 *  * Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 *  * 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 THE COPYRIGHT HOLDERS ``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 THE COPYRIGHT OWNER 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// must include config.h first for webkit to fiddle with new/delete
27#include "config.h"
28#include "SkANP.h"
29#include "WebViewCore.h"
30#include "PluginView.h"
31#include "PluginWidgetAndroid.h"
32
33#include "JavaSharedClient.h"
34
35using namespace android;
36
37struct WrappedANPEvent {
38    WebViewCore*            fWVC;
39    PluginWidgetAndroid*    fPWA;
40    ANPEvent                fEvent;
41};
42
43/*  Its possible we may be called after the plugin that initiated the event
44    has been torn-down. Thus we check that the assicated webviewcore and
45    pluginwidget are still active before dispatching the event.
46 */
47static void send_anpevent(void* data) {
48    WrappedANPEvent* wrapper = static_cast<WrappedANPEvent*>(data);
49    WebViewCore* core = wrapper->fWVC;
50    PluginWidgetAndroid* widget = wrapper->fPWA;
51
52    // be sure we're still alive before delivering the event
53    if (WebViewCore::isInstance(core) && core->isPlugin(widget)) {
54        widget->sendEvent(wrapper->fEvent);
55    }
56    delete wrapper;
57}
58
59static void anp_postEvent(NPP instance, const ANPEvent* event) {
60    if (instance && instance->ndata && event) {
61        PluginView* pluginView = static_cast<PluginView*>(instance->ndata);
62        PluginWidgetAndroid* pluginWidget = pluginView->platformPluginWidget();
63        WebViewCore* wvc = pluginWidget->webViewCore();
64
65        WrappedANPEvent* wrapper = new WrappedANPEvent;
66        // recored these, and recheck that they are valid before delivery
67        // in send_anpevent
68        wrapper->fWVC = pluginWidget->webViewCore();
69        wrapper->fPWA = pluginWidget;
70        // make a copy of the event
71        wrapper->fEvent = *event;
72        JavaSharedClient::EnqueueFunctionPtr(send_anpevent, wrapper);
73    }
74}
75
76///////////////////////////////////////////////////////////////////////////////
77
78#define ASSIGN(obj, name)   (obj)->name = anp_##name
79
80void ANPEventInterfaceV0_Init(ANPInterface* value) {
81    ANPEventInterfaceV0* i = reinterpret_cast<ANPEventInterfaceV0*>(value);
82
83    ASSIGN(i, postEvent);
84}
85