Native Client(NaCl)をやってみる (2) - NativeClient側でメッセージ受信 -

2012-07-15T12:00:00+09:00 C JavaScript Native Client

前回のはNative Client側からメッセージングでデータを送信して、それをNative Clientを使ってる側から受信するっていうのをやりました。今度はそれを双方向性にしてみようと思います。まぁ単純に

  • Native Client側からHTML側にデータを送る
  • HTML側からNative Client側にデータを送る (で受け取ったらデータをHTML側に送る)

index.html

<html>
  <body>
    <div id="message"></div>
    <script type="text/javascript">
(function(undefined) {
  window.addEventListener("message", function(e) {
    var h2 = document.createElement("h2");
    h2.innerText = e.data;

    document.getElementById("message").appendChild(h2);
  }, true);
})();

// 追加
function send() {
  document.getElementById("module").postMessage("abon");
}
    </script>

    <!-- 追加 -->
    <button onclick="send()">send</button>

    <embed name="nacl_module" id="module" width="0" height="0" src="/hoge.nmf" type="application/x-nacl" />
  </body>
</html>

単純にメッセージを受信する側な所は前と同じなんですが、メッセージを送信する処理を前回のに付け加える。んまぁ何をしているのかについては(ry

hoge.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "ppapi/c/pp_errors.h"
#include "ppapi/c/pp_module.h"
#include "ppapi/c/pp_var.h"
#include "ppapi/c/ppb.h"
#include "ppapi/c/ppb_instance.h"
#include "ppapi/c/ppb_messaging.h"
#include "ppapi/c/ppb_var.h"
#include "ppapi/c/ppp.h"
#include "ppapi/c/ppp_instance.h"
#include "ppapi/c/ppp_messaging.h"

static PPB_Messaging* ppb_messaging_interface = NULL;
static PPB_Var* ppb_var_interface = NULL;

static struct PP_Var StrToVar(const char* str) {
    if (ppb_var_interface != NULL) {
        return ppb_var_interface->VarFromUtf8(str, strlen(str));
    }

    return PP_MakeUndefined();
}

static PP_Bool Instance_DidCreate(PP_Instance instance, uint32_t argc, const char* argn[], const char* argv[]) {
    if (ppb_var_interface != NULL) {
        ppb_messaging_interface->PostMessage(instance, StrToVar("hoge fuga foobar"));

        return PP_TRUE;
    }

    return PP_FALSE;
}

static void Instance_DidDestroy(PP_Instance instance) {
}

static void Instance_DidChangeView(PP_Instance instance, PP_Resource view_resource) {
}

static void Instance_DidChangeFocus(PP_Instance instance, PP_Bool has_focus) {
}

static PP_Bool Instance_HandleDocumentLoad(PP_Instance instance, PP_Resource url_loader) {
    return PP_FALSE;
}

// 追加
static void HandleMessage(PP_Instance instance, struct PP_Var message) {
    if (ppb_messaging_interface != NULL) {
        ppb_messaging_interface->PostMessage(instance, StrToVar("ダァシェリアス!!"));
    }
}

PP_EXPORT int32_t PPP_InitializeModule(PP_Module a_module_id, PPB_GetInterface get_browser) {
    ppb_messaging_interface = (PPB_Messaging*)(get_browser(PPB_MESSAGING_INTERFACE));
    ppb_var_interface = (PPB_Var*)(get_browser(PPB_VAR_INTERFACE));

    return PP_OK;
}

PP_EXPORT const void* PPP_GetInterface(const char* interface_name) {
    printf("Interface: %sn", interface_name);

    if (strcmp(interface_name, PPP_INSTANCE_INTERFACE) == 0) {
        static PPP_Instance instance_interface = {
            &Instance_DidCreate,
            &Instance_DidDestroy,
            &Instance_DidChangeView,
            &Instance_DidChangeFocus,
            &Instance_HandleDocumentLoad
        };

        return &instance_interface;
    }

    // 追加
    if (strcmp(interface_name, PPP_MESSAGING_INTERFACE) == 0) {
        static PPP_Messaging messaging_interface = { &HandleMessage };

        return &messaging_interface;
    }

    return NULL;
}

PP_EXPORT void PPP_ShutdownModule() {
}

というような感じで、PPP_Messagingで指定したコールバックにHTML側からpostMessageをされるとそのコールバックでキャッチが出来る模様

Chrome Extension開発を勉強してみる (9) - webRequest - Native Client(NaCl)をやってみる