Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // | 4 // |
| 5 // The DownloadManager object manages the process of downloading, including | 5 // The DownloadManager object manages the process of downloading, including |
| 6 // updates to the history system and providing the information for displaying | 6 // updates to the history system and providing the information for displaying |
| 7 // the downloads view in the Destinations tab. There is one DownloadManager per | 7 // the downloads view in the Destinations tab. There is one DownloadManager per |
| 8 // active browser context in Chrome. | 8 // active browser context in Chrome. |
| 9 // | 9 // |
| 10 // Download observers: | 10 // Download observers: |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 28 #define CONTENT_PUBLIC_BROWSER_DOWNLOAD_MANAGER_H_ | 28 #define CONTENT_PUBLIC_BROWSER_DOWNLOAD_MANAGER_H_ |
| 29 #pragma once | 29 #pragma once |
| 30 | 30 |
| 31 #include <string> | 31 #include <string> |
| 32 #include <vector> | 32 #include <vector> |
| 33 | 33 |
| 34 #include "base/basictypes.h" | 34 #include "base/basictypes.h" |
| 35 #include "base/callback.h" | 35 #include "base/callback.h" |
| 36 #include "base/file_path.h" | 36 #include "base/file_path.h" |
| 37 #include "base/gtest_prod_util.h" | 37 #include "base/gtest_prod_util.h" |
| 38 #include "base/memory/scoped_ptr.h" | |
| 38 #include "base/message_loop_helpers.h" | 39 #include "base/message_loop_helpers.h" |
| 39 #include "base/time.h" | 40 #include "base/time.h" |
| 41 #include "base/values.h" | |
| 42 #include "content/public/browser/browser_thread.h" | |
| 40 #include "content/public/browser/download_id.h" | 43 #include "content/public/browser/download_id.h" |
| 41 #include "content/public/browser/download_interrupt_reasons.h" | 44 #include "content/public/browser/download_interrupt_reasons.h" |
| 42 #include "content/public/browser/download_item.h" | 45 #include "content/public/browser/download_item.h" |
| 43 #include "content/public/browser/browser_thread.h" | 46 #include "content/public/browser/download_save_info.h" |
| 47 #include "googleurl/src/gurl.h" | |
| 48 #include "net/base/net_errors.h" | |
| 44 #include "net/base/net_log.h" | 49 #include "net/base/net_log.h" |
| 45 #include "net/base/net_errors.h" | |
| 46 | 50 |
| 47 class DownloadRequestHandle; | 51 class DownloadRequestHandle; |
| 48 class GURL; | |
| 49 struct DownloadCreateInfo; | 52 struct DownloadCreateInfo; |
| 50 struct DownloadRetrieveInfo; | 53 struct DownloadRetrieveInfo; |
| 51 | 54 |
| 52 namespace content { | 55 namespace content { |
| 53 class BrowserContext; | 56 class BrowserContext; |
| 54 class DownloadManagerDelegate; | 57 class DownloadManagerDelegate; |
| 55 class DownloadQuery; | 58 class DownloadQuery; |
| 56 class WebContents; | 59 class ResourceContext; |
| 57 struct DownloadSaveInfo; | 60 class ResourceDispatcherHostImpl; |
| 61 | |
| 62 // Pass an instance of DownloadUrlParameters to DownloadManager::DownloadUrl in | |
| 63 // order to download the content at |url|. |referrer| and |referrer_encoding| | |
| 64 // are the referrer for the download, and may be empty. If |prefer_cache| is | |
| 65 // true, then if the response to |url| is in the HTTP cache it will be used | |
| 66 // without revalidation. If |post_id| is non-negative, then it identifies the | |
| 67 // post transaction used to originally retrieve the |url| resource - it also | |
| 68 // requires |prefer_cache| to be |true| since re-post'ing is not done. | |
| 69 // |save_info| specifies where the downloaded file should be saved, and whether | |
| 70 // the user should be prompted about the download. |web_contents| is the web | |
| 71 // page that the download is done in context of, and must be non-NULL. | |
| 72 // |callback| will be called when the download starts, or if an error occurs | |
| 73 // that prevents a download item from being created. We send a pointer to | |
| 74 // content::ResourceContext instead of the usual reference so that a copy of | |
| 75 // the object isn't made. | |
| 76 class CONTENT_EXPORT DownloadUrlParameters { | |
|
jam
2012/04/26 20:55:16
please put this in a separate file. in general for
benjhayden
2012/04/27 14:17:08
Some of the members are non-POD, and one (extra_he
| |
| 77 public: | |
| 78 // NOTE: If there is an error, the DownloadId will be invalid. | |
| 79 typedef base::Callback<void(DownloadId, net::Error)> OnStartedCallback; | |
| 80 | |
| 81 static DownloadUrlParameters* FromWebContents( | |
| 82 content::WebContents* web_contents, | |
| 83 const GURL& url, | |
| 84 const content::DownloadSaveInfo& save_info); | |
| 85 | |
| 86 DownloadUrlParameters( | |
| 87 const GURL& url, | |
| 88 int render_process_host_id, | |
| 89 int render_view_host_routing_id, | |
| 90 content::ResourceContext* resource_context, | |
| 91 const content::DownloadSaveInfo& save_info); | |
| 92 | |
| 93 ~DownloadUrlParameters(); | |
| 94 | |
| 95 void set_extra_headers(scoped_ptr<base::ListValue> extra_headers) { | |
| 96 extra_headers_ = extra_headers.Pass(); | |
| 97 } | |
| 98 void set_referrer(const GURL& referrer) { referrer_ = referrer; } | |
| 99 void set_referrer_encoding(const std::string& referrer_encoding) { | |
| 100 referrer_encoding_ = referrer_encoding; | |
| 101 } | |
| 102 void set_load_flags(int load_flags) { load_flags_ |= load_flags; } | |
| 103 void set_method(const std::string& method) { method_ = method; } | |
| 104 void set_post_body(const std::string& post_body) { | |
| 105 post_body_ = post_body; | |
| 106 } | |
| 107 void set_prefer_cache(bool prefer_cache) { prefer_cache_ = prefer_cache; } | |
| 108 void set_post_id(int64 post_id) { | |
| 109 post_id_ = post_id; | |
| 110 set_method("POST"); | |
| 111 set_prefer_cache(true); | |
| 112 } | |
| 113 void set_callback(const OnStartedCallback& callback) { | |
| 114 callback_ = callback; | |
| 115 } | |
| 116 | |
| 117 const OnStartedCallback& callback() const { return callback_; } | |
| 118 base::ListValue* extra_headers() const { return extra_headers_.get(); } | |
| 119 int load_flags() const { return load_flags_; } | |
| 120 const std::string& method() const { return method_; } | |
| 121 const std::string& post_body() const { return post_body_; } | |
| 122 int64 post_id() const { return post_id_; } | |
| 123 bool prefer_cache() const { return prefer_cache_; } | |
| 124 const GURL& referrer() const { return referrer_; } | |
| 125 const std::string& referrer_encoding() const { return referrer_encoding_; } | |
| 126 int render_process_host_id() const { return render_process_host_id_; } | |
| 127 int render_view_host_routing_id() const { | |
| 128 return render_view_host_routing_id_; | |
| 129 } | |
| 130 content::ResourceContext* resource_context() const { | |
| 131 return resource_context_; | |
| 132 } | |
| 133 ResourceDispatcherHostImpl* resource_dispatcher_host() const { | |
| 134 return resource_dispatcher_host_; | |
| 135 } | |
| 136 const content::DownloadSaveInfo& save_info() const { return save_info_; } | |
| 137 const GURL& url() const { return url_; } | |
| 138 | |
| 139 private: | |
| 140 OnStartedCallback callback_; | |
| 141 scoped_ptr<base::ListValue> extra_headers_; | |
| 142 int load_flags_; | |
| 143 std::string method_; | |
| 144 std::string post_body_; | |
| 145 int64 post_id_; | |
| 146 bool prefer_cache_; | |
| 147 GURL referrer_; | |
| 148 std::string referrer_encoding_; | |
| 149 int render_process_host_id_; | |
| 150 int render_view_host_routing_id_; | |
| 151 ResourceContext* resource_context_; | |
| 152 ResourceDispatcherHostImpl* resource_dispatcher_host_; | |
| 153 DownloadSaveInfo save_info_; | |
| 154 GURL url_; | |
| 155 | |
| 156 DISALLOW_COPY_AND_ASSIGN(DownloadUrlParameters); | |
| 157 }; | |
| 158 | |
| 58 | 159 |
| 59 // Browser's download manager: manages all downloads and destination view. | 160 // Browser's download manager: manages all downloads and destination view. |
| 60 class CONTENT_EXPORT DownloadManager | 161 class CONTENT_EXPORT DownloadManager |
| 61 : public base::RefCountedThreadSafe<DownloadManager> { | 162 : public base::RefCountedThreadSafe<DownloadManager> { |
| 62 public: | 163 public: |
| 63 // NOTE: If there is an error, the DownloadId will be invalid. | |
| 64 typedef base::Callback<void(DownloadId, net::Error)> OnStartedCallback; | |
| 65 | |
| 66 virtual ~DownloadManager() {} | 164 virtual ~DownloadManager() {} |
| 67 | 165 |
| 68 static DownloadManager* Create( | 166 static DownloadManager* Create( |
| 69 DownloadManagerDelegate* delegate, | 167 DownloadManagerDelegate* delegate, |
| 70 net::NetLog* net_log); | 168 net::NetLog* net_log); |
| 71 | 169 |
| 72 // A method that can be used in tests to ensure that all the internal download | 170 // A method that can be used in tests to ensure that all the internal download |
| 73 // classes have no pending downloads. | 171 // classes have no pending downloads. |
| 74 static bool EnsureNoPendingDownloadsForTesting(); | 172 static bool EnsureNoPendingDownloadsForTesting(); |
| 75 | 173 |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 165 | 263 |
| 166 // Remove downloads will delete all downloads that have a timestamp that is | 264 // Remove downloads will delete all downloads that have a timestamp that is |
| 167 // the same or more recent than |remove_begin|. The number of downloads | 265 // the same or more recent than |remove_begin|. The number of downloads |
| 168 // deleted is returned back to the caller. | 266 // deleted is returned back to the caller. |
| 169 virtual int RemoveDownloads(base::Time remove_begin) = 0; | 267 virtual int RemoveDownloads(base::Time remove_begin) = 0; |
| 170 | 268 |
| 171 // Remove all downloads will delete all downloads. The number of downloads | 269 // Remove all downloads will delete all downloads. The number of downloads |
| 172 // deleted is returned back to the caller. | 270 // deleted is returned back to the caller. |
| 173 virtual int RemoveAllDownloads() = 0; | 271 virtual int RemoveAllDownloads() = 0; |
| 174 | 272 |
| 175 // Downloads the content at |url|. |referrer| and |referrer_encoding| are the | 273 // Takes ownership of |parameters|. |
| 176 // referrer for the download, and may be empty. If |prefer_cache| is true, | 274 virtual void DownloadUrl(DownloadUrlParameters* parameters) = 0; |
| 177 // then if the response to |url| is in the HTTP cache it will be used without | |
| 178 // revalidation. If |post_id| is non-negative, then it identifies the post | |
| 179 // transaction used to originally retrieve the |url| resource - it also | |
| 180 // requires |prefer_cache| to be |true| since re-post'ing is not done. | |
| 181 // |save_info| specifies where the downloaded file should be | |
| 182 // saved, and whether the user should be prompted about the download. | |
| 183 // |web_contents| is the web page that the download is done in context of, | |
| 184 // and must be non-NULL. | |
| 185 // |callback| will be called when the download starts, or if an error | |
| 186 // occurs that prevents a download item from being created. | |
| 187 virtual void DownloadUrl(const GURL& url, | |
| 188 const GURL& referrer, | |
| 189 const std::string& referrer_encoding, | |
| 190 bool prefer_cache, | |
| 191 int64 post_id, | |
| 192 const DownloadSaveInfo& save_info, | |
| 193 WebContents* web_contents, | |
| 194 const OnStartedCallback& callback) = 0; | |
| 195 | 275 |
| 196 // Allow objects to observe the download creation process. | 276 // Allow objects to observe the download creation process. |
| 197 virtual void AddObserver(Observer* observer) = 0; | 277 virtual void AddObserver(Observer* observer) = 0; |
| 198 | 278 |
| 199 // Remove a download observer from ourself. | 279 // Remove a download observer from ourself. |
| 200 virtual void RemoveObserver(Observer* observer) = 0; | 280 virtual void RemoveObserver(Observer* observer) = 0; |
| 201 | 281 |
| 202 // Called by the embedder, after creating the download manager, to let it know | 282 // Called by the embedder, after creating the download manager, to let it know |
| 203 // about downloads from previous runs of the browser. | 283 // about downloads from previous runs of the browser. |
| 204 virtual void OnPersistentStoreQueryComplete( | 284 virtual void OnPersistentStoreQueryComplete( |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 270 private: | 350 private: |
| 271 friend class base::RefCountedThreadSafe< | 351 friend class base::RefCountedThreadSafe< |
| 272 DownloadManager, BrowserThread::DeleteOnUIThread>; | 352 DownloadManager, BrowserThread::DeleteOnUIThread>; |
| 273 friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>; | 353 friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>; |
| 274 friend class base::DeleteHelper<DownloadManager>; | 354 friend class base::DeleteHelper<DownloadManager>; |
| 275 }; | 355 }; |
| 276 | 356 |
| 277 } // namespace content | 357 } // namespace content |
| 278 | 358 |
| 279 #endif // CONTENT_PUBLIC_BROWSER_DOWNLOAD_MANAGER_H_ | 359 #endif // CONTENT_PUBLIC_BROWSER_DOWNLOAD_MANAGER_H_ |
| OLD | NEW |