1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef IOS_WEB_PUBLIC_DOWNLOAD_DOWNLOAD_TASK_H_
6 #define IOS_WEB_PUBLIC_DOWNLOAD_DOWNLOAD_TASK_H_
7 
8 #import <Foundation/Foundation.h>
9 
10 #include <stdint.h>
11 #include <string>
12 
13 #include "base/macros.h"
14 #include "base/strings/string16.h"
15 #include "ui/base/page_transition_types.h"
16 
17 class GURL;
18 
19 namespace net {
20 class URLFetcherResponseWriter;
21 }  // namespace net
22 
23 namespace web {
24 
25 class DownloadTaskObserver;
26 class WebState;
27 
28 // Provides API for a single browser download task. This is the model class that
29 // stores all the state for a download. Must be used on the UI thread.
30 class DownloadTask {
31  public:
32   enum class State {
33     // Download has not started yet.
34     kNotStarted = 0,
35 
36     // Download is actively progressing.
37     kInProgress,
38 
39     // Download is cancelled.
40     kCancelled,
41 
42     // Download is completely finished.
43     kComplete,
44   };
45 
46   // Returns WebState which requested this download.
47   virtual WebState* GetWebState() = 0;
48 
49   // Returns the download task state.
50   virtual State GetState() const = 0;
51 
52   // Starts the download. |writer| allows clients to perform in-memory or
53   // in-file downloads and must not be null. Start() can only be called if
54   // DownloadTask is not in progress.
55   virtual void Start(std::unique_ptr<net::URLFetcherResponseWriter> writer) = 0;
56 
57   // Cancels the download.
58   virtual void Cancel() = 0;
59 
60   // Response writer, which was passed to Start(). Can be used to obtain the
61   // download data.
62   virtual net::URLFetcherResponseWriter* GetResponseWriter() const = 0;
63 
64   // Unique indentifier for this task. Also can be used to resume unfinished
65   // downloads after the application relaunch (see example in DownloadController
66   // class comments).
67   virtual NSString* GetIndentifier() const = 0;
68 
69   // The URL that the download request originally attempted to fetch. This may
70   // differ from the final download URL if there were redirects.
71   virtual const GURL& GetOriginalUrl() const = 0;
72 
73   // HTTP method for this download task (only @"GET" and @"POST" are currently
74   // supported).
75   virtual NSString* GetHttpMethod() const = 0;
76 
77   // Returns true if the download is in a terminal state. This includes
78   // completed downloads, cancelled downloads, and interrupted downloads that
79   // can't be resumed.
80   virtual bool IsDone() const = 0;
81 
82   // Error code for this download task. 0 if the download is still in progress
83   // or the download has sucessfully completed. See net_errors.h for the
84   // possible error codes.
85   virtual int GetErrorCode() const = 0;
86 
87   // HTTP response code for this download task. -1 the response has not been
88   // received yet or the response not an HTTP response.
89   virtual int GetHttpCode() const = 0;
90 
91   // Total number of expected bytes (a best-guess upper-bound). Returns -1 if
92   // the total size is unknown.
93   virtual int64_t GetTotalBytes() const = 0;
94 
95   // Total number of bytes that have been received.
96   virtual int64_t GetReceivedBytes() const = 0;
97 
98   // Rough percent complete. Returns -1 if progress is unknown. 100 if the
99   // download is already complete.
100   virtual int GetPercentComplete() const = 0;
101 
102   // Content-Disposition header value from HTTP response.
103   virtual std::string GetContentDisposition() const = 0;
104 
105   // MIME type that the download request originally attempted to fetch.
106   virtual std::string GetOriginalMimeType() const = 0;
107 
108   // Effective MIME type of downloaded content.
109   virtual std::string GetMimeType() const = 0;
110 
111   // Suggested name for the downloaded file.
112   virtual base::string16 GetSuggestedFilename() const = 0;
113 
114   // Returns true if the last download operation was fully or partially
115   // performed while the application was not active.
116   virtual bool HasPerformedBackgroundDownload() const = 0;
117 
118   // Adds and Removes DownloadTaskObserver. Clients must remove self from
119   // observers before the task is destroyed.
120   virtual void AddObserver(DownloadTaskObserver* observer) = 0;
121   virtual void RemoveObserver(DownloadTaskObserver* observer) = 0;
122 
123   DownloadTask() = default;
124   virtual ~DownloadTask() = default;
125 
126   DISALLOW_COPY_AND_ASSIGN(DownloadTask);
127 };
128 
129 }  // namespace web
130 
131 #endif  // IOS_WEB_PUBLIC_DOWNLOAD_DOWNLOAD_TASK_H_
132