Title:Chrome Native Messaging技术示例
Author: kagula
Revison: 1
First publish Date:2015-10-23
Last modify date:2015-10-23
环境:
[1]Visaul Stuio 2013 Update4
[2]Google chrome 45.0.2454.101
[3]Windows7 SP1 64bits
正文
在Chrome浏览器中,Native Messaging是唯一能让web page调用Native API的技术,这里记录要达到这个目的所要涉及到的知识。
最后由native messaging host access native OS API.
本文主体由native messaging host、extension、web page三部份组成。
这里基于google echo示例代码做了大量修改,用于实现web page同native application的通讯。
第一部份 native messaging host
native messaging host从stdin读取消息从stdout中返回消息,通过stderr返回错误消息给chrome。
一次最多能向native messaging host发送4GB字节的请求,native messaging host一次最多能返回1M字节的消息。
这部份由C++源文件和manifest文件两部份组成:
在VisualStudio中新建chrome_native_messaging_host win32 console project,把下面的文件加进去并编译。
下面是native messaing host源文件清单
下面是com.google.chrome.example.echo-win.json源文清单
manifest文件和C++源
Author: kagula
Revison: 1
First publish Date:2015-10-23
Last modify date:2015-10-23
环境:
[1]Visaul Stuio 2013 Update4
[2]Google chrome 45.0.2454.101
[3]Windows7 SP1 64bits
正文
在Chrome浏览器中,Native Messaging是唯一能让web page调用Native API的技术,这里记录要达到这个目的所要涉及到的知识。
web page、extension和native messaging host三者之间用UTF-8编码的json字符串通讯。
最后由native messaging host access native OS API.
本文主体由native messaging host、extension、web page三部份组成。
这里基于google echo示例代码做了大量修改,用于实现web page同native application的通讯。
第一部份 native messaging host
native messaging host从stdin读取消息从stdout中返回消息,通过stderr返回错误消息给chrome。
一次最多能向native messaging host发送4GB字节的请求,native messaging host一次最多能返回1M字节的消息。
这部份由C++源文件和manifest文件两部份组成:
在VisualStudio中新建chrome_native_messaging_host win32 console project,把下面的文件加进去并编译。
下面是native messaing host源文件清单
#include "stdafx.h"
/*
Title: native messaging c++ program
Author: kagula
Date: 2015-10-22
*/
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
#include "SimpleLog.h"
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
void sendMessage(const string &strMsg)
{
// We need to send the 4 bytes of length information
unsigned int len = strMsg.length();
std::cout << char(((len >> 0) & 0xFF))
<< char(((len >> 8) & 0xFF))
<< char(((len >> 16) & 0xFF))
<< char(((len >> 24) & 0xFF));
//output integer value directly will lead byte order problem.
// Now we can output our message
cout << strMsg;
cout.flush();
}
simpleLogClass logger;
int _tmain(int argc, _TCHAR* argv[])
{
logger.fileMaxSize = 64*1024;
logger.fileName = "e:\\a.log";
logger.fileOldName = "e:\\a.old.log";
_setmode(_fileno(stdin), O_BINARY);
_setmode(_fileno(stdout), O_BINARY);
_setmode(_fileno(stderr), O_BINARY);
int bufSize;
do
{
bufSize = 0;
//unlike >> operator and scanf function,
//read function will wait until read all 4 bytes!
cin.read((char*)&bufSize, 4);
stringstream ss;
ss << "bufSize = " << bufSize << endl;
logger.info(ss.str());
if (bufSize> 0)
{
char *pData = new char[bufSize+1];
memset(pData, 0, bufSize + 1);
cin.read(pData, bufSize);
string response = "{\"echo\":";
response.append(pData);
response.append("}");
sendMessage(response);
logger.info(response);
delete pData;
} else
{
break;
}
} while (true);
return 0;
}
下面是com.google.chrome.example.echo-win.json源文清单
{
"name": "com.google.chrome.example.echo",
"description": "Chrome Native Messaging API Example Host",
"path": "chrome_native_messaging_host.exe",
"type": "stdio",
"allowed_origins": [
"chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/"
]
}
manifest文件和C++源