diff options
author | Alexander Tkachev | 2016-05-27 15:21:06 +0600 |
---|---|---|
committer | Alexander Tkachev | 2016-08-24 16:07:55 +0600 |
commit | 98150beb38f73b56c7bc76f95dcc1d72290e4ac7 (patch) | |
tree | c56f8d97d30fb15f41f07f402c80e83a13cc03fc /backends/cloud/dropbox | |
parent | 83b349a033d71e92e292d1f1da0578d557ec6411 (diff) | |
download | scummvm-rg350-98150beb38f73b56c7bc76f95dcc1d72290e4ac7.tar.gz scummvm-rg350-98150beb38f73b56c7bc76f95dcc1d72290e4ac7.tar.bz2 scummvm-rg350-98150beb38f73b56c7bc76f95dcc1d72290e4ac7.zip |
CLOUD: Refactor ConnectionManager/Requests system
ConnectionManager now storages Request * (not generates ids for it),
Requests have control on their RequestState, RequestIdPair is now called
Response and storages Request * with some response together.
All related classes are changed to use it in more clean and
understandable way.
Request, RequestState and Response are carefully commented/documented.
Diffstat (limited to 'backends/cloud/dropbox')
-rw-r--r-- | backends/cloud/dropbox/dropboxlistdirectoryrequest.cpp | 32 | ||||
-rw-r--r-- | backends/cloud/dropbox/dropboxlistdirectoryrequest.h | 7 | ||||
-rw-r--r-- | backends/cloud/dropbox/dropboxstorage.cpp | 31 | ||||
-rw-r--r-- | backends/cloud/dropbox/dropboxstorage.h | 22 |
4 files changed, 51 insertions, 41 deletions
diff --git a/backends/cloud/dropbox/dropboxlistdirectoryrequest.cpp b/backends/cloud/dropbox/dropboxlistdirectoryrequest.cpp index 3158149c02..04fbf46ac6 100644 --- a/backends/cloud/dropbox/dropboxlistdirectoryrequest.cpp +++ b/backends/cloud/dropbox/dropboxlistdirectoryrequest.cpp @@ -32,7 +32,7 @@ namespace Dropbox { DropboxListDirectoryRequest::DropboxListDirectoryRequest(Common::String token, Common::String path, Storage::FileArrayCallback cb, bool recursive): Networking::Request(0), _requestedPath(path), _requestedRecursive(recursive), _filesCallback(cb), - _token(token), _complete(false), _requestId(-1) { + _token(token), _complete(false), _innerRequest(nullptr) { startupWork(); } @@ -40,7 +40,7 @@ void DropboxListDirectoryRequest::startupWork() { _files.clear(); _complete = false; - Networking::JsonCallback innerCallback = new Common::Callback<DropboxListDirectoryRequest, Networking::RequestJsonPair>(this, &DropboxListDirectoryRequest::responseCallback); + Networking::JsonCallback innerCallback = new Common::Callback<DropboxListDirectoryRequest, Networking::JsonResponse>(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"); @@ -54,11 +54,11 @@ void DropboxListDirectoryRequest::startupWork() { Common::JSONValue value(jsonRequestParameters); request->addPostField(Common::JSON::stringify(&value)); - _requestId = ConnMan.addRequest(request); + _innerRequest = ConnMan.addRequest(request); } -void DropboxListDirectoryRequest::responseCallback(Networking::RequestJsonPair pair) { +void DropboxListDirectoryRequest::responseCallback(Networking::JsonResponse pair) { Common::JSONValue *json = pair.value; if (json) { Common::JSONObject response = json->asObject(); @@ -89,7 +89,7 @@ void DropboxListDirectoryRequest::responseCallback(Networking::RequestJsonPair p bool hasMore = response.getVal("has_more")->asBool(); if (hasMore) { - Networking::JsonCallback innerCallback = new Common::Callback<DropboxListDirectoryRequest, Networking::RequestJsonPair>(this, &DropboxListDirectoryRequest::responseCallback); + Networking::JsonCallback innerCallback = new Common::Callback<DropboxListDirectoryRequest, Networking::JsonResponse>(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"); @@ -113,22 +113,28 @@ void DropboxListDirectoryRequest::responseCallback(Networking::RequestJsonPair p } void DropboxListDirectoryRequest::handle() { - if (_complete) { - ConnMan.getRequestInfo(_id).state = Networking::FINISHED; - if (_filesCallback) (*_filesCallback)(Storage::RequestFileArrayPair(_id, _files)); - } + if (_complete) finishFiles(_files); } void DropboxListDirectoryRequest::restart() { - if (_requestId != -1) { - Networking::RequestInfo &info = ConnMan.getRequestInfo(_requestId); + if (_innerRequest) { //TODO: I'm really not sure some CurlRequest would handle this (it must stop corresponding CURL transfer) - info.state = Networking::FINISHED; //may be CANCELED or INTERRUPTED or something? - _requestId = -1; + _innerRequest->finish(); //may be CANCELED or INTERRUPTED or something? + _innerRequest = nullptr; } startupWork(); } +void DropboxListDirectoryRequest::finish() { + Common::Array<StorageFile> files; + finishFiles(files); +} + +void DropboxListDirectoryRequest::finishFiles(Common::Array<StorageFile> &files) { + Request::finish(); + if (_filesCallback) (*_filesCallback)(Storage::FileArrayResponse(this, files)); +} + } //end of namespace Dropbox } //end of namespace Cloud diff --git a/backends/cloud/dropbox/dropboxlistdirectoryrequest.h b/backends/cloud/dropbox/dropboxlistdirectoryrequest.h index 9a82ef7091..e2a9ebf3a4 100644 --- a/backends/cloud/dropbox/dropboxlistdirectoryrequest.h +++ b/backends/cloud/dropbox/dropboxlistdirectoryrequest.h @@ -39,17 +39,20 @@ class DropboxListDirectoryRequest: public Networking::Request { Common::String _token; bool _complete; Common::Array<StorageFile> _files; - int32 _requestId; + Request *_innerRequest; - void responseCallback(Networking::RequestJsonPair pair); + void responseCallback(Networking::JsonResponse pair); void startupWork(); + void finishFiles(Common::Array<StorageFile> &files); + public: DropboxListDirectoryRequest(Common::String token, Common::String path, Storage::FileArrayCallback cb, bool recursive = false); virtual ~DropboxListDirectoryRequest() { delete _filesCallback; } virtual void handle(); virtual void restart(); + virtual void finish(); }; } //end of namespace Dropbox diff --git a/backends/cloud/dropbox/dropboxstorage.cpp b/backends/cloud/dropbox/dropboxstorage.cpp index b5292c83c7..a1a97e00dd 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::RequestJsonPair pair) { +static void saveAccessTokenCallback(Networking::JsonResponse pair) { Common::JSONValue *json = (Common::JSONValue *)pair.value; if (json) { debug("saveAccessTokenCallback:"); @@ -55,6 +55,7 @@ static void saveAccessTokenCallback(Networking::RequestJsonPair pair) { ConfMan.set("storage1_access_token", result.getVal("access_token")->asString(), "cloud"); ConfMan.set("storage1_user_id", result.getVal("uid")->asString(), "cloud"); ConfMan.removeKey("dropbox_code", "cloud"); + ConfMan.flushToDisk(); debug("Now please restart ScummVM to apply the changes."); } @@ -84,11 +85,11 @@ void DropboxStorage::printFiles(Common::Array<StorageFile> files) { debug("\t%s", files[i].name().c_str()); } -int32 DropboxStorage::listDirectory(Common::String path, FileArrayCallback outerCallback, bool recursive) { +Networking::Request *DropboxStorage::listDirectory(Common::String path, FileArrayCallback outerCallback, bool recursive) { return ConnMan.addRequest(new DropboxListDirectoryRequest(_token, path, outerCallback, recursive)); } -int32 DropboxStorage::streamFile(Common::String path, ReadStreamCallback callback) { +Networking::Request *DropboxStorage::streamFile(Common::String path, Networking::NetworkReadStreamCallback callback) { Common::JSONObject jsonRequestParameters; jsonRequestParameters.setVal("path", new Common::JSONValue(path)); Common::JSONValue value(jsonRequestParameters); @@ -98,32 +99,32 @@ int32 DropboxStorage::streamFile(Common::String path, ReadStreamCallback callbac request->addHeader("Dropbox-API-Arg: " + Common::JSON::stringify(&value)); request->addHeader("Content-Type: "); //required to be empty (as we do POST, it's usually app/form-url-encoded) - RequestReadStreamPair pair = request->execute(); + Networking::NetworkReadStreamResponse pair = request->execute(); if (callback) (*callback)(pair); - return pair.id; + return pair.request; } -int32 DropboxStorage::download(Common::String remotePath, Common::String localPath, BoolCallback callback) { +Networking::Request *DropboxStorage::download(Common::String remotePath, Common::String localPath, BoolCallback callback) { Common::DumpFile *f = new Common::DumpFile(); if (!f->open(localPath, true)) { warning("DropboxStorage: unable to open file to download into"); - if (callback) (*callback)(RequestBoolPair(-1, false)); + if (callback) (*callback)(BoolResponse(nullptr, false)); delete f; - return -1; + return nullptr; } return ConnMan.addRequest(new DownloadRequest(this, callback, remotePath, f)); } -int32 DropboxStorage::syncSaves(BoolCallback callback) { +Networking::Request *DropboxStorage::syncSaves(BoolCallback callback) { //this is not the real syncSaves() implementation //"" is root in Dropbox, not "/" //this must create all these directories: return download("/remote/test.jpg", "local/a/b/c/d/test.jpg", 0); } -int32 DropboxStorage::info(StorageInfoCallback outerCallback) { - Networking::JsonCallback innerCallback = new Common::CallbackBridge<DropboxStorage, RequestStorageInfoPair, Networking::RequestJsonPair>(this, &DropboxStorage::infoInnerCallback, outerCallback); +Networking::Request *DropboxStorage::info(StorageInfoCallback outerCallback) { + Networking::JsonCallback innerCallback = new Common::CallbackBridge<DropboxStorage, StorageInfoResponse, Networking::JsonResponse>(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); @@ -133,7 +134,7 @@ int32 DropboxStorage::info(StorageInfoCallback outerCallback) { //and then calls the outerCallback (which wants to receive StorageInfo, not void *) } -void DropboxStorage::infoInnerCallback(StorageInfoCallback outerCallback, Networking::RequestJsonPair pair) { +void DropboxStorage::infoInnerCallback(StorageInfoCallback outerCallback, Networking::JsonResponse pair) { Common::JSONValue *json = pair.value; if (!json) { warning("NULL passed instead of JSON"); @@ -151,14 +152,14 @@ void DropboxStorage::infoInnerCallback(StorageInfoCallback outerCallback, Networ uint32 quotaNormal = quota.getVal("normal")->asNumber(); uint32 quotaShared = quota.getVal("shared")->asNumber(); uint32 quotaAllocated = quota.getVal("quota")->asNumber(); - (*outerCallback)(RequestStorageInfoPair(-1, StorageInfo(uid, name, email, quotaNormal+quotaShared, quotaAllocated))); + (*outerCallback)(StorageInfoResponse(nullptr, StorageInfo(uid, name, email, quotaNormal+quotaShared, quotaAllocated))); delete outerCallback; } delete json; } -void DropboxStorage::infoMethodCallback(RequestStorageInfoPair pair) { +void DropboxStorage::infoMethodCallback(StorageInfoResponse pair) { debug("\nStorage info:"); debug("User name: %s", pair.value.name().c_str()); debug("Email: %s", pair.value.email().c_str()); @@ -216,7 +217,7 @@ void DropboxStorage::authThroughConsole() { } void DropboxStorage::getAccessToken(Common::String code) { - Networking::JsonCallback callback = new Common::GlobalFunctionCallback<Networking::RequestJsonPair>(saveAccessTokenCallback); + Networking::JsonCallback callback = new Common::GlobalFunctionCallback<Networking::JsonResponse>(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 dd082b25f1..f95d0c9812 100644 --- a/backends/cloud/dropbox/dropboxstorage.h +++ b/backends/cloud/dropbox/dropboxstorage.h @@ -41,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::RequestJsonPair json); + void infoInnerCallback(StorageInfoCallback outerCallback, Networking::JsonResponse json); void printFiles(Common::Array<StorageFile> files); @@ -65,34 +65,34 @@ public: /** Public Cloud API comes down there. */ /** Returns Common::Array<StorageFile>. */ - virtual int32 listDirectory(Common::String path, FileArrayCallback callback, bool recursive = false); + virtual Networking::Request *listDirectory(Common::String path, FileArrayCallback callback, bool recursive = false); /** Calls the callback when finished. */ - virtual int32 upload(Common::String path, Common::ReadStream *contents, BoolCallback callback) { return -1; } //TODO + virtual Networking::Request *upload(Common::String path, Common::ReadStream *contents, BoolCallback callback) { return nullptr; } //TODO /** Returns pointer to Networking::NetworkReadStream. */ - virtual int32 streamFile(Common::String path, ReadStreamCallback callback); + virtual Networking::Request *streamFile(Common::String path, Networking::NetworkReadStreamCallback callback); /** Calls the callback when finished. */ - virtual int32 download(Common::String remotePath, Common::String localPath, BoolCallback callback); + virtual Networking::Request *download(Common::String remotePath, Common::String localPath, BoolCallback callback); /** Calls the callback when finished. */ - virtual int32 remove(Common::String path, BoolCallback callback) { return -1; } //TODO + virtual Networking::Request *remove(Common::String path, BoolCallback callback) { return nullptr; } //TODO /** Calls the callback when finished. */ - virtual int32 syncSaves(BoolCallback callback); + virtual Networking::Request *syncSaves(BoolCallback callback); /** Calls the callback when finished. */ - virtual int32 createDirectory(Common::String path, BoolCallback callback) { return -1; } //TODO + virtual Networking::Request *createDirectory(Common::String path, BoolCallback callback) { return nullptr; } //TODO /** Calls the callback when finished. */ - virtual int32 touch(Common::String path, BoolCallback callback) { return -1; } //TODO + virtual Networking::Request *touch(Common::String path, BoolCallback callback) { return nullptr; } //TODO /** Returns the StorageInfo struct. */ - virtual int32 info(StorageInfoCallback callback); + virtual Networking::Request *info(StorageInfoCallback callback); /** This method is passed into info(). (Temporary) */ - void infoMethodCallback(RequestStorageInfoPair pair); + void infoMethodCallback(StorageInfoResponse pair); /** Returns whether saves sync process is running. */ virtual bool isSyncing() { return false; } //TODO |