npwin.cpp 16.3 KB
Newer Older
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2
 *
3 4
 * Mozilla/Firefox plugin for VLC
 * Copyright (C) 2009, Jean-Paul Saman <jpsaman@videolan.org>
5
 *
6 7 8 9 10 11 12 13 14 15 16 17 18
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19 20 21
 *
 * The Original Code is Mozilla Communicator client code.
 *
22
 * The Initial Developer of the Original Code is
23 24 25 26
 * Netscape Communications Corporation.
 * Portions created by the Initial Developer are Copyright (C) 1998
 * the Initial Developer. All Rights Reserved.
 *
27 28 29 30
 */

//#define OJI 1

31 32 33 34
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

35 36
#include "../vlcplugin.h"

37
#if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
38
#include "npupp.h"
39
#else
40
#include <npfunctions.h>
41
#endif
42

43
#include "../vlcshell.h"
44

45 46 47 48 49
//\\// DEFINE
#define NP_EXPORT

//\\// GLOBAL DATA
NPNetscapeFuncs* g_pNavigatorFuncs = 0;
50

51
#ifdef OJI
52 53 54 55 56 57
JRIGlobalRef Private_GetJavaClass(void);

//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\.
////\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//.
// Private_GetJavaClass (global function)
//
58 59
//	Given a Java class reference (thru NPP_GetJavaClass) inform JRT
//	of this class existence
60 61 62 63 64 65
//
JRIGlobalRef
Private_GetJavaClass(void)
{
    jref clazz = NPP_GetJavaClass();
    if (clazz) {
66 67
        JRIEnv* env = NPN_GetJavaEnv();
        return JRI_NewGlobalRef(env, clazz);
68 69 70
    }
    return NULL;
}
71
#endif /* OJI */
72

73 74
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\.
////\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//.
75
//						PLUGIN DLL entry points
76
//
77
// These are the Windows specific DLL entry points. They must be exported
78 79 80 81 82 83 84 85 86 87 88 89
//

// we need these to be global since we have to fill one of its field
// with a data (class) which requires knowlwdge of the navigator
// jump-table. This jump table is known at Initialize time (NP_Initialize)
// which is called after NP_GetEntryPoint
static NPPluginFuncs* g_pluginFuncs;

//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\.
////\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//.
// NP_GetEntryPoints
//
90
//	fills in the func table used by Navigator to call entry points in
91 92 93
//  plugin DLL.  Note that these entry points ensure that DS is loaded
//  by using the NP_LOADDS macro, when compiling for Win16
//
94 95 96
#ifdef __MINGW32__
extern "C" __declspec(dllexport) NPError WINAPI
#else
97
NPError WINAPI NP_EXPORT
98
#endif
99 100
NP_GetEntryPoints(NPPluginFuncs* pFuncs)
{
101
    // trap a NULL ptr
102 103 104 105
    if(pFuncs == NULL)
        return NPERR_INVALID_FUNCTABLE_ERROR;

    // if the plugin's function table is smaller than the plugin expects,
106
    // then they are incompatible, and should return an error
107 108 109 110 111 112 113 114 115 116 117

    pFuncs->version       = (NP_VERSION_MAJOR << 8) | NP_VERSION_MINOR;
    pFuncs->newp          = NPP_New;
    pFuncs->destroy       = NPP_Destroy;
    pFuncs->setwindow     = NPP_SetWindow;
    pFuncs->newstream     = NPP_NewStream;
    pFuncs->destroystream = NPP_DestroyStream;
    pFuncs->asfile        = NPP_StreamAsFile;
    pFuncs->writeready    = NPP_WriteReady;
    pFuncs->write         = NPP_Write;
    pFuncs->print         = NPP_Print;
118 119 120
    pFuncs->event         = 0;       /// reserved
    pFuncs->getvalue      = NPP_GetValue;
    pFuncs->setvalue      = NPP_SetValue;
121

122
    g_pluginFuncs         = pFuncs;
123 124 125 126 127 128 129 130

    return NPERR_NO_ERROR;
}

