一、Fancybrowser 浏览器开发
1、webkitplugininterface.h
#ifndef WEBKITPLUGININTERFACE_H
#define WEBKITPLUGININTERFACE_H
#include <QWebPluginFactory>
class WebKitPluginInterface
{
public:
WebKitPluginInterface(){};
virtual ~WebKitPluginInterface(){};
virtual QList<QWebPluginFactory::Plugin> plugins()const =0;
virtual QObject *create(const QString &mimeType,
const QUrl &url,
const QStringList &argumentNames,
const QStringList &argumentValues) const =0;
};
//声明WebKitPluginInterface为一个接口
Q_DECLARE_INTERFACE(WebKitPluginInterface, "com.plugin.uvchip.www/1.0")
#endif // WEBKITPLUGININTERFACE_H
2、webkitpluginfactory.h
#ifndef WEBKITPLUGINFACTORY_H
#define WEBKITPLUGINFACTORY_H
#include <QWebPluginFactory>
#include <QUrl>
#include "webkitplugininterface.h"
class WebkitPluginFactory : public QWebPluginFactory
{
Q_OBJECT
public:
WebkitPluginFactory();
QObject *create ( const QString & mimeType, const QUrl & url, const QStringList & argumentNames, const QStringList & argumentValues ) const;
QList<QWebPluginFactory::Plugin> plugins () const;
private:
mutable QList<QList<QWebPluginFactory::Plugin> > pluginslist; // 插件列表
mutable QList<WebKitPluginInterface *> interfaces; //插件接口,这个接口是我们自定义的插件的同意接口。
};
#endif // WEBKITPLUGINFACTORY_H
3、webkitpluginfactory.cpp
#include "webkitpluginfactory.h"
#include <QPluginLoader>
#include <QDebug>
#include <QDir>
WebkitPluginFactory::WebkitPluginFactory() : QWebPluginFactory()
{
qDebug()<<"=== jhluroom === WebkitPluginFactory::WebkitPluginFactory";
}
QList<QWebPluginFactory::Plugin> WebkitPluginFactory::plugins () const
{
qDebug()<<"=== jhluroom === WebkitPluginFactory::create";
const char *pluginPath = "/home/jhlu/qt/qt-project/webkit/flashplugin-build-desktop";
static bool isFirst=true;
static QList<QWebPluginFactory::Plugin> plugins;
if(!isFirst)
{
return plugins;
}
isFirst=false;
plugins.clear();
QDir dir(pluginPath);
QStringList filters;
QString abspath=dir.absolutePath();
qDebug()<<"the webkit plugin dir is:"<< abspath;
//获取指定目录下的所有插件,linux下是插件库的后缀为so,windows下则是dll
filters<<"lib*.so";
QStringList files=dir.entryList(filters);
foreach(QString file,files)
{
file=dir.filePath(file);
qDebug()<<"the webkit plugin path is: "<<file;
QPluginLoader loader(file);
QObject * obj= loader.instance();
if(obj==0){qDebug()<<"error: "<<loader.errorString();}
WebKitPluginInterface * interface= qobject_cast<WebKitPluginInterface*> (obj);//载入自定义的接口,支持动态插件创建
qDebug()<<"loading plugins is: "<<interface->plugins().at(0).name;
plugins.append(interface->plugins());
pluginslist.append(interface->plugins());
interfaces.append(interface);
}
if(plugins.isEmpty()){
qDebug()<<"no plugins is loaded!";
}
return plugins;
}
QObject * WebkitPluginFactory::create ( const QString & mimeType, const QUrl & url, const QStringList & argumentNames, const QStringList & argumentValues ) const
{
qDebug()<<"=== jhluroom === WebkitPluginFactory::create";
for(int i=0;i<pluginslist.size();i++)
{
for( int j=0;j< pluginslist[i].size();j++)
{
foreach(QWebPluginFactory::MimeType mt, pluginslist[i][j].mimeTypes)
{
if(mt.name == mimeType) //根据MIME类型,创建相应的插件实例
{
return interfaces[i]->create( mimeType, url, argumentNames, argumentValues);
}
}
}
}
return NULL; //如果没有,直接返回NULL,webkit会进行处理的
}
4、main.cpp
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
** the names of its contributors may be used to endorse or promote
** products derived from this software without specific prior written
** permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtGui>
#include <QWebView>
#include "webkitpluginfactory.h"
int main(int argc, char * argv[])
{
QApplication app(argc, argv);
QWebView view;
view.settings()->setAttribute(QWebSettings::PluginsEnabled, true);
view.page()->setPluginFactory(new WebkitPluginFactory());
view.load(QUrl("file:///home/jhlu/qt/qt-project/webkit/webkit-plugin-test.html"));
view.show();
return app.exec();
}
二、Plugin 开发
1、webkitplugininterface.h
#ifndef WEBKITPLUGININTERFACE_H
#define WEBKITPLUGININTERFACE_H
#include <QWebPluginFactory>
class WebKitPluginInterface
{
public:
WebKitPluginInterface(){};
virtual ~WebKitPluginInterface(){};
virtual QList<QWebPluginFactory::Plugin> plugins()const =0;
virtual QObject *create(const QString &mimeType,
const QUrl &url,
const QStringList &argumentNames,
const QStringList &argumentValues) const =0;
};
//声明WebKitPluginInterface为一个接口
Q_DECLARE_INTERFACE(WebKitPluginInterface, "com.plugin.uvchip.www/1.0")
#endif // WEBKITPLUGININTERFACE_H
2、flashplugin.h
#ifndef FLASHPLUGIN_H
#define FLASHPLUGIN_H
#if defined(FLASHPLUGIN_LIBRARY)
# define FLASHPLUGINSHARED_EXPORT Q_DECL_EXPORT
#else
# define FLASHPLUGINSHARED_EXPORT Q_DECL_IMPORT
#endif
#include "webkitplugininterface.h"
#include <QtPlugin>
class FLASHPLUGINSHARED_EXPORT FlashPlugin : public QObject, public WebKitPluginInterface {
Q_OBJECT
Q_INTERFACES(WebKitPluginInterface) //声明WebKitPluginInterface是一个接口
public:
FlashPlugin();
~FlashPlugin();
QList<QWebPluginFactory::Plugin> plugins()const ;
QObject *create(const QString &mimeType,
const QUrl &url,
const QStringList &argumentNames,
const QStringList &argumentValues) const ;
};
#endif // FLASHPLUGIN_H
3、flashplugin.cpp
#include "flashplugin.h"
#include <QTextEdit>
#include <QUrl>
#include <QDebug>
FlashPlugin::FlashPlugin(): WebKitPluginInterface(){};
FlashPlugin::~FlashPlugin(){};
QList<QWebPluginFactory::Plugin> FlashPlugin::plugins()const
{
qDebug()<<"FlashPlugin::plugins";
QWebPluginFactory::MimeType mimeType;
mimeType.name="application/x-shockwave-flash";
mimeType.description=QObject::tr("flash");
mimeType.fileExtensions.append(".flv");
mimeType.fileExtensions.append(".f4v");
mimeType.fileExtensions.append(".swf");
QList<QWebPluginFactory::MimeType> mimeTypes;
mimeTypes.append(mimeType);
QWebPluginFactory::Plugin plugin;
plugin.name=QObject::tr("jhluroom flash plugin");
plugin.description=QObject::tr("jhluroom flash plugin description");
plugin.mimeTypes=mimeTypes;
QList<QWebPluginFactory::Plugin> plugins ;
plugins.append(plugin);
return plugins;
}
QObject *FlashPlugin::create(const QString &mimeType,
const QUrl &url,
const QStringList &argumentNames,
const QStringList &argumentValues) const
{
qDebug()<<"FlashPlugin::create";
QTextEdit * edit= new QTextEdit();
edit->setObjectName("flashPlugin");
edit->setPlainText(mimeType + ":\n" + url.toString() +"\n"
+QString::fromUtf8("Flash webkit plugin implement by jhluroom")+"\n");
Q_UNUSED(argumentNames);
Q_UNUSED(argumentValues);
return edit;
}
//Q_EXPORT_PLUGIN2()必不可少,只有这样FlashPlugin插件类才为外部可见,插件名为WebkitPluginFlash
Q_EXPORT_PLUGIN2(WebkitPluginFlash, FlashPlugin)
三、webkit-plugin-test.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>webkit plugin test</title>
</head>
<body>
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="500" height="300">
<param name="movie" value="lvjh.swf" />
<param name="quality" value="high" />
<embed src="sf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="500" height="300"></embed>
</object>
</body>
</html>