Home | History | Annotate | Download | only in browser
      1 // Copyright 2013 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 package org.chromium.content.browser;
      6 
      7 /**
      8  * Class representing the state of a single download.
      9  */
     10 public final class DownloadInfo {
     11     private final String mUrl;
     12     private final String mUserAgent;
     13     private final String mMimeType;
     14     private final String mCookie;
     15     private final String mFileName;
     16     private final String mDescription;
     17     private final String mFilePath;
     18     private final String mReferer;
     19     private final long mContentLength;
     20     private final boolean mHasDownloadId;
     21     private final int mDownloadId;
     22     private final String mContentDisposition;
     23     private final boolean mIsGETRequest;
     24     private final boolean mIsSuccessful;
     25     private final int mPercentCompleted;
     26     private final long mTimeRemainingInMillis;
     27 
     28     private DownloadInfo(Builder builder) {
     29         mUrl = builder.mUrl;
     30         mUserAgent = builder.mUserAgent;
     31         mMimeType = builder.mMimeType;
     32         mCookie = builder.mCookie;
     33         mFileName = builder.mFileName;
     34         mDescription = builder.mDescription;
     35         mFilePath = builder.mFilePath;
     36         mReferer = builder.mReferer;
     37         mContentLength = builder.mContentLength;
     38         mHasDownloadId = builder.mHasDownloadId;
     39         mDownloadId = builder.mDownloadId;
     40         mIsSuccessful = builder.mIsSuccessful;
     41         mIsGETRequest = builder.mIsGETRequest;
     42         mContentDisposition = builder.mContentDisposition;
     43         mPercentCompleted = builder.mPercentCompleted;
     44         mTimeRemainingInMillis = builder.mTimeRemainingInMillis;
     45     }
     46 
     47     public String getUrl() {
     48         return mUrl;
     49     }
     50 
     51     public String getUserAgent() {
     52         return mUserAgent;
     53     }
     54 
     55     public String getMimeType() {
     56         return mMimeType;
     57     }
     58 
     59     public String getCookie() {
     60         return mCookie;
     61     }
     62 
     63     public String getFileName() {
     64         return mFileName;
     65     }
     66 
     67     public String getDescription() {
     68         return mDescription;
     69     }
     70 
     71     public String getFilePath() {
     72         return mFilePath;
     73     }
     74 
     75     public String getReferer() {
     76         return mReferer;
     77     }
     78 
     79     public long getContentLength() {
     80         return mContentLength;
     81     }
     82 
     83     public boolean isGETRequest() {
     84         return mIsGETRequest;
     85     }
     86 
     87     public boolean hasDownloadId() {
     88         return mHasDownloadId;
     89     }
     90 
     91     public int getDownloadId() {
     92         return mDownloadId;
     93     }
     94 
     95     public boolean isSuccessful() {
     96         return mIsSuccessful;
     97     }
     98 
     99     public String getContentDisposition() {
    100         return mContentDisposition;
    101     }
    102 
    103     /**
    104      * @return percent completed as an integer, -1 if there is no download progress.
    105      */
    106     public int getPercentCompleted() {
    107         return mPercentCompleted;
    108     }
    109 
    110     public long getTimeRemainingInMillis() {
    111         return mTimeRemainingInMillis;
    112     }
    113 
    114     public static class Builder {
    115         private String mUrl;
    116         private String mUserAgent;
    117         private String mMimeType;
    118         private String mCookie;
    119         private String mFileName;
    120         private String mDescription;
    121         private String mFilePath;
    122         private String mReferer;
    123         private long mContentLength;
    124         private boolean mIsGETRequest;
    125         private boolean mHasDownloadId;
    126         private int mDownloadId;
    127         private boolean mIsSuccessful;
    128         private String mContentDisposition;
    129         private int mPercentCompleted = -1;
    130         private long mTimeRemainingInMillis;
    131 
    132         public Builder setUrl(String url) {
    133             mUrl = url;
    134             return this;
    135         }
    136 
    137         public Builder setUserAgent(String userAgent) {
    138             mUserAgent = userAgent;
    139             return this;
    140         }
    141 
    142         public Builder setMimeType(String mimeType) {
    143             mMimeType = mimeType;
    144             return this;
    145         }
    146 
    147         public Builder setCookie(String cookie) {
    148             mCookie = cookie;
    149             return this;
    150         }
    151 
    152         public Builder setFileName(String fileName) {
    153             mFileName = fileName;
    154             return this;
    155         }
    156 
    157         public Builder setDescription(String description) {
    158             mDescription = description;
    159             return this;
    160         }
    161 
    162         public Builder setFilePath(String filePath) {
    163             mFilePath = filePath;
    164             return this;
    165         }
    166 
    167         public Builder setReferer(String referer) {
    168             mReferer = referer;
    169             return this;
    170         }
    171 
    172         public Builder setContentLength(long contentLength) {
    173             mContentLength = contentLength;
    174             return this;
    175         }
    176 
    177         public Builder setIsGETRequest(boolean isGETRequest) {
    178             mIsGETRequest = isGETRequest;
    179             return this;
    180         }
    181 
    182         public Builder setHasDownloadId(boolean hasDownloadId) {
    183             mHasDownloadId = hasDownloadId;
    184             return this;
    185         }
    186 
    187         public Builder setDownloadId(int downloadId) {
    188             mDownloadId = downloadId;
    189             return this;
    190         }
    191 
    192         public Builder setIsSuccessful(boolean isSuccessful) {
    193             mIsSuccessful = isSuccessful;
    194             return this;
    195         }
    196 
    197         public Builder setContentDisposition(String contentDisposition) {
    198             mContentDisposition = contentDisposition;
    199             return this;
    200         }
    201 
    202         public Builder setPercentCompleted(int percentCompleted) {
    203             assert percentCompleted <= 100;
    204             mPercentCompleted = percentCompleted;
    205             return this;
    206         }
    207 
    208         public Builder setTimeRemainingInMillis(long timeRemainingInMillis) {
    209             mTimeRemainingInMillis = timeRemainingInMillis;
    210             return this;
    211         }
    212 
    213         public DownloadInfo build() {
    214             return new DownloadInfo(this);
    215         }
    216 
    217         /**
    218          * Create a builder from the DownloadInfo object.
    219          * @param downloadInfo DownloadInfo object from which builder fields are populated.
    220          * @return A builder initialized with fields from downloadInfo object.
    221          */
    222         public static Builder fromDownloadInfo(final DownloadInfo downloadInfo) {
    223             Builder builder = new Builder();
    224             builder
    225                     .setUrl(downloadInfo.getUrl())
    226                     .setUserAgent(downloadInfo.getUserAgent())
    227                     .setMimeType(downloadInfo.getMimeType())
    228                     .setCookie(downloadInfo.getCookie())
    229                     .setFileName(downloadInfo.getFileName())
    230                     .setDescription(downloadInfo.getDescription())
    231                     .setFilePath(downloadInfo.getFilePath())
    232                     .setReferer(downloadInfo.getReferer())
    233                     .setContentLength(downloadInfo.getContentLength())
    234                     .setHasDownloadId(downloadInfo.hasDownloadId())
    235                     .setDownloadId(downloadInfo.getDownloadId())
    236                     .setContentDisposition(downloadInfo.getContentDisposition())
    237                     .setIsGETRequest(downloadInfo.isGETRequest())
    238                     .setIsSuccessful(downloadInfo.isSuccessful())
    239                     .setPercentCompleted(downloadInfo.getPercentCompleted())
    240                     .setTimeRemainingInMillis(downloadInfo.getTimeRemainingInMillis());
    241             return builder;
    242         }
    243 
    244     }
    245 }
    246