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 #include "content/browser/download/save_package.h" | 5 #include "content/browser/download/save_package.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 } | 98 } |
99 | 99 |
100 // Check whether we can save page as complete-HTML for the contents which | 100 // Check whether we can save page as complete-HTML for the contents which |
101 // have specified a MIME type. Now only contents which have the MIME type | 101 // have specified a MIME type. Now only contents which have the MIME type |
102 // "text/html" can be saved as complete-HTML. | 102 // "text/html" can be saved as complete-HTML. |
103 bool CanSaveAsComplete(const std::string& contents_mime_type) { | 103 bool CanSaveAsComplete(const std::string& contents_mime_type) { |
104 return contents_mime_type == "text/html" || | 104 return contents_mime_type == "text/html" || |
105 contents_mime_type == "application/xhtml+xml"; | 105 contents_mime_type == "application/xhtml+xml"; |
106 } | 106 } |
107 | 107 |
| 108 // Request handle for SavePackage downloads. Currently doesn't support |
| 109 // pause/resume/cancel, but returns a WebContents. |
| 110 class SavePackageRequestHandle : public DownloadRequestHandleInterface { |
| 111 public: |
| 112 SavePackageRequestHandle(base::WeakPtr<SavePackage> save_package) |
| 113 : save_package_(save_package) {} |
| 114 |
| 115 // DownloadRequestHandleInterface |
| 116 virtual WebContents* GetWebContents() const OVERRIDE { |
| 117 return save_package_.get() ? save_package_->web_contents() : NULL; |
| 118 } |
| 119 virtual DownloadManager* GetDownloadManager() const OVERRIDE { |
| 120 return NULL; |
| 121 } |
| 122 virtual void PauseRequest() const OVERRIDE {} |
| 123 virtual void ResumeRequest() const OVERRIDE {} |
| 124 virtual void CancelRequest() const OVERRIDE {} |
| 125 virtual std::string DebugString() const OVERRIDE { |
| 126 return "SavePackage DownloadRequestHandle"; |
| 127 } |
| 128 |
| 129 private: |
| 130 base::WeakPtr<SavePackage> save_package_; |
| 131 }; |
| 132 |
108 } // namespace | 133 } // namespace |
109 | 134 |
110 const base::FilePath::CharType SavePackage::kDefaultHtmlExtension[] = | 135 const base::FilePath::CharType SavePackage::kDefaultHtmlExtension[] = |
111 #if defined(OS_WIN) | 136 #if defined(OS_WIN) |
112 FILE_PATH_LITERAL("htm"); | 137 FILE_PATH_LITERAL("htm"); |
113 #else | 138 #else |
114 FILE_PATH_LITERAL("html"); | 139 FILE_PATH_LITERAL("html"); |
115 #endif | 140 #endif |
116 | 141 |
117 SavePackage::SavePackage(WebContents* web_contents, | 142 SavePackage::SavePackage(WebContents* web_contents, |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
278 | 303 |
279 wait_state_ = START_PROCESS; | 304 wait_state_ = START_PROCESS; |
280 | 305 |
281 // Initialize the request context and resource dispatcher. | 306 // Initialize the request context and resource dispatcher. |
282 BrowserContext* browser_context = web_contents()->GetBrowserContext(); | 307 BrowserContext* browser_context = web_contents()->GetBrowserContext(); |
283 if (!browser_context) { | 308 if (!browser_context) { |
284 NOTREACHED(); | 309 NOTREACHED(); |
285 return false; | 310 return false; |
286 } | 311 } |
287 | 312 |
| 313 scoped_ptr<DownloadRequestHandleInterface> request_handle( |
| 314 new SavePackageRequestHandle(AsWeakPtr())); |
288 // The download manager keeps ownership but adds us as an observer. | 315 // The download manager keeps ownership but adds us as an observer. |
289 download_ = download_manager_->CreateSavePackageDownloadItem( | 316 download_ = download_manager_->CreateSavePackageDownloadItem( |
290 saved_main_file_path_, | 317 saved_main_file_path_, |
291 page_url_, | 318 page_url_, |
292 ((save_type_ == SAVE_PAGE_TYPE_AS_MHTML) ? | 319 ((save_type_ == SAVE_PAGE_TYPE_AS_MHTML) ? |
293 "multipart/related" : "text/html"), | 320 "multipart/related" : "text/html"), |
| 321 request_handle.Pass(), |
294 this); | 322 this); |
295 // Confirm above didn't delete the tab out from under us. | 323 // Confirm above didn't delete the tab out from under us. |
296 if (!download_created_callback.is_null()) | 324 if (!download_created_callback.is_null()) |
297 download_created_callback.Run(download_); | 325 download_created_callback.Run(download_); |
298 | 326 |
299 // Check save type and process the save page job. | 327 // Check save type and process the save page job. |
300 if (save_type_ == SAVE_PAGE_TYPE_AS_COMPLETE_HTML) { | 328 if (save_type_ == SAVE_PAGE_TYPE_AS_COMPLETE_HTML) { |
301 // Get directory | 329 // Get directory |
302 DCHECK(!saved_main_directory_path_.empty()); | 330 DCHECK(!saved_main_directory_path_.empty()); |
303 GetAllSavableResourceLinksForCurrentPage(); | 331 GetAllSavableResourceLinksForCurrentPage(); |
(...skipping 1095 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1399 | 1427 |
1400 void SavePackage::FinalizeDownloadEntry() { | 1428 void SavePackage::FinalizeDownloadEntry() { |
1401 DCHECK(download_); | 1429 DCHECK(download_); |
1402 DCHECK(download_manager_); | 1430 DCHECK(download_manager_); |
1403 | 1431 |
1404 download_manager_->OnSavePackageSuccessfullyFinished(download_); | 1432 download_manager_->OnSavePackageSuccessfullyFinished(download_); |
1405 StopObservation(); | 1433 StopObservation(); |
1406 } | 1434 } |
1407 | 1435 |
1408 } // namespace content | 1436 } // namespace content |
OLD | NEW |