aboutsummaryrefslogtreecommitdiff
path: root/backends/cloud/basestorage.cpp
diff options
context:
space:
mode:
authorAlexander Tkachev2019-07-16 20:36:27 +0700
committerMatan Bareket2019-07-30 14:51:41 -0400
commit31628d642881499f7d6833732b096c028087e14e (patch)
tree24c95b69f5b77e016cf6b3dbdee9332d329ea8ea /backends/cloud/basestorage.cpp
parent72c7f8226c4295fb103e5e32d4e96b12659ab67b (diff)
downloadscummvm-rg350-31628d642881499f7d6833732b096c028087e14e.tar.gz
scummvm-rg350-31628d642881499f7d6833732b096c028087e14e.tar.bz2
scummvm-rg350-31628d642881499f7d6833732b096c028087e14e.zip
CLOUD: Refactor BaseStorage largest methods
Not sure if that's really better, but it was really annoying to copy-paste `delete a; delete b; return;` in every error-handling section.
Diffstat (limited to 'backends/cloud/basestorage.cpp')
-rw-r--r--backends/cloud/basestorage.cpp153
1 files changed, 71 insertions, 82 deletions
diff --git a/backends/cloud/basestorage.cpp b/backends/cloud/basestorage.cpp
index e856bbe3d7..805cb472a2 100644
--- a/backends/cloud/basestorage.cpp
+++ b/backends/cloud/basestorage.cpp
@@ -48,67 +48,68 @@ void BaseStorage::getAccessToken(Common::String code) {
}
void BaseStorage::codeFlowComplete(Networking::JsonResponse response) {
+ bool success = true;
+
Common::JSONValue *json = (Common::JSONValue *)response.value;
if (json == nullptr) {
debug(9, "BaseStorage::codeFlowComplete: got NULL instead of JSON!");
- CloudMan.removeStorage(this);
- return;
+ success = false;
}
- if (!json->isObject()) {
+ if (success && !json->isObject()) {
debug(9, "BaseStorage::codeFlowComplete: passed JSON is not an object!");
- CloudMan.removeStorage(this);
- delete json;
- return;
+ success = false;
}
- Common::JSONObject result = json->asObject();
- if (!Networking::CurlJsonRequest::jsonContainsAttribute(result, "error", "BaseStorage::codeFlowComplete")) {
- warning("BaseStorage: bad response, no 'error' attribute passed");
- debug(9, "%s", json->stringify(true).c_str());
- CloudMan.removeStorage(this);
- delete json;
- return;
+ Common::JSONObject result;
+ if (success) {
+ result = json->asObject();
+ if (!Networking::CurlJsonRequest::jsonContainsAttribute(result, "error", "BaseStorage::codeFlowComplete")) {
+ warning("BaseStorage: bad response, no 'error' attribute passed");
+ debug(9, "%s", json->stringify(true).c_str());
+ success = false;
+ }
}
- if (result.getVal("error")->asBool()) {
+ if (success && result.getVal("error")->asBool()) {
Common::String errorMessage = "{error: true}, message is missing";
if (Networking::CurlJsonRequest::jsonContainsString(result, "message", "BaseStorage::codeFlowComplete")) {
errorMessage = result.getVal("message")->asString();
}
warning("BaseStorage: response says error occurred: %s", errorMessage.c_str());
- CloudMan.removeStorage(this);
- delete json;
- return;
+ success = false;
}
- if (!Networking::CurlJsonRequest::jsonContainsObject(result, "oauth", "BaseStorage::codeFlowComplete")) {
+ if (success && !Networking::CurlJsonRequest::jsonContainsObject(result, "oauth", "BaseStorage::codeFlowComplete")) {
warning("BaseStorage: bad response, no 'oauth' attribute passed");
debug(9, "%s", json->stringify(true).c_str());
- CloudMan.removeStorage(this);
- delete json;
- return;
+ success = false;
}
- Common::JSONObject oauth = result.getVal("oauth")->asObject();
+ Common::JSONObject oauth;
bool requiresRefreshToken = needsRefreshToken();
- if (!Networking::CurlJsonRequest::jsonContainsString(oauth, "access_token", "BaseStorage::codeFlowComplete") ||
- !Networking::CurlJsonRequest::jsonContainsString(oauth, "refresh_token", "BaseStorage::codeFlowComplete", !requiresRefreshToken)) {
- warning("BaseStorage: bad response, no 'access_token' or 'refresh_token' attribute passed");
- debug(9, "%s", json->stringify(true).c_str());
- CloudMan.removeStorage(this);
- delete json;
- return;
+ if (success) {
+ oauth = result.getVal("oauth")->asObject();
+ if (!Networking::CurlJsonRequest::jsonContainsString(oauth, "access_token", "BaseStorage::codeFlowComplete") ||
+ !Networking::CurlJsonRequest::jsonContainsString(oauth, "refresh_token", "BaseStorage::codeFlowComplete", !requiresRefreshToken)) {
+ warning("BaseStorage: bad response, no 'access_token' or 'refresh_token' attribute passed");
+ debug(9, "%s", json->stringify(true).c_str());
+ success = false;
+ }
}
- debug(9, "%s", json->stringify(true).c_str()); // TODO: remove when done testing against cloud.scummvm.org
- _token = oauth.getVal("access_token")->asString();
- if (requiresRefreshToken) {
- _refreshToken = oauth.getVal("refresh_token")->asString();
+ if (success) {
+ debug(9, "%s", json->stringify(true).c_str()); // TODO: remove when done testing against cloud.scummvm.org
+ _token = oauth.getVal("access_token")->asString();
+ if (requiresRefreshToken) {
+ _refreshToken = oauth.getVal("refresh_token")->asString();
+ }
+ CloudMan.replaceStorage(this, storageIndex());
+ ConfMan.flushToDisk();
}
- CloudMan.replaceStorage(this, storageIndex());
- ConfMan.flushToDisk();
+ if (!success)
+ CloudMan.removeStorage(this);
delete json;
}
@@ -135,80 +136,68 @@ void BaseStorage::refreshAccessToken(BoolCallback callback, Networking::ErrorCal
}
void BaseStorage::tokenRefreshed(BoolCallback callback, Networking::JsonResponse response) {
+ bool success = true;
+
Common::JSONValue *json = response.value;
if (json == nullptr) {
debug(9, "BaseStorage::tokenRefreshed: got NULL instead of JSON!");
- if (callback)
- (*callback)(BoolResponse(nullptr, false));
- delete callback;
- return;
+ success = false;
}
- if (!json->isObject()) {
+ if (success && !json->isObject()) {
debug(9, "BaseStorage::tokenRefreshed: passed JSON is not an object!");
- if (callback)
- (*callback)(BoolResponse(nullptr, false));
- delete json;
- delete callback;
- return;
+ success = false;
}
- Common::JSONObject result = json->asObject();
- if (!Networking::CurlJsonRequest::jsonContainsAttribute(result, "error", "BaseStorage::tokenRefreshed")) {
- warning("BaseStorage: bad response, no 'error' attribute passed");
- debug(9, "%s", json->stringify(true).c_str());
- if (callback)
- (*callback)(BoolResponse(nullptr, false));
- delete json;
- delete callback;
- return;
+ Common::JSONObject result;
+ if (success) {
+ result = json->asObject();
+ if (!Networking::CurlJsonRequest::jsonContainsAttribute(result, "error", "BaseStorage::tokenRefreshed")) {
+ warning("BaseStorage: bad response, no 'error' attribute passed");
+ debug(9, "%s", json->stringify(true).c_str());
+ success = false;
+ }
}
- if (result.getVal("error")->asBool()) {
+ if (success && result.getVal("error")->asBool()) {
Common::String errorMessage = "{error: true}, message is missing";
if (Networking::CurlJsonRequest::jsonContainsString(result, "message", "BaseStorage::tokenRefreshed")) {
errorMessage = result.getVal("message")->asString();
}
warning("BaseStorage: response says error occurred: %s", errorMessage.c_str());
- if (callback)
- (*callback)(BoolResponse(nullptr, false));
- delete json;
- delete callback;
- return;
+ success = false;
}
- if (!Networking::CurlJsonRequest::jsonContainsObject(result, "oauth", "BaseStorage::tokenRefreshed")) {
+ if (success && !Networking::CurlJsonRequest::jsonContainsObject(result, "oauth", "BaseStorage::tokenRefreshed")) {
warning("BaseStorage: bad response, no 'oauth' attribute passed");
debug(9, "%s", json->stringify(true).c_str());
- if (callback)
- (*callback)(BoolResponse(nullptr, false));
- delete json;
- delete callback;
- return;
+ success = false;
}
- Common::JSONObject oauth = result.getVal("oauth")->asObject();
+ Common::JSONObject oauth;
bool requiresRefreshToken = !canReuseRefreshToken();
- if (!Networking::CurlJsonRequest::jsonContainsString(oauth, "access_token", "BaseStorage::tokenRefreshed") ||
- !Networking::CurlJsonRequest::jsonContainsString(oauth, "refresh_token", "BaseStorage::tokenRefreshed", !requiresRefreshToken)) {
- warning("BaseStorage: bad response, no 'access_token' or 'refresh_token' attribute passed");
- debug(9, "%s", json->stringify(true).c_str());
- if (callback)
- (*callback)(BoolResponse(nullptr, false));
- delete json;
- delete callback;
- return;
+ if (success) {
+ oauth = result.getVal("oauth")->asObject();
+ if (!Networking::CurlJsonRequest::jsonContainsString(oauth, "access_token", "BaseStorage::tokenRefreshed") ||
+ !Networking::CurlJsonRequest::jsonContainsString(oauth, "refresh_token", "BaseStorage::tokenRefreshed", !requiresRefreshToken)) {
+ warning("BaseStorage: bad response, no 'access_token' or 'refresh_token' attribute passed");
+ debug(9, "%s", json->stringify(true).c_str());
+ success = false;
+ }
}
- debug(9, "%s", json->stringify(true).c_str()); // TODO: remove when done testing against cloud.scummvm.org
+ if (success) {
+ debug(9, "%s", json->stringify(true).c_str()); // TODO: remove when done testing against cloud.scummvm.org
- _token = oauth.getVal("access_token")->asString();
- if (requiresRefreshToken) {
- _refreshToken = oauth.getVal("refresh_token")->asString();
+ _token = oauth.getVal("access_token")->asString();
+ if (requiresRefreshToken) {
+ _refreshToken = oauth.getVal("refresh_token")->asString();
+ }
+ CloudMan.save(); //ask CloudManager to save our new access_token and refresh_token
}
- CloudMan.save(); //ask CloudManager to save our new access_token and refresh_token
+
if (callback)
- (*callback)(BoolResponse(nullptr, true));
+ (*callback)(BoolResponse(nullptr, success));
delete json;
delete callback;
}