mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-08-15 10:33:59 +02:00
Implement Tomahawk.ajax native implementation on behalf of NativeScriptJobs
This commit is contained in:
@@ -170,6 +170,22 @@ JSAccount::syncInvoke( const scriptobject_ptr& scriptObject, const QString& meth
|
||||
return evaluateJavaScriptWithResult( eval );
|
||||
}
|
||||
|
||||
void
|
||||
JSAccount::reportNativeScriptJobResult( int resultId, const QVariantMap& result )
|
||||
{
|
||||
QString eval = QString(
|
||||
"Tomahawk.NativeScriptJobManager.reportNativeScriptJobResult("
|
||||
"%1," // requestId
|
||||
"%2" // results
|
||||
");"
|
||||
).arg( resultId )
|
||||
.arg( serializeQVariantMap( result ) );
|
||||
|
||||
// Remove when new scripting api turned out to work reliably
|
||||
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << eval;
|
||||
|
||||
evaluateJavaScript( eval );
|
||||
}
|
||||
|
||||
QVariant
|
||||
JSAccount::evaluateJavaScriptInternal( const QString& scriptSource )
|
||||
|
@@ -71,6 +71,8 @@ public:
|
||||
|
||||
static QString serializeQVariantMap(const QVariantMap& map);
|
||||
|
||||
void reportNativeScriptJobResult( int resultId, const QVariantMap& result ) override;
|
||||
|
||||
private:
|
||||
/**
|
||||
* Wrap the pure evaluateJavaScript call in here, while the threadings guards are in public methods
|
||||
|
@@ -47,6 +47,8 @@
|
||||
#include <QMap>
|
||||
#include <QWebFrame>
|
||||
#include <QLocale>
|
||||
#include <QNetworkReply>
|
||||
|
||||
#include <taglib/asffile.h>
|
||||
#include <taglib/flacfile.h>
|
||||
#include <taglib/id3v2framefactory.h>
|
||||
@@ -627,12 +629,24 @@ JSResolverHelper::nativeRetrieveMetadata( int metadataId, const QString& url,
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
JSResolverHelper::invokeNativeScriptJob( int requestId, const QString& methodName, const QVariantMap& params )
|
||||
{
|
||||
if ( methodName == "httpRequest" ) {
|
||||
nativeAsyncRequest( requestId, params );
|
||||
} else {
|
||||
// TODO: make promise reject instead
|
||||
Q_ASSERT_X(false, "invokeNativeScriptJob", "NativeScriptJob methodName was not found");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
JSResolverHelper::nativeAsyncRequest( const int requestId, const QString& url,
|
||||
const QVariantMap& headers,
|
||||
const QVariantMap& options )
|
||||
JSResolverHelper::nativeAsyncRequest( const int requestId, const QVariantMap& options )
|
||||
{
|
||||
QString url = options[ "url" ].toString();
|
||||
QVariantMap headers = options[ "headers" ].toMap();
|
||||
|
||||
QNetworkRequest req( url );
|
||||
foreach ( const QString& key, headers.keys() )
|
||||
{
|
||||
@@ -688,17 +702,16 @@ JSResolverHelper::nativeAsyncRequestDone( int requestId, NetworkReply* reply )
|
||||
map["status"] = reply->reply()->attribute( QNetworkRequest::HttpStatusCodeAttribute ).toInt();
|
||||
map["statusText"] = QString("%1 %2").arg( map["status"].toString() )
|
||||
.arg( reply->reply()->attribute( QNetworkRequest::HttpReasonPhraseAttribute ).toString() );
|
||||
if (reply->reply()->hasRawHeader( "Content-Type" ))
|
||||
map["contentType"] = reply->reply()->rawHeader( "Content-Type" );
|
||||
|
||||
bool ok = false;
|
||||
QString json = QString::fromUtf8( TomahawkUtils::toJson( map, &ok ) );
|
||||
Q_ASSERT( ok );
|
||||
|
||||
QString javascript = QString( "Tomahawk.nativeAsyncRequestDone( %1, %2 );" )
|
||||
.arg( QString::number( requestId ) )
|
||||
.arg( json );
|
||||
m_resolver->d_func()->scriptAccount->evaluateJavaScript( javascript );
|
||||
QVariantMap responseHeaders;
|
||||
foreach( const QNetworkReply::RawHeaderPair& pair, reply->reply()->rawHeaderPairs() )
|
||||
{
|
||||
responseHeaders[ pair.first ] = pair.second;
|
||||
}
|
||||
map["responseHeaders"] = responseHeaders;
|
||||
|
||||
m_resolver->d_func()->scriptAccount->reportNativeScriptJobResult( requestId, map );
|
||||
}
|
||||
|
||||
|
||||
|
@@ -86,20 +86,9 @@ public:
|
||||
int sizehint,
|
||||
const QVariantMap& options );
|
||||
|
||||
/**
|
||||
* Native handler for asynchronous HTTP requests.
|
||||
*
|
||||
* This handler shall only be used if we cannot achieve the request with
|
||||
* XMLHttpRequest as that would be more efficient.
|
||||
* Use cases are:
|
||||
* * Referer header: Stripped on MacOS and the specification says it
|
||||
* should be stripped
|
||||
*
|
||||
* INTERNAL USE ONLY!
|
||||
*/
|
||||
Q_INVOKABLE void nativeAsyncRequest( int requestId, const QString& url,
|
||||
const QVariantMap& headers,
|
||||
const QVariantMap& options );
|
||||
Q_INVOKABLE void invokeNativeScriptJob( int requestId,
|
||||
const QString& methodName,
|
||||
const QVariantMap& params );
|
||||
|
||||
/**
|
||||
* Lucene++ indices for JS resolvers
|
||||
@@ -145,6 +134,10 @@ private:
|
||||
bool indexDataFromVariant( const QVariantMap& map, struct Tomahawk::IndexData& indexData );
|
||||
QVariantList searchInFuzzyIndex( const Tomahawk::query_ptr& query );
|
||||
|
||||
// native script jobs
|
||||
void nativeAsyncRequest( int requestId, const QVariantMap& options );
|
||||
|
||||
|
||||
QVariantMap m_resolverConfig;
|
||||
JSResolver* m_resolver;
|
||||
QString m_scriptPath;
|
||||
|
@@ -65,13 +65,14 @@ public:
|
||||
|
||||
ScriptJob* invoke( const scriptobject_ptr& scriptObject, const QString& methodName, const QVariantMap& arguments );
|
||||
virtual QVariant syncInvoke( const scriptobject_ptr& scriptObject, const QString& methodName, const QVariantMap& arguments ) = 0;
|
||||
|
||||
virtual void startJob( ScriptJob* scriptJob ) = 0;
|
||||
|
||||
void reportScriptJobResult( const QVariantMap& result );
|
||||
void registerScriptPlugin( const QString& type, const QString& objectId );
|
||||
void unregisterScriptPlugin( const QString& type, const QString& objectId );
|
||||
|
||||
virtual void reportNativeScriptJobResult( int resultId, const QVariantMap& result ) = 0;
|
||||
|
||||
virtual void scriptPluginFactory( const QString& type, const scriptobject_ptr& object );
|
||||
|
||||
QList< Tomahawk::result_ptr > parseResultVariantList( const QVariantList& reslist );
|
||||
|
Reference in New Issue
Block a user