home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / libhook / src / hk_hook.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  3.7 KB  |  178 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18. #include "xp_core.h"
  19. #include "xp_mcom.h"
  20. #include "jsapi.h"
  21. #include "prefapi.h"
  22. #include "hk_funcs.h"
  23.  
  24. #include "hk_private.h"
  25.  
  26.  
  27. static JSObject *HookObject = NULL;
  28.  
  29.  
  30. JSObject *
  31. hk_GetHookObject(void)
  32. {
  33.     return HookObject;
  34. }
  35.  
  36.  
  37. void
  38. hk_SetHookObject(JSObject *hook_obj)
  39. {
  40.     HookObject = hook_obj;
  41. }
  42.  
  43.  
  44. /*
  45.  * Some hooks have dynamic names and can't be in the
  46.  * fast hook array lookup, this covers looking up those
  47.  * hooks.  For example, if I invented a <BLURT> tag,
  48.  * this would look for a BLURT_hook function.
  49.  */
  50. intn
  51. hk_IsUnknownTagHook(void *extra)
  52. {
  53.     const char *hook_func;
  54.     jsval tmp_val;
  55.     JSContext *j_context;
  56.  
  57.     hook_func = HK_GetFunctionName(HK_TAG, extra);
  58.     if (hook_func == NULL)
  59.     {
  60.         return 0;
  61.     }
  62.  
  63.     if (!PREF_GetConfigContext(&j_context))
  64.     {
  65.         return 0;
  66.     }
  67.  
  68.     if ((j_context == NULL)||(HookObject == NULL))
  69.     {
  70.         return 0;
  71.     }
  72.  
  73.     if ((JS_GetProperty(j_context, HookObject, hook_func, &tmp_val))&&
  74.         (JS_TypeOfValue(j_context, tmp_val) == JSTYPE_FUNCTION))
  75.     {
  76.         return 1;
  77.     }
  78.  
  79.     return 0;
  80. }
  81.  
  82.  
  83. /*
  84.  * There is no guarantee that someone has checked the existence of the
  85.  * hook before calling it, so we check again here.  We don't use the
  86.  * fast array lookup since it can't catch all hooks anyway, and we
  87.  * may well have to fall back to the slow method.
  88.  */
  89. intn
  90. HK_CallHook(int32 hook_id, void *extra, int32 window_id,
  91.         char *hook_str, char **hook_ret)
  92. {
  93.     const char *hook_func;
  94.     intn ok;
  95.     jsval tmp_val;
  96.     jsval argv[2];
  97.     JSContext *j_context;
  98.     JSString *str;
  99.     const char *result_str;
  100.     char *return_str;
  101.  
  102.     *hook_ret = NULL;
  103.  
  104.     if (hook_str == NULL)
  105.     {
  106.         return 0;
  107.     }
  108.  
  109.     /*
  110.      * Make a function name form the hook_is and possibly the PA_Tag
  111.      * pointed to by extra.
  112.      */
  113.     hook_func = HK_GetFunctionName(hook_id, extra);
  114.     if (hook_func == NULL)
  115.     {
  116.         return 0;
  117.     }
  118.  
  119.     /*
  120.      * Make sure you have a javascript context and object.
  121.      */
  122.     if (!PREF_GetConfigContext(&j_context))
  123.     {
  124.         return 0;
  125.     }
  126.     if ((j_context == NULL)||(HookObject == NULL))
  127.     {
  128.         return 0;
  129.     }
  130.  
  131.     /*
  132.      * Check that there is a function to call.
  133.      */
  134.     if ((!JS_GetProperty(j_context, HookObject, hook_func, &tmp_val))||
  135.         (JS_TypeOfValue(j_context, tmp_val) != JSTYPE_FUNCTION))
  136.     {
  137.         return 0;
  138.     }
  139.  
  140.     /*
  141.      * Create the argument/parameter list, and call the function.
  142.      */
  143.     str = JS_NewStringCopyZ(j_context, hook_str);
  144.     if (str == NULL)
  145.     {
  146.         return 0;
  147.     }
  148.     argv[0] = STRING_TO_JSVAL(str);
  149.     argv[1] = INT_TO_JSVAL(window_id);
  150.     if (!JS_CallFunctionName(j_context, HookObject,    hook_func, 2, argv,
  151.                              &tmp_val))
  152.     {
  153.         return 0;
  154.     }
  155.  
  156.     /*
  157.      * A false return from the function means leave the
  158.      * passed string unchanged.
  159.      */
  160.     if (tmp_val != JSVAL_FALSE)
  161.     {
  162.         str = JS_ValueToString(j_context, tmp_val);
  163.         if (str == NULL)
  164.         {
  165.             return 0;
  166.         }
  167.         result_str = JS_GetStringBytes(str);
  168.         return_str = XP_STRDUP(result_str);
  169.         if (return_str == NULL)
  170.         {
  171.             return 0;
  172.         }
  173.         *hook_ret = return_str;
  174.     }
  175.  
  176.     return 1;
  177. }
  178.