aboutsummaryrefslogtreecommitdiff
path: root/backends
diff options
context:
space:
mode:
authorAlexander Tkachev2016-05-21 23:21:42 +0600
committerAlexander Tkachev2016-08-24 16:07:55 +0600
commit9e531e3ce7f5b3a1cc87b43beb6f72911cb41bdd (patch)
treeaabc1995e2208f8c7c51bc208716a8948dbb5a92 /backends
parente1109c0c328aaf671e2b03b3b4e6de1ae9061754 (diff)
downloadscummvm-rg350-9e531e3ce7f5b3a1cc87b43beb6f72911cb41bdd.tar.gz
scummvm-rg350-9e531e3ce7f5b3a1cc87b43beb6f72911cb41bdd.tar.bz2
scummvm-rg350-9e531e3ce7f5b3a1cc87b43beb6f72911cb41bdd.zip
CLOUD: Polish Callbacks
Cleaned up all example code and old callbacks. New Callback classes are introduced in "common/callback.h" and documented.
Diffstat (limited to 'backends')
-rw-r--r--backends/cloud/dropbox/dropboxstorage.cpp113
-rw-r--r--backends/cloud/dropbox/dropboxstorage.h31
-rw-r--r--backends/cloud/manager.cpp2
-rw-r--r--backends/cloud/manager.h2
-rw-r--r--backends/cloud/storage.h26
-rw-r--r--backends/networking/curl/curljsonrequest.cpp2
-rw-r--r--backends/networking/curl/curljsonrequest.h7
-rw-r--r--backends/networking/curl/request.h19
8 files changed, 64 insertions, 138 deletions
diff --git a/backends/cloud/dropbox/dropboxstorage.cpp b/backends/cloud/dropbox/dropboxstorage.cpp
index 04e9eeef0b..964b95bb09 100644
--- a/backends/cloud/dropbox/dropboxstorage.cpp
+++ b/backends/cloud/dropbox/dropboxstorage.cpp
@@ -35,17 +35,6 @@ 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 printJsonCallback(Networking::Request* rq, void *ptr) {
- Common::JSONValue *json = (Common::JSONValue *)ptr;
- if (json) {
- debug("printJsonCallback:");
- debug("%s", json->stringify(true).c_str());
- delete json;
- } else {
- debug("printJsonCallback: got NULL instead of JSON!");
- }
-}
-
static void saveAccessTokenCallback(void *ptr) {
Common::JSONValue *json = (Common::JSONValue *)ptr;
if (json) {
@@ -69,53 +58,6 @@ static void saveAccessTokenCallback(void *ptr) {
}
}
-void infoCallback(Networking::Request* request, void *jsonPointer) {
- if (!request) {
- warning("infoCallback: got NULL instead of Request");
-
- Common::JSONValue *json = (Common::JSONValue *)jsonPointer;
- if (json) delete json; //yeah I know we can delete NULL safely
- return;
- }
-
- Storage::InfoCallback callback = (Storage::InfoCallback)request->pointer();
-
- Common::JSONValue *json = (Common::JSONValue *)jsonPointer;
- if (json) {
- //Common::JSONObject result = json->asObject();
- if (callback) {
- callback(StorageInfo(json->stringify()));
- }
- delete json;
- } else {
- warning("infoCallback: got NULL instead of JSON!");
- }
-}
-
-void info2Callback(Networking::Request* request, void *jsonPointer) {
- if (!request) {
- warning("infoCallback: got NULL instead of Request");
-
- Common::JSONValue *json = (Common::JSONValue *)jsonPointer;
- if (json) delete json; //yeah I know we can delete NULL safely
- return;
- }
-
- Common::BaseCallback *callback = (Common::BaseCallback *)request->pointer();
-
- Common::JSONValue *json = (Common::JSONValue *)jsonPointer;
- if (json) {
- //Common::JSONObject result = json->asObject();
- if (callback) {
- (*callback)(new StorageInfo(json->stringify()));
- delete callback;
- }
- delete json;
- } else {
- warning("infoCallback: got NULL instead of JSON!");
- }
-}
-
DropboxStorage::DropboxStorage(Common::String accessToken, Common::String userId): _token(accessToken), _uid(userId) {
curl_global_init(CURL_GLOBAL_ALL);
}
@@ -124,41 +66,42 @@ DropboxStorage::~DropboxStorage() {
curl_global_cleanup();
}
-void syncSavesInfoCallback(StorageInfo info) {
- debug("info: %s", info.info().c_str());
-}
-
-void DropboxStorage::infoMethodCallback(void *storageInfo) {
- StorageInfo *info = (StorageInfo *)storageInfo;
- debug("info: %s", info->info().c_str());
+void DropboxStorage::syncSaves(Common::BaseCallback<bool> *callback) {
+ //this is not the real syncSaves() implementation
+ info(new Common::Callback<DropboxStorage, StorageInfo>(this, &DropboxStorage::infoMethodCallback));
+ //that line meant the following:
+ //"please, do the info API request and, when it's finished, call the infoMethodCallback() of me"
}
-void DropboxStorage::syncSaves(OperationCallback callback) {
- //this is not the real syncSaves() implementation
- info2(new Common::Callback<DropboxStorage>(this, &DropboxStorage::infoMethodCallback));
-}
-
-void DropboxStorage::info(InfoCallback callback) {
- /*
- Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(infoCallback, "https://api.dropboxapi.com/1/account/info");
+void DropboxStorage::info(Common::BaseCallback<StorageInfo> *outerCallback) {
+ Common::BaseCallback<> *innerCallback = new Common::CallbackBridge<DropboxStorage, StorageInfo>(this, &DropboxStorage::infoInnerCallback, 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);
- */
+ //that callback bridge wraps the outerCallback (passed in arguments from user) into innerCallback
+ //so, when CurlJsonRequest is finished, it calls the innerCallback
+ //innerCallback (which is DropboxStorage::infoInnerCallback in this case) processes the void *ptr
+ //and then calls the outerCallback (which wants to receive StorageInfo, not void *)
}
-void DropboxStorage::info2BridgeCallback(Common::BaseCallback *outerCallback, void *ptr) {
- //no NULL checks, delete and such yet
+void DropboxStorage::infoInnerCallback(Common::BaseCallback<StorageInfo> *outerCallback, void *ptr) {
Common::JSONValue *json = (Common::JSONValue *)ptr;
- (*outerCallback)(new StorageInfo(json->stringify()));
+ if (!json) {
+ warning("NULL passed instead of JSON");
+ delete outerCallback;
+ return;
+ }
+
+ if (outerCallback) {
+ (*outerCallback)(StorageInfo(json->stringify()));
+ delete outerCallback;
+ }
+
+ delete json;
}
-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);
+void DropboxStorage::infoMethodCallback(StorageInfo storageInfo) {
+ debug("info: %s", storageInfo.info().c_str());
}
DropboxStorage *DropboxStorage::loadFromConfig() {
@@ -212,7 +155,7 @@ void DropboxStorage::authThroughConsole() {
}
void DropboxStorage::getAccessToken(Common::String code) {
- Common::BaseCallback *callback = new Common::GlobalFunctionCallback(saveAccessTokenCallback);
+ 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");
diff --git a/backends/cloud/dropbox/dropboxstorage.h b/backends/cloud/dropbox/dropboxstorage.h
index efd0eea257..8cc9312a87 100644
--- a/backends/cloud/dropbox/dropboxstorage.h
+++ b/backends/cloud/dropbox/dropboxstorage.h
@@ -24,7 +24,6 @@
#define BACKENDS_CLOUD_DROPBOX_STORAGE_H
#include "backends/cloud/storage.h"
-#include "backends/cloud/manager.h"
#include "common/callback.h"
namespace Cloud {
@@ -40,36 +39,38 @@ class DropboxStorage: public Cloud::Storage {
static void getAccessToken(Common::String code);
- void infoMethodCallback(void *serviceInfoPtr);
-
public:
virtual ~DropboxStorage();
- /** Returns pointer to Common::Array<CloudFile>. */
- virtual void listDirectory(Common::String path, ListDirectoryCallback callback) {} //TODO
+ /** Returns pointer to Common::Array<StorageFile>. */
+ virtual void listDirectory(Common::String path, Common::BaseCallback< Common::Array<StorageFile> > *callback) {} //TODO
/** Calls the callback when finished. */
- virtual void upload(Common::String path, Common::ReadStream* contents, OperationCallback callback) {} //TODO
+ virtual void upload(Common::String path, Common::ReadStream* contents, Common::BaseCallback<bool> *callback) {} //TODO
/** Returns pointer to Common::ReadStream. */
- virtual void download(Common::String path, DownloadCallback callback) {} //TODO
+ virtual void download(Common::String path, Common::BaseCallback<Common::ReadStream> *callback) {} //TODO
/** Calls the callback when finished. */
- virtual void remove(Common::String path, OperationCallback callback) {} //TODO
+ virtual void remove(Common::String path, Common::BaseCallback<bool> *callback) {} //TODO
/** Calls the callback when finished. */
- virtual void syncSaves(OperationCallback callback);
+ virtual void syncSaves(Common::BaseCallback<bool> *callback);
/** Calls the callback when finished. */
- virtual void createDirectory(Common::String path, OperationCallback callback) {} //TODO
+ virtual void createDirectory(Common::String path, Common::BaseCallback<bool> *callback) {} //TODO
/** Calls the callback when finished. */
- virtual void touch(Common::String path, OperationCallback callback) {} //TODO
+ virtual void touch(Common::String path, Common::BaseCallback<bool> *callback) {} //TODO
+
+ /** Returns pointer to the StorageInfo struct. */
+ virtual void info(Common::BaseCallback<StorageInfo> *callback);
+
+ /** This is what is called by CurlJsonRequest. */
+ void infoInnerCallback(Common::BaseCallback<StorageInfo> *outerCallback, void *ptr);
- /** Returns pointer to the ServiceInfo struct. */
- virtual void info(InfoCallback callback);
- void info2(Common::BaseCallback *outerCallback);
- void info2BridgeCallback(Common::BaseCallback *outerCallback, void *ptr);
+ /** This is what is called by infoInnerCallback() (it's its outer callback). */
+ void infoMethodCallback(StorageInfo storageInfo);
/** Returns whether saves sync process is running. */
virtual bool isSyncing() { return false; } //TODO
diff --git a/backends/cloud/manager.cpp b/backends/cloud/manager.cpp
index fc271485bf..a9a2b8a232 100644
--- a/backends/cloud/manager.cpp
+++ b/backends/cloud/manager.cpp
@@ -46,7 +46,7 @@ Storage* Manager::getCurrentStorage() {
return _currentStorage;
}
-void Manager::syncSaves(Storage::OperationCallback callback) {
+void Manager::syncSaves(Common::BaseCallback<bool> *callback) {
Storage* storage = getCurrentStorage();
if (storage) storage->syncSaves(callback);
}
diff --git a/backends/cloud/manager.h b/backends/cloud/manager.h
index 47109cc146..c247132707 100644
--- a/backends/cloud/manager.h
+++ b/backends/cloud/manager.h
@@ -38,7 +38,7 @@ public:
virtual void init();
virtual Storage* getCurrentStorage();
- virtual void syncSaves(Storage::OperationCallback callback);
+ virtual void syncSaves(Common::BaseCallback<bool> *callback);
};
} //end of namespace Cloud
diff --git a/backends/cloud/storage.h b/backends/cloud/storage.h
index 4fb06f6a9d..f2079eb919 100644
--- a/backends/cloud/storage.h
+++ b/backends/cloud/storage.h
@@ -26,6 +26,7 @@
#include "common/array.h"
#include "common/stream.h"
#include "common/str.h"
+#include "common/callback.h"
namespace Cloud {
@@ -70,37 +71,32 @@ public:
class Storage {
public:
- typedef void(*ListDirectoryCallback)(Common::Array<StorageFile>& result);
- typedef void(*DownloadCallback)(Common::ReadStream* result);
- typedef void(*InfoCallback)(StorageInfo result);
- typedef void(*OperationCallback)(bool successed);
-
Storage() {}
virtual ~Storage() {}
- /** Returns pointer to Common::Array<CloudFile>. */
- virtual void listDirectory(Common::String path, ListDirectoryCallback callback) = 0;
+ /** Returns pointer to Common::Array<StorageFile>. */
+ virtual void listDirectory(Common::String path, Common::BaseCallback< Common::Array<StorageFile> > *callback) = 0;
/** Calls the callback when finished. */
- virtual void upload(Common::String path, Common::ReadStream* contents, OperationCallback callback) = 0;
+ virtual void upload(Common::String path, Common::ReadStream* contents, Common::BaseCallback<bool> *callback) = 0;
/** Returns pointer to Common::ReadStream. */
- virtual void download(Common::String path, DownloadCallback callback) = 0;
+ virtual void download(Common::String path, Common::BaseCallback<Common::ReadStream> *callback) = 0;
/** Calls the callback when finished. */
- virtual void remove(Common::String path, OperationCallback callback) = 0;
+ virtual void remove(Common::String path, Common::BaseCallback<bool> *callback) = 0;
/** Calls the callback when finished. */
- virtual void syncSaves(OperationCallback callback) = 0;
+ virtual void syncSaves(Common::BaseCallback<bool> *callback) = 0;
/** Calls the callback when finished. */
- virtual void createDirectory(Common::String path, OperationCallback callback) = 0;
+ virtual void createDirectory(Common::String path, Common::BaseCallback<bool> *callback) = 0;
/** Calls the callback when finished. */
- virtual void touch(Common::String path, OperationCallback callback) = 0;
+ virtual void touch(Common::String path, Common::BaseCallback<bool> *callback) = 0;
- /** Returns pointer to the ServiceInfo struct. */
- virtual void info(InfoCallback callback) = 0;
+ /** Returns pointer to the StorageInfo struct. */
+ virtual void info(Common::BaseCallback<StorageInfo> *callback) = 0;
/** Returns whether saves sync process is running. */
virtual bool isSyncing() = 0;
diff --git a/backends/networking/curl/curljsonrequest.cpp b/backends/networking/curl/curljsonrequest.cpp
index c3064681ef..702de22f46 100644
--- a/backends/networking/curl/curljsonrequest.cpp
+++ b/backends/networking/curl/curljsonrequest.cpp
@@ -31,7 +31,7 @@
namespace Networking {
-CurlJsonRequest::CurlJsonRequest(Common::BaseCallback* 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;
}
diff --git a/backends/networking/curl/curljsonrequest.h b/backends/networking/curl/curljsonrequest.h
index 1098638609..50fa8183dd 100644
--- a/backends/networking/curl/curljsonrequest.h
+++ b/backends/networking/curl/curljsonrequest.h
@@ -25,10 +25,7 @@
#include "backends/networking/curl/request.h"
#include "common/memstream.h"
-
-namespace Common {
-class BaseCallback;
-}
+#include "common/json.h"
struct curl_slist;
@@ -47,7 +44,7 @@ class CurlJsonRequest : public Request {
char *getPreparedContents();
public:
- CurlJsonRequest(Common::BaseCallback *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 860784f2a7..d3efd588dc 100644
--- a/backends/networking/curl/request.h
+++ b/backends/networking/curl/request.h
@@ -22,30 +22,22 @@
#ifndef BACKENDS_NETWORKING_CURL_REQUEST_H
#define BACKENDS_NETWORKING_CURL_REQUEST_H
-#include <common/callback.h>
+
+#include "common/callback.h"
namespace Networking {
class Request {
protected:
- typedef void(*SimpleCallback)(Request* request, void *result);
-
/**
* Callback, which should be called before Request returns true in handle().
* That's the way Requests pass the result to the code which asked to create this request.
*/
- Common::BaseCallback* _callback;
-
- /**
- * Pointer, which could be set by Request creating code. It might be accessed
- * from this Request when callback is called, for example.
- */
-
- void *_pointer;
+ Common::BaseCallback<> *_callback;
public:
- Request(Common::BaseCallback* cb): _callback(cb) {};
+ Request(Common::BaseCallback<> *cb): _callback(cb) {};
virtual ~Request() {};
/**
@@ -55,9 +47,6 @@ public:
*/
virtual bool handle() = 0;
-
- void setPointer(void *ptr) { _pointer = ptr; }
- void *pointer() const { return _pointer; }
};
} //end of namespace Cloud