//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\.
////\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//.
// NP_Initialize
//
131
//	called immediately after the plugin DLL is loaded
132
//
133 134 135 136 137
#ifdef __MINGW32__
extern "C" __declspec(dllexport) NPError WINAPI
#else
NPError WINAPI NP_EXPORT
#endif
138
NP_Initialize(NPNetscapeFuncs* pFuncs)
139
{
140
    // trap a NULL ptr
141 142 143
    if(pFuncs == NULL)
        return NPERR_INVALID_FUNCTABLE_ERROR;

144
    g_pNavigatorFuncs = pFuncs; // save it for future reference
145 146

    // if the plugin's major ver level is lower than the Navigator's,
147
    // then they are incompatible, and should return an error
148 149 150
    if(HIBYTE(pFuncs->version) > NP_VERSION_MAJOR)
        return NPERR_INCOMPATIBLE_VERSION_ERROR;

151
    // We have to defer these assignments until g_pNavigatorFuncs is set
152 153
    int navMinorVers = g_pNavigatorFuncs->version & 0xFF;

154 155 156
    if( navMinorVers >= NPVERS_HAS_NOTIFICATION ) {
        g_pluginFuncs->urlnotify = NPP_URLNotify;
    }
157
#ifdef OJI
158 159 160
    if( navMinorVers >= NPVERS_HAS_LIVECONNECT ) {
        g_pluginFuncs->javaClass = Private_GetJavaClass();
    }
161
#endif
162
    // NPP_Initialize is a standard (cross-platform) initialize function.
163 164 165 166 167 168 169
    return NPP_Initialize();
}

//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\.
////\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//.
// NP_Shutdown
//
170 171
//	called immediately before the plugin DLL is unloaded.
//	This function should check for some ref count on the dll to see if it is
172
//	unloadable or it needs to stay in memory.
173
//
174 175 176
#ifdef __MINGW32__
extern "C" __declspec(dllexport) NPError WINAPI
#else
177
NPError WINAPI NP_EXPORT
178
#endif
179 180 181 182 183 184 185
NP_Shutdown()
{
    NPP_Shutdown();
    g_pNavigatorFuncs = NULL;
    return NPERR_NO_ERROR;
}

186
NPP_GET_MIME_CONST char * NP_GetMIMEDescription()
187
{
188
  return NPP_GetMIMEDescription();
189 190
}

191
//						END - PLUGIN DLL entry points
192 193 194 195 196 197 198 199 200 201
////\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//.
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\.

/*    NAVIGATOR Entry points    */

/* These entry points expect to be called from within the plugin.  The
   noteworthy assumption is that DS has already been set to point to the
   plugin's DLL data segment.  Don't call these functions from outside
   the plugin without ensuring DS is set to the DLLs data segment first,
   typically using the NP_LOADDS macro
202
 */
203 204 205

/* returns the major/minor version numbers of the Plugin API for the plugin
   and the Navigator
206
 */
207 208 209 210 211 212 213 214
void NPN_Version(int* plugin_major, int* plugin_minor, int* netscape_major, int* netscape_minor)
{
    *plugin_major   = NP_VERSION_MAJOR;
    *plugin_minor   = NP_VERSION_MINOR;
    *netscape_major = HIBYTE(g_pNavigatorFuncs->version);
    *netscape_minor = LOBYTE(g_pNavigatorFuncs->version);
}

215 216 217 218 219 220 221
void NPN_PluginThreadAsyncCall(NPP plugin, void (*func)(void *), void *userData)
{
#if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) >= 20)
    (g_pNavigatorFuncs->pluginthreadasynccall)(plugin, func, userData);
#endif
}

222 223 224 225 226
NPError NPN_GetValue(NPP instance, NPNVariable variable, void *result)
{
    return g_pNavigatorFuncs->getvalue(instance, variable, result);
}

227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
NPError NPN_SetValue(NPP instance, NPPVariable variable, void *value)
{
    return g_pNavigatorFuncs->setvalue(instance, variable, value);
}

void NPN_InvalidateRect(NPP instance, NPRect *rect)
{
    g_pNavigatorFuncs->invalidaterect(instance, rect);
}

void NPN_InvalidateRegion(NPP instance, NPRegion region)
{
    g_pNavigatorFuncs->invalidateregion(instance, region);
}

void NPN_ForceRedraw(NPP instance)
{
    g_pNavigatorFuncs->forceredraw(instance);
}

NPIdentifier NPN_GetStringIdentifier(const NPUTF8 *name)
{
    int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
    if( navMinorVers >= 14 )
    {
        return g_pNavigatorFuncs->getstringidentifier(name);
    }
    return NULL;
}

void NPN_GetStringIdentifiers(const NPUTF8 **names, int32_t nameCount, NPIdentifier *identifiers)
{
    int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
    if( navMinorVers >= 14 )
    {
        g_pNavigatorFuncs->getstringidentifiers(names, nameCount, identifiers);
    }
}

NPIdentifier NPN_GetIntIdentifier(int32_t intid)
{
    int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
    if( navMinorVers >= 14 )
    {
        return g_pNavigatorFuncs->getintidentifier(intid);
    }
    return NULL;
}

bool NPN_IdentifierIsString(NPIdentifier identifier)
{
    int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
    if( navMinorVers >= 14 )
    {
        return g_pNavigatorFuncs->identifierisstring(identifier);
    }
    return false;
}

