VirtualBox

Ignore:
Timestamp:
Oct 15, 2015 4:02:35 PM (9 years ago)
Author:
vboxsync
Message:

FE/Qt: Networking cleanup/rework (part 14): Moving classes and members into appropriate places.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VirtualBox/src/net/UINetworkReply.cpp

    r58265 r58266  
    5454
    5555/** QThread extension
    56   * used as network-reply thread instance. */
     56  * used as network-reply private thread interface. */
    5757class UINetworkReplyPrivateThread : public QThread
    5858{
     
    7373    UINetworkReplyPrivateThread(const QNetworkRequest &request, UINetworkRequestType type);
    7474
    75     /** Returns short descriptive context of thread's current operation. */
    76     const QString context() const { return m_strContext; }
    77 
    7875    /** @name APIs
    7976     * @{ */
     77        /** Aborts reply. */
     78        void abort();
     79
     80        /** Returns the URL of the reply which is the URL of the request for now. */
     81        QUrl url() const { return m_request.url(); }
     82
     83        /** Returns the last cached IPRT HTTP error of the reply. */
     84        int error() const { return m_iError; }
     85
    8086        /** Returns binary content of the reply. */
    8187        const QByteArray& readAll() const { return m_reply; }
    82         /** Returns the last cached IPRT HTTP error of the reply. */
    83         int error() const { return m_iError; }
    84         /** Aborts reply. */
    85         void abort();
    8688        /** Returns value for the cached reply header of the passed @a type. */
    87         QString header(QNetworkRequest::KnownHeaders type) const
    88         {
    89             /* Look for known header type: */
    90             switch (type)
    91             {
    92                 case QNetworkRequest::ContentTypeHeader:   return m_headers.value("Content-Type");
    93                 case QNetworkRequest::ContentLengthHeader: return m_headers.value("Content-Length");
    94                 case QNetworkRequest::LastModifiedHeader:  return m_headers.value("Last-Modified");
    95                 default: break;
    96             }
    97             /* Return null-string by default: */
    98             return QString();
    99         }
    100         /** Returns the URL of the reply which is the URL of the request for now. */
    101         QUrl url() const { return m_request.url(); }
     89        QString header(QNetworkRequest::KnownHeaders type) const;
     90        /** Returns value for the cached reply attribute of the passed @a code. */
     91        QVariant attribute(QNetworkRequest::Attribute code) const { /** @todo r=dsen: Fix that. */ Q_UNUSED(code); return QVariant(); }
     92
     93        /** Returns short descriptive context of thread's current operation. */
     94        const QString context() const { return m_strContext; }
    10295    /** @} */
    10396
    10497private:
    10598
    106     /** @name Helpers - HTTP stuff
     99    /** @name Helpers for HTTP and Certificates handling.
    107100     * @{ */
    108101        /** Applies configuration. */
     
    116109        /** Performs main request. */
    117110        int performMainRequest();
     111
    118112        /** Performs whole thread functionality. */
    119113        void run();
     114
     115        /** Handles download progress callback.
     116          * @param  cbDownloadTotal  Brings the total amount of bytes to be received.
     117          * @param  cbDownloaded     Brings the current amount of bytes received. */
     118        void handleProgressChange(uint64_t cbDownloadTotal, uint64_t cbDownloaded);
    120119    /** @} */
    121 
    122     /** Additinoal download nfo about wanted certificate. */
    123     typedef struct CERTINFO
    124     {
    125         /** Holds the filename of the zip file we download (PEM). */
    126         const char *pszZipFile;
    127         /** Lists direct URLs to PEM formatted files. */
    128         const char *apszUrls[4];
    129     } CERTINFO;
    130120
    131121    /** @name Static helpers for HTTP and Certificates handling.
     
    180170        static int convertVerifyAndAddPemCertificateToStore(RTCRSTORE hStore, void const *pvResponse,
    181171                                                            size_t cbResponse, PCRTCRCERTWANTED pWantedCert);
    182     /** @} */
    183 
    184     /** @name HTTP download progress handling.
    185      * @{ */
     172
    186173        /** Redirects download progress callback to particular object which can handle it.
    187174          * @param  hHttp            Brings the HTTP client instance.
     
    191178          * @param  cbDownloaded     Brings the current amount of bytes received. */
    192179        static DECLCALLBACK(void) handleProgressChange(RTHTTP hHttp, void *pvUser, uint64_t cbDownloadTotal, uint64_t cbDownloaded);
    193 
    194         /** Handles download progress callback.
    195           * @param  cbDownloadTotal  Brings the total amount of bytes to be received.
    196           * @param  cbDownloaded     Brings the current amount of bytes received. */
    197         void handleProgressChange(uint64_t cbDownloadTotal, uint64_t cbDownloaded);
    198180    /** @} */
    199181
    200     /** Holds short descriptive context of thread's current operation. */
    201     QString m_strContext;
     182    /** Additinoal download nfo about wanted certificate. */
     183    typedef struct CERTINFO
     184    {
     185        /** Holds the filename of the zip file we download (PEM). */
     186        const char *pszZipFile;
     187        /** Lists direct URLs to PEM formatted files. */
     188        const char *apszUrls[4];
     189    } CERTINFO;
    202190
    203191    /** Holds the request instance. */
     
    206194    UINetworkRequestType m_type;
    207195
     196    /** Holds the IPRT HTTP client instance handle. */
     197    RTHTTP m_hHttp;
    208198    /** Holds the last cached IPRT HTTP error of the reply. */
    209199    int m_iError;
    210     /** Holds the IPRT HTTP client instance handle. */
    211     RTHTTP m_hHttp;
     200    /** Holds short descriptive context of thread's current operation. */
     201    QString m_strContext;
    212202    /** Holds the reply instance. */
    213203    QByteArray m_reply;
     
    231221#endif /* VBOX_GUI_IN_TST_SSL_CERT_DOWNLOADS */
    232222};
     223
     224
     225#ifndef VBOX_GUI_IN_TST_SSL_CERT_DOWNLOADS
     226/** QObject extension
     227  * used as network-reply private data interface. */
     228class UINetworkReplyPrivate : public QObject
     229{
     230    Q_OBJECT;
     231
     232signals:
     233
     234    /** Notifies listeners about reply progress change.
     235      * @param  iBytesReceived  Holds the current amount of bytes received.
     236      * @param  iBytesTotal     Holds the total amount of bytes to be received. */
     237    void downloadProgress(qint64 iBytesReceived, qint64 iBytesTotal);
     238
     239    /** Notifies listeners about reply has finished processing. */
     240    void finished();
     241
     242public:
     243
     244    /** Constructs reply private data for the passed @a request of the passed @a type. */
     245    UINetworkReplyPrivate(const QNetworkRequest &request, UINetworkRequestType type);
     246    /** Destructs reply private data. */
     247    ~UINetworkReplyPrivate();
     248
     249    /** Aborts reply. */
     250    void abort() { m_pThread->abort(); }
     251
     252    /** Returns URL of the reply. */
     253    QUrl url() const { return m_pThread->url(); }
     254
     255    /** Returns the last cached error of the reply. */
     256    QNetworkReply::NetworkError error() const { return m_error; }
     257    /** Returns the user-oriented string corresponding to the last cached error of the reply. */
     258    QString errorString() const;
     259
     260    /** Returns binary content of the reply. */
     261    QByteArray readAll() const { return m_pThread->readAll(); }
     262    /** Returns value for the cached reply header of the passed @a type. */
     263    QString header(QNetworkRequest::KnownHeaders type) const { return m_pThread->header(type); }
     264    /** Returns value for the cached reply attribute of the passed @a code. */
     265    QVariant attribute(QNetworkRequest::Attribute code) const { return m_pThread->attribute(code); }
     266
     267private slots:
     268
     269    /** Handles signal about reply has finished processing. */
     270    void sltFinished();
     271
     272private:
     273
     274    /** Holds full error template in "Context description: Error description" form. */
     275    QString m_strErrorTemplate;
     276
     277    /** Holds the last cached error of the reply. */
     278    QNetworkReply::NetworkError m_error;
     279
     280    /** Holds the reply thread instance. */
     281    UINetworkReplyPrivateThread *m_pThread;
     282};
     283#endif /* !VBOX_GUI_IN_TST_SSL_CERT_DOWNLOADS */
     284
     285
     286/*********************************************************************************************************************************
     287*   Class UINetworkReplyPrivateThread implementation.                                                                            *
     288*********************************************************************************************************************************/
    233289
    234290/* static */
     
    289345    : m_request(request)
    290346    , m_type(type)
     347    , m_hHttp(NIL_RTHTTP)
    291348    , m_iError(VINF_SUCCESS)
    292     , m_hHttp(NIL_RTHTTP)
    293349{
    294350}
     
    299355    if (m_hHttp != NIL_RTHTTP)
    300356        RTHttpAbort(m_hHttp);
     357}
     358
     359QString UINetworkReplyPrivateThread::header(QNetworkRequest::KnownHeaders type) const
     360{
     361    /* Look for known header type: */
     362    switch (type)
     363    {
     364        case QNetworkRequest::ContentTypeHeader:   return m_headers.value("Content-Type");
     365        case QNetworkRequest::ContentLengthHeader: return m_headers.value("Content-Length");
     366        case QNetworkRequest::LastModifiedHeader:  return m_headers.value("Last-Modified");
     367        default: break;
     368    }
     369    /* Return null-string by default: */
     370    return QString();
    301371}
    302372
     
    541611        }
    542612    }
     613}
     614
     615void UINetworkReplyPrivateThread::handleProgressChange(uint64_t cbDownloadTotal, uint64_t cbDownloaded)
     616{
     617#ifndef VBOX_GUI_IN_TST_SSL_CERT_DOWNLOADS
     618    /* Notify listeners about progress change: */
     619    emit sigDownloadProgress(cbDownloaded, cbDownloadTotal);
     620#else /* VBOX_GUI_IN_TST_SSL_CERT_DOWNLOADS */
     621    Q_UNUSED(cbDownloaded);
     622    Q_UNUSED(cbDownloadTotal);
     623#endif /* VBOX_GUI_IN_TST_SSL_CERT_DOWNLOADS */
    543624}
    544625
     
    830911}
    831912
    832 void UINetworkReplyPrivateThread::handleProgressChange(uint64_t cbDownloadTotal, uint64_t cbDownloaded)
    833 {
     913
    834914#ifndef VBOX_GUI_IN_TST_SSL_CERT_DOWNLOADS
    835     /* Notify listeners about progress change: */
    836     emit sigDownloadProgress(cbDownloaded, cbDownloadTotal);
    837 #else /* VBOX_GUI_IN_TST_SSL_CERT_DOWNLOADS */
    838     Q_UNUSED(cbDownloaded);
    839     Q_UNUSED(cbDownloadTotal);
    840 #endif /* VBOX_GUI_IN_TST_SSL_CERT_DOWNLOADS */
    841 }
    842 
    843 #ifndef VBOX_GUI_IN_TST_SSL_CERT_DOWNLOADS
    844 
    845 /** QObject extension
    846   * used as network-reply private data instance. */
    847 class UINetworkReplyPrivate : public QObject
    848 {
    849     Q_OBJECT;
    850 
    851 signals:
    852 
    853     /** Notifies listeners about reply progress change.
    854       * @param  iBytesReceived  Holds the current amount of bytes received.
    855       * @param  iBytesTotal     Holds the total amount of bytes to be received. */
    856     void downloadProgress(qint64 iBytesReceived, qint64 iBytesTotal);
    857 
    858     /** Notifies listeners about reply has finished processing. */
    859     void finished();
    860 
    861 public:
    862 
    863     /** Constructs reply private data for the passed @a request of the passed @a type. */
    864     UINetworkReplyPrivate(const QNetworkRequest &request, UINetworkRequestType type)
    865         : m_error(QNetworkReply::NoError)
    866         , m_pThread(0)
    867     {
    868         /* Prepare full error template: */
    869         m_strErrorTemplate = tr("%1: %2", "Context description: Error description");
    870         /* Create and run reply thread: */
    871         m_pThread = new UINetworkReplyPrivateThread(request, type);
    872 #ifndef VBOX_GUI_IN_TST_SSL_CERT_DOWNLOADS
    873         connect(m_pThread, SIGNAL(sigDownloadProgress(qint64, qint64)),
    874                 this, SIGNAL(downloadProgress(qint64, qint64)), Qt::QueuedConnection);
    875 #endif /* !VBOX_GUI_IN_TST_SSL_CERT_DOWNLOADS */
    876         connect(m_pThread, SIGNAL(finished()), this, SLOT(sltFinished()));
    877         m_pThread->start();
    878     }
    879 
    880     /** Destructs reply private data. */
    881     ~UINetworkReplyPrivate()
    882     {
    883         /* Terminate network-reply thread: */
    884         m_pThread->abort();
    885         m_pThread->wait();
    886         delete m_pThread;
    887         m_pThread = 0;
    888     }
    889 
    890     /** Aborts reply. */
    891     void abort() { m_pThread->abort(); }
    892 
    893     /** Returns URL of the reply. */
    894     QUrl url() const { return m_pThread->url(); }
    895 
    896     /** Returns the last cached error of the reply. */
    897     QNetworkReply::NetworkError error() const { return m_error; }
    898 
    899     /** Returns the user-oriented string corresponding to the last cached error of the reply. */
    900     QString errorString() const
    901     {
    902         switch (m_error)
    903         {
    904             case QNetworkReply::NoError:                     break;
    905             case QNetworkReply::RemoteHostClosedError:       return m_strErrorTemplate.arg(m_pThread->context(), tr("Unable to initialize HTTP library"));
    906             case QNetworkReply::HostNotFoundError:           return m_strErrorTemplate.arg(m_pThread->context(), tr("Host not found"));
    907             case QNetworkReply::ContentAccessDenied:         return m_strErrorTemplate.arg(m_pThread->context(), tr("Content access denied"));
    908             case QNetworkReply::ProtocolFailure:             return m_strErrorTemplate.arg(m_pThread->context(), tr("Protocol failure"));
    909             case QNetworkReply::ConnectionRefusedError:      return m_strErrorTemplate.arg(m_pThread->context(), tr("Connection refused"));
    910             case QNetworkReply::SslHandshakeFailedError:     return m_strErrorTemplate.arg(m_pThread->context(), tr("SSL authentication failed"));
    911             case QNetworkReply::AuthenticationRequiredError: return m_strErrorTemplate.arg(m_pThread->context(), tr("Wrong SSL certificate format"));
    912             case QNetworkReply::ContentReSendError:          return m_strErrorTemplate.arg(m_pThread->context(), tr("Content moved"));
    913             case QNetworkReply::ProxyNotFoundError:          return m_strErrorTemplate.arg(m_pThread->context(), tr("Proxy not found"));
    914             default:                                         return m_strErrorTemplate.arg(m_pThread->context(), tr("Unknown reason"));
    915         }
    916         return QString();
    917     }
    918 
    919     /** Returns binary content of the reply. */
    920     QByteArray readAll() const { return m_pThread->readAll(); }
    921 
    922     /** Returns value for the cached reply header of the passed @a type. */
    923     QString header(QNetworkRequest::KnownHeaders type) const { return m_pThread->header(type); }
    924 
    925     /** Returns value for the cached reply attribute of the passed @a code. */
    926     QVariant attribute(QNetworkRequest::Attribute code) const { /** @todo r=dsen: Fix that. */ Q_UNUSED(code); return QVariant(); }
    927 
    928 private slots:
    929 
    930     /** Handles signal about reply has finished processing. */
    931     void sltFinished()
    932     {
    933         switch (m_pThread->error())
    934         {
    935             case VINF_SUCCESS:                         m_error = QNetworkReply::NoError; break;
    936             case VERR_HTTP_INIT_FAILED:                m_error = QNetworkReply::RemoteHostClosedError; break;
    937             case VERR_HTTP_NOT_FOUND:                  m_error = QNetworkReply::HostNotFoundError; break;
    938             case VERR_HTTP_ACCESS_DENIED:              m_error = QNetworkReply::ContentAccessDenied; break;
    939             case VERR_HTTP_BAD_REQUEST:                m_error = QNetworkReply::ProtocolFailure; break;
    940             case VERR_HTTP_COULDNT_CONNECT:            m_error = QNetworkReply::ConnectionRefusedError; break;
    941             case VERR_HTTP_SSL_CONNECT_ERROR:          m_error = QNetworkReply::SslHandshakeFailedError; break;
    942             case VERR_HTTP_CACERT_WRONG_FORMAT:        m_error = QNetworkReply::AuthenticationRequiredError; break;
    943             case VERR_HTTP_CACERT_CANNOT_AUTHENTICATE: m_error = QNetworkReply::AuthenticationRequiredError; break;
    944             case VERR_HTTP_ABORTED:                    m_error = QNetworkReply::OperationCanceledError; break;
    945             case VERR_HTTP_REDIRECTED:                 m_error = QNetworkReply::ContentReSendError; break;
    946             case VERR_HTTP_PROXY_NOT_FOUND:            m_error = QNetworkReply::ProxyNotFoundError; break;
    947             default:                                   m_error = QNetworkReply::UnknownNetworkError; break;
    948         }
    949         emit finished();
    950     }
    951 
    952 private:
    953 
    954     /** Holds full error template in "Context description: Error description" form. */
    955     QString m_strErrorTemplate;
    956 
    957     /** Holds the last cached error of the reply. */
    958     QNetworkReply::NetworkError m_error;
    959 
    960     /** Holds the reply thread instance. */
    961     UINetworkReplyPrivateThread *m_pThread;
    962 };
     915
     916/*********************************************************************************************************************************
     917*   Class UINetworkReplyPrivate implementation.                                                                                  *
     918*********************************************************************************************************************************/
     919
     920UINetworkReplyPrivate::UINetworkReplyPrivate(const QNetworkRequest &request, UINetworkRequestType type)
     921    : m_error(QNetworkReply::NoError)
     922    , m_pThread(0)
     923{
     924    /* Prepare full error template: */
     925    m_strErrorTemplate = tr("%1: %2", "Context description: Error description");
     926
     927    /* Create and run reply thread: */
     928    m_pThread = new UINetworkReplyPrivateThread(request, type);
     929    connect(m_pThread, SIGNAL(sigDownloadProgress(qint64, qint64)),
     930            this, SIGNAL(downloadProgress(qint64, qint64)), Qt::QueuedConnection);
     931    connect(m_pThread, SIGNAL(finished()), this, SLOT(sltFinished()));
     932    m_pThread->start();
     933}
     934
     935UINetworkReplyPrivate::~UINetworkReplyPrivate()
     936{
     937    /* Terminate network-reply thread: */
     938    m_pThread->abort();
     939    m_pThread->wait();
     940    delete m_pThread;
     941    m_pThread = 0;
     942}
     943
     944QString UINetworkReplyPrivate::errorString() const
     945{
     946    /* Look for known error codes: */
     947    switch (m_error)
     948    {
     949        case QNetworkReply::NoError:                     break;
     950        case QNetworkReply::RemoteHostClosedError:       return m_strErrorTemplate.arg(m_pThread->context(), tr("Unable to initialize HTTP library"));
     951        case QNetworkReply::HostNotFoundError:           return m_strErrorTemplate.arg(m_pThread->context(), tr("Host not found"));
     952        case QNetworkReply::ContentAccessDenied:         return m_strErrorTemplate.arg(m_pThread->context(), tr("Content access denied"));
     953        case QNetworkReply::ProtocolFailure:             return m_strErrorTemplate.arg(m_pThread->context(), tr("Protocol failure"));
     954        case QNetworkReply::ConnectionRefusedError:      return m_strErrorTemplate.arg(m_pThread->context(), tr("Connection refused"));
     955        case QNetworkReply::SslHandshakeFailedError:     return m_strErrorTemplate.arg(m_pThread->context(), tr("SSL authentication failed"));
     956        case QNetworkReply::AuthenticationRequiredError: return m_strErrorTemplate.arg(m_pThread->context(), tr("Wrong SSL certificate format"));
     957        case QNetworkReply::ContentReSendError:          return m_strErrorTemplate.arg(m_pThread->context(), tr("Content moved"));
     958        case QNetworkReply::ProxyNotFoundError:          return m_strErrorTemplate.arg(m_pThread->context(), tr("Proxy not found"));
     959        default:                                         return m_strErrorTemplate.arg(m_pThread->context(), tr("Unknown reason"));
     960    }
     961    /* Return null-string by default: */
     962    return QString();
     963}
     964
     965void UINetworkReplyPrivate::sltFinished()
     966{
     967    /* Look for known error codes: */
     968    switch (m_pThread->error())
     969    {
     970        case VINF_SUCCESS:                         m_error = QNetworkReply::NoError; break;
     971        case VERR_HTTP_INIT_FAILED:                m_error = QNetworkReply::RemoteHostClosedError; break;
     972        case VERR_HTTP_NOT_FOUND:                  m_error = QNetworkReply::HostNotFoundError; break;
     973        case VERR_HTTP_ACCESS_DENIED:              m_error = QNetworkReply::ContentAccessDenied; break;
     974        case VERR_HTTP_BAD_REQUEST:                m_error = QNetworkReply::ProtocolFailure; break;
     975        case VERR_HTTP_COULDNT_CONNECT:            m_error = QNetworkReply::ConnectionRefusedError; break;
     976        case VERR_HTTP_SSL_CONNECT_ERROR:          m_error = QNetworkReply::SslHandshakeFailedError; break;
     977        case VERR_HTTP_CACERT_WRONG_FORMAT:        m_error = QNetworkReply::AuthenticationRequiredError; break;
     978        case VERR_HTTP_CACERT_CANNOT_AUTHENTICATE: m_error = QNetworkReply::AuthenticationRequiredError; break;
     979        case VERR_HTTP_ABORTED:                    m_error = QNetworkReply::OperationCanceledError; break;
     980        case VERR_HTTP_REDIRECTED:                 m_error = QNetworkReply::ContentReSendError; break;
     981        case VERR_HTTP_PROXY_NOT_FOUND:            m_error = QNetworkReply::ProxyNotFoundError; break;
     982        default:                                   m_error = QNetworkReply::UnknownNetworkError; break;
     983    }
     984    /* Redirect signal to external listeners: */
     985    emit finished();
     986}
    963987
    964988
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette