~unity-api-team/storage-framework/vivid

« back to all changes in this revision

Viewing changes to include/unity/storage/client/Downloader.h

  • Committer: Bileto Bot
  • Author(s): James Henstridge
  • Date: 2016-08-04 07:19:51 UTC
  • mfrom: (8.25.12 merge-devel)
  • Revision ID: ci-train-bot@canonical.com-20160804071951-3mfrcheyjvif6o99
Initial release of storage framework.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#pragma once
2
 
 
3
 
#include <memory>
4
 
 
5
 
namespace unity
6
 
{
7
 
 
8
 
namespace storage
9
 
{
10
 
 
11
 
namespace client
12
 
{
13
 
 
14
 
class DownloadResult;
15
 
 
16
 
class Downloader
17
 
{
18
 
public:
19
 
    /**
20
 
    \brief Destroys the downloader.
21
 
 
22
 
    The destructor implicitly calls close() if it has not been called already.
23
 
    */
24
 
    ~Downloader();
25
 
 
26
 
    Downloader(Downloader const&) = delete;
27
 
    Downloader& operator=(Downloader const&) = delete;
28
 
    Downloader(Downloader&&);
29
 
    Downloader& operator=(Downloader&&);
30
 
 
31
 
    typedef std::unique_ptr<Downloader> UPtr;
32
 
 
33
 
    /**
34
 
    \brief Returns a file descriptor that is open for reading.
35
 
 
36
 
    To download the file contents, read from the returned file descriptor. The descriptor
37
 
    may not be able to deliver arbitrarily large amounts of data without blocking. If the
38
 
    code that reads from the descriptor cannot block, use non-blocking reads. Once the
39
 
    descriptor indicates EOF, call close() to check for errors.
40
 
 
41
 
    \return A file descriptor open for reading. If fd() is called after close() or cancel()
42
 
    have been called, the return value is `-1`.
43
 
    \throws TODO: All sorts of exceptions when things go wrong, list them.
44
 
    */
45
 
    int fd() const;
46
 
 
47
 
    /**
48
 
    \brief Finalizes the download.
49
 
 
50
 
    Once the descriptor returned by fd() indicates EOF, you must call close(). The runtime
51
 
    closes the descriptor and calls close_callback once the state of the download has been verified.
52
 
    Call DownloadResult::check_error() from the callback to verify whether the download completed successfully.
53
 
 
54
 
    If a read on the descriptor returns an error, this indicates a fatal error condition, and further
55
 
    reads on the descriptor will fail as well. In this case, a call to close() results in close_callback()
56
 
    being invoked as usual, and DownloadResult::check_error() will return the error details
57
 
    via an exception.
58
 
 
59
 
    \param close_callback The functor called by the runtime once the download is finalized.
60
 
    The passed value must not be `nullptr`.
61
 
    \warning Do not assume that a download completed successfully once you detect EOF on the file descriptor.
62
 
    If something goes wrong during a download on the server side, the file descriptor will return EOF
63
 
    for a partially-downloaded file.
64
 
    */
65
 
    void close(std::function<void(DownloadResult const&)> close_callback);
66
 
 
67
 
    /**
68
 
    \brief Cancels a download.
69
 
 
70
 
    Calling cancel() informs the provider that the download is no longer needed. The provider
71
 
    will make a best-effort attempt to cancel the download from the remote service.
72
 
 
73
 
    Calling cancel() more than once, or calling cancel() after a call to close() is safe and does nothing.
74
 
    \throws TODO: All sorts of things.
75
 
    */
76
 
    void cancel();
77
 
 
78
 
private:
79
 
    Downloader();
80
 
};
81
 
 
82
 
}  // namespace client
83
 
 
84
 
}  // namespace storage
85
 
 
86
 
}  // namespace unity