Browse Source

URL: Make downloadToFile return unique_ptr

tags/2021-05-28
reuk 5 years ago
parent
commit
a9492679da
7 changed files with 28 additions and 25 deletions
  1. +1
    -1
      modules/juce_core/native/juce_android_Network.cpp
  2. +1
    -1
      modules/juce_core/native/juce_curl_Network.cpp
  3. +1
    -1
      modules/juce_core/native/juce_linux_Network.cpp
  4. +4
    -4
      modules/juce_core/native/juce_mac_Network.mm
  5. +1
    -1
      modules/juce_core/native/juce_win32_Network.cpp
  6. +15
    -12
      modules/juce_core/network/juce_URL.cpp
  7. +5
    -5
      modules/juce_core/network/juce_URL.h

+ 1
- 1
modules/juce_core/native/juce_android_Network.cpp View File

@@ -554,7 +554,7 @@ private:
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Pimpl)
};
URL::DownloadTask* URL::downloadToFile (const File& targetLocation, String extraHeaders, DownloadTask::Listener* listener, bool shouldUsePost)
std::unique_ptr<URL::DownloadTask> URL::downloadToFile (const File& targetLocation, String extraHeaders, DownloadTask::Listener* listener, bool shouldUsePost)
{
return URL::DownloadTask::createFallbackDownloader (*this, targetLocation, extraHeaders, listener, shouldUsePost);
}


+ 1
- 1
modules/juce_core/native/juce_curl_Network.cpp View File

@@ -642,7 +642,7 @@ public:
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Pimpl)
};
URL::DownloadTask* URL::downloadToFile (const File& targetLocation, String extraHeaders, DownloadTask::Listener* listener, bool shouldUsePost)
std::unique_ptr<URL::DownloadTask> URL::downloadToFile (const File& targetLocation, String extraHeaders, DownloadTask::Listener* listener, bool shouldUsePost)
{
return URL::DownloadTask::createFallbackDownloader (*this, targetLocation, extraHeaders, listener, shouldUsePost);
}


+ 1
- 1
modules/juce_core/native/juce_linux_Network.cpp View File

@@ -576,7 +576,7 @@ private:
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Pimpl)
};
URL::DownloadTask* URL::downloadToFile (const File& targetLocation, String extraHeaders, DownloadTask::Listener* listener, bool shouldUsePost)
std::unique_ptr<URL::DownloadTask> URL::downloadToFile (const File& targetLocation, String extraHeaders, DownloadTask::Listener* listener, bool shouldUsePost)
{
return URL::DownloadTask::createFallbackDownloader (*this, targetLocation, extraHeaders, listener, shouldUsePost);
}


+ 4
- 4
modules/juce_core/native/juce_mac_Network.mm View File

@@ -648,12 +648,12 @@ struct BackgroundDownloadTask : public URL::DownloadTask
HashMap<String, BackgroundDownloadTask*, DefaultHashFunctions, CriticalSection> BackgroundDownloadTask::activeSessions;
URL::DownloadTask* URL::downloadToFile (const File& targetLocation, String extraHeaders, DownloadTask::Listener* listener, bool usePostRequest)
std::unique_ptr<URL::DownloadTask> URL::downloadToFile (const File& targetLocation, String extraHeaders, DownloadTask::Listener* listener, bool usePostRequest)
{
std::unique_ptr<BackgroundDownloadTask> downloadTask (new BackgroundDownloadTask (*this, targetLocation, extraHeaders, listener, usePostRequest));
if (downloadTask->initOK() && downloadTask->connect())
return downloadTask.release();
return downloadTask;
return nullptr;
}
@@ -663,7 +663,7 @@ void URL::DownloadTask::juce_iosURLSessionNotify (const String& identifier)
BackgroundDownloadTask::invokeNotify (identifier);
}
#else
URL::DownloadTask* URL::downloadToFile (const File& targetLocation, String extraHeaders, DownloadTask::Listener* listener, bool usePost)
std::unique_ptr<URL::DownloadTask> URL::downloadToFile (const File& targetLocation, String extraHeaders, DownloadTask::Listener* listener, bool usePost)
{
return URL::DownloadTask::createFallbackDownloader (*this, targetLocation, extraHeaders, listener, usePost);
}
@@ -930,7 +930,7 @@ private:
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (URLConnectionState)
};
URL::DownloadTask* URL::downloadToFile (const File& targetLocation, String extraHeaders, DownloadTask::Listener* listener, bool shouldUsePost)
std::unique_ptr<URL::DownloadTask> URL::downloadToFile (const File& targetLocation, String extraHeaders, DownloadTask::Listener* listener, bool shouldUsePost)
{
return URL::DownloadTask::createFallbackDownloader (*this, targetLocation, extraHeaders, listener, shouldUsePost);
}


