【Firebreath】基础教程之四:基本编程

Firebreath插件编程详解:C++与JS互调
本文深入解析Firebreath插件的编程原理,阐述其如何提供JS接口供外部调用,并展示C++调用JS函数的实现。通过示例代码解释了WebTestAPI.h和WebTestAPI.cpp中函数的用途,包括C++回调JS函数的机制。同时,介绍了如何在HTML中嵌入和使用Firebreath插件,以及页面绘图的应用,如视频播放和鼠标画图。

上一篇博客给出Firebreath版Hello World!的代码,这篇博客将对代码进行详细解析

Firebreath插件从本质上来讲,和一般的C++ DLL编程是一样的。区别在于,一般的C++ DLL提供的是C/C++接口以供外部调用,而Firebreath插件提供的是JS接口以供外部调用。因此,Firebreath实际上是提供了C++和JS相互调用的平台。

理解了这一点之后,就可以将一般C++ DLL编程经验全部转移到Firebreath编程之中了

一个刚创建的Firebreath插件工程如下图所示

这里写图片描述

WebTestAPI.hWebTestAPI.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));
    }

    //////////////////////////////////////////
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值