diff options
author | Alexander Tkachev | 2016-05-26 19:22:27 +0600 |
---|---|---|
committer | Alexander Tkachev | 2016-08-24 16:07:55 +0600 |
commit | b246c17850687e7b15b644b761fbfe835ffc1c32 (patch) | |
tree | c8ddc679b4ccf46d291cfc4e7bab5d201d8e8771 | |
parent | a7b28605a01b59de6f3acc9df4cd1cac707c39e7 (diff) | |
download | scummvm-rg350-b246c17850687e7b15b644b761fbfe835ffc1c32.tar.gz scummvm-rg350-b246c17850687e7b15b644b761fbfe835ffc1c32.tar.bz2 scummvm-rg350-b246c17850687e7b15b644b761fbfe835ffc1c32.zip |
CLOUD: Fix CurlJsonRequest to use JsonCallback
Type safety first.
-rw-r--r-- | backends/cloud/dropbox/dropboxlistdirectoryrequest.cpp | 8 | ||||
-rw-r--r-- | backends/cloud/dropbox/dropboxlistdirectoryrequest.h | 3 | ||||
-rw-r--r-- | backends/cloud/dropbox/dropboxstorage.cpp | 10 | ||||
-rw-r--r-- | backends/cloud/dropbox/dropboxstorage.h | 3 | ||||
-rw-r--r-- | backends/cloud/onedrive/onedrivestorage.cpp | 12 | ||||
-rw-r--r-- | backends/cloud/onedrive/onedrivestorage.h | 5 | ||||
-rw-r--r-- | backends/networking/curl/curljsonrequest.cpp | 8 | ||||
-rw-r--r-- | backends/networking/curl/curljsonrequest.h | 9 |
8 files changed, 33 insertions, 25 deletions
diff --git a/backends/cloud/dropbox/dropboxlistdirectoryrequest.cpp b/backends/cloud/dropbox/dropboxlistdirectoryrequest.cpp index 31f015a1cd..3158149c02 100644 --- a/backends/cloud/dropbox/dropboxlistdirectoryrequest.cpp +++ b/backends/cloud/dropbox/dropboxlistdirectoryrequest.cpp @@ -40,7 +40,7 @@ void DropboxListDirectoryRequest::startupWork() { _files.clear(); _complete = false; - Networking::DataCallback innerCallback = new Common::Callback<DropboxListDirectoryRequest, Networking::RequestDataPair>(this, &DropboxListDirectoryRequest::responseCallback); + Networking::JsonCallback innerCallback = new Common::Callback<DropboxListDirectoryRequest, Networking::RequestJsonPair>(this, &DropboxListDirectoryRequest::responseCallback); Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(innerCallback, "https://api.dropboxapi.com/2/files/list_folder"); request->addHeader("Authorization: Bearer " + _token); request->addHeader("Content-Type: application/json"); @@ -58,8 +58,8 @@ void DropboxListDirectoryRequest::startupWork() { } -void DropboxListDirectoryRequest::responseCallback(Networking::RequestDataPair pair) { - Common::JSONValue *json = (Common::JSONValue *)pair.value; +void DropboxListDirectoryRequest::responseCallback(Networking::RequestJsonPair pair) { + Common::JSONValue *json = pair.value; if (json) { Common::JSONObject response = json->asObject(); @@ -89,7 +89,7 @@ void DropboxListDirectoryRequest::responseCallback(Networking::RequestDataPair p bool hasMore = response.getVal("has_more")->asBool(); if (hasMore) { - Networking::DataCallback innerCallback = new Common::Callback<DropboxListDirectoryRequest, Networking::RequestDataPair>(this, &DropboxListDirectoryRequest::responseCallback); + Networking::JsonCallback innerCallback = new Common::Callback<DropboxListDirectoryRequest, Networking::RequestJsonPair>(this, &DropboxListDirectoryRequest::responseCallback); Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(innerCallback, "https://api.dropboxapi.com/2/files/list_folder/continue"); request->addHeader("Authorization: Bearer " + _token); request->addHeader("Content-Type: application/json"); diff --git a/backends/cloud/dropbox/dropboxlistdirectoryrequest.h b/backends/cloud/dropbox/dropboxlistdirectoryrequest.h index afa544de69..9a82ef7091 100644 --- a/backends/cloud/dropbox/dropboxlistdirectoryrequest.h +++ b/backends/cloud/dropbox/dropboxlistdirectoryrequest.h @@ -26,6 +26,7 @@ #include "backends/cloud/storage.h" #include "backends/networking/curl/request.h" #include "common/callback.h" +#include "backends/networking/curl/curljsonrequest.h" namespace Cloud { namespace Dropbox { @@ -40,7 +41,7 @@ class DropboxListDirectoryRequest: public Networking::Request { Common::Array<StorageFile> _files; int32 _requestId; - void responseCallback(Networking::RequestDataPair pair); + void responseCallback(Networking::RequestJsonPair pair); void startupWork(); public: diff --git a/backends/cloud/dropbox/dropboxstorage.cpp b/backends/cloud/dropbox/dropboxstorage.cpp index 02b033fced..47576d7cda 100644 --- a/backends/cloud/dropbox/dropboxstorage.cpp +++ b/backends/cloud/dropbox/dropboxstorage.cpp @@ -38,7 +38,7 @@ namespace Dropbox { Common::String DropboxStorage::KEY; //can't use ConfMan there yet, loading it on instance creation/auth Common::String DropboxStorage::SECRET; //TODO: hide these secrets somehow -static void saveAccessTokenCallback(Networking::RequestDataPair pair) { +static void saveAccessTokenCallback(Networking::RequestJsonPair pair) { Common::JSONValue *json = (Common::JSONValue *)pair.value; if (json) { debug("saveAccessTokenCallback:"); @@ -121,7 +121,7 @@ int32 DropboxStorage::syncSaves(BoolCallback callback) { } int32 DropboxStorage::info(StorageInfoCallback outerCallback) { - Networking::DataCallback innerCallback = new Common::CallbackBridge<DropboxStorage, RequestStorageInfoPair, Networking::RequestDataPair>(this, &DropboxStorage::infoInnerCallback, outerCallback); + Networking::JsonCallback innerCallback = new Common::CallbackBridge<DropboxStorage, RequestStorageInfoPair, Networking::RequestJsonPair>(this, &DropboxStorage::infoInnerCallback, outerCallback); Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(innerCallback, "https://api.dropboxapi.com/1/account/info"); request->addHeader("Authorization: Bearer " + _token); return ConnMan.addRequest(request); @@ -131,8 +131,8 @@ int32 DropboxStorage::info(StorageInfoCallback outerCallback) { //and then calls the outerCallback (which wants to receive StorageInfo, not void *) } -void DropboxStorage::infoInnerCallback(StorageInfoCallback outerCallback, Networking::RequestDataPair pair) { - Common::JSONValue *json = (Common::JSONValue *)pair.value; +void DropboxStorage::infoInnerCallback(StorageInfoCallback outerCallback, Networking::RequestJsonPair pair) { + Common::JSONValue *json = pair.value; if (!json) { warning("NULL passed instead of JSON"); delete outerCallback; @@ -214,7 +214,7 @@ void DropboxStorage::authThroughConsole() { } void DropboxStorage::getAccessToken(Common::String code) { - Networking::DataCallback callback = new Common::GlobalFunctionCallback<Networking::RequestDataPair>(saveAccessTokenCallback); + Networking::JsonCallback callback = new Common::GlobalFunctionCallback<Networking::RequestJsonPair>(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"); diff --git a/backends/cloud/dropbox/dropboxstorage.h b/backends/cloud/dropbox/dropboxstorage.h index 4fe6109c17..4f1e6cd48a 100644 --- a/backends/cloud/dropbox/dropboxstorage.h +++ b/backends/cloud/dropbox/dropboxstorage.h @@ -25,6 +25,7 @@ #include "backends/cloud/storage.h" #include "common/callback.h" +#include "backends/networking/curl/curljsonrequest.h" namespace Cloud { namespace Dropbox { @@ -40,7 +41,7 @@ class DropboxStorage: public Cloud::Storage { static void getAccessToken(Common::String code); /** Constructs StorageInfo based on JSON response from cloud. */ - void infoInnerCallback(StorageInfoCallback outerCallback, Networking::RequestDataPair json); + void infoInnerCallback(StorageInfoCallback outerCallback, Networking::RequestJsonPair json); void printFiles(Common::Array<StorageFile> files); diff --git a/backends/cloud/onedrive/onedrivestorage.cpp b/backends/cloud/onedrive/onedrivestorage.cpp index 36b3e26f1b..833ba8e558 100644 --- a/backends/cloud/onedrive/onedrivestorage.cpp +++ b/backends/cloud/onedrive/onedrivestorage.cpp @@ -56,7 +56,7 @@ void OneDriveStorage::getAccessToken(BoolCallback callback, Common::String code) return; } - Networking::DataCallback innerCallback = new Common::CallbackBridge<OneDriveStorage, RequestBoolPair, Networking::RequestDataPair>(this, &OneDriveStorage::tokenRefreshed, callback); + Networking::JsonCallback innerCallback = new Common::CallbackBridge<OneDriveStorage, RequestBoolPair, Networking::RequestJsonPair>(this, &OneDriveStorage::tokenRefreshed, callback); Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(innerCallback, "https://login.live.com/oauth20_token.srf"); if (codeFlow) { request->addPostField("code=" + code); @@ -71,8 +71,8 @@ void OneDriveStorage::getAccessToken(BoolCallback callback, Common::String code) ConnMan.addRequest(request); } -void OneDriveStorage::tokenRefreshed(BoolCallback callback, Networking::RequestDataPair pair) { - Common::JSONValue *json = (Common::JSONValue *)pair.value; +void OneDriveStorage::tokenRefreshed(BoolCallback callback, Networking::RequestJsonPair pair) { + Common::JSONValue *json = pair.value; if (!json) { warning("OneDriveStorage: got NULL instead of JSON"); if (callback) (*callback)(RequestBoolPair(-1, false)); @@ -117,8 +117,8 @@ void OneDriveStorage::printJsonTokenReceived(RequestBoolPair pair) { if (pair.value) syncSaves(0); //try again } -void OneDriveStorage::printJson(Networking::RequestDataPair pair) { - Common::JSONValue *json = (Common::JSONValue *)pair.value; +void OneDriveStorage::printJson(Networking::RequestJsonPair pair) { + Common::JSONValue *json = pair.value; if (!json) { warning("printJson: NULL"); return; @@ -139,7 +139,7 @@ void OneDriveStorage::printJson(Networking::RequestDataPair pair) { int32 OneDriveStorage::syncSaves(BoolCallback callback) { //this is not the real syncSaves() implementation - Networking::DataCallback innerCallback = new Common::Callback<OneDriveStorage, Networking::RequestDataPair>(this, &OneDriveStorage::printJson); + Networking::JsonCallback innerCallback = new Common::Callback<OneDriveStorage, Networking::RequestJsonPair>(this, &OneDriveStorage::printJson); Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(innerCallback, "https://api.onedrive.com/v1.0/drives/"); request->addHeader("Authorization: bearer " + _token); return ConnMan.addRequest(request); diff --git a/backends/cloud/onedrive/onedrivestorage.h b/backends/cloud/onedrive/onedrivestorage.h index 3c92880750..1cb7017d64 100644 --- a/backends/cloud/onedrive/onedrivestorage.h +++ b/backends/cloud/onedrive/onedrivestorage.h @@ -25,6 +25,7 @@ #include "backends/cloud/storage.h" #include "common/callback.h" +#include "backends/networking/curl/curljsonrequest.h" namespace Cloud { namespace OneDrive { @@ -49,10 +50,10 @@ class OneDriveStorage: public Cloud::Storage { * continue your work when new token is available. */ void getAccessToken(BoolCallback callback, Common::String code = ""); - void tokenRefreshed(BoolCallback callback, Networking::RequestDataPair pair); + void tokenRefreshed(BoolCallback callback, Networking::RequestJsonPair pair); void codeFlowComplete(RequestBoolPair pair); - void printJson(Networking::RequestDataPair pair); + void printJson(Networking::RequestJsonPair pair); void printJsonTokenReceived(RequestBoolPair pair); public: virtual ~OneDriveStorage(); diff --git a/backends/networking/curl/curljsonrequest.cpp b/backends/networking/curl/curljsonrequest.cpp index fd3d631ba9..a323b34ed2 100644 --- a/backends/networking/curl/curljsonrequest.cpp +++ b/backends/networking/curl/curljsonrequest.cpp @@ -31,8 +31,8 @@ namespace Networking { -CurlJsonRequest::CurlJsonRequest(DataCallback cb, const char *url): - CurlRequest(cb, url), _contentsStream(DisposeAfterUse::YES) {} +CurlJsonRequest::CurlJsonRequest(JsonCallback cb, const char *url): + CurlRequest(0, url), _jsonCallback(cb), _contentsStream(DisposeAfterUse::YES) {} CurlJsonRequest::~CurlJsonRequest() {} @@ -70,12 +70,12 @@ void CurlJsonRequest::handle() { warning("HTTP response code is not 200 OK (it's %ld)", _stream->httpResponseCode()); ConnMan.getRequestInfo(_id).state = Networking::FINISHED; - if (_callback) { + if (_jsonCallback) { char *contents = getPreparedContents(); if (_stream->httpResponseCode() != 200) debug("%s", contents); Common::JSONValue *json = Common::JSON::parse(contents); - (*_callback)(RequestDataPair(_id, json)); //potential memory leak, free it in your callbacks! + (*_jsonCallback)(RequestJsonPair(_id, json)); //potential memory leak, free it in your callbacks! } } } diff --git a/backends/networking/curl/curljsonrequest.h b/backends/networking/curl/curljsonrequest.h index 3d5dd7858b..75d4a6df81 100644 --- a/backends/networking/curl/curljsonrequest.h +++ b/backends/networking/curl/curljsonrequest.h @@ -25,19 +25,24 @@ #include "backends/networking/curl/curlrequest.h" #include "common/memstream.h" +#include "common/json.h" namespace Networking { class NetworkReadStream; -class CurlJsonRequest: public CurlRequest { +typedef RequestIdPair<Common::JSONValue*> RequestJsonPair; +typedef Common::BaseCallback<RequestJsonPair> *JsonCallback; + +class CurlJsonRequest: public CurlRequest { + JsonCallback _jsonCallback; Common::MemoryWriteStreamDynamic _contentsStream; /** Prepares raw bytes from _contentsStream to be parsed with Common::JSON::parse(). */ char *getPreparedContents(); public: - CurlJsonRequest(DataCallback cb, const char *url); //TODO: use some Callback<JSON> already + CurlJsonRequest(JsonCallback cb, const char *url); virtual ~CurlJsonRequest(); virtual void handle(); |