[Curl] Reimplement CurlDownload with CurlRequest
[WebKit-https.git] / Source / WebCore / platform / network / curl / CurlDownload.h
index e70a248..5bb7a1d 100644 (file)
 
 #pragma once
 
-#include "CurlJobManager.h"
-#include "FileSystem.h"
-#include "ResourceHandle.h"
+#if USE(CURL)
+
+#include "CurlRequest.h"
+#include "CurlRequestDelegate.h"
 #include "ResourceRequest.h"
 #include "ResourceResponse.h"
-#include <wtf/Lock.h>
-#include <wtf/Threading.h>
 
 namespace WebCore {
 
+class CurlRequest;
+class ResourceHandle;
+
 class CurlDownloadListener {
 public:
-    virtual void didReceiveResponse() { }
+    virtual void didReceiveResponse(const ResourceResponse&) { }
     virtual void didReceiveDataOfLength(int) { }
     virtual void didFinish() { }
     virtual void didFail() { }
 };
 
-class CurlDownload : public ThreadSafeRefCounted<CurlDownload>, public CurlJobClient {
+class CurlDownload : public ThreadSafeRefCounted<CurlDownload>, public CurlRequestDelegate {
 public:
-    CurlDownload();
+    CurlDownload() = default;
     ~CurlDownload();
 
-    void init(CurlDownloadListener*, const URL&);
-    void init(CurlDownloadListener*, ResourceHandle*, const ResourceRequest&, const ResourceResponse&);
+    void init(CurlDownloadListener&, const URL&);
+    void init(CurlDownloadListener&, ResourceHandle*, const ResourceRequest&, const ResourceResponse&);
 
     void setListener(CurlDownloadListener* listener) { m_listener = listener; }
 
     bool start();
     bool cancel();
 
-    ResourceResponse getResponse() const;
-
     bool deletesFileUponFailure() const { return m_deletesFileUponFailure; }
     void setDeletesFileUponFailure(bool deletesFileUponFailure) { m_deletesFileUponFailure = deletesFileUponFailure; }
 
     void setDestination(const String& destination) { m_destination = destination; }
 
 private:
-    void retain() override;
-    void release() override;
-
-    CURL* handle() override { return m_curlHandle ? m_curlHandle->handle() : nullptr; }
-    CURL* setupTransfer() override;
-    void didCompleteTransfer(CURLcode) override;
-    void didCancelTransfer() override;
-
-    void closeFile();
-    void moveFileToDestination();
-    void writeDataToFile(const char* data, int size);
-
-    // Called on download thread.
-    void didReceiveHeader(const String& header);
-    void didReceiveData(void* data, int size);
+    Ref<CurlRequest> createCurlRequest(ResourceRequest&);
+    void curlDidReceiveResponse(const CurlResponse&) override;
+    void curlDidReceiveBuffer(Ref<SharedBuffer>&&) override;
+    void curlDidComplete() override;
+    void curlDidFailWithError(const ResourceError&) override;
 
-    // Called on main thread.
-    void didReceiveResponse();
-    void didReceiveDataOfLength(int size);
-    void didFinish();
-    void didFail();
+    bool shouldRedirectAsGET(const ResourceRequest&, bool crossOrigin);
+    void willSendRequest();
 
-    static size_t writeCallback(char* ptr, size_t, size_t nmemb, void* data);
-    static size_t headerCallback(char* ptr, size_t, size_t nmemb, void* data);
+    CurlDownloadListener* m_listener { nullptr };
+    bool m_isCancelled { false };
 
-    std::unique_ptr<CurlHandle> m_curlHandle;
     ResourceRequest m_request;
     ResourceResponse m_response;
-
-    String m_tempPath;
-    String m_destination;
-    PlatformFileHandle m_tempHandle { invalidPlatformFileHandle };
     bool m_deletesFileUponFailure { false };
-    mutable Lock m_mutex;
-    CurlDownloadListener* m_listener { nullptr };
+    String m_destination;
+    unsigned m_redirectCount { 0 };
+    RefPtr<CurlRequest> m_curlRequest;
 };
 
-}
+} // namespace WebCore
+
+#endif