1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-09 15:47:38 +02:00

Pull basic JS functionality out of JSResolver into JSPlugin

This commit is contained in:
Dominik Schmidt
2014-11-29 16:46:56 +01:00
committed by Dominik Schmidt
parent 683f9229f6
commit fb26cc5dd4
14 changed files with 155 additions and 97 deletions

View File

@@ -89,6 +89,7 @@ set( libGuiSources
resolvers/JSResolver.cpp resolvers/JSResolver.cpp
resolvers/JSResolverHelper.cpp resolvers/JSResolverHelper.cpp
resolvers/ScriptEngine.cpp resolvers/ScriptEngine.cpp
resolvers/JSPlugin.cpp
utils/DpiScaler.cpp utils/DpiScaler.cpp
utils/ImageRegistry.cpp utils/ImageRegistry.cpp

View File

@@ -26,7 +26,7 @@
using namespace Tomahawk; using namespace Tomahawk;
JSInfoPlugin::JSInfoPlugin( int id, JSResolver *resolver ) JSInfoPlugin::JSInfoPlugin( int id, JSPlugin *resolver )
: d_ptr( new JSInfoPluginPrivate( this, id, resolver ) ) : d_ptr( new JSInfoPluginPrivate( this, id, resolver ) )
{ {
Q_ASSERT( resolver ); Q_ASSERT( resolver );
@@ -35,7 +35,7 @@ JSInfoPlugin::JSInfoPlugin( int id, JSResolver *resolver )
m_supportedGetTypes = parseSupportedTypes( callMethodOnInfoPluginWithResult( "supportedGetTypes" ) ); m_supportedGetTypes = parseSupportedTypes( callMethodOnInfoPluginWithResult( "supportedGetTypes" ) );
m_supportedPushTypes = parseSupportedTypes( callMethodOnInfoPluginWithResult( "supportedPushTypes" ) ); m_supportedPushTypes = parseSupportedTypes( callMethodOnInfoPluginWithResult( "supportedPushTypes" ) );
setFriendlyName( QString( "JSInfoPlugin: %1" ).arg( resolver->name() ) ); setFriendlyName( QString( "JSInfoPlugin: %1" ) ); // TODO: .arg( resolver->name() )
} }
@@ -70,8 +70,6 @@ JSInfoPlugin::getInfo( Tomahawk::InfoSystem::InfoRequestData requestData )
void void
JSInfoPlugin::pushInfo( Tomahawk::InfoSystem::InfoPushData pushData ) JSInfoPlugin::pushInfo( Tomahawk::InfoSystem::InfoPushData pushData )
{ {
Q_D( JSInfoPlugin );
QString eval = QString( "pushInfo({ type: %1, pushFlags: %2, input: %3, additionalInput: %4})" ) QString eval = QString( "pushInfo({ type: %1, pushFlags: %2, input: %3, additionalInput: %4})" )
.arg( pushData.type ) .arg( pushData.type )
.arg( pushData.pushFlags ) .arg( pushData.pushFlags )
@@ -239,7 +237,7 @@ JSInfoPlugin::serializeQVariantMap( const QVariantMap& map )
QByteArray serialized = TomahawkUtils::toJson( localMap ); QByteArray serialized = TomahawkUtils::toJson( localMap );
return QString( "JSON.parse('%1')" ).arg( JSResolver::escape( QString::fromUtf8( serialized ) ) ); return QString( "JSON.parse('%1')" ).arg( JSPlugin::escape( QString::fromUtf8( serialized ) ) );
} }

View File

@@ -27,7 +27,7 @@
namespace Tomahawk namespace Tomahawk
{ {
class JSResolver; class JSPlugin;
class JSInfoPluginPrivate; class JSInfoPluginPrivate;
@@ -39,7 +39,7 @@ public:
/** /**
* @param id unique identifier to identify an infoplugin in its scope * @param id unique identifier to identify an infoplugin in its scope
*/ */
JSInfoPlugin( int id, JSResolver* resolver ); JSInfoPlugin( int id, JSPlugin* resolver );
virtual ~JSInfoPlugin(); virtual ~JSInfoPlugin();

View File

@@ -28,7 +28,7 @@ class JSInfoPluginPrivate
{ {
friend class JSInfoPlugin; friend class JSInfoPlugin;
public: public:
JSInfoPluginPrivate( JSInfoPlugin* q, int id, JSResolver* resolver ) JSInfoPluginPrivate( JSInfoPlugin* q, int id, JSPlugin* resolver )
: q_ptr ( q ) : q_ptr ( q )
, id( id ) , id( id )
, resolver( resolver ) , resolver( resolver )
@@ -39,7 +39,8 @@ public:
private: private:
int id; int id;
JSResolver* resolver; // HACK: JSInfoPlugin needs to be refactored to track a ScriptObject
JSPlugin* resolver;
QMap< int, Tomahawk::InfoSystem::InfoRequestData > requestDataCache; QMap< int, Tomahawk::InfoSystem::InfoRequestData > requestDataCache;
QMap< int, Tomahawk::InfoSystem::InfoStringHash > criteriaCache; QMap< int, Tomahawk::InfoSystem::InfoStringHash > criteriaCache;

View File

@@ -23,7 +23,7 @@
using namespace Tomahawk; using namespace Tomahawk;
JSInfoSystemHelper::JSInfoSystemHelper( JSResolver* parent ) JSInfoSystemHelper::JSInfoSystemHelper( JSPlugin* parent )
: QObject( parent ) : QObject( parent )
, d_ptr( new JSInfoSystemHelperPrivate( this, parent ) ) , d_ptr( new JSInfoSystemHelperPrivate( this, parent ) )
{ {
@@ -48,7 +48,7 @@ JSInfoSystemHelper::nativeAddInfoPlugin( int id )
Q_D( JSInfoSystemHelper ); Q_D( JSInfoSystemHelper );
// create infoplugin instance // create infoplugin instance
JSInfoPlugin* jsInfoPlugin = new JSInfoPlugin( id, d->resolver ); JSInfoPlugin* jsInfoPlugin = new JSInfoPlugin( id, d->scriptPlugin );
Tomahawk::InfoSystem::InfoPluginPtr infoPlugin( jsInfoPlugin ); Tomahawk::InfoSystem::InfoPluginPtr infoPlugin( jsInfoPlugin );
// move it to infosystem thread // move it to infosystem thread

View File

@@ -24,7 +24,7 @@
namespace Tomahawk namespace Tomahawk
{ {
class JSResolver; class JSPlugin;
class JSInfoSystemHelperPrivate; class JSInfoSystemHelperPrivate;
class JSInfoSystemHelper : public QObject class JSInfoSystemHelper : public QObject
@@ -32,7 +32,7 @@ class JSInfoSystemHelper : public QObject
Q_OBJECT Q_OBJECT
public: public:
JSInfoSystemHelper( JSResolver* parent ); JSInfoSystemHelper( JSPlugin* parent );
~JSInfoSystemHelper(); ~JSInfoSystemHelper();
QStringList requiredScriptPaths() const; QStringList requiredScriptPaths() const;

View File

@@ -29,9 +29,9 @@ class JSInfoSystemHelperPrivate
{ {
friend class JSInfoSystemHelper; friend class JSInfoSystemHelper;
public: public:
JSInfoSystemHelperPrivate( JSInfoSystemHelper* q, JSResolver* resolver ) JSInfoSystemHelperPrivate( JSInfoSystemHelper* q, JSPlugin* scriptPlugin )
: q_ptr ( q ) : q_ptr ( q )
, resolver ( resolver ) , scriptPlugin ( scriptPlugin )
{ {
} }
@@ -39,7 +39,7 @@ public:
Q_DECLARE_PUBLIC ( JSInfoSystemHelper ) Q_DECLARE_PUBLIC ( JSInfoSystemHelper )
private: private:
JSResolver* resolver; JSPlugin* scriptPlugin;
QMap<int,JSInfoPlugin*> infoPlugins; QMap<int,JSInfoPlugin*> infoPlugins;
}; };

View File

@@ -0,0 +1,29 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2014, Dominik Schmidt <domme@tomahawk-player.org>
*
* Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Tomahawk is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
*/
#include "JSResolver.h"
#include <QWebFrame>
using namespace Tomahawk;
void
JSPlugin::addToJavaScriptWindowObject( const QString& name, QObject* object )
{
m_engine->mainFrame()->addToJavaScriptWindowObject( name, object );
};

View File

@@ -63,7 +63,7 @@ JSResolver::JSResolver( const QString& accountId, const QString& scriptPath, con
Q_D( JSResolver ); Q_D( JSResolver );
tLog() << Q_FUNC_INFO << "Loading JS resolver:" << scriptPath; tLog() << Q_FUNC_INFO << "Loading JS resolver:" << scriptPath;
d->engine = new ScriptEngine( this ); d->scriptPlugin = new JSPlugin();
d->name = QFileInfo( filePath() ).baseName(); d->name = QFileInfo( filePath() ).baseName();
// set the icon, if we launch properly we'll get the icon the resolver reports // set the icon, if we launch properly we'll get the icon the resolver reports
@@ -87,7 +87,7 @@ JSResolver::~JSResolver()
if ( !d->stopped ) if ( !d->stopped )
stop(); stop();
delete d->engine; delete d->scriptPlugin;
} }
@@ -205,20 +205,17 @@ JSResolver::init()
} }
const QByteArray scriptContents = scriptFile.readAll(); const QByteArray scriptContents = scriptFile.readAll();
d->engine->mainFrame()->setHtml( "<html><body></body></html>", QUrl( "file:///invalid/file/for/security/policy" ) );
// tomahawk.js // tomahawk.js
{ {
// add c++ part of tomahawk javascript library // add c++ part of tomahawk javascript library
d->engine->mainFrame()->addToJavaScriptWindowObject( "Tomahawk", d->resolverHelper ); d->scriptPlugin->addToJavaScriptWindowObject( "Tomahawk", d->resolverHelper );
// load es6-promises shim // load es6-promises shim
loadScript( RESPATH "js/es6-promise-2.0.0.min.js" ); d->scriptPlugin->loadScript( RESPATH "js/es6-promise-2.0.0.min.js" );
// Load CrytoJS core // Load CrytoJS core
loadScript( RESPATH "js/cryptojs-core.js" ); d->scriptPlugin->loadScript( RESPATH "js/cryptojs-core.js" );
// Load CryptoJS modules // Load CryptoJS modules
QStringList jsfiles; QStringList jsfiles;
@@ -226,29 +223,30 @@ JSResolver::init()
QDir cryptojs( RESPATH "js/cryptojs" ); QDir cryptojs( RESPATH "js/cryptojs" );
foreach ( QString jsfile, cryptojs.entryList( jsfiles ) ) foreach ( QString jsfile, cryptojs.entryList( jsfiles ) )
{ {
loadScript( RESPATH "js/cryptojs/" + jsfile ); d->scriptPlugin->loadScript( RESPATH "js/cryptojs/" + jsfile );
} }
// Load tomahawk.js // Load tomahawk.js
loadScript( RESPATH "js/tomahawk.js" ); d->scriptPlugin->loadScript( RESPATH "js/tomahawk.js" );
} }
// tomahawk-infosystem.js // tomahawk-infosystem.js
{ {
// add c++ part of tomahawk infosystem bindings as Tomahawk.InfoSystem // add c++ part of tomahawk infosystem bindings as Tomahawk.InfoSystem
d->engine->mainFrame()->addToJavaScriptWindowObject( "_TomahawkInfoSystem", d->infoSystemHelper ); d->infoSystemHelper = new JSInfoSystemHelper( d->scriptPlugin );
d->engine->mainFrame()->evaluateJavaScript( "Tomahawk.InfoSystem = _TomahawkInfoSystem;" ); d->scriptPlugin->addToJavaScriptWindowObject( "_TomahawkInfoSystem", d->infoSystemHelper );
d->scriptPlugin->evaluateJavaScript( "Tomahawk.InfoSystem = _TomahawkInfoSystem;" );
// add deps // add deps
loadScripts( d->infoSystemHelper->requiredScriptPaths() ); d->scriptPlugin->loadScripts( d->infoSystemHelper->requiredScriptPaths() );
} }
// add resolver dependencies, if any // add resolver dependencies, if any
loadScripts( d->requiredScriptPaths ); d->scriptPlugin->loadScripts( d->requiredScriptPaths );
// add resolver // add resolver
loadScript( filePath() ); d->scriptPlugin->loadScript( filePath() );
// init resolver // init resolver
resolverInit(); resolverInit();
@@ -327,7 +325,7 @@ JSResolver::artists( const Tomahawk::collection_ptr& collection )
} }
QString eval = QString( "artists( '%1' )" ) QString eval = QString( "artists( '%1' )" )
.arg( escape( collection->name() ) ); .arg( JSPlugin::escape( collection->name() ) );
QVariantMap m = callOnResolver( eval ).toMap(); QVariantMap m = callOnResolver( eval ).toMap();
if ( m.isEmpty() ) if ( m.isEmpty() )
@@ -363,8 +361,8 @@ JSResolver::albums( const Tomahawk::collection_ptr& collection, const Tomahawk::
} }
QString eval = QString( "albums( '%1', '%2' )" ) QString eval = QString( "albums( '%1', '%2' )" )
.arg( escape( collection->name() ) ) .arg( JSPlugin::escape( collection->name() ) )
.arg( escape( artist->name() ) ); .arg( JSPlugin::escape( artist->name() ) );
QVariantMap m = callOnResolver( eval ).toMap(); QVariantMap m = callOnResolver( eval ).toMap();
if ( m.isEmpty() ) if ( m.isEmpty() )
@@ -400,9 +398,9 @@ JSResolver::tracks( const Tomahawk::collection_ptr& collection, const Tomahawk::
} }
QString eval = QString( "tracks( '%1', '%2', '%3' )" ) QString eval = QString( "tracks( '%1', '%2', '%3' )" )
.arg( escape( collection->name() ) ) .arg( JSPlugin::escape( collection->name() ) )
.arg( escape( album->artist()->name() ) ) .arg( JSPlugin::escape( album->artist()->name() ) )
.arg( escape( album->name() ) ); .arg( JSPlugin::escape( album->name() ) );
QVariantMap m = callOnResolver( eval ).toMap(); QVariantMap m = callOnResolver( eval ).toMap();
if ( m.isEmpty() ) if ( m.isEmpty() )
@@ -433,7 +431,7 @@ JSResolver::canParseUrl( const QString& url, UrlType type )
if ( d->capabilities.testFlag( UrlLookup ) ) if ( d->capabilities.testFlag( UrlLookup ) )
{ {
QString eval = QString( "canParseUrl( '%1', %2 )" ) QString eval = QString( "canParseUrl( '%1', %2 )" )
.arg( escape( QString( url ) ) ) .arg( JSPlugin::escape( QString( url ) ) )
.arg( (int) type ); .arg( (int) type );
return callOnResolver( eval ).toBool(); return callOnResolver( eval ).toBool();
} }
@@ -464,7 +462,7 @@ JSResolver::lookupUrl( const QString& url )
} }
QString eval = QString( "lookupUrl( '%1' )" ) QString eval = QString( "lookupUrl( '%1' )" )
.arg( escape( QString( url ) ) ); .arg( JSPlugin::escape( QString( url ) ) );
QVariantMap m = callOnResolver( eval ).toMap(); QVariantMap m = callOnResolver( eval ).toMap();
if ( m.isEmpty() ) if ( m.isEmpty() )
@@ -480,16 +478,14 @@ JSResolver::lookupUrl( const QString& url )
QVariant QVariant
JSResolver::evaluateJavaScriptInternal(const QString& scriptSource) JSPlugin::evaluateJavaScriptInternal( const QString& scriptSource )
{ {
Q_D( JSResolver ); return m_engine->mainFrame()->evaluateJavaScript( scriptSource );
return d->engine->mainFrame()->evaluateJavaScript( scriptSource );
} }
void void
JSResolver::evaluateJavaScript( const QString& scriptSource ) JSPlugin::evaluateJavaScript( const QString& scriptSource )
{ {
if ( QThread::currentThread() != thread() ) if ( QThread::currentThread() != thread() )
{ {
@@ -502,7 +498,7 @@ JSResolver::evaluateJavaScript( const QString& scriptSource )
QVariant QVariant
JSResolver::evaluateJavaScriptWithResult( const QString& scriptSource ) JSPlugin::evaluateJavaScriptWithResult( const QString& scriptSource )
{ {
Q_ASSERT( QThread::currentThread() == thread() ); Q_ASSERT( QThread::currentThread() == thread() );
@@ -532,16 +528,16 @@ JSResolver::resolve( const Tomahawk::query_ptr& query )
if ( !query->isFullTextQuery() ) if ( !query->isFullTextQuery() )
{ {
eval = QString( "resolve( '%1', '%2', '%3', '%4' )" ) eval = QString( "resolve( '%1', '%2', '%3', '%4' )" )
.arg( escape( query->id() ) ) .arg( JSPlugin::escape( query->id() ) )
.arg( escape( query->queryTrack()->artist() ) ) .arg( JSPlugin::escape( query->queryTrack()->artist() ) )
.arg( escape( query->queryTrack()->album() ) ) .arg( JSPlugin::escape( query->queryTrack()->album() ) )
.arg( escape( query->queryTrack()->track() ) ); .arg( JSPlugin::escape( query->queryTrack()->track() ) );
} }
else else
{ {
eval = QString( "search( '%1', '%2' )" ) eval = QString( "search( '%1', '%2' )" )
.arg( escape( query->id() ) ) .arg( JSPlugin::escape( query->id() ) )
.arg( escape( query->fullTextQuery() ) ); .arg( JSPlugin::escape( query->fullTextQuery() ) );
} }
QVariantMap m = callOnResolver( eval ).toMap(); QVariantMap m = callOnResolver( eval ).toMap();
@@ -964,10 +960,8 @@ JSResolver::resolverCollections()
void void
JSResolver::loadScript( const QString& path ) JSPlugin::loadScript( const QString& path )
{ {
Q_D( JSResolver );
QFile file( path ); QFile file( path );
if ( !file.open( QIODevice::ReadOnly ) ) if ( !file.open( QIODevice::ReadOnly ) )
@@ -979,15 +973,15 @@ JSResolver::loadScript( const QString& path )
const QByteArray contents = file.readAll(); const QByteArray contents = file.readAll();
d->engine->setScriptPath( path ); m_engine->setScriptPath( path );
d->engine->mainFrame()->evaluateJavaScript( contents ); m_engine->mainFrame()->evaluateJavaScript( contents );
file.close(); file.close();
} }
void void
JSResolver::loadScripts( const QStringList& paths ) JSPlugin::loadScripts( const QStringList& paths )
{ {
foreach ( const QString& path, paths ) foreach ( const QString& path, paths )
{ {
@@ -1003,7 +997,9 @@ JSResolver::callOnResolver( const QString& scriptSource )
QString propertyName = scriptSource.split('(').first(); QString propertyName = scriptSource.split('(').first();
return d->engine->mainFrame()->evaluateJavaScript( QString( tLog() << "JAVASCRIPT: run: " << scriptSource;
return d->scriptPlugin->evaluateJavaScriptWithResult( QString(
"if(Tomahawk.resolver.instance['_adapter_%1']) {" "if(Tomahawk.resolver.instance['_adapter_%1']) {"
" Tomahawk.resolver.instance._adapter_%2;" " Tomahawk.resolver.instance._adapter_%2;"
"} else {" "} else {"
@@ -1014,7 +1010,7 @@ JSResolver::callOnResolver( const QString& scriptSource )
QString QString
JSResolver::escape( const QString& source ) JSPlugin::JSPlugin::escape( const QString& source )
{ {
QString copy = source; QString copy = source;
return copy.replace( "\\", "\\\\" ).replace( "'", "\\'" ); return copy.replace( "\\", "\\\\" ).replace( "'", "\\'" );

View File

@@ -27,6 +27,10 @@
#include "ExternalResolverGui.h" #include "ExternalResolverGui.h"
#include "Typedefs.h" #include "Typedefs.h"
#include <memory> // unique_ptr
#include "ScriptEngine.h" // hack, also should be renamed to JSEngine
namespace Tomahawk namespace Tomahawk
{ {
@@ -34,6 +38,54 @@ class JSInfoPlugin;
class JSResolverHelper; class JSResolverHelper;
class JSResolverPrivate; class JSResolverPrivate;
class ScriptEngine; class ScriptEngine;
class ScriptJob;
class ScriptObject;
class ScriptPlugin;
class DLLEXPORT ScriptPlugin
{
public:
virtual ~ScriptPlugin() {}
};
class DLLEXPORT JSPlugin : public QObject, public ScriptPlugin
{
Q_OBJECT
public:
JSPlugin()
: m_engine( new ScriptEngine( this ) )
{
}
/**
* Evaluate JavaScript on the WebKit thread
*/
Q_INVOKABLE void evaluateJavaScript( const QString& scriptSource );
/**
* This method must be called from the WebKit thread
*/
QVariant evaluateJavaScriptWithResult( const QString& scriptSource );
/**
* Escape \ and ' in strings so they are safe to use in JavaScript
*/
static QString escape( const QString& source );
void loadScript( const QString& path );
void loadScripts( const QStringList& paths );
void addToJavaScriptWindowObject( const QString& name, QObject* object );
private:
/**
* Wrap the pure evaluateJavaScript call in here, while the threadings guards are in public methods
*/
QVariant evaluateJavaScriptInternal( const QString& scriptSource );
std::unique_ptr<ScriptEngine> m_engine;
};
class DLLEXPORT JSResolver : public Tomahawk::ExternalResolverGui class DLLEXPORT JSResolver : public Tomahawk::ExternalResolverGui
{ {
@@ -64,20 +116,6 @@ public:
bool canParseUrl( const QString& url, UrlType type ) override; bool canParseUrl( const QString& url, UrlType type ) override;
/**
* Evaluate JavaScript on the WebKit thread
*/
Q_INVOKABLE void evaluateJavaScript( const QString& scriptSource );
/**
* This method must be called from the WebKit thread
*/
QVariant evaluateJavaScriptWithResult( const QString& scriptSource );
/**
* Escape \ and ' in strings so they are safe to use in JavaScript
*/
static QString escape( const QString& source );
public slots: public slots:
void resolve( const Tomahawk::query_ptr& query ) override; void resolve( const Tomahawk::query_ptr& query ) override;
@@ -110,13 +148,6 @@ private:
void fillDataInWidgets( const QVariantMap& data ); void fillDataInWidgets( const QVariantMap& data );
void onCapabilitiesChanged( Capabilities capabilities ); void onCapabilitiesChanged( Capabilities capabilities );
void loadCollections(); void loadCollections();
void loadScript( const QString& path );
void loadScripts( const QStringList& paths );
/**
* Wrap the pure evaluateJavaScript call in here, while the threadings guards are in public methods
*/
QVariant evaluateJavaScriptInternal( const QString& scriptSource );
// encapsulate javascript calls // encapsulate javascript calls
QVariantMap resolverSettings(); QVariantMap resolverSettings();

View File

@@ -484,14 +484,14 @@ JSResolverHelper::customIODeviceFactory( const Tomahawk::result_ptr&, const QStr
.arg( url ); .arg( url );
m_streamCallbacks.insert( qid, callback ); m_streamCallbacks.insert( qid, callback );
m_resolver->d_func()->engine->mainFrame()->evaluateJavaScript( getUrl ); m_resolver->d_func()->scriptPlugin->evaluateJavaScript( getUrl );
} }
else else
{ {
QString getUrl = QString( "Tomahawk.resolver.instance.%1( '%2' );" ).arg( m_urlCallback ) QString getUrl = QString( "Tomahawk.resolver.instance.%1( '%2' );" ).arg( m_urlCallback )
.arg( url ); .arg( url );
QString urlStr = m_resolver->d_func()->engine->mainFrame()->evaluateJavaScript( getUrl ).toString(); QString urlStr = m_resolver->d_func()->scriptPlugin->evaluateJavaScriptWithResult( getUrl ).toString();
returnStreamUrl( urlStr, QMap<QString, QString>(), callback ); returnStreamUrl( urlStr, QMap<QString, QString>(), callback );
} }
@@ -528,7 +528,7 @@ JSResolverHelper::nativeRetrieveMetadata( int metadataId, const QString& url,
{ {
QString javascript = QString( "Tomahawk.retrievedMetadata( %1, null, 'Supplied size is not (yet) supported');" ) QString javascript = QString( "Tomahawk.retrievedMetadata( %1, null, 'Supplied size is not (yet) supported');" )
.arg( metadataId ); .arg( metadataId );
m_resolver->d_func()->engine->mainFrame()->evaluateJavaScript( javascript ); m_resolver->d_func()->scriptPlugin->evaluateJavaScript( javascript );
return; return;
} }
@@ -595,7 +595,7 @@ JSResolverHelper::nativeRetrieveMetadata( int metadataId, const QString& url,
{ {
QString javascript = QString( "Tomahawk.retrievedMetadata( %1, null, 'Unknown mime type for tagging: %2');" ) QString javascript = QString( "Tomahawk.retrievedMetadata( %1, null, 'Unknown mime type for tagging: %2');" )
.arg( metadataId ).arg( mime_type ); .arg( metadataId ).arg( mime_type );
m_resolver->d_func()->engine->mainFrame()->evaluateJavaScript( javascript ); m_resolver->d_func()->scriptPlugin->evaluateJavaScript( javascript );
return; return;
} }
@@ -610,7 +610,7 @@ JSResolverHelper::nativeRetrieveMetadata( int metadataId, const QString& url,
{ {
QString javascript = QString( "Tomahawk.retrievedMetadata( %1, null, 'Could not read tag information.');" ) QString javascript = QString( "Tomahawk.retrievedMetadata( %1, null, 'Could not read tag information.');" )
.arg( metadataId ); .arg( metadataId );
m_resolver->d_func()->engine->mainFrame()->evaluateJavaScript( javascript ); m_resolver->d_func()->scriptPlugin->evaluateJavaScript( javascript );
return; return;
} }
@@ -624,7 +624,7 @@ JSResolverHelper::nativeRetrieveMetadata( int metadataId, const QString& url,
{ {
QString javascript = QString( "Tomahawk.retrievedMetadata( %1, null, 'Empty track returnd');" ) QString javascript = QString( "Tomahawk.retrievedMetadata( %1, null, 'Empty track returnd');" )
.arg( metadataId ); .arg( metadataId );
m_resolver->d_func()->engine->mainFrame()->evaluateJavaScript( javascript ); m_resolver->d_func()->scriptPlugin->evaluateJavaScript( javascript );
return; return;
} }
@@ -632,7 +632,7 @@ JSResolverHelper::nativeRetrieveMetadata( int metadataId, const QString& url,
{ {
QString javascript = QString( "Tomahawk.retrievedMetadata( %1, null, 'Empty artist returnd');" ) QString javascript = QString( "Tomahawk.retrievedMetadata( %1, null, 'Empty artist returnd');" )
.arg( metadataId ); .arg( metadataId );
m_resolver->d_func()->engine->mainFrame()->evaluateJavaScript( javascript ); m_resolver->d_func()->scriptPlugin->evaluateJavaScript( javascript );
return; return;
} }
@@ -647,13 +647,13 @@ JSResolverHelper::nativeRetrieveMetadata( int metadataId, const QString& url,
QString javascript = QString( "Tomahawk.retrievedMetadata( %1, %2 );" ) QString javascript = QString( "Tomahawk.retrievedMetadata( %1, %2 );" )
.arg( metadataId ) .arg( metadataId )
.arg( QString::fromLatin1( TomahawkUtils::toJson( m ) ) ); .arg( QString::fromLatin1( TomahawkUtils::toJson( m ) ) );
m_resolver->d_func()->engine->mainFrame()->evaluateJavaScript( javascript ); m_resolver->d_func()->scriptPlugin->evaluateJavaScript( javascript );
} }
else else
{ {
QString javascript = QString( "Tomahawk.retrievedMetadata( %1, null, 'Protocol not supported');" ) QString javascript = QString( "Tomahawk.retrievedMetadata( %1, null, 'Protocol not supported');" )
.arg( metadataId ); .arg( metadataId );
m_resolver->d_func()->engine->mainFrame()->evaluateJavaScript( javascript ); m_resolver->d_func()->scriptPlugin->evaluateJavaScript( javascript );
} }
} }
@@ -717,7 +717,7 @@ JSResolverHelper::nativeAsyncRequestDone( int requestId, NetworkReply* reply )
QString javascript = QString( "Tomahawk.nativeAsyncRequestDone( %1, %2 );" ) QString javascript = QString( "Tomahawk.nativeAsyncRequestDone( %1, %2 );" )
.arg( QString::number( requestId ) ) .arg( QString::number( requestId ) )
.arg( json ); .arg( json );
m_resolver->d_func()->engine->mainFrame()->evaluateJavaScript( javascript ); m_resolver->d_func()->scriptPlugin->evaluateJavaScript( javascript );
} }

View File

@@ -43,7 +43,7 @@ public:
, error( Tomahawk::ExternalResolver::NoError ) , error( Tomahawk::ExternalResolver::NoError )
, resolverHelper( new JSResolverHelper( scriptPath, q ) ) , resolverHelper( new JSResolverHelper( scriptPath, q ) )
// TODO: be smarter about this, only instantiate this if the resolver supports infoplugins // TODO: be smarter about this, only instantiate this if the resolver supports infoplugins
, infoSystemHelper( new JSInfoSystemHelper( q ) ) , infoSystemHelper( nullptr )
, requiredScriptPaths( additionalScriptPaths ) , requiredScriptPaths( additionalScriptPaths )
{ {
} }
@@ -51,8 +51,6 @@ public:
Q_DECLARE_PUBLIC ( JSResolver ) Q_DECLARE_PUBLIC ( JSResolver )
private: private:
ScriptEngine* engine;
QString accountId; QString accountId;
QString name; QString name;
QPixmap icon; QPixmap icon;
@@ -69,6 +67,8 @@ private:
QPointer< AccountConfigWidget > configWidget; QPointer< AccountConfigWidget > configWidget;
QList< QVariant > dataWidgets; QList< QVariant > dataWidgets;
QStringList requiredScriptPaths; QStringList requiredScriptPaths;
JSPlugin* scriptPlugin;
}; };
} // ns: Tomahawk } // ns: Tomahawk

View File

@@ -35,7 +35,7 @@
using namespace Tomahawk; using namespace Tomahawk;
ScriptEngine::ScriptEngine( JSResolver* parent ) ScriptEngine::ScriptEngine( JSPlugin* parent )
: QWebPage( (QObject*) parent ) : QWebPage( (QObject*) parent )
, m_parent( parent ) , m_parent( parent )
{ {
@@ -54,6 +54,8 @@ ScriptEngine::ScriptEngine( JSResolver* parent )
, "" ); , "" );
tLog( LOGVERBOSE ) << "JSResolver Using header" << m_header; tLog( LOGVERBOSE ) << "JSResolver Using header" << m_header;
mainFrame()->setHtml( "<html><body></body></html>", QUrl( "file:///invalid/file/for/security/policy" ) );
connect( networkAccessManager(), SIGNAL( sslErrors( QNetworkReply*, QList<QSslError> ) ), connect( networkAccessManager(), SIGNAL( sslErrors( QNetworkReply*, QList<QSslError> ) ),
SLOT( sslErrorHandler( QNetworkReply*, QList<QSslError> ) ) ); SLOT( sslErrorHandler( QNetworkReply*, QList<QSslError> ) ) );
} }

View File

@@ -32,14 +32,14 @@ class QNetworkReply;
namespace Tomahawk namespace Tomahawk
{ {
class JSResolver; class JSPlugin;
class DLLEXPORT ScriptEngine : public QWebPage class DLLEXPORT ScriptEngine : public QWebPage
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit ScriptEngine( JSResolver* parent ); explicit ScriptEngine( JSPlugin* parent );
QString userAgentForUrl( const QUrl& url ) const; QString userAgentForUrl( const QUrl& url ) const;
void setScriptPath( const QString& scriptPath ); void setScriptPath( const QString& scriptPath );
@@ -54,7 +54,7 @@ private slots:
void sslErrorHandler( QNetworkReply* qnr, const QList<QSslError>& errlist ); void sslErrorHandler( QNetworkReply* qnr, const QList<QSslError>& errlist );
private: private:
JSResolver* m_parent; JSPlugin* m_parent;
QString m_scriptPath; QString m_scriptPath;
QString m_header; QString m_header;
}; };