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)#ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h"
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/callback_forward.h"
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/files/file_path.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/weak_ptr.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/observer_list.h"
16eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/time/time.h"
17eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/timer/timer.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/browser/download/download_net_log_parameters.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/browser/download/download_request_handle.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/common/content_export.h"
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/download_destination_observer.h"
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "content/public/browser/download_interrupt_reasons.h"
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/download_item.h"
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_log.h"
257dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "url/gurl.h"
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace content {
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class DownloadFile;
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class DownloadItemImplDelegate;
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// See download_item.h for usage.
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class CONTENT_EXPORT DownloadItemImpl
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : public DownloadItem,
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      public DownloadDestinationObserver {
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  enum ResumeMode {
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    RESUME_MODE_INVALID = 0,
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    RESUME_MODE_IMMEDIATE_CONTINUE,
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    RESUME_MODE_IMMEDIATE_RESTART,
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    RESUME_MODE_USER_CONTINUE,
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    RESUME_MODE_USER_RESTART
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  };
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The maximum number of attempts we will make to resume automatically.
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static const int kMaxAutoResumeAttempts;
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Note that it is the responsibility of the caller to ensure that a
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // DownloadItemImplDelegate passed to a DownloadItemImpl constructor
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // outlives the DownloadItemImpl.
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Constructing from persistent store:
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |bound_net_log| is constructed externally for our use.
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DownloadItemImpl(DownloadItemImplDelegate* delegate,
547dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                   uint32 id,
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   const base::FilePath& current_path,
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   const base::FilePath& target_path,
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   const std::vector<GURL>& url_chain,
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   const GURL& referrer_url,
59f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                   const std::string& mime_type,
60f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                   const std::string& original_mime_type,
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   const base::Time& start_time,
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   const base::Time& end_time,
63ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                   const std::string& etag,
64ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                   const std::string& last_modified,
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   int64 received_bytes,
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   int64 total_bytes,
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   DownloadItem::DownloadState state,
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   DownloadDangerType danger_type,
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   DownloadInterruptReason interrupt_reason,
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   bool opened,
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   const net::BoundNetLog& bound_net_log);
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Constructing for a regular download.
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |bound_net_log| is constructed externally for our use.
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DownloadItemImpl(DownloadItemImplDelegate* delegate,
767dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                   uint32 id,
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   const DownloadCreateInfo& info,
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   const net::BoundNetLog& bound_net_log);
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Constructing for the "Save Page As..." feature:
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |bound_net_log| is constructed externally for our use.
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DownloadItemImpl(DownloadItemImplDelegate* delegate,
837dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                   uint32 id,
842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   const base::FilePath& path,
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   const GURL& url,
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   const std::string& mime_type,
872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   scoped_ptr<DownloadRequestHandleInterface> request_handle,
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   const net::BoundNetLog& bound_net_log);
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~DownloadItemImpl();
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // DownloadItem
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void AddObserver(DownloadItem::Observer* observer) OVERRIDE;
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void RemoveObserver(DownloadItem::Observer* observer) OVERRIDE;
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void UpdateObservers() OVERRIDE;
96868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  virtual void ValidateDangerousDownload() OVERRIDE;
97868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  virtual void StealDangerousDownload(const AcquireFileCallback& callback)
98868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      OVERRIDE;
992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void Pause() OVERRIDE;
1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void Resume() OVERRIDE;
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void Cancel(bool user_cancel) OVERRIDE;
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void Remove() OVERRIDE;
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void OpenDownload() OVERRIDE;
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void ShowDownloadInShell() OVERRIDE;
1057dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  virtual uint32 GetId() const OVERRIDE;
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual DownloadState GetState() const OVERRIDE;
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual DownloadInterruptReason GetLastReason() const OVERRIDE;
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool IsPaused() const OVERRIDE;
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool IsTemporary() const OVERRIDE;
11090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual bool CanResume() const OVERRIDE;
111868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  virtual bool IsDone() const OVERRIDE;
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual const GURL& GetURL() const OVERRIDE;
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual const std::vector<GURL>& GetUrlChain() const OVERRIDE;
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual const GURL& GetOriginalUrl() const OVERRIDE;
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual const GURL& GetReferrerUrl() const OVERRIDE;
116effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  virtual const GURL& GetTabUrl() const OVERRIDE;
117effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  virtual const GURL& GetTabReferrerUrl() const OVERRIDE;
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual std::string GetSuggestedFilename() const OVERRIDE;
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual std::string GetContentDisposition() const OVERRIDE;
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual std::string GetMimeType() const OVERRIDE;
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual std::string GetOriginalMimeType() const OVERRIDE;
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual std::string GetRemoteAddress() const OVERRIDE;
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool HasUserGesture() const OVERRIDE;
1241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  virtual ui::PageTransition GetTransitionType() const OVERRIDE;
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual const std::string& GetLastModifiedTime() const OVERRIDE;
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual const std::string& GetETag() const OVERRIDE;
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool IsSavePackageDownload() const OVERRIDE;
1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual const base::FilePath& GetFullPath() const OVERRIDE;
1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual const base::FilePath& GetTargetFilePath() const OVERRIDE;
1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual const base::FilePath& GetForcedFilePath() const OVERRIDE;
1312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual base::FilePath GetFileNameToReportUser() const OVERRIDE;
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual TargetDisposition GetTargetDisposition() const OVERRIDE;
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual const std::string& GetHash() const OVERRIDE;
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual const std::string& GetHashState() const OVERRIDE;
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetFileExternallyRemoved() const OVERRIDE;
1365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void DeleteFile(const base::Callback<void(bool)>& callback) OVERRIDE;
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool IsDangerous() const OVERRIDE;
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual DownloadDangerType GetDangerType() const OVERRIDE;
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool TimeRemaining(base::TimeDelta* remaining) const OVERRIDE;
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int64 CurrentSpeed() const OVERRIDE;
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int PercentComplete() const OVERRIDE;
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool AllDataSaved() const OVERRIDE;
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int64 GetTotalBytes() const OVERRIDE;
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int64 GetReceivedBytes() const OVERRIDE;
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual base::Time GetStartTime() const OVERRIDE;
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual base::Time GetEndTime() const OVERRIDE;
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool CanShowInFolder() OVERRIDE;
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool CanOpenDownload() OVERRIDE;
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool ShouldOpenFileBasedOnExtension() OVERRIDE;
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetOpenWhenComplete() const OVERRIDE;
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetAutoOpened() OVERRIDE;
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetOpened() const OVERRIDE;
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual BrowserContext* GetBrowserContext() const OVERRIDE;
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual WebContents* GetWebContents() const OVERRIDE;
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void OnContentCheckCompleted(DownloadDangerType danger_type) OVERRIDE;
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void SetOpenWhenComplete(bool open) OVERRIDE;
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void SetIsTemporary(bool temporary) OVERRIDE;
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void SetOpened(bool opened) OVERRIDE;
1592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void SetDisplayName(const base::FilePath& name) OVERRIDE;
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual std::string DebugString(bool verbose) const OVERRIDE;
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // All remaining public interfaces virtual to allow for DownloadItemImpl
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // mocks.
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
165a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // Determines the resume mode for an interrupted download. Requires
166a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // last_reason_ to be set, but doesn't require the download to be in
167a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // INTERRUPTED state.
1682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual ResumeMode GetResumeMode() const;
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Notify the download item that new origin information is available due to a
1715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // resumption request receiving a response.
1725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void MergeOriginInfoOnResume(
1735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const DownloadCreateInfo& new_create_info);
1745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // State transition operations on regular downloads --------------------------
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Start the download.
1782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // |download_file| is the associated file on the storage medium.
1792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // |req_handle| is the new request handle associated with the download.
1802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void Start(scoped_ptr<DownloadFile> download_file,
1812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                     scoped_ptr<DownloadRequestHandleInterface> req_handle);
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Needed because of intertwining with DownloadManagerImpl -------------------
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO(rdsmith): Unwind DownloadManagerImpl and DownloadItemImpl,
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // removing these from the public interface.
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Notify observers that this item is being removed by the user.
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void NotifyRemoved();
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void OnDownloadedFileRemoved();
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Provide a weak pointer reference to a DownloadDestinationObserver
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // for use by download destinations.
1952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual base::WeakPtr<DownloadDestinationObserver>
1962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      DestinationObserverAsWeakPtr();
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Get the download's BoundNetLog.
1992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual const net::BoundNetLog& GetBoundNetLog() const;
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // DownloadItemImpl routines only needed by SavePackage ----------------------
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Called by SavePackage to set the total number of bytes on the item.
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void SetTotalBytes(int64 total_bytes);
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void OnAllDataSaved(const std::string& final_hash);
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Called by SavePackage to display progress when the DownloadItem
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // should be considered complete.
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void MarkAsComplete();
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
21290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // DownloadDestinationObserver
21390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual void DestinationUpdate(int64 bytes_so_far,
21490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                 int64 bytes_per_sec,
21590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                 const std::string& hash_state) OVERRIDE;
21690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual void DestinationError(DownloadInterruptReason reason) OVERRIDE;
21790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual void DestinationCompleted(const std::string& final_hash) OVERRIDE;
21890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
22090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Fine grained states of a download. Note that active downloads are created
22190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // in IN_PROGRESS_INTERNAL state. However, downloads creates via history can
22290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // be created in COMPLETE_INTERNAL, CANCELLED_INTERNAL and
22390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // INTERRUPTED_INTERNAL.
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
22590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  enum DownloadInternalState {
22690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // Includes both before and after file name determination, and paused
22790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // downloads.
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // TODO(rdsmith): Put in state variable for file name determination.
22990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // Transitions from:
23090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    //   <Initial creation>    Active downloads are created in this state.
23190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    //   RESUMING_INTERNAL
23290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // Transitions to:
23390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    //   COMPLETING_INTERNAL   On final rename completion.
23490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    //   CANCELLED_INTERNAL    On cancel.
23590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    //   INTERRUPTED_INTERNAL  On interrupt.
23690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    //   COMPLETE_INTERNAL     On SavePackage download completion.
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    IN_PROGRESS_INTERNAL,
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Between commit point (dispatch of download file release) and completed.
24090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // Embedder may be opening the file in this state.
24190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // Transitions from:
24290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    //   IN_PROGRESS_INTERNAL
24390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // Transitions to:
24490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    //   COMPLETE_INTERNAL     On successful completion.
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    COMPLETING_INTERNAL,
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // After embedder has had a chance to auto-open.  User may now open
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // or auto-open based on extension.
24990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // Transitions from:
25090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    //   COMPLETING_INTERNAL
25190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    //   IN_PROGRESS_INTERNAL  SavePackage only.
25290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    //   <Initial creation>    Completed persisted downloads.
25390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // Transitions to:
25490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    //   <none>                Terminal state.
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    COMPLETE_INTERNAL,
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // User has cancelled the download.
25890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // Transitions from:
25990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    //   IN_PROGRESS_INTERNAL
26090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    //   INTERRUPTED_INTERNAL
26190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    //   RESUMING_INTERNAL
26290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    //   <Initial creation>    Canceleld persisted downloads.
26390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // Transitions to:
26490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    //   <none>                Terminal state.
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CANCELLED_INTERNAL,
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // An error has interrupted the download.
26890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // Transitions from:
26990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    //   IN_PROGRESS_INTERNAL
27090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    //   RESUMING_INTERNAL
27190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    //   <Initial creation>    Interrupted persisted downloads.
27290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // Transitions to:
27390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    //   RESUMING_INTERNAL     On resumption.
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    INTERRUPTED_INTERNAL,
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
27690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // A request to resume this interrupted download is in progress.
27790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // Transitions from:
27890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    //   INTERRUPTED_INTERNAL
27990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // Transitions to:
28090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    //   IN_PROGRESS_INTERNAL  Once a server response is received from a
28190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    //                         resumption.
28290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    //   INTERRUPTED_INTERNAL  If the resumption request fails.
28390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    //   CANCELLED_INTERNAL    On cancel.
28490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    RESUMING_INTERNAL,
28590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    MAX_DOWNLOAD_INTERNAL_STATE,
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
289eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // Used with TransitionTo() to indicate whether or not to call
290eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // UpdateObservers() after the state transition.
291eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  enum ShouldUpdateObservers {
292eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    UPDATE_OBSERVERS,
293eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    DONT_UPDATE_OBSERVERS
294eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  };
295eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Normal progression of a download ------------------------------------------
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // These are listed in approximately chronological order.  There are also
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // public methods involved in normal download progression; see
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the implementation ordering in download_item_impl.cc.
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Construction common to all constructors. |active| should be true for new
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // downloads and false for downloads from the history.
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |download_type| indicates to the net log system what kind of download
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // this is.
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Init(bool active, DownloadType download_type);
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Called when the target path has been determined. |target_path| is the
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // suggested target path. |disposition| indicates how the target path should
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // be used (see TargetDisposition). |danger_type| is the danger level of
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |target_path| as determined by the caller. |intermediate_path| is the path
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // to use to store the download until OnDownloadCompleting() is called.
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void OnDownloadTargetDetermined(
3142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const base::FilePath& target_path,
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      TargetDisposition disposition,
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      DownloadDangerType danger_type,
3172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const base::FilePath& intermediate_path);
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Callback from file thread when we initialize the DownloadFile.
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void OnDownloadFileInitialized(DownloadInterruptReason result);
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void OnDownloadRenamedToIntermediateName(
3232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      DownloadInterruptReason reason, const base::FilePath& full_path);
3242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If all pre-requisites have been met, complete download processing, i.e. do
3262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // internal cleanup, file rename, and potentially auto-open.  (Dangerous
3272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // downloads still may block on user acceptance after this point.)
3282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void MaybeCompleteDownload();
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Called when the download is ready to complete.
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This may perform final rename if necessary and will eventually call
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // DownloadItem::Completed().
3332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void OnDownloadCompleting();
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void OnDownloadRenamedToFinalName(DownloadInterruptReason reason,
3362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                    const base::FilePath& full_path);
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Called if the embedder took over opening a download, to indicate that
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the download has been opened.
3402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void DelayedDownloadOpened(bool auto_opened);
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Called when the entire download operation (including renaming etc)
3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // is completed.
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Completed();
3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
34690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Callback invoked when the URLRequest for a download resumption has started.
3475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void OnResumeRequestStarted(DownloadItem* item,
3485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                              DownloadInterruptReason interrupt_reason);
34990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Helper routines -----------------------------------------------------------
3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Indicate that an error has occurred on the download.
3532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void Interrupt(DownloadInterruptReason reason);
3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Destroy the DownloadFile object.  If |destroy_file| is true, the file is
356a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // destroyed with it.  Otherwise, DownloadFile::Detach() is called before
357a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // object destruction to prevent file destruction. Destroying the file also
358a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // resets |current_path_|.
3592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void ReleaseDownloadFile(bool destroy_file);
3605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Check if a download is ready for completion.  The callback provided
3622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // may be called at some point in the future if an external entity
3632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // state has change s.t. this routine should be checked again.
3642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool IsDownloadReadyForCompletion(const base::Closure& state_change_notify);
3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Call to transition state; all state transitions should go through this.
367eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // |notify_action| specifies whether or not to call UpdateObservers() after
368eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // the state transition.
369eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  void TransitionTo(DownloadInternalState new_state,
370eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                    ShouldUpdateObservers notify_action);
3715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Set the |danger_type_| and invoke obserers if necessary.
3735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetDangerType(DownloadDangerType danger_type);
3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void SetFullPath(const base::FilePath& new_path);
3762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void AutoResumeIfValid();
3785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
379a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  void ResumeInterruptedDownload();
380a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
3815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static DownloadState InternalToExternalState(
3825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      DownloadInternalState internal_state);
3835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static DownloadInternalState ExternalToInternalState(
3845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      DownloadState external_state);
3855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Debugging routines --------------------------------------------------------
3875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static const char* DebugDownloadStateString(DownloadInternalState state);
3882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static const char* DebugResumeModeString(ResumeMode mode);
3895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Will be false for save package downloads retrieved from the history.
3915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO(rdsmith): Replace with a generalized enum for "download source".
3925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const bool is_save_package_download_;
3935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The handle to the request information.  Used for operations outside the
3955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // download system.
3965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<DownloadRequestHandleInterface> request_handle_;
3975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3987dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  uint32 download_id_;
3995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Display name for the download. If this is empty, then the display name is
4015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // considered to be |target_path_.BaseName()|.
4022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath display_name_;
4035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Full path to the downloaded or downloading file. This is the path to the
4055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // physical file, if one exists. The final target path is specified by
4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |target_path_|. |current_path_| can be empty if the in-progress path hasn't
4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // been determined.
4082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath current_path_;
4095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Target path of an in-progress download. We may be downloading to a
4115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // temporary or intermediate file (specified by |current_path_|.  Once the
4125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // download completes, we will rename the file to |target_path_|.
4132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath target_path_;
4145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Whether the target should be overwritten, uniquified or prompted for.
4165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TargetDisposition target_disposition_;
4175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The chain of redirects that leading up to and including the final URL.
4195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<GURL> url_chain_;
4205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The URL of the page that initiated the download.
4225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GURL referrer_url_;
4235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
424effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // The URL of the tab that initiated the download.
425effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  GURL tab_url_;
426effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
427effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // The URL of the referrer of the tab that initiated the download.
428effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  GURL tab_referrer_url_;
429effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
4305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Filename suggestion from DownloadSaveInfo. It could, among others, be the
4315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // suggested filename in 'download' attribute of an anchor. Details:
4325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // http://www.whatwg.org/specs/web-apps/current-work/#downloading-hyperlinks
4335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string suggested_filename_;
4345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If non-empty, contains an externally supplied path that should be used as
4365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the target path.
4372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath forced_file_path_;
4385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Page transition that triggerred the download.
4401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  ui::PageTransition transition_type_;
4415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Whether the download was triggered with a user gesture.
4435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool has_user_gesture_;
4445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Information from the request.
4465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Content-disposition field from the header.
4475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string content_disposition_;
4485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Mime-type from the header.  Subject to change.
4505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string mime_type_;
4515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The value of the content type header sent with the downloaded item.  It
4535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // may be different from |mime_type_|, which may be set based on heuristics
4545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // which may look at the file extension and first few bytes of the file.
4555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string original_mime_type_;
4565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The remote IP address where the download was fetched from.  Copied from
4585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // DownloadCreateInfo::remote_address.
4595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string remote_address_;
4605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Total bytes expected.
4625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int64 total_bytes_;
4635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Current received bytes.
4655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int64 received_bytes_;
4665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Current speed. Calculated by the DownloadFile.
4685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int64 bytes_per_sec_;
4695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Sha256 hash of the content.  This might be empty either because
4715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the download isn't done yet or because the hash isn't needed
4725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // (ChromeDownloadManagerDelegate::GenerateFileHash() returned false).
4735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string hash_;
4745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // A blob containing the state of the hash algorithm.  Only valid while the
4765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // download is in progress.
4775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string hash_state_;
4785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Server's time stamp for the file.
4805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string last_modified_time_;
4815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Server's ETAG for the file.
4835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string etag_;
4845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Last reason.
4865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DownloadInterruptReason last_reason_;
4875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Start time for recording statistics.
4895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::TimeTicks start_tick_;
4905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The current state of this download.
4925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DownloadInternalState state_;
4935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Current danger type for the download.
4955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DownloadDangerType danger_type_;
4965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The views of this item in the download shelf and download contents.
4985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ObserverList<Observer> observers_;
4995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Time the download was started.
5015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::Time start_time_;
5025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Time the download completed.
5045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::Time end_time_;
5055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Our delegate.
5075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DownloadItemImplDelegate* delegate_;
5085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // In progress downloads may be paused by the user, we note it here.
5105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool is_paused_;
5115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The number of times this download has been resumed automatically.
5132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  int auto_resume_count_;
5142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // A flag for indicating if the download should be opened at completion.
5165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool open_when_complete_;
5175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // A flag for indicating if the downloaded file is externally removed.
5195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool file_externally_removed_;
5205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // True if the download was auto-opened. We set this rather than using
5225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // an observer as it's frequently possible for the download to be auto opened
5235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // before the observer is added.
5245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool auto_opened_;
5255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // True if the item was downloaded temporarily.
5275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool is_temporary_;
5285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // True if we've saved all the data for the download.
5305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool all_data_saved_;
5315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Error return from DestinationError.  Stored separately from
5332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // last_reason_ so that we can avoid handling destination errors until
5342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // after file name determination has occurred.
5352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DownloadInterruptReason destination_error_;
5362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Did the user open the item either directly or indirectly (such as by
5385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // setting always open files of this type)? The shelf also sets this field
5395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // when the user closes the shelf before the item has been opened but should
5405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // be treated as though the user opened it.
5415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool opened_;
5425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Did the delegate delay calling Complete on this download?
5445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool delegate_delayed_complete_;
5455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // DownloadFile associated with this download.  Note that this
5475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // pointer may only be used or destroyed on the FILE thread.
5485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This pointer will be non-null only while the DownloadItem is in
5495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the IN_PROGRESS state.
5505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<DownloadFile> download_file_;
5515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Net log to use for this download.
5535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const net::BoundNetLog bound_net_log_;
5545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::WeakPtrFactory<DownloadItemImpl> weak_ptr_factory_;
5565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(DownloadItemImpl);
5585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
5595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace content
5615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_
563