Javascript 铬氯化钠延伸。postMessage不起作用
我创建了使用NaCl的扩展。 在我的示例扩展中,通过postMessage将消息发送到NaCl模块,并将信息保存到文件中。 谷歌浏览器(35版)运行良好。 但当我将其安装到Chromium browser ver.34时,它无法工作 清单:Javascript 铬氯化钠延伸。postMessage不起作用,javascript,google-chrome,google-chrome-extension,chromium,ppapi,Javascript,Google Chrome,Google Chrome Extension,Chromium,Ppapi,我创建了使用NaCl的扩展。 在我的示例扩展中,通过postMessage将消息发送到NaCl模块,并将信息保存到文件中。 谷歌浏览器(35版)运行良好。 但当我将其安装到Chromium browser ver.34时,它无法工作 清单: { "manifest_version": 2, "name": "Test Ext", "version": "0.1", "icons": {"16": "ico/ico16.png", "48": "ico/ico48.png", "1
{
"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"
]
}
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);
}
JavaScript代码:
{
"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"
]
}
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();
}
}
#包括
#包括
#包括“ppapi/c/pp_stdint.h”
#包括“ppapi/c/ppb_文件_io.h”
#包括“ppapi/cpp/file_io.h”
#包括“ppapi/cpp/file_ref.h”
#包括“ppapi/cpp/file_system.h”
#包括“ppapi/cpp/instance.h”
#包括“ppapi/cpp/message_loop.h”
#包括“ppapi/cpp/module.h”
#包括“ppapi/cpp/var.h”
#包括“ppapi/utility/completion\u callback\u factory.h”
#包括“ppapi/utility/threading/simple_thread.h”
类TestInstance:公共pp::实例{
公众:
显式测试(PP_实例)
:pp::实例(实例),
callbackFactory(此),
文件系统(这是PP\u FILESYSTEMTYPE\u LOCALPERSISTENT),
fileSystemReady(错误),
fileThread(this){}
virtual~TestInstance(){fileThread.Join();}
虚拟布尔初始化(uint32\u t,常量字符*[],常量字符*[])){
fileThread.Start();
fileThread.message_loop().PostWork(callbackFactory.NewCallback(&TestInstance::OpenFileSystem));
返回true;
}
私人:
pp::CompletionCallbackFactory callbackFactory;
文件系统;
bool文件系统就绪;
pp::SimpleThread文件线程;
虚拟void HandleMessage(const pp::Var&Var_消息){
如果(!var_message.is_string())
返回;
std::string filePath=“/test.txt”;
std::string message=var_message.AsString();
如果(!message.empty()){
pp::Var Var_回复(消息);
邮递信息(var_回复);
fileThread.message_loop().PostWork(callbackFactory.NewCallback(&TestInstance::Save,filePath,message));
}
}
void OpenFileSystem(int32\t){
int32_t rv=fileSystem.Open(1024*1024,pp::BlockUntilComplete());
如果(rv==PP_正常){
fileSystemReady=true;
PostMessage(“READY |”);
}否则{
错误消息(“打开文件系统失败”,rv);
}
}
无效保存(int32_t/*结果*/,
常量std::字符串和文件名,
const std::字符串和文件(内容){
如果(!fileSystemReady){
错误消息(“文件系统未打开”,PP\u错误\u失败);
返回;
}
pp::FileRef-ref(文件系统,file_name.c_str());
pp::FileIO文件(此文件);
int32\u t打开\u结果=
文件。打开(参考,
PP_FILEOPENFLAG_WRITE|PP_FILEOPENFLAG_CREATE|
PP_FILEOPENFLAG_TRUNCATE,
pp::BlockUntilComplete());
如果(打开结果!=PP\U确定){
错误消息(“文件打开写入失败”,打开结果);
返回;
}
如果(!file_contents.empty()){
如果(文件内容.length()>INT32\u MAX){
错误消息(“文件太大”,PP\u错误\u文件太大);
返回;
}
int64_t offset=0;
写入的int32字节=0;
做{
字节写入=文件写入(偏移量,
文件内容.data()+偏移量,
文件\u contents.length(),
pp::BlockUntilComplete());
如果(写入的字节数>0){
偏移量+=写入的字节数;
}否则{
错误消息(“文件写入失败”,写入字节);
返回;
}
}while(bytes_write ss您试图让它在旧版本上运行有什么特别的原因吗?没有,我只尝试保存文件。我创建了这个示例扩展来研究PPAPI。这个扩展是我在Linux(基本操作系统)上构建的。我可以上传*.crx文件并发送链接给您进行测试