NPUTF8 *NPN_UTF8FromIdentifier(NPIdentifier identifier)
{
    int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
    if( navMinorVers >= 14 )
    {
        return g_pNavigatorFuncs->utf8fromidentifier(identifier);
    }
    return NULL;
}

int32_t NPN_IntFromIdentifier(NPIdentifier identifier)
{
    int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
    if( navMinorVers >= 14 )
    {
        return g_pNavigatorFuncs->intfromidentifier(identifier);
    }
    return 0;
}

NPObject *NPN_CreateObject(NPP instance, NPClass *aClass)
{
    int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
    if( navMinorVers >= 14 )
    {
        return g_pNavigatorFuncs->createobject(instance, aClass);
    }
    return NULL;
}

NPObject *NPN_RetainObject(NPObject *npobj)
{
    int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
    if( navMinorVers >= 14 )
    {
        return g_pNavigatorFuncs->retainobject(npobj);
    }
    return NULL;
}

void NPN_ReleaseObject(NPObject *npobj)
{
    int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
    if( navMinorVers >= 14 )
    {
        g_pNavigatorFuncs->releaseobject(npobj);
    }
}

bool NPN_Invoke(NPP instance, NPObject *npobj, NPIdentifier methodName, const NPVariant *args, uint32_t argCount, NPVariant *result)
{
    int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
    if( navMinorVers >= 14 )
    {
        return g_pNavigatorFuncs->invoke(instance, npobj, methodName, args, argCount, result);
    }
    return false;
}

bool NPN_InvokeDefault(NPP instance, NPObject *npobj, const NPVariant *args, uint32_t argCount, NPVariant *result)
{
    int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
    if( navMinorVers >= 14 )
    {
        return g_pNavigatorFuncs->invokeDefault(instance, npobj, args, argCount, result);
    }
    return false;
}

bool NPN_Evaluate(NPP instance, NPObject *npobj, NPString *script, NPVariant *result)
{
    int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
    if( navMinorVers >= 14 )
    {
        return g_pNavigatorFuncs->evaluate(instance, npobj, script, result);
    }
    return false;
}

bool NPN_GetProperty(NPP instance, NPObject *npobj, NPIdentifier propertyName, NPVariant *result)
{
    int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
    if( navMinorVers >= 14 )
    {
        return g_pNavigatorFuncs->getproperty(instance, npobj, propertyName, result);
    }
    return false;
}

bool NPN_SetProperty(NPP instance, NPObject *npobj, NPIdentifier propertyName, const NPVariant *value)
{
    int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
    if( navMinorVers >= 14 )
    {
        return g_pNavigatorFuncs->setproperty(instance, npobj, propertyName, value);
    }
    return false;
}

bool NPN_RemoveProperty(NPP instance, NPObject *npobj, NPIdentifier propertyName)
{
    int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
    if( navMinorVers >= 14 )
    {
        return g_pNavigatorFuncs->removeproperty(instance, npobj, propertyName);
    }
    return false;
}

bool NPN_HasProperty(NPP instance, NPObject *npobj, NPIdentifier propertyName)
{
    int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
    if( navMinorVers >= 14 )
    {
        return g_pNavigatorFuncs->hasproperty(instance, npobj, propertyName);
    }
    return false;
}

bool NPN_HasMethod(NPP instance, NPObject *npobj, NPIdentifier methodName)
{
    int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
    if( navMinorVers >= 14 )
    {
        return g_pNavigatorFuncs->hasmethod(instance, npobj, methodName);
    }
    return false;
}

void NPN_ReleaseVariantValue(NPVariant *variant)
{
    int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
    if( navMinorVers >= 14 )
    {
        g_pNavigatorFuncs->releasevariantvalue(variant);
    }
}

void NPN_SetException(NPObject *npobj, const NPUTF8 *message)
{
    int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
    if( navMinorVers >= 14 )
    {
        g_pNavigatorFuncs->setexception(npobj, message);
    }
}
432

433
/* causes the specified URL to be fetched and streamed in
434
 */
435 436 437
NPError NPN_GetURLNotify(NPP instance, const char *url, const char *target, void* notifyData)

{
438 439 440 441 442 443 444 445 446
    int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
    NPError err;
    if( navMinorVers >= NPVERS_HAS_NOTIFICATION ) {
        err = g_pNavigatorFuncs->geturlnotify(instance, url, target, notifyData);
    }
    else {
        err = NPERR_INCOMPATIBLE_VERSION_ERROR;
    }
    return err;
447 448 449 450 451 452 453
}

