1/*
2 * Copyright (C) 2009 Google 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 are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#ifndef WebBindings_h
32#define WebBindings_h
33
34#include "WebCommon.h"
35#include "WebString.h"
36#include "WebVector.h"
37#include <bindings/npruntime.h>
38
39namespace WebKit {
40
41class WebDragData;
42class WebElement;
43class WebNode;
44class WebRange;
45
46// A haphazard collection of functions for dealing with plugins.
47class WebBindings {
48public:
49    // NPN Functions ------------------------------------------------------
50    // These are all defined in npruntime.h and are well documented.
51
52    // NPN_Construct
53    WEBKIT_API static bool construct(NPP, NPObject*, const NPVariant* args, uint32_t argCount, NPVariant* result);
54
55    // NPN_CreateObject
56    WEBKIT_API static NPObject* createObject(NPP, NPClass*);
57
58    // NPN_Enumerate
59    WEBKIT_API static bool enumerate(NPP, NPObject*, NPIdentifier**, uint32_t* identifierCount);
60
61    // NPN_Evaluate
62    WEBKIT_API static bool evaluate(NPP, NPObject*, NPString* script, NPVariant* result);
63
64    // NPN_EvaluateHelper
65    WEBKIT_API static bool evaluateHelper(NPP, bool popupsAllowed, NPObject*, NPString* script, NPVariant* result);
66
67    // NPN_GetIntIdentifier
68    WEBKIT_API static NPIdentifier getIntIdentifier(int32_t number);
69
70    // NPN_GetProperty
71    WEBKIT_API static bool getProperty(NPP, NPObject*, NPIdentifier property, NPVariant *result);
72
73    // NPN_GetStringIdentifier
74    WEBKIT_API static NPIdentifier getStringIdentifier(const NPUTF8* string);
75
76    // NPN_GetStringIdentifiers
77    WEBKIT_API static void getStringIdentifiers(const NPUTF8** names, int32_t nameCount, NPIdentifier*);
78
79    // NPN_HasMethod
80    WEBKIT_API static bool hasMethod(NPP, NPObject*, NPIdentifier method);
81
82    // NPN_HasProperty
83    WEBKIT_API static bool hasProperty(NPP, NPObject*, NPIdentifier property);
84
85    // NPN_IdentifierIsString
86    WEBKIT_API static bool identifierIsString(NPIdentifier);
87
88    // NPN_InitializeVariantWithStringCopy (though sometimes prefixed with an underscore)
89    WEBKIT_API static void initializeVariantWithStringCopy(NPVariant*, const NPString*);
90
91    // NPN_IntFromIdentifier
92    WEBKIT_API static int32_t intFromIdentifier(NPIdentifier);
93
94    // NPN_Invoke
95    WEBKIT_API static bool invoke(NPP, NPObject*, NPIdentifier method, const NPVariant* args, uint32_t argCount, NPVariant* result);
96
97    // NPN_InvokeDefault
98    WEBKIT_API static bool invokeDefault(NPP, NPObject*, const NPVariant* args, uint32_t argCount, NPVariant* result);
99
100    // NPN_ReleaseObject
101    WEBKIT_API static void releaseObject(NPObject*);
102
103    // NPN_ReleaseVariantValue
104    WEBKIT_API static void releaseVariantValue(NPVariant*);
105
106    // NPN_RemoveProperty
107    WEBKIT_API static bool removeProperty(NPP, NPObject*, NPIdentifier);
108
109    // NPN_RetainObject
110    WEBKIT_API static NPObject* retainObject(NPObject*);
111
112    // NPN_SetException
113    WEBKIT_API static void setException(NPObject*, const NPUTF8* message);
114
115    // NPN_SetProperty
116    WEBKIT_API static bool setProperty(NPP, NPObject*, NPIdentifier, const NPVariant*);
117
118    // _NPN_UnregisterObject
119    WEBKIT_API static void unregisterObject(NPObject*);
120
121    // NPN_UTF8FromIdentifier
122    WEBKIT_API static NPUTF8* utf8FromIdentifier(NPIdentifier);
123
124    // Miscellaneous utility functions ----------------------------------------
125
126    // Complement to NPN_Get___Identifier functions.  Extracts data from the NPIdentifier data
127    // structure.  If isString is true upon return, string will be set but number's value is
128    // undefined.  If iString is false, the opposite is true.
129    WEBKIT_API static void extractIdentifierData(const NPIdentifier&, const NPUTF8*& string, int32_t& number, bool& isString);
130
131    // Return true (success) if the given npobj is a range object.
132    // If so, return that range as a WebRange object.
133    WEBKIT_API static bool getRange(NPObject* range, WebRange*);
134
135    // Return true (success) if the given npobj is an element.
136    // If so, return that element as a WebElement object.
137    WEBKIT_API static bool getElement(NPObject* element, WebElement*);
138
139    WEBKIT_API static NPObject* makeIntArray(const WebVector<int>&);
140    WEBKIT_API static NPObject* makeStringArray(const WebVector<WebString>&);
141    WEBKIT_API static NPObject* makeNode(const WebNode&);
142
143    // Exceptions -------------------------------------------------------------
144
145    typedef void (ExceptionHandler)(void* data, const NPUTF8* message);
146
147    // The exception handler will be notified of any exceptions thrown while
148    // operating on a NPObject.
149    WEBKIT_API static void pushExceptionHandler(ExceptionHandler, void* data);
150    WEBKIT_API static void popExceptionHandler();
151};
152
153} // namespace WebKit
154
155#endif
156