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
29#include "CString.h"
30#include "JavaSharedClient.h"
31#include "PluginClient.h"
32#include "PluginPackage.h"
33#include "PluginView.h"
34#include "PluginWidgetAndroid.h"
35#include "SkString.h"
36#include "WebViewCore.h"
37
38#include "ANPSystem_npapi.h"
39
40static const char* gApplicationDataDir = NULL;
41
42using namespace android;
43
44static const char* anp_getApplicationDataDirectory() {
45    if (NULL == gApplicationDataDir) {
46        PluginClient* client = JavaSharedClient::GetPluginClient();
47        if (!client)
48            return NULL;
49
50        WebCore::String path = client->getPluginSharedDataDirectory();
51        int length = path.length();
52        if (length == 0)
53            return NULL;
54
55        char* storage = (char*) malloc(length + 1);
56        if (NULL == storage)
57            return NULL;
58
59        memcpy(storage, path.utf8().data(), length);
60        storage[length] = '\0';
61
62        // save this assignment for last, so that if multiple threads call us
63        // (which should never happen), we never return an incomplete global.
64        // At worst, we would allocate storage for the path twice.
65        gApplicationDataDir = storage;
66    }
67    return gApplicationDataDir;
68}
69
70static WebCore::PluginView* pluginViewForInstance(NPP instance) {
71    if (instance && instance->ndata)
72        return static_cast<WebCore::PluginView*>(instance->ndata);
73    return PluginView::currentPluginView();
74}
75
76static jclass anp_loadJavaClass(NPP instance, const char* className) {
77    WebCore::PluginView* pluginView = pluginViewForInstance(instance);
78    PluginWidgetAndroid* pluginWidget = pluginView->platformPluginWidget();
79
80    jclass result;
81    result = pluginWidget->webViewCore()->getPluginClass(pluginView->plugin()->path(),
82                                                         className);
83    return result;
84}
85
86///////////////////////////////////////////////////////////////////////////////
87
88#define ASSIGN(obj, name)   (obj)->name = anp_##name
89
90void ANPSystemInterfaceV0_Init(ANPInterface* v) {
91    ANPSystemInterfaceV0* i = reinterpret_cast<ANPSystemInterfaceV0*>(v);
92
93    ASSIGN(i, getApplicationDataDirectory);
94    ASSIGN(i, loadJavaClass);
95}
96