| 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" |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/i18n/file_util_icu.h" | 12 #include "base/i18n/file_util_icu.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
| 16 #include "base/strings/string_piece.h" | 16 #include "base/strings/string_piece.h" |
| 17 #include "base/strings/string_split.h" | 17 #include "base/strings/string_split.h" |
| 18 #include "base/strings/sys_string_conversions.h" | 18 #include "base/strings/sys_string_conversions.h" |
| 19 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
| 20 #include "base/threading/thread.h" | 20 #include "base/threading/thread.h" |
| 21 #include "content/browser/download/download_create_info.h" |
| 21 #include "content/browser/download/download_item_impl.h" | 22 #include "content/browser/download/download_item_impl.h" |
| 22 #include "content/browser/download/download_manager_impl.h" | 23 #include "content/browser/download/download_manager_impl.h" |
| 23 #include "content/browser/download/download_stats.h" | 24 #include "content/browser/download/download_stats.h" |
| 24 #include "content/browser/download/save_file.h" | 25 #include "content/browser/download/save_file.h" |
| 25 #include "content/browser/download/save_file_manager.h" | 26 #include "content/browser/download/save_file_manager.h" |
| 26 #include "content/browser/download/save_item.h" | 27 #include "content/browser/download/save_item.h" |
| 27 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 28 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
| 28 #include "content/browser/renderer_host/render_process_host_impl.h" | 29 #include "content/browser/renderer_host/render_process_host_impl.h" |
| 29 #include "content/browser/renderer_host/render_view_host_delegate.h" | 30 #include "content/browser/renderer_host/render_view_host_delegate.h" |
| 30 #include "content/browser/renderer_host/render_view_host_impl.h" | 31 #include "content/browser/renderer_host/render_view_host_impl.h" |
| 31 #include "content/common/view_messages.h" | 32 #include "content/common/view_messages.h" |
| 32 #include "content/public/browser/browser_context.h" | 33 #include "content/public/browser/browser_context.h" |
| 33 #include "content/public/browser/browser_thread.h" | 34 #include "content/public/browser/browser_thread.h" |
| 34 #include "content/public/browser/content_browser_client.h" | 35 #include "content/public/browser/content_browser_client.h" |
| 35 #include "content/public/browser/download_manager_delegate.h" | 36 #include "content/public/browser/download_manager_delegate.h" |
| 37 #include "content/public/browser/download_save_info.h" |
| 36 #include "content/public/browser/navigation_entry.h" | 38 #include "content/public/browser/navigation_entry.h" |
| 37 #include "content/public/browser/notification_service.h" | 39 #include "content/public/browser/notification_service.h" |
| 38 #include "content/public/browser/notification_types.h" | 40 #include "content/public/browser/notification_types.h" |
| 39 #include "content/public/browser/resource_context.h" | 41 #include "content/public/browser/resource_context.h" |
| 40 #include "content/public/browser/web_contents.h" | 42 #include "content/public/browser/web_contents.h" |
| 41 #include "content/public/common/url_constants.h" | 43 #include "content/public/common/url_constants.h" |
| 42 #include "net/base/io_buffer.h" | 44 #include "net/base/io_buffer.h" |
| 43 #include "net/base/mime_util.h" | 45 #include "net/base/mime_util.h" |
| 44 #include "net/base/net_util.h" | 46 #include "net/base/net_util.h" |
| 45 #include "net/url_request/url_request_context.h" | 47 #include "net/url_request/url_request_context.h" |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 } | 100 } |
| 99 | 101 |
| 100 // Check whether we can save page as complete-HTML for the contents which | 102 // 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 | 103 // have specified a MIME type. Now only contents which have the MIME type |
| 102 // "text/html" can be saved as complete-HTML. | 104 // "text/html" can be saved as complete-HTML. |
| 103 bool CanSaveAsComplete(const std::string& contents_mime_type) { | 105 bool CanSaveAsComplete(const std::string& contents_mime_type) { |
| 104 return contents_mime_type == "text/html" || | 106 return contents_mime_type == "text/html" || |
| 105 contents_mime_type == "application/xhtml+xml"; | 107 contents_mime_type == "application/xhtml+xml"; |
| 106 } | 108 } |
| 107 | 109 |
| 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 | |
| 133 } // namespace | 110 } // namespace |
| 134 | 111 |
| 135 const base::FilePath::CharType SavePackage::kDefaultHtmlExtension[] = | 112 const base::FilePath::CharType SavePackage::kDefaultHtmlExtension[] = |
| 136 #if defined(OS_WIN) | 113 #if defined(OS_WIN) |
| 137 FILE_PATH_LITERAL("htm"); | 114 FILE_PATH_LITERAL("htm"); |
| 138 #else | 115 #else |
| 139 FILE_PATH_LITERAL("html"); | 116 FILE_PATH_LITERAL("html"); |
| 140 #endif | 117 #endif |
| 141 | 118 |
| 142 SavePackage::SavePackage(WebContents* web_contents, | 119 SavePackage::SavePackage(WebContents* web_contents, |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 | 281 |
| 305 wait_state_ = START_PROCESS; | 282 wait_state_ = START_PROCESS; |
| 306 | 283 |
| 307 // Initialize the request context and resource dispatcher. | 284 // Initialize the request context and resource dispatcher. |
| 308 BrowserContext* browser_context = web_contents()->GetBrowserContext(); | 285 BrowserContext* browser_context = web_contents()->GetBrowserContext(); |
| 309 if (!browser_context) { | 286 if (!browser_context) { |
| 310 NOTREACHED(); | 287 NOTREACHED(); |
| 311 return false; | 288 return false; |
| 312 } | 289 } |
| 313 | 290 |
| 314 scoped_ptr<DownloadRequestHandleInterface> request_handle( | 291 scoped_ptr<DownloadSaveInfo> save_info(new DownloadSaveInfo); |
| 315 new SavePackageRequestHandle(AsWeakPtr())); | 292 save_info->file_path = saved_main_file_path_; |
| 316 // The download manager keeps ownership but adds us as an observer. | 293 scoped_ptr<DownloadCreateInfo> create_info(new DownloadCreateInfo); |
| 294 create_info->save_info.swap(save_info); |
| 295 create_info->url_chain.push_back(page_url_); |
| 296 create_info->mime_type = (save_type_ == SAVE_PAGE_TYPE_AS_MHTML) |
| 297 ? "multipart/related" |
| 298 : "text/html"; |
| 299 create_info->original_mime_type = create_info->mime_type; |
| 300 create_info->route_id = |
| 301 GlobalRoutingID(web_contents()->GetRenderProcessHost()->GetID(), |
| 302 web_contents()->GetRoutingID()); |
| 303 |
| 304 // The download manager keeps ownership. |
| 317 download_manager_->CreateSavePackageDownloadItem( | 305 download_manager_->CreateSavePackageDownloadItem( |
| 318 saved_main_file_path_, | 306 create_info.Pass(), |
| 319 page_url_, | 307 base::Bind(&SavePackage::InitWithDownloadItem, |
| 320 ((save_type_ == SAVE_PAGE_TYPE_AS_MHTML) ? | 308 AsWeakPtr(), |
| 321 "multipart/related" : "text/html"), | |
| 322 request_handle.Pass(), | |
| 323 base::Bind(&SavePackage::InitWithDownloadItem, AsWeakPtr(), | |
| 324 download_created_callback)); | 309 download_created_callback)); |
| 325 return true; | 310 return true; |
| 326 } | 311 } |
| 327 | 312 |
| 328 void SavePackage::InitWithDownloadItem( | 313 void SavePackage::InitWithDownloadItem( |
| 329 const SavePackageDownloadCreatedCallback& download_created_callback, | 314 const SavePackageDownloadCreatedCallback& download_created_callback, |
| 330 DownloadItemImpl* item) { | 315 DownloadItemImpl* item) { |
| 331 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 316 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 332 DCHECK(item); | 317 DCHECK(item); |
| 333 download_ = item; | 318 download_ = item; |
| (...skipping 1110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1444 | 1429 |
| 1445 void SavePackage::FinalizeDownloadEntry() { | 1430 void SavePackage::FinalizeDownloadEntry() { |
| 1446 DCHECK(download_); | 1431 DCHECK(download_); |
| 1447 DCHECK(download_manager_); | 1432 DCHECK(download_manager_); |
| 1448 | 1433 |
| 1449 download_manager_->OnSavePackageSuccessfullyFinished(download_); | 1434 download_manager_->OnSavePackageSuccessfullyFinished(download_); |
| 1450 StopObservation(); | 1435 StopObservation(); |
| 1451 } | 1436 } |
| 1452 | 1437 |
| 1453 } // namespace content | 1438 } // namespace content |
| OLD | NEW |