15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Each download is represented by a DownloadItem, and all DownloadItems
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// are owned by the DownloadManager which maintains a global list of all
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// downloads. DownloadItems are created when a user initiates a download,
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// and exist for the duration of the browser life time.
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Download observers:
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   DownloadItem::Observer:
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     - allows observers to receive notifications about one download from start
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//       to completion
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use AddObserver() / RemoveObserver() on the appropriate download object to
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// receive state updates.
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef CONTENT_PUBLIC_BROWSER_DOWNLOAD_ITEM_H_
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CONTENT_PUBLIC_BROWSER_DOWNLOAD_ITEM_H_
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <map>
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
24868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/callback_forward.h"
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/files/file_path.h"
267d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/strings/string16.h"
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/supports_user_data.h"
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/download_danger_type.h"
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/download_interrupt_reasons.h"
301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "ui/base/page_transition_types.h"
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class GURL;
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace base {
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class FilePath;
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Time;
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class TimeDelta;
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace content {
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BrowserContext;
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class DownloadManager;
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class WebContents;
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// One DownloadItem per download. This is the model class that stores all the
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// state for a download. Multiple views, such as a tab's download shelf and the
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Destination tab's download view, may refer to a given DownloadItem.
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This is intended to be used only on the UI thread.
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class CONTENT_EXPORT DownloadItem : public base::SupportsUserData {
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum DownloadState {
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Download is actively progressing.
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    IN_PROGRESS = 0,
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Download is completely finished.
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    COMPLETE,
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Download has been cancelled.
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CANCELLED,
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // This state indicates that the download has been interrupted.
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    INTERRUPTED,
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Maximum value.
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    MAX_DOWNLOAD_STATE
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // How the final target path should be used.
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum TargetDisposition {
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TARGET_DISPOSITION_OVERWRITE, // Overwrite if the target already exists.
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TARGET_DISPOSITION_PROMPT     // Prompt the user for the actual
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  // target. Implies
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  // TARGET_DISPOSITION_OVERWRITE.
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
78868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Callback used with AcquireFileAndDeleteDownload().
79868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  typedef base::Callback<void(const base::FilePath&)> AcquireFileCallback;
80868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
817dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  static const uint32 kInvalidId;
827dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static const char kEmptyFileHash[];
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Interface that observers of a particular download must implement in order
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // to receive updates to the download's status.
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class CONTENT_EXPORT Observer {
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   public:
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual void OnDownloadUpdated(DownloadItem* download) {}
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual void OnDownloadOpened(DownloadItem* download) {}
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual void OnDownloadRemoved(DownloadItem* download) {}
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Called when the download is being destroyed. This happens after
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // every OnDownloadRemoved() as well as when the DownloadManager is going
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // down.
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual void OnDownloadDestroyed(DownloadItem* download) {}
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   protected:
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual ~Observer() {}
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~DownloadItem() {}
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Observation ---------------------------------------------------------------
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void AddObserver(DownloadItem::Observer* observer) = 0;
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void RemoveObserver(DownloadItem::Observer* observer) = 0;
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void UpdateObservers() = 0;
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // User Actions --------------------------------------------------------------
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Called when the user has validated the download of a dangerous file.
113868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  virtual void ValidateDangerousDownload() = 0;
114868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
115868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Called to acquire a dangerous download and remove the DownloadItem from
116868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // views and history. |callback| will be invoked on the UI thread with the
117868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // path to the downloaded file. The caller is responsible for cleanup.
118868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Note: It is important for |callback| to be valid since the downloaded file
119868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // will not be cleaned up if the callback fails.
120868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  virtual void StealDangerousDownload(const AcquireFileCallback& callback) = 0;
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Pause a download.  Will have no effect if the download is already
1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // paused.
1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void Pause() = 0;
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
126a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // Resume a download that has been paused or interrupted. Will have no effect
127a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // if the download is neither.
1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void Resume() = 0;
1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Cancel the download operation. We need to distinguish between cancels at
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // exit (DownloadManager destructor) from user interface initiated cancels
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // because at exit, the history system may not exist, and any updates to it
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // require AddRef'ing the DownloadManager in the destructor which results in
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // a DCHECK failure. Set |user_cancel| to false when canceling from at
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // exit to prevent this crash. This may result in a difference between the
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // downloaded file's size on disk, and what the history system's last record
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // of it is. At worst, we'll end up re-downloading a small portion of the file
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // when resuming a download (assuming the server supports byte ranges).
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void Cancel(bool user_cancel) = 0;
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
141868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Removes the download from the views and history. If the download was
142868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // in-progress or interrupted, then the intermediate file will also be
143868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // deleted.
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void Remove() = 0;
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Open the file associated with this download.  If the download is
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // still in progress, marks the download to be opened when it is complete.
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void OpenDownload() = 0;
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Show the download via the OS shell.
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void ShowDownloadInShell() = 0;
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // State accessors -----------------------------------------------------------
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1557dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  virtual uint32 GetId() const = 0;
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual DownloadState GetState() const = 0;
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
158868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Returns the most recent interrupt reason for this download. Returns
159868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // DOWNLOAD_INTERRUPT_REASON_NONE if there is no previous interrupt reason.
160868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Cancelled downloads return DOWNLOAD_INTERRUPT_REASON_USER_CANCELLED. If
161868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // the download was resumed, then the return value is the interrupt reason
162868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // prior to resumption.
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual DownloadInterruptReason GetLastReason() const = 0;
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool IsPaused() const = 0;
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool IsTemporary() const = 0;
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
16890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Returns true if the download can be resumed. A download can be resumed if
16990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // an in-progress download was paused or if an interrupted download requires
17090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // user-interaction to resume.
17190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual bool CanResume() const = 0;
17290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
173868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Returns true if the download is in a terminal state. This includes
174868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // completed downloads, cancelled downloads, and interrupted downloads that
175868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // can't be resumed.
176868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  virtual bool IsDone() const = 0;
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //    Origin State accessors -------------------------------------------------
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual const GURL& GetURL() const = 0;
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual const std::vector<GURL>& GetUrlChain() const = 0;
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual const GURL& GetOriginalUrl() const = 0;
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual const GURL& GetReferrerUrl() const = 0;
184effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  virtual const GURL& GetTabUrl() const = 0;
185effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  virtual const GURL& GetTabReferrerUrl() const = 0;
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual std::string GetSuggestedFilename() const = 0;
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual std::string GetContentDisposition() const = 0;
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual std::string GetMimeType() const = 0;
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual std::string GetOriginalMimeType() const = 0;
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual std::string GetRemoteAddress() const = 0;
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool HasUserGesture() const = 0;
1921320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  virtual ui::PageTransition GetTransitionType() const = 0;
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual const std::string& GetLastModifiedTime() const = 0;
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual const std::string& GetETag() const = 0;
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool IsSavePackageDownload() const = 0;
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //    Destination State accessors --------------------------------------------
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Full path to the downloaded or downloading file. This is the path to the
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // physical file, if one exists. It should be considered a hint; changes to
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // this value and renames of the file on disk are not atomic with each other.
202b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // May be empty if the in-progress path hasn't been determined yet or if the
203b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // download was interrupted.
204b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  //
205b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // DO NOT USE THIS METHOD to access the target path of the DownloadItem. Use
206b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // GetTargetFilePath() instead. While the download is in progress, the
207b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // intermediate file named by GetFullPath() may be renamed or disappear
208b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // completely on the FILE thread. The path may also be reset to empty when the
209b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // download is interrupted.
2102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual const base::FilePath& GetFullPath() const = 0;
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Target path of an in-progress download. We may be downloading to a
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // temporary or intermediate file (specified by GetFullPath()); this is the
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // name we will use once the download completes.
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // May be empty if the target path hasn't yet been determined.
2162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual const base::FilePath& GetTargetFilePath() const = 0;
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If the download forced a path rather than requesting name determination,
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // return the path requested.
2202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual const base::FilePath& GetForcedFilePath() const = 0;
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the file-name that should be reported to the user. If a display
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // name has been explicitly set using SetDisplayName(), this function returns
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // that display name. Otherwise returns the final target filename.
2252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual base::FilePath GetFileNameToReportUser() const = 0;
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual TargetDisposition GetTargetDisposition() const = 0;
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Final hash of completely downloaded file; not valid if
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // GetState() != COMPLETED.
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual const std::string& GetHash() const = 0;
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Intermediate hash state, for persisting partial downloads.
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual const std::string& GetHashState() const = 0;
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // True if the file associated with the download has been removed by
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // external action.
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetFileExternallyRemoved() const = 0;
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
240558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  // If the file is successfully deleted, then GetFileExternallyRemoved() will
2415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // become true, GetFullPath() will become empty, and
2425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // DownloadItem::OnDownloadUpdated() will be called. Does nothing if
2435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // GetState() == COMPLETE or GetFileExternallyRemoved() is already true or
2445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // GetFullPath() is already empty. The callback is always run, and it is
2455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // always run asynchronously. It will be passed true if the file is
2465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // successfully deleted or if GetFilePath() was already empty or if
2475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // GetFileExternallyRemoved() was already true. The callback will be passed
2485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // false if the DownloadItem was not yet complete or if the file could not be
2495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // deleted for any reason.
2505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void DeleteFile(const base::Callback<void(bool)>& callback) = 0;
251558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // True if the file that will be written by the download is dangerous
253868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // and we will require a call to ValidateDangerousDownload() to complete.
2542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // False if the download is safe or that function has been called.
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool IsDangerous() const = 0;
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Why |safety_state_| is not SAFE.
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual DownloadDangerType GetDangerType() const = 0;
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //    Progress State accessors -----------------------------------------------
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Simple calculation of the amount of time remaining to completion. Fills
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |*remaining| with the amount of time remaining if successful. Fails and
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // returns false if we do not have the number of bytes or the speed so can
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // not estimate.
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool TimeRemaining(base::TimeDelta* remaining) const = 0;
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Simple speed estimate in bytes/s
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int64 CurrentSpeed() const = 0;
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Rough percent complete, -1 means we don't know (== we didn't receive a
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // total size).
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int PercentComplete() const = 0;
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if this download has saved all of its data.
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool AllDataSaved() const = 0;
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int64 GetTotalBytes() const = 0;
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int64 GetReceivedBytes() const = 0;
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual base::Time GetStartTime() const = 0;
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual base::Time GetEndTime() const = 0;
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //    Open/Show State accessors ----------------------------------------------
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if it is OK to open a folder which this file is inside.
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool CanShowInFolder() = 0;
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if it is OK to open the download.
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool CanOpenDownload() = 0;
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Tests if a file type should be opened automatically.
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool ShouldOpenFileBasedOnExtension() = 0;
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if the download will be auto-opened when complete.
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetOpenWhenComplete() const = 0;
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if the download has been auto-opened by the system.
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetAutoOpened() = 0;
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if the download has been opened.
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetOpened() const = 0;
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //    Misc State accessors ---------------------------------------------------
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual BrowserContext* GetBrowserContext() const = 0;
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual WebContents* GetWebContents() const = 0;
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // External state transitions/setters ----------------------------------------
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO(rdsmith): These should all be removed; the download item should
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // control its own state transitions.
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Called if a check of the download contents was performed and the results of
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the test are available. This should only be called after AllDataSaved() is
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // true.
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void OnContentCheckCompleted(DownloadDangerType danger_type) = 0;
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Mark the download to be auto-opened when completed.
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void SetOpenWhenComplete(bool open) = 0;
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Mark the download as temporary (not visible in persisted store or
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // SearchDownloads(), removed from main UI upon completion).
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void SetIsTemporary(bool temporary) = 0;
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Mark the download as having been opened (without actually opening it).
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void SetOpened(bool opened) = 0;
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Set a display name for the download that will be independent of the target
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // filename. If |name| is not empty, then GetFileNameToReportUser() will
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // return |name|. Has no effect on the final target filename.
3302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void SetDisplayName(const base::FilePath& name) = 0;
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Debug/testing -------------------------------------------------------------
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual std::string DebugString(bool verbose) const = 0;
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace content
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // CONTENT_PUBLIC_BROWSER_DOWNLOAD_ITEM_H_
339