2013-11-14 00:08:28 +05:30
|
|
|
/* Copyright 2013 MultiMC Contributors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <logic/tasks/Task.h>
|
|
|
|
|
|
|
|
#include <QString>
|
|
|
|
#include <QJsonObject>
|
2013-12-08 22:04:45 +05:30
|
|
|
#include <QTimer>
|
2013-12-25 04:08:37 +05:30
|
|
|
#include <qsslerror.h>
|
2013-11-14 00:08:28 +05:30
|
|
|
|
2013-11-18 23:35:35 +05:30
|
|
|
#include "logic/auth/MojangAccount.h"
|
2013-11-14 00:08:28 +05:30
|
|
|
|
|
|
|
class QNetworkReply;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A Yggdrasil task is a task that performs an operation on a given mojang account.
|
|
|
|
*/
|
|
|
|
class YggdrasilTask : public Task
|
|
|
|
{
|
2013-12-01 06:30:42 +05:30
|
|
|
Q_OBJECT
|
2013-11-14 00:08:28 +05:30
|
|
|
public:
|
2013-12-05 07:09:52 +05:30
|
|
|
explicit YggdrasilTask(MojangAccount * account, QObject *parent = 0);
|
2013-11-14 00:08:28 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* Class describing a Yggdrasil error response.
|
|
|
|
*/
|
2013-12-08 22:04:45 +05:30
|
|
|
struct Error
|
2013-11-14 00:08:28 +05:30
|
|
|
{
|
2013-12-08 22:04:45 +05:30
|
|
|
QString m_errorMessageShort;
|
|
|
|
QString m_errorMessageVerbose;
|
2013-11-14 00:08:28 +05:30
|
|
|
QString m_cause;
|
|
|
|
};
|
|
|
|
|
2013-11-14 04:29:50 +05:30
|
|
|
protected:
|
2013-11-14 00:08:28 +05:30
|
|
|
/**
|
|
|
|
* Enum for describing the state of the current task.
|
|
|
|
* Used by the getStateMessage function to determine what the status message should be.
|
|
|
|
*/
|
|
|
|
enum State
|
|
|
|
{
|
|
|
|
STATE_SENDING_REQUEST,
|
|
|
|
STATE_PROCESSING_RESPONSE,
|
|
|
|
STATE_OTHER,
|
|
|
|
};
|
|
|
|
|
|
|
|
virtual void executeTask();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the JSON object that will be sent to the authentication server.
|
|
|
|
* Should be overridden by subclasses.
|
|
|
|
*/
|
|
|
|
virtual QJsonObject getRequestContent() const = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the endpoint to POST to.
|
|
|
|
* No leading slash.
|
|
|
|
*/
|
|
|
|
virtual QString getEndpoint() const = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Processes the response received from the server.
|
|
|
|
* If an error occurred, this should emit a failed signal and return false.
|
|
|
|
* If Yggdrasil gave an error response, it should call setError() first, and then return false.
|
|
|
|
* Otherwise, it should return true.
|
2013-11-29 08:15:52 +05:30
|
|
|
* Note: If the response from the server was blank, and the HTTP code was 200, this function is called with
|
|
|
|
* an empty QJsonObject.
|
2013-11-14 00:08:28 +05:30
|
|
|
*/
|
|
|
|
virtual bool processResponse(QJsonObject responseData) = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Processes an error response received from the server.
|
|
|
|
* The default implementation will read data from Yggdrasil's standard error response format and set it as this task's Error.
|
|
|
|
* \returns a QString error message that will be passed to emitFailed.
|
|
|
|
*/
|
|
|
|
virtual QString processError(QJsonObject responseData);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the state message for the given state.
|
|
|
|
* Used to set the status message for the task.
|
|
|
|
* Should be overridden by subclasses that want to change messages for a given state.
|
|
|
|
*/
|
2013-11-15 02:02:43 +05:30
|
|
|
virtual QString getStateMessage(const State state) const;
|
2013-11-14 00:08:28 +05:30
|
|
|
|
2013-12-01 06:30:42 +05:30
|
|
|
protected
|
|
|
|
slots:
|
2013-12-08 22:04:45 +05:30
|
|
|
void processReply();
|
|
|
|
void refreshTimers(qint64, qint64);
|
|
|
|
void heartbeat();
|
2013-12-25 04:08:37 +05:30
|
|
|
void sslErrors(QList<QSslError>);
|
2013-12-08 22:04:45 +05:30
|
|
|
|
|
|
|
public
|
|
|
|
slots:
|
|
|
|
virtual void abort() override;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// FIXME: segfault disaster waiting to happen
|
|
|
|
MojangAccount *m_account = nullptr;
|
|
|
|
QNetworkReply *m_netReply = nullptr;
|
|
|
|
std::shared_ptr<Error> m_error;
|
|
|
|
QTimer timeout_keeper;
|
|
|
|
QTimer counter;
|
|
|
|
int count = 0; // num msec since time reset
|
|
|
|
|
|
|
|
const int timeout_max = 10000;
|
|
|
|
const int time_step = 50;
|
2013-11-14 00:08:28 +05:30
|
|
|
};
|