| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/ui/download/download_tab_helper.h" | 5 #include "chrome/browser/ui/download/download_tab_helper.h" |
| 6 | 6 |
| 7 #include "chrome/browser/download/download_manager.h" | 7 #include "chrome/browser/download/download_manager.h" |
| 8 #include "chrome/browser/download/download_util.h" | 8 #include "chrome/browser/download/download_util.h" |
| 9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/common/render_messages.h" |
| 10 #include "content/browser/tab_contents/tab_contents.h" | 11 #include "content/browser/tab_contents/tab_contents.h" |
| 12 #include "content/common/view_messages.h" |
| 11 | 13 |
| 12 DownloadTabHelper::DownloadTabHelper(TabContents* tab_contents) | 14 DownloadTabHelper::DownloadTabHelper(TabContents* tab_contents) |
| 13 : tab_contents_(tab_contents) { | 15 : TabContentsObserver(tab_contents) { |
| 14 DCHECK(tab_contents_); | 16 DCHECK(tab_contents); |
| 15 } | 17 } |
| 16 | 18 |
| 17 DownloadTabHelper::~DownloadTabHelper() { | 19 DownloadTabHelper::~DownloadTabHelper() { |
| 18 } | 20 } |
| 19 | 21 |
| 20 void DownloadTabHelper::OnSavePage() { | 22 void DownloadTabHelper::OnSavePage() { |
| 21 // If we can not save the page, try to download it. | 23 // If we can not save the page, try to download it. |
| 22 if (!SavePackage::IsSavableContents(tab_contents_->contents_mime_type())) { | 24 if (!SavePackage::IsSavableContents(tab_contents()->contents_mime_type())) { |
| 23 DownloadManager* dlm = tab_contents_->profile()->GetDownloadManager(); | 25 DownloadManager* dlm = tab_contents()->profile()->GetDownloadManager(); |
| 24 const GURL& current_page_url = tab_contents_->GetURL(); | 26 const GURL& current_page_url = tab_contents()->GetURL(); |
| 25 if (dlm && current_page_url.is_valid()) { | 27 if (dlm && current_page_url.is_valid()) { |
| 26 dlm->DownloadUrl(current_page_url, GURL(), "", tab_contents_); | 28 dlm->DownloadUrl(current_page_url, GURL(), "", tab_contents()); |
| 27 download_util::RecordDownloadCount( | 29 download_util::RecordDownloadCount( |
| 28 download_util::INITIATED_BY_SAVE_PACKAGE_FAILURE_COUNT); | 30 download_util::INITIATED_BY_SAVE_PACKAGE_FAILURE_COUNT); |
| 29 } | 31 } |
| 30 return; | 32 return; |
| 31 } | 33 } |
| 32 | 34 |
| 33 tab_contents_->Stop(); | 35 tab_contents()->Stop(); |
| 34 | 36 |
| 35 // Create the save package and possibly prompt the user for the name to save | 37 // Create the save package and possibly prompt the user for the name to save |
| 36 // the page as. The user prompt is an asynchronous operation that runs on | 38 // the page as. The user prompt is an asynchronous operation that runs on |
| 37 // another thread. | 39 // another thread. |
| 38 save_package_ = new SavePackage(tab_contents_); | 40 save_package_ = new SavePackage(tab_contents()); |
| 39 save_package_->GetSaveInfo(); | 41 save_package_->GetSaveInfo(); |
| 40 } | 42 } |
| 41 | 43 |
| 42 // Used in automated testing to bypass prompting the user for file names. | 44 // Used in automated testing to bypass prompting the user for file names. |
| 43 // Instead, the names and paths are hard coded rather than running them through | 45 // Instead, the names and paths are hard coded rather than running them through |
| 44 // file name sanitation and extension / mime checking. | 46 // file name sanitation and extension / mime checking. |
| 45 bool DownloadTabHelper::SavePage(const FilePath& main_file, | 47 bool DownloadTabHelper::SavePage(const FilePath& main_file, |
| 46 const FilePath& dir_path, | 48 const FilePath& dir_path, |
| 47 SavePackage::SavePackageType save_type) { | 49 SavePackage::SavePackageType save_type) { |
| 48 // Stop the page from navigating. | 50 // Stop the page from navigating. |
| 49 tab_contents_->Stop(); | 51 tab_contents()->Stop(); |
| 50 | 52 |
| 51 save_package_ = | 53 save_package_ = |
| 52 new SavePackage(tab_contents_, save_type, main_file, dir_path); | 54 new SavePackage(tab_contents(), save_type, main_file, dir_path); |
| 53 return save_package_->Init(); | 55 return save_package_->Init(); |
| 54 } | 56 } |
| 57 |
| 58 bool DownloadTabHelper::OnMessageReceived(const IPC::Message& message) { |
| 59 bool handled = true; |
| 60 IPC_BEGIN_MESSAGE_MAP(DownloadTabHelper, message) |
| 61 IPC_MESSAGE_HANDLER(ViewHostMsg_SaveAs, OnSavePage) |
| 62 IPC_MESSAGE_UNHANDLED(handled = false) |
| 63 IPC_END_MESSAGE_MAP() |
| 64 |
| 65 return handled; |
| 66 } |
| 67 |
| OLD | NEW |