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)// The DownloadManager object manages the process of downloading, including 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// updates to the history system and providing the information for displaying 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the downloads view in the Destinations tab. There is one DownloadManager per 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// active browser context in Chrome. 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Download observers: 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Objects that are interested in notifications about new downloads, or progress 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// updates for a given download must implement one of the download observer 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// interfaces: 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// DownloadManager::Observer: 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// - allows observers, primarily views, to be notified when changes to the 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// set of all downloads (such as new downloads, or deletes) occur 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use AddObserver() / RemoveObserver() on the appropriate download object to 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// receive state updates. 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Download state persistence: 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The DownloadManager uses the history service for storing persistent 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// information about the state of all downloads. The history system maintains a 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// separate table for this called 'downloads'. At the point that the 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// DownloadManager is constructed, we query the history service for the state of 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// all persisted downloads. 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef CONTENT_PUBLIC_BROWSER_DOWNLOAD_MANAGER_H_ 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CONTENT_PUBLIC_BROWSER_DOWNLOAD_MANAGER_H_ 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string> 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector> 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h" 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/callback.h" 352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/files/file_path.h" 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/gtest_prod_util.h" 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/sequenced_task_runner_helpers.h" 38eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/time/time.h" 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/download_interrupt_reasons.h" 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/download_item.h" 417dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "content/public/browser/download_url_parameters.h" 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_errors.h" 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_log.h" 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class GURL; 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace content { 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BrowserContext; 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ByteStreamReader; 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class DownloadManagerDelegate; 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class DownloadQuery; 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class DownloadRequestHandle; 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct DownloadCreateInfo; 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Browser's download manager: manages all downloads and destination view. 5790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)class CONTENT_EXPORT DownloadManager : public base::SupportsUserData::Data { 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 5990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) virtual ~DownloadManager() {} 6090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Sets/Gets the delegate for this DownloadManager. The delegate has to live 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // past its Shutdown method being called (by the DownloadManager). 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void SetDelegate(DownloadManagerDelegate* delegate) = 0; 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual DownloadManagerDelegate* GetDelegate() const = 0; 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Shutdown the download manager. Content calls this when BrowserContext is 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // being destructed. If the embedder needs this to be called earlier, it can 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // call it. In that case, the delegate's Shutdown() method will only be called 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // once. 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void Shutdown() = 0; 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Interface to implement for observers that wish to be informed of changes 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // to the DownloadManager's collection of downloads. 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) class CONTENT_EXPORT Observer { 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // A DownloadItem was created. This item may be visible before the filename 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // is determined; in this case the return value of GetTargetFileName() will 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // be null. This method may be called an arbitrary number of times, e.g. 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // when loading history on startup. As a result, consumers should avoid 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // doing large amounts of work in OnDownloadCreated(). TODO(<whoever>): 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // When we've fully specified the possible states of the DownloadItem in 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // download_item.h, we should remove the caveat above. 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void OnDownloadCreated( 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DownloadManager* manager, DownloadItem* item) {} 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // A SavePackage has successfully finished. 872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual void OnSavePackageSuccessfullyFinished( 882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DownloadManager* manager, DownloadItem* item) {} 892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Called when the DownloadManager is being destroyed to prevent Observers 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // from calling back to a stale pointer. 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void ManagerGoingDown(DownloadManager* manager) {} 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected: 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual ~Observer() {} 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) }; 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) typedef std::vector<DownloadItem*> DownloadVector; 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Add all download items to |downloads|, no matter the type or state, without 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // clearing |downloads| first. 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void GetAllDownloads(DownloadVector* downloads) = 0; 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Called by a download source (Currently DownloadResourceHandler) 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // to initiate the non-source portions of a download. 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns the id assigned to the download. If the DownloadCreateInfo 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // specifies an id, that id will be used. 1087dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch virtual void StartDownload( 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_ptr<DownloadCreateInfo> info, 1107dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch scoped_ptr<ByteStreamReader> stream, 1117dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch const DownloadUrlParameters::OnStartedCallback& on_started) = 0; 1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Remove downloads after remove_begin (inclusive) and before remove_end 1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // (exclusive). You may pass in null Time values to do an unbounded delete 1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // in either direction. 1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual int RemoveDownloadsBetween(base::Time remove_begin, 1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::Time remove_end) = 0; 1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Remove downloads will delete all downloads that have a timestamp that is 1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // the same or more recent than |remove_begin|. The number of downloads 1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // deleted is returned back to the caller. 1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual int RemoveDownloads(base::Time remove_begin) = 0; 1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Remove all downloads will delete all downloads. The number of downloads 1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // deleted is returned back to the caller. 1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual int RemoveAllDownloads() = 0; 1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // See DownloadUrlParameters for details about controlling the download. 1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void DownloadUrl(scoped_ptr<DownloadUrlParameters> parameters) = 0; 1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Allow objects to observe the download creation process. 1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void AddObserver(Observer* observer) = 0; 1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Remove a download observer from ourself. 1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void RemoveObserver(Observer* observer) = 0; 1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Called by the embedder, after creating the download manager, to let it know 1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // about downloads from previous runs of the browser. 1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual DownloadItem* CreateDownloadItem( 1407dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch uint32 id, 1412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const base::FilePath& current_path, 1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const base::FilePath& target_path, 1432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::vector<GURL>& url_chain, 1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const GURL& referrer_url, 145f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) const std::string& mime_type, 146f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) const std::string& original_mime_type, 1472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const base::Time& start_time, 1482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const base::Time& end_time, 149ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch const std::string& etag, 150ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch const std::string& last_modified, 1512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) int64 received_bytes, 1522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) int64 total_bytes, 1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DownloadItem::DownloadState state, 1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DownloadDangerType danger_type, 1552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DownloadInterruptReason interrupt_reason, 1562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) bool opened) = 0; 1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The number of in progress (including paused) downloads. 1592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Performance note: this loops over all items. If profiling finds that this 1602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // is too slow, use an AllDownloadItemNotifier to count in-progress items. 1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual int InProgressCount() const = 0; 1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1638bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) // The number of in progress (including paused) downloads. 1648bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) // Performance note: this loops over all items. If profiling finds that this 1658bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) // is too slow, use an AllDownloadItemNotifier to count in-progress items. 1668bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) // This excludes downloads that are marked as malicious. 1678bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) virtual int NonMaliciousInProgressCount() const = 0; 1688bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual BrowserContext* GetBrowserContext() const = 0; 1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Checks whether downloaded files still exist. Updates state of downloads 1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // that refer to removed files. The check runs in the background and may 1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // finish asynchronously after this method returns. 1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void CheckForHistoryFilesRemoval() = 0; 1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Get the download item for |id| if present, no matter what type of download 1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // it is or state it's in. 1787dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch virtual DownloadItem* GetDownload(uint32 id) = 0; 1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace content 1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif // CONTENT_PUBLIC_BROWSER_DOWNLOAD_MANAGER_H_ 184