上一篇博客给出Firebreath版Hello World!的代码,这篇博客将对代码进行详细解析
Firebreath插件从本质上来讲,和一般的C++ DLL编程是一样的。区别在于,一般的C++ DLL提供的是C/C++接口以供外部调用,而Firebreath插件提供的是JS接口以供外部调用。因此,Firebreath实际上是提供了C++和JS相互调用的平台。
理解了这一点之后,就可以将一般C++ DLL编程经验全部转移到Firebreath编程之中了
一个刚创建的Firebreath插件工程如下图所示
WebTestAPI.h和WebTestAPI.cpp即DLL对外JS接口的声明和实现文件,上一篇博客中完整的WebTestAPI.h文件如下
/**********************************************************\
Auto-generated WebTestAPI.h
\**********************************************************/
#include <string>
#include <sstream>
#include <boost/weak_ptr.hpp>
#include "JSAPIAuto.h"
#include "BrowserHost.h"
#include "WebTest.h"
#ifndef H_WebTestAPI
#define H_WebTestAPI
//为便于使用添加命名空间的引用
using namespace std;
using namespace FB;
class WebTestAPI : public FB::JSAPIAuto
{
public:
////////////////////////////////////////////////////////////////////////////
/// @fn WebTestAPI::WebTestAPI(const WebTestPtr& plugin, const FB::BrowserHostPtr host)
///
/// @brief Constructor for your JSAPI object.
/// You should register your methods, properties, and events
/// that should be accessible to Javascript from here.
///
/// @see FB::JSAPIAuto::registerMethod
/// @see FB::JSAPIAuto::registerProperty
/// @see FB::JSAPIAuto::registerEvent
////////////////////////////////////////////////////////////////////////////
WebTestAPI(const WebTestPtr& plugin, const FB::BrowserHostPtr& host) :
m_plugin(plugin), m_host(host)
{
registerMethod("echo", make_method(this, &WebTestAPI::echo));
registerMethod("testEvent", make_method(this, &WebTestAPI::testEvent));
// Read-write property
registerProperty("testString",
make_property(this,
&WebTestAPI::get_testString,
&WebTestAPI::set_testString));
// Read-only property
registerProperty("version",
make_property(this,
&WebTestAPI::get_version));
//自定义功能
registerMethod("testPlugin", make_method(this, &WebTestAPI::testPlugin));
}
//////////////////////////////////////////