+ 1
- 1
modules/juce_core/native/juce_win32_Network.cpp View File

@@ -636,7 +636,7 @@ bool JUCE_CALLTYPE Process::openEmailWithAttachments (const String& targetEmailA
return mapiSendMail (0, 0, &message, MAPI_DIALOG | MAPI_LOGON_UI, 0) == SUCCESS_SUCCESS;
}
URL::DownloadTask* URL::downloadToFile (const File& targetLocation, String extraHeaders, DownloadTask::Listener* listener, bool shouldUsePost)
std::unique_ptr<URL::DownloadTask> URL::downloadToFile (const File& targetLocation, String extraHeaders, DownloadTask::Listener* listener, bool shouldUsePost)
{
return URL::DownloadTask::createFallbackDownloader (*this, targetLocation, extraHeaders, listener, shouldUsePost);
}


+ 15
- 12
modules/juce_core/network/juce_URL.cpp View File

@@ -26,13 +26,13 @@ namespace juce
struct FallbackDownloadTask : public URL::DownloadTask,
public Thread
{
FallbackDownloadTask (FileOutputStream* outputStreamToUse,
FallbackDownloadTask (std::unique_ptr<FileOutputStream> outputStreamToUse,
size_t bufferSizeToUse,
WebInputStream* streamToUse,
std::unique_ptr<WebInputStream> streamToUse,
URL::DownloadTask::Listener* listenerToUse)
: Thread ("DownloadTask thread"),
fileStream (outputStreamToUse),
stream (streamToUse),
fileStream (std::move (outputStreamToUse)),
stream (std::move (streamToUse)),
bufferSize (bufferSizeToUse),
buffer (bufferSize),
listener (listenerToUse)
@@ -110,22 +110,25 @@ void URL::DownloadTask::Listener::progress (DownloadTask*, int64, int64) {}
URL::DownloadTask::Listener::~Listener() {}
//==============================================================================
URL::DownloadTask* URL::DownloadTask::createFallbackDownloader (const URL& urlToUse,
const File& targetFileToUse,
const String& extraHeadersToUse,
Listener* listenerToUse,
bool usePostRequest)
std::unique_ptr<URL::DownloadTask> URL::DownloadTask::createFallbackDownloader (const URL& urlToUse,
const File& targetFileToUse,
const String& extraHeadersToUse,
Listener* listenerToUse,
bool usePostRequest)
{
const size_t bufferSize = 0x8000;
targetFileToUse.deleteFile();
if (auto outputStream = std::unique_ptr<FileOutputStream> (targetFileToUse.createOutputStream (bufferSize)))
if (auto outputStream = targetFileToUse.createOutputStream (bufferSize))
{
std::unique_ptr<WebInputStream> stream (new WebInputStream (urlToUse, usePostRequest));
auto stream = std::make_unique<WebInputStream> (urlToUse, usePostRequest);
stream->withExtraHeaders (extraHeadersToUse);
if (stream->connect (nullptr))
return new FallbackDownloadTask (outputStream.release(), bufferSize, stream.release(), listenerToUse);
return std::make_unique<FallbackDownloadTask> (std::move (outputStream),
bufferSize,
std::move (stream),
listenerToUse);
}
return nullptr;


+ 5
- 5
modules/juce_core/network/juce_URL.h View File

@@ -415,7 +415,7 @@ public:
private:
friend class URL;
static DownloadTask* createFallbackDownloader (const URL&, const File&, const String&, Listener*, bool);
static std::unique_ptr<DownloadTask> createFallbackDownloader (const URL&, const File&, const String&, Listener*, bool);
public:
#if JUCE_IOS
@@ -436,10 +436,10 @@ public:
using a native OS background network task. Such tasks automatically deal with
network re-connections and continuing your download while your app is suspended.
*/
DownloadTask* downloadToFile (const File& targetLocation,
String extraHeaders = String(),
DownloadTask::Listener* listener = nullptr,
bool usePostCommand = false);
std::unique_ptr<DownloadTask> downloadToFile (const File& targetLocation,
String extraHeaders = String(),
DownloadTask::Listener* listener = nullptr,
bool usePostCommand = false);
//==============================================================================
/** Tries to download the entire contents of this URL into a binary data block.


Loading…
Cancel
Save