示例#1
0
文件: main.c 项目: ceda018/iguana_dev
/**
 * Given a message from JavaScript, parse it for functions and parameters.
 *
 * The format of the message is:
 * {
 *  "cmd": <function name>,
 *  "args": [<arg0>, <arg1>, ...]
 * }
 *
 * @param[in] message The message to parse.
 * @param[out] out_function The function name.
 * @param[out] out_params A PP_Var array.
 * @return 0 if successful, otherwise 1.
 */
static int ParseMessage(struct PP_Var message,const char **out_function,struct PP_Var *out_params)
{
    if ( message.type != PP_VARTYPE_DICTIONARY )
        return(1);
    struct PP_Var cmd_value = GetDictVar(message, "cmd");
    *out_function = VarToCStr(cmd_value);
    g_ppb_var->Release(cmd_value);
    *out_params = GetDictVar(message, "args");
    PostMessage("Parse.(%s) cmd.(%s)\n",*out_function,VarToCStr(*out_params));
    if ( cmd_value.type != PP_VARTYPE_STRING )
        return(1);
    if ( out_params->type != PP_VARTYPE_ARRAY )
        return(1);
    return(0);
}
示例#2
0
文件: main.c 项目: ceda018/iguana_dev
static void Messaging_HandleMessage(PP_Instance instance,struct PP_Var message)
{
    if ( message.type != PP_VARTYPE_DICTIONARY ) // Special case for jspipe input handling
    {
        PostMessage("Got unexpected message type: %d\n", message.type);
        return;
    }
    struct PP_Var pipe_var = CStrToVar("pipe");
    struct PP_Var pipe_name = g_ppb_var_dictionary->Get(message, pipe_var);
    g_ppb_var->Release(pipe_var);
    if ( pipe_name.type == PP_VARTYPE_STRING ) // Special case for jspipe input handling
    {
        char file_name[PATH_MAX];
        snprintf(file_name, PATH_MAX, "/dev/%s", VarToCStr(pipe_name));
        int fd = open(file_name, O_RDONLY);
        g_ppb_var->Release(pipe_name);
        if ( fd < 0 )
        {
            PostMessage("Warning: opening %s failed.", file_name);
            goto done;
        }
        //if ( ioctl(fd, NACL_IOC_HANDLEMESSAGE, &message) != 0 )
        //    PostMessage("Error: ioctl on %s failed: %s", file_name, strerror(errno));
        close(fd);
        goto done;
    }
    g_ppb_var->AddRef(message);
    if ( !EnqueueMessage(message) )
    {
        g_ppb_var->Release(message);
        PostMessage("Warning: dropped message because the queue was full.");
    }
done:
    g_ppb_var->Release(pipe_name);
}
示例#3
0
static void Messaging_HandleMessage(PP_Instance instance,
                                    struct PP_Var message) {
  char buffer[1024];
  VarToCStr(message, &buffer[0], 1024);
  if (!EnqueueMessage(strdup(buffer))) {
    struct PP_Var var;
    var = PrintfToVar(
        "Warning: dropped message \"%s\" because the queue was full.", message);
    ppb_messaging_interface->PostMessage(g_instance, var);
  }
}
示例#4
0
/**
 * Check existence of the function associated with @a name.
 * @param[in] object unused
 * @param[in] name method name
 * @param[out] exception pointer to the exception object, unused
 * @return If the method does exist, return true.
 * If the method does not exist, return false and don't set the exception.
 */
static bool
Squeak_HasMethod(void* object,
		 struct PP_Var name,
		 struct PP_Var* exception)
{
  const char* method_name = VarToCStr(name);
  if (NULL != method_name) {
    if (strcmp(method_name, kPaintMethodId) == 0)
      return true;
    if (strcmp(method_name, kGetStatusMethodId) == 0)
      return true;
  }
  return false;
}
示例#5
0
/**
 * Invoke the function associated with @a name.
 * @param[in] object unused
 * @param[in] name method name
 * @param[in] argc number of arguments
 * @param[in] argv array of arguments
 * @param[out] exception pointer to the exception object, unused
 * @return If the method does exist, return true.
 */
static struct PP_Var
Squeak_Call(void* object,
	    struct PP_Var name,
	    uint32_t argc,
	    struct PP_Var* argv,
	    struct PP_Var* exception)
{
  struct PP_Var v = PP_MakeInt32(0);
  const char* method_name = VarToCStr(name);
  if (NULL != method_name) {
    if (strcmp(method_name, kPaintMethodId) == 0) {
      Paint();
      return v;
    }
    if (strcmp(method_name, kGetStatusMethodId) == 0)
      return StrToVar(status);
  }
  return v;
}
示例#6
0
/**
 * Handle as message from JavaScript on the worker thread.
 *
 * @param[in] message The message to parse and handle.
 */
static void HandleMessage(struct PP_Var message) {
  pp_post_message("debug", "Received a message.");

  char* command;
  struct PP_Var params;
  if (ParseMessage(message, &command, &params)) {
    pp_post_message("error", "Unable to parse message.");
    return;
  }

  if (!strcmp(command, "k2pdfopt")) {
    pp_post_message("debug", "Received k2pdfopt command.");

    int numargs = g_ppb_var_array->GetLength(params);
    int argc = numargs + 1;
    char **argv;
    argv = malloc(argc * sizeof(char*));
    argv[0] = command;

    for (int i=0; i<numargs; i++) {
      struct PP_Var argvar = g_ppb_var_array->Get(params, i);
      argv[i+1] = VarToCStr(argvar);
      g_ppb_var->Release(argvar);
    }

    pp_post_message("status", "start");
    k2pdfoptmain(numargs+1, argv);
    pp_post_message("status", "done");

    // Fre argv[1...numargs+1]; argv[0] will be freed below by calling free(command)
    for (int i=1; i<numargs+1; i++) {
      free(argv[i]);
    }
    free(argv);
  }

  free(command);
}
示例#7
0
/**
 * Given a message from JavaScript, parse it for functions and parameters.
 *
 * The format of the message is:
 * {
 *  "cmd": <function name>,
 *  "args": [<arg0>, <arg1>, ...]
 * }
 *
 * @param[in] message The message to parse.
 * @param[out] out_function The function name.
 * @param[out] out_params A PP_Var array.
 * @return 0 if successful, otherwise 1.
 */
static int ParseMessage(struct PP_Var message,
                        char** out_function,
                        struct PP_Var* out_params) {
  if (message.type != PP_VARTYPE_DICTIONARY) {
    return 1;
  }

  struct PP_Var cmd_value = GetDictVar(message, "cmd");
  if (cmd_value.type != PP_VARTYPE_STRING) {
    g_ppb_var->Release(cmd_value);
    return 1;
  }
  *out_function = VarToCStr(cmd_value);
  g_ppb_var->Release(cmd_value);

  *out_params = GetDictVar(message, "args");
  if (out_params->type != PP_VARTYPE_ARRAY) {
    g_ppb_var->Release(*out_params);
    return 1;
  }

  return 0;
}