示例#1
0
文件: main.c 项目: ceda018/iguana_dev
/**
 * Create a response PP_Var to send back to JavaScript.
 * @param[out] response_var The response PP_Var.
 * @param[in] cmd The name of the function that is being executed.
 * @param[out] out_error An error message, if this call failed.
 */
static void CreateResponse(struct PP_Var* response_var,
                           const char* cmd,
                           const char** out_error) {
    PP_Bool result;

    struct PP_Var dict_var = g_ppb_var_dictionary->Create();
    struct PP_Var cmd_key = CStrToVar("cmd");
    struct PP_Var cmd_value = CStrToVar(cmd);

    result = g_ppb_var_dictionary->Set(dict_var, cmd_key, cmd_value);
    g_ppb_var->Release(cmd_key);
    g_ppb_var->Release(cmd_value);

    if (!result) {
        g_ppb_var->Release(dict_var);
        *out_error =
            PrintfToNewString("Unable to set \"cmd\" key in result dictionary");
        return;
    }

    struct PP_Var args_key = CStrToVar("args");
    struct PP_Var args_value = g_ppb_var_array->Create();
    result = g_ppb_var_dictionary->Set(dict_var, args_key, args_value);
    g_ppb_var->Release(args_key);
    g_ppb_var->Release(args_value);

    if (!result) {
        g_ppb_var->Release(dict_var);
        *out_error =
            PrintfToNewString("Unable to set \"args\" key in result dictionary");
        return;
    }

    *response_var = dict_var;
}
示例#2
0
/**
 * Post a message to JavaScript.
 * @param[in] format A printf format string.
 * @param[in] ... The printf arguments.
 */
int pp_post_message(const char* category, const char* format, ...) {
  va_list args;

  struct PP_Var dict_var = g_ppb_var_dictionary->Create();

  struct PP_Var cat_key = CStrToVar("category");
  struct PP_Var cat_value = CStrToVar(category);
  PP_Bool result = g_ppb_var_dictionary->Set(dict_var, cat_key, cat_value);
  g_ppb_var->Release(cat_key);
  g_ppb_var->Release(cat_value);

  if (!result) {
    g_ppb_var->Release(dict_var);
    return 1;
  }

  struct PP_Var msg_key = CStrToVar("msg");
  va_start(args, format);
  struct PP_Var msg_value = VprintfToVar(format, args);
  va_end(args);

  result = g_ppb_var_dictionary->Set(dict_var, msg_key, msg_value);
  g_ppb_var->Release(msg_key);
  g_ppb_var->Release(msg_value);

  if (!result) {
    g_ppb_var->Release(dict_var);
    return 2;
  }

  g_ppb_messaging->PostMessage(g_instance, dict_var);
  g_ppb_var->Release(dict_var);

  return 0;
}
void PostMessage(const char *str) {
  struct PP_CompletionCallback cb;

  if (NULL == str) return;
  if (NULL == ppb_messaging_interface) return;
  if (0 == g_Instance) return;

  if (strncmp(str, "ERR:", 4)) {
    fprintf(stderr, "%s\n", str);
    fflush(stderr);
  }
  else {
    fprintf(stdout, "%s\n", str);
    fflush(stdout);
  }

  /* If on Main Pepper thread, then call interface directly. */
  if (pthread_self() == g_PPAPIThread) {
    ppb_messaging_interface->PostMessage(g_Instance, CStrToVar(str));
    return;
  }

  /* Otherwise use call on main thread. */
  cb = PP_MakeCompletionCallback(PostCompletionCallback, strdup(str));
  ppb_core_interface->CallOnMainThread(0, cb, 0);
}
示例#4
0
文件: main.c 项目: ceda018/iguana_dev
/**
 * Append a string to the response dictionary.
 * @param[in,out] response_var The response PP_var.
 * @param[in] value The value to add to the response args.
 * @param[out] out_error An error message, if this call failed.
 */
static void AppendResponseString(struct PP_Var* response_var,
                                 const char* value,
                                 const char** out_error) {
    struct PP_Var value_var = CStrToVar(value);
    AppendResponseVar(response_var, value_var, out_error);
    g_ppb_var->Release(value_var);
}
示例#5
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);
}
示例#6
0
文件: main.c 项目: ceda018/iguana_dev
struct PP_Var GetDictVar(struct PP_Var dict, const char* key)
{
    struct PP_Var key_var = CStrToVar(key);
    struct PP_Var value = g_ppb_var_dictionary->Get(dict, key_var);
    g_ppb_var->Release(key_var);
    return value;
}
/**
 * Called when the NaCl module is instantiated on the web page. The identifier
 * of the new instance will be passed in as the first argument (this value is
 * generated by the browser and is an opaque handle).  This is called for each
 * instantiation of the NaCl module, which is each time the <embed> tag for
 * this module is encountered.
 *
 * If this function reports a failure (by returning @a PP_FALSE), the NaCl
 * module will be deleted and DidDestroy will be called.
 * @param[in] instance The identifier of the new instance representing this
 *     NaCl module.
 * @param[in] argc The number of arguments contained in @a argn and @a argv.
 * @param[in] argn An array of argument names.  These argument names are
 *     supplied in the <embed> tag, for example:
 *       <embed id="nacl_module" dimensions="2">
 *     will produce two arguments, one named "id" and one named "dimensions".
 * @param[in] argv An array of argument values.  These are the values of the
 *     arguments listed in the <embed> tag.  In the above example, there will
 *     be two elements in this array, "nacl_module" and "2".  The indices of
 *     these values match the indices of the corresponding names in @a argn.
 * @return @a PP_TRUE on success.
 */
