2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2020-02-06 14:04:03 +00:00
|
|
|
#include <LibCore/NetworkJob.h>
|
|
|
|
#include <LibCore/NetworkResponse.h>
|
2019-04-07 12:36:10 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2019-10-15 17:14:11 +00:00
|
|
|
//#define CNETWORKJOB_DEBUG
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
namespace Core {
|
|
|
|
|
|
|
|
NetworkJob::NetworkJob()
|
2019-04-07 12:36:10 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
NetworkJob::~NetworkJob()
|
2019-04-07 12:36:10 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-01-30 23:21:10 +00:00
|
|
|
void NetworkJob::start()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkJob::shutdown()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
void NetworkJob::did_finish(NonnullRefPtr<NetworkResponse>&& response)
|
2019-04-07 12:36:10 +00:00
|
|
|
{
|
2019-11-23 20:42:02 +00:00
|
|
|
// NOTE: We protect ourselves here, since the on_finish callback may otherwise
|
|
|
|
// trigger destruction of this job somehow.
|
2020-02-02 11:34:39 +00:00
|
|
|
NonnullRefPtr<NetworkJob> protector(*this);
|
2019-11-23 20:42:02 +00:00
|
|
|
|
2019-04-07 12:36:10 +00:00
|
|
|
m_response = move(response);
|
2019-10-15 17:14:11 +00:00
|
|
|
#ifdef CNETWORKJOB_DEBUG
|
2019-08-04 20:10:54 +00:00
|
|
|
dbg() << *this << " job did_finish!";
|
2019-10-15 17:14:11 +00:00
|
|
|
#endif
|
2019-04-07 12:36:10 +00:00
|
|
|
ASSERT(on_finish);
|
|
|
|
on_finish(true);
|
2019-09-21 15:32:26 +00:00
|
|
|
shutdown();
|
2019-04-07 12:36:10 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
void NetworkJob::did_fail(Error error)
|
2019-04-07 12:36:10 +00:00
|
|
|
{
|
2019-11-23 20:42:02 +00:00
|
|
|
// NOTE: We protect ourselves here, since the on_finish callback may otherwise
|
|
|
|
// trigger destruction of this job somehow.
|
2020-02-02 11:34:39 +00:00
|
|
|
NonnullRefPtr<NetworkJob> protector(*this);
|
2019-11-23 20:42:02 +00:00
|
|
|
|
2019-04-07 12:36:10 +00:00
|
|
|
m_error = error;
|
2019-10-15 17:14:11 +00:00
|
|
|
#ifdef CNETWORKJOB_DEBUG
|
2019-06-22 20:46:05 +00:00
|
|
|
dbgprintf("%s{%p} job did_fail! error: %u (%s)\n", class_name(), this, (unsigned)error, to_string(error));
|
2019-10-15 17:14:11 +00:00
|
|
|
#endif
|
2019-04-07 12:36:10 +00:00
|
|
|
ASSERT(on_finish);
|
|
|
|
on_finish(false);
|
2019-09-21 15:32:26 +00:00
|
|
|
shutdown();
|
2019-06-22 20:46:05 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
const char* to_string(NetworkJob::Error error)
|
2019-06-22 20:46:05 +00:00
|
|
|
{
|
|
|
|
switch (error) {
|
2020-02-02 11:34:39 +00:00
|
|
|
case NetworkJob::Error::ProtocolFailed:
|
2019-06-22 20:46:05 +00:00
|
|
|
return "ProtocolFailed";
|
2020-02-02 11:34:39 +00:00
|
|
|
case NetworkJob::Error::ConnectionFailed:
|
2019-06-22 20:46:05 +00:00
|
|
|
return "ConnectionFailed";
|
2020-02-02 11:34:39 +00:00
|
|
|
case NetworkJob::Error::TransmissionFailed:
|
2019-06-22 20:46:05 +00:00
|
|
|
return "TransmissionFailed";
|
2020-02-02 11:34:39 +00:00
|
|
|
case NetworkJob::Error::Cancelled:
|
2019-09-21 15:32:26 +00:00
|
|
|
return "Cancelled";
|
2019-06-22 20:46:05 +00:00
|
|
|
default:
|
|
|
|
return "(Unknown error)";
|
|
|
|
}
|
2019-04-07 12:36:10 +00:00
|
|
|
}
|
2020-02-02 11:34:39 +00:00
|
|
|
|
|
|
|
}
|