铬NaCl延伸。 postMessage无法正常工作

时间:2014-06-09 16:14:21

标签: javascript google-chrome google-chrome-extension chromium ppapi

我创建了使用NaCl的扩展。 我的示例扩展,通过postMessage发送消息到NaCl模块并将信息保存到文件中。 在谷歌浏览器上(版本35)运行良好。 但是当我将它安装到Chromium浏览器ver.34时,这不起作用。

清单:

{
  "manifest_version": 2,
  "name": "Test Ext",
  "version": "0.1",
  "icons": {"16": "ico/ico16.png", "48": "ico/ico48.png", "128": "ico/ico128.png"},
  "background": { "page": "index.html" },
  "description": "Test Ext",
  "permissions": [
     "tabs", "http://*/*", "unlimitedStorage"
  ]
}

JavaScript代码:

window.addEventListener("DOMContentLoaded", docLoaded, false);

var NaclMod = null;

function docLoaded() {
    moduleDidLoad();
}

function moduleDidLoad() {
    NaclMod = document.getElementById('test_nacl');

    if (NaclMod == null) {
        console.log('Module not load');
    }
    else {
        console.log (NaclMod);       
    }

    NaclMod.addEventListener('message', handleMessage, false);
}

function handleMessage(message) {
    console.log(message.data);
}

的index.html

<body data-name="test" data-tools="newlib glibc pnacl linux" data-configs="Release" data-path="{tc}/{config}">
    <div id="listener">
        <embed name="test" id="test_nacl" width=0 height=0 src="test.nmf" type="application/x-nacl" />
    </div>
</body>

test.cc

#include <sstream>
#include <string>

#include "ppapi/c/pp_stdint.h"
#include "ppapi/c/ppb_file_io.h"
#include "ppapi/cpp/file_io.h"
#include "ppapi/cpp/file_ref.h"
#include "ppapi/cpp/file_system.h"
#include "ppapi/cpp/instance.h"
#include "ppapi/cpp/message_loop.h"
#include "ppapi/cpp/module.h"
#include "ppapi/cpp/var.h"
#include "ppapi/utility/completion_callback_factory.h"
#include "ppapi/utility/threading/simple_thread.h"

class TestInstance: public pp::Instance {
public:
    explicit TestInstance(PP_Instance instance) 
              : pp::Instance(instance),
                callbackFactory(this),
                fileSystem(this, PP_FILESYSTEMTYPE_LOCALPERSISTENT),
                fileSystemReady(false),
                fileThread(this) {}

    virtual ~TestInstance() { fileThread.Join(); }

    virtual bool Init(uint32_t, const char* [], const char* []) {
        fileThread.Start();
        fileThread.message_loop().PostWork(callbackFactory.NewCallback(&TestInstance::OpenFileSystem));        
        return true;
    }

private:
    pp::CompletionCallbackFactory<TestInstance> callbackFactory;
    pp::FileSystem fileSystem;
    bool fileSystemReady;
    pp::SimpleThread fileThread;

    virtual void HandleMessage(const pp::Var& var_message) {
        if(!var_message.is_string())
            return;

        std::string filePath = "/test.txt";
        std::string message = var_message.AsString();
        if (!message.empty()) {
            pp::Var var_reply(message);
            PostMessage(var_reply);
            fileThread.message_loop().PostWork(callbackFactory.NewCallback(&TestInstance::Save, filePath, message));
        }
    }

    void OpenFileSystem(int32_t) {
        int32_t rv = fileSystem.Open(1024*1024, pp::BlockUntilComplete());
        if (rv == PP_OK) {
            fileSystemReady = true;
            PostMessage("READY|");
        } else {
            ShowErrorMessage("Failed to open file system", rv);
        }
    }

  void Save(int32_t /*result*/,
            const std::string& file_name,
            const std::string& file_contents) {
    if (!fileSystemReady) {
      ShowErrorMessage("File system is not open", PP_ERROR_FAILED);
      return;
    }
    pp::FileRef ref(fileSystem, file_name.c_str());
    pp::FileIO file(this);

    int32_t open_result =
        file.Open(ref,
                  PP_FILEOPENFLAG_WRITE | PP_FILEOPENFLAG_CREATE |
                      PP_FILEOPENFLAG_TRUNCATE,
                  pp::BlockUntilComplete());
    if (open_result != PP_OK) {
      ShowErrorMessage("File open for write failed", open_result);
      return;
    }

    if (!file_contents.empty()) {
      if (file_contents.length() > INT32_MAX) {
        ShowErrorMessage("File too big", PP_ERROR_FILETOOBIG);
        return;
      }
      int64_t offset = 0;
      int32_t bytes_written = 0;
      do {
        bytes_written = file.Write(offset,
                                   file_contents.data() + offset,
                                   file_contents.length(),
                                   pp::BlockUntilComplete());
        if (bytes_written > 0) {
          offset += bytes_written;
        } else {
          ShowErrorMessage("File write failed", bytes_written);
          return;
        }
      } while (bytes_written < static_cast<int64_t>(file_contents.length()));
    }

    int32_t flush_result = file.Flush(pp::BlockUntilComplete());
    if (flush_result != PP_OK) {
      ShowErrorMessage("File fail to flush", flush_result);
      return;
    }
    ShowStatusMessage("Save success");
  }

  void ShowStatusMessage(const std::string& message) {
    std::stringstream ss;
    ss << "STAT|" << message;
    PostMessage(ss.str());
  }

  void ShowErrorMessage(const std::string& message, int32_t result) {
    std::stringstream ss;
    ss << "ERR|" << message << " -- Error #: " << result;
    PostMessage(ss.str());
  }
};

class TestModule: public pp::Module {

public:
    TestModule() : pp::Module() {}
    virtual ~TestModule() {}

    virtual pp::Instance* CreateInstance(PP_Instance instance) {
        return new TestInstance(instance);
    }

};

namespace pp {
    Module* CreateModule() {
        return new TestModule();
    }
}

我用pepper_27构建NaCl模块

P.S。在标志中的Chromium和Chrome上,我启用了Native Client而没有调试

0 个答案:

没有答案