static PP_Bool Instance_DidCreate(PP_Instance instance,
                                  uint32_t argc,
                                  const char* argn[],
                                  const char* argv[]) {
  ppb_messaging_interface->PostMessage(instance,
                                       CStrToVar("Hello a World (PNACL)"));
  return PP_TRUE;
}
示例#8
0
static PP_Bool Instance_DidCreate(PP_Instance instance,
                                  uint32_t argc,
                                  const char* argn[],
                                  const char* argv[]) {

	PP_Var v =  CStrToVar("Hello a World (NEWLIB)");
  ppb_messaging_interface->PostMessage(instance,v);
  //fprintf(stdout,"Hello a World (NEWLIB)");

  initInstance(); //CLM ADDED
  return PP_TRUE;
}
示例#9
0
/**
 * Post a "progress" message to JavaScript. It has the form
 * {
 *   "category": "progress",
 *   "current": <number>,
 *   "total": <number>
 * }
 */
int pp_post_progress(int current_page, int page_count) {
  struct PP_Var dict_var = g_ppb_var_dictionary->Create();

  struct PP_Var cat_key = CStrToVar("category");
  struct PP_Var cat_value = CStrToVar("progress");
  PP_Bool result = g_ppb_var_dictionary->Set(dict_var, cat_key, cat_value);
  g_ppb_var->Release(cat_key);
  g_ppb_var->Release(cat_value);
  if (!result) {
    g_ppb_var->Release(dict_var);
    return 1;
  }

  struct PP_Var cur_key = CStrToVar("current");
  struct PP_Var cur_value = PP_MakeInt32(current_page);
  result = g_ppb_var_dictionary->Set(dict_var, cur_key, cur_value);
  g_ppb_var->Release(cur_key);
  g_ppb_var->Release(cur_value);
  if (!result) {
    g_ppb_var->Release(dict_var);
    return 2;
  }

  struct PP_Var total_key = CStrToVar("total");
  struct PP_Var total_value = PP_MakeInt32(page_count);
  result = g_ppb_var_dictionary->Set(dict_var, total_key, total_value);
  g_ppb_var->Release(total_key);
  g_ppb_var->Release(total_value);
  if (!result) {
    g_ppb_var->Release(dict_var);
    return 3;
  }

  g_ppb_messaging->PostMessage(g_instance, dict_var);
  g_ppb_var->Release(dict_var);

  return 0;
}
示例#10
0
/** Handle as message from JavaScript on the worker thread.
 *
 * @param[in] message The message to parse and handle. */
static void HandleMessage(char* message) {
  char* function_name;
  char* params[MAX_PARAMS];
  size_t num_params;
  char* output = NULL;
  int result;
  HandleFunc function;

  num_params = ParseMessage(message, &function_name, &params[0], MAX_PARAMS);

  function = GetFunctionByName(function_name);
  if (!function) {
    /* Function name wasn't found. Error. */
    ppb_messaging_interface->PostMessage(
        g_instance, PrintfToVar("Error: Unknown function \"%s\"", function));
  }

  /* Function name was found, call it. */
  result = (*function)(num_params, &params[0], &output);
  if (result != 0) {
    /* Error. */
    struct PP_Var var;
    if (output != NULL) {
      var = PrintfToVar("Error: Function \"%s\" returned error %d. "
                        "Additional output: %s",
                        function_name,
                        result,
                        output);
      free(output);
    } else {
      var = PrintfToVar(
          "Error: Function \"%s\" returned error %d.", function_name, result);
    }

    /* Post the error to JavaScript, so the user can see it. */
    ppb_messaging_interface->PostMessage(g_instance, var);
    return;
  }

  if (output != NULL) {
    /* Function returned an output string. Send it to JavaScript. */
    ppb_messaging_interface->PostMessage(g_instance, CStrToVar(output));
    free(output);
  }
}
示例#11
0
static void HandleMessage(char* message) {
  char* function_name;
  char* params[MAX_PARAMS];
  size_t num_params;
  char* output = NULL;
  int result;
  HandleFunc function;

  num_params = ParseMessage(message, &function_name, &params[0], MAX_PARAMS);

  function = GetFunctionByName(function_name);
  if (!function) {
    
    ppb_messaging_interface->PostMessage(
        g_instance,
        PrintfToVar("Error: Unknown function \"%s\"", function_name));
    return;
  }

  
  result = (*function)(num_params, &params[0], &output);
  if (result != 0) {
    
    struct PP_Var var;
    if (output != NULL) {
      var = PrintfToVar("Error: \"%s\" failed: %d: %s.", function_name,
                        result, output);
      free(output);
    } else {
      var = PrintfToVar(
          "Error: \"%s\" failed: %d.", function_name, result);
    }

    
    ppb_messaging_interface->PostMessage(g_instance, var);
    return;
  }

  if (output != NULL) {
    
    ppb_messaging_interface->PostMessage(g_instance, CStrToVar(output));
    free(output);
  }
}
示例#12
0
static void PostCompletionCallback(void* user_data, int32_t result) {
  ppb_messaging_interface->PostMessage(g_Instance, CStrToVar(user_data));
  free(user_data);
}
void SendString(const char* str)
{
	psNaCLContext->psMessagingInterface->PostMessage(psNaCLContext->hModule, CStrToVar(str));
}
示例#14
0
static void PostCompletionCallback(void* user_data, int32_t result) {
    const char* str = (const char*)user_data;
    ppb_messaging_interface->PostMessage(g_Instance, CStrToVar(str));
    free(user_data);
}