diff options
author | Alexander Tkachev | 2016-05-21 21:30:25 +0600 |
---|---|---|
committer | Alexander Tkachev | 2016-08-24 16:07:55 +0600 |
commit | e1109c0c328aaf671e2b03b3b4e6de1ae9061754 (patch) | |
tree | df4beeb28006b68c6d60953c164f53660f74b376 | |
parent | ca456a7241fb4b46f9c76c86eeecc273ca31d8f6 (diff) | |
download | scummvm-rg350-e1109c0c328aaf671e2b03b3b4e6de1ae9061754.tar.gz scummvm-rg350-e1109c0c328aaf671e2b03b3b4e6de1ae9061754.tar.bz2 scummvm-rg350-e1109c0c328aaf671e2b03b3b4e6de1ae9061754.zip |
CLOUD: Add CallbackBridge
This commit also adds GlobalFunctionCallback, because it was needed in
order to replace plain C pointers to functions (which were used in
Request) into our object-oriented BaseCallback pointers.
-rw-r--r-- | backends/cloud/dropbox/dropboxstorage.cpp | 20 | ||||
-rw-r--r-- | backends/cloud/dropbox/dropboxstorage.h | 3 | ||||
-rw-r--r-- | backends/networking/curl/curljsonrequest.cpp | 4 | ||||
-rw-r--r-- | backends/networking/curl/curljsonrequest.h | 6 | ||||
-rw-r--r-- | backends/networking/curl/request.h | 5 | ||||
-rw-r--r-- | common/callback.h | 24 |
6 files changed, 50 insertions, 12 deletions
diff --git a/backends/cloud/dropbox/dropboxstorage.cpp b/backends/cloud/dropbox/dropboxstorage.cpp index 94bdb43450..04e9eeef0b 100644 --- a/backends/cloud/dropbox/dropboxstorage.cpp +++ b/backends/cloud/dropbox/dropboxstorage.cpp @@ -46,7 +46,7 @@ static void printJsonCallback(Networking::Request* rq, void *ptr) { } } -static void saveAccessTokenCallback(Networking::Request* rq, void *ptr) { +static void saveAccessTokenCallback(void *ptr) { Common::JSONValue *json = (Common::JSONValue *)ptr; if (json) { debug("saveAccessTokenCallback:"); @@ -139,19 +139,26 @@ void DropboxStorage::syncSaves(OperationCallback callback) { } void DropboxStorage::info(InfoCallback callback) { + /* Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(infoCallback, "https://api.dropboxapi.com/1/account/info"); request->addHeader("Authorization: Bearer " + _token); ConnMan.addRequest(request); request->setPointer(callback); + */ } -void DropboxStorage::info2(Common::Callback<DropboxStorage> *callback) { - Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(info2Callback, "https://api.dropboxapi.com/1/account/info"); +void DropboxStorage::info2BridgeCallback(Common::BaseCallback *outerCallback, void *ptr) { + //no NULL checks, delete and such yet + Common::JSONValue *json = (Common::JSONValue *)ptr; + (*outerCallback)(new StorageInfo(json->stringify())); +} + +void DropboxStorage::info2(Common::BaseCallback *outerCallback) { + Common::BaseCallback *innerCallback = new Common::CallbackBridge<DropboxStorage>(this, &DropboxStorage::info2BridgeCallback, outerCallback); + Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(innerCallback, "https://api.dropboxapi.com/1/account/info"); request->addHeader("Authorization: Bearer " + _token); ConnMan.addRequest(request); - - request->setPointer(callback); } DropboxStorage *DropboxStorage::loadFromConfig() { @@ -205,7 +212,8 @@ void DropboxStorage::authThroughConsole() { } void DropboxStorage::getAccessToken(Common::String code) { - Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(saveAccessTokenCallback, "https://api.dropboxapi.com/1/oauth2/token"); + Common::BaseCallback *callback = new Common::GlobalFunctionCallback(saveAccessTokenCallback); + Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(callback, "https://api.dropboxapi.com/1/oauth2/token"); request->addPostField("code=" + code); request->addPostField("grant_type=authorization_code"); request->addPostField("client_id=" + KEY); diff --git a/backends/cloud/dropbox/dropboxstorage.h b/backends/cloud/dropbox/dropboxstorage.h index 1cf657bc36..efd0eea257 100644 --- a/backends/cloud/dropbox/dropboxstorage.h +++ b/backends/cloud/dropbox/dropboxstorage.h @@ -68,7 +68,8 @@ public: /** Returns pointer to the ServiceInfo struct. */ virtual void info(InfoCallback callback); - void info2(Common::Callback<DropboxStorage> *callback); + void info2(Common::BaseCallback *outerCallback); + void info2BridgeCallback(Common::BaseCallback *outerCallback, void *ptr); /** Returns whether saves sync process is running. */ virtual bool isSyncing() { return false; } //TODO diff --git a/backends/networking/curl/curljsonrequest.cpp b/backends/networking/curl/curljsonrequest.cpp index fe6e218269..c3064681ef 100644 --- a/backends/networking/curl/curljsonrequest.cpp +++ b/backends/networking/curl/curljsonrequest.cpp @@ -31,7 +31,7 @@ namespace Networking { -CurlJsonRequest::CurlJsonRequest(SimpleCallback cb, const char *url) : Request(cb), _stream(0), _headersList(0), _contentsStream(DisposeAfterUse::YES) { +CurlJsonRequest::CurlJsonRequest(Common::BaseCallback* cb, const char *url) : Request(cb), _stream(0), _headersList(0), _contentsStream(DisposeAfterUse::YES) { _url = url; } @@ -75,7 +75,7 @@ bool CurlJsonRequest::handle() { if (_callback) { char *contents = getPreparedContents(); Common::JSONValue *json = Common::JSON::parse(contents); - _callback(this, json); //potential memory leak, free it in your callbacks! + (*_callback)(json); //potential memory leak, free it in your callbacks! } return true; } diff --git a/backends/networking/curl/curljsonrequest.h b/backends/networking/curl/curljsonrequest.h index 56e7205512..1098638609 100644 --- a/backends/networking/curl/curljsonrequest.h +++ b/backends/networking/curl/curljsonrequest.h @@ -26,6 +26,10 @@ #include "backends/networking/curl/request.h" #include "common/memstream.h" +namespace Common { +class BaseCallback; +} + struct curl_slist; namespace Networking { @@ -43,7 +47,7 @@ class CurlJsonRequest : public Request { char *getPreparedContents(); public: - CurlJsonRequest(SimpleCallback cb, const char *url); + CurlJsonRequest(Common::BaseCallback *cb, const char *url); virtual ~CurlJsonRequest(); virtual bool handle(); diff --git a/backends/networking/curl/request.h b/backends/networking/curl/request.h index b9571075cb..860784f2a7 100644 --- a/backends/networking/curl/request.h +++ b/backends/networking/curl/request.h @@ -22,6 +22,7 @@ #ifndef BACKENDS_NETWORKING_CURL_REQUEST_H #define BACKENDS_NETWORKING_CURL_REQUEST_H +#include <common/callback.h> namespace Networking { @@ -34,7 +35,7 @@ protected: * That's the way Requests pass the result to the code which asked to create this request. */ - SimpleCallback _callback; + Common::BaseCallback* _callback; /** * Pointer, which could be set by Request creating code. It might be accessed @@ -44,7 +45,7 @@ protected: void *_pointer; public: - Request(SimpleCallback cb): _callback(cb) {}; + Request(Common::BaseCallback* cb): _callback(cb) {}; virtual ~Request() {}; /** diff --git a/common/callback.h b/common/callback.h index 4cfdd53fd9..4cc63ed346 100644 --- a/common/callback.h +++ b/common/callback.h @@ -32,6 +32,18 @@ public: virtual void operator()(void *ptr) = 0; }; +class GlobalFunctionCallback: public BaseCallback { + typedef void(*GlobalFunction)(void *result); + GlobalFunction _callback; + +public: + GlobalFunctionCallback(GlobalFunction cb): _callback(cb) {} + virtual ~GlobalFunctionCallback() {} + virtual void operator()(void *ptr) { + if (_callback) _callback(ptr); + } +}; + template<class T> class Callback: public BaseCallback { typedef void(T::*TMethod)(void *); T *_object; @@ -42,6 +54,18 @@ public: void operator()(void *ptr) { (_object->*_method)(ptr); } }; +template<class T> class CallbackBridge: public BaseCallback { + typedef void(T::*TCallbackMethod)(BaseCallback *, void *); + T *_object; + TCallbackMethod _method; + BaseCallback *_outerCallback; +public: + CallbackBridge(T *object, TCallbackMethod method, BaseCallback *outerCallback): + _object(object), _method(method), _outerCallback(outerCallback) {} + virtual ~CallbackBridge() {} + void operator()(void *ptr) { (_object->*_method)(_outerCallback, ptr); } +}; + } // End of namespace Common #endif |