NPError NPN_GetURL(NPP instance, const char *url, const char *target)
{
    return g_pNavigatorFuncs->geturl(instance, url, target);
}

454
NPError NPN_PostURLNotify(NPP instance, const char* url, const char* window, uint32_t len, const char* buf, NPBool file, void* notifyData)
455
{
456 457 458 459 460 461 462 463 464
    int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
    NPError err;
    if( navMinorVers >= NPVERS_HAS_NOTIFICATION ) {
        err = g_pNavigatorFuncs->posturlnotify(instance, url, window, len, buf, file, notifyData);
    }
    else {
        err = NPERR_INCOMPATIBLE_VERSION_ERROR;
    }
    return err;
465 466 467
}


468
NPError NPN_PostURL(NPP instance, const char* url, const char* window, uint32_t len, const char* buf, NPBool file)
469 470 471 472 473 474 475
{
    return g_pNavigatorFuncs->posturl(instance, url, window, len, buf, file);
}

/* Requests that a number of bytes be provided on a stream.  Typically
   this would be used if a stream was in "pull" mode.  An optional
   position can be provided for streams which are seekable.
476
 */
477 478 479 480 481 482
NPError NPN_RequestRead(NPStream* stream, NPByteRange* rangeList)
{
    return g_pNavigatorFuncs->requestread(stream, rangeList);
}

/* Creates a new stream of data from the plug-in to be interpreted
483 484
 * by Netscape in the current window.
 */
485
NPError NPN_NewStream(NPP instance, NPMIMEType type,
486
                      const char* target, NPStream** stream)
487
{
488 489
    int navMinorVersion = g_pNavigatorFuncs->version & 0xFF;
    NPError err;
490

491 492 493 494 495 496 497
    if( navMinorVersion >= NPVERS_HAS_STREAMOUTPUT ) {
        err = g_pNavigatorFuncs->newstream(instance, type, target, stream);
    }
    else {
        err = NPERR_INCOMPATIBLE_VERSION_ERROR;
    }
    return err;
498 499 500
}

/* Provides len bytes of data.
501
 */
502 503
int32_t NPN_Write(NPP instance, NPStream *stream,
                int32_t len, void *buffer)
504
{
505
    int navMinorVersion = g_pNavigatorFuncs->version & 0xFF;
506
    int32_t result;
507

508 509 510 511 512 513 514
    if( navMinorVersion >= NPVERS_HAS_STREAMOUTPUT ) {
        result = g_pNavigatorFuncs->write(instance, stream, len, buffer);
    }
    else {
        result = -1;
    }
    return result;
515 516
}

517 518 519
/* Closes a stream object.
 * reason indicates why the stream was closed.
 */
520 521
NPError NPN_DestroyStream(NPP instance, NPStream* stream, NPError reason)
{
522 523
    int navMinorVersion = g_pNavigatorFuncs->version & 0xFF;
    NPError err;
524

525 526 527 528 529 530 531
    if( navMinorVersion >= NPVERS_HAS_STREAMOUTPUT ) {
        err = g_pNavigatorFuncs->destroystream(instance, stream, reason);
    }
    else {
        err = NPERR_INCOMPATIBLE_VERSION_ERROR;
    }
    return err;
532 533 534
}

/* Provides a text status message in the Netscape client user interface
535
 */
536 537 538 539 540 541
void NPN_Status(NPP instance, const char *message)
{
    g_pNavigatorFuncs->status(instance, message);
}

/* returns the user agent string of Navigator, which contains version info
542
 */
543 544 545 546 547 548
const char* NPN_UserAgent(NPP instance)
{
    return g_pNavigatorFuncs->uagent(instance);
}

/* allocates memory from the Navigator's memory space.  Necessary so that
549 550
 * saved instance data may be freed by Navigator when exiting.
 */
551
void *NPN_MemAlloc(uint32_t size)
552 553 554 555 556
{
    return g_pNavigatorFuncs->memalloc(size);
}

/* reciprocal of MemAlloc() above
557
 */
558 559 560 561
void NPN_MemFree(void* ptr)
{
    g_pNavigatorFuncs->memfree(ptr);
}
562

563
#ifdef OJI
564
/* private function to Netscape.  do not use!
565
 */
566 567 568 569 570 571 572
void NPN_ReloadPlugins(NPBool reloadPages)
{
    g_pNavigatorFuncs->reloadplugins(reloadPages);
}

JRIEnv* NPN_GetJavaEnv(void)
{
573
    return g_pNavigatorFuncs->getJavaEnv();
574 575 576 577
}

jref NPN_GetJavaPeer(NPP instance)
{
578
    return g_pNavigatorFuncs->getJavaPeer(instance);
579
}
580
#endif