Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(83)

Side by Side Diff: chrome/browser/ui/download/download_tab_helper.cc

Issue 6973052: When the download folder does not exist, change the download folder to a user's "Downloads" (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Correct typo Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "base/path_service.h"
7 #include "chrome/browser/browser_process.h" 8 #include "chrome/browser/browser_process.h"
8 #include "chrome/browser/download/download_manager.h" 9 #include "chrome/browser/download/download_manager.h"
10 #include "chrome/browser/download/download_prefs.h"
9 #include "chrome/browser/download/download_request_limiter.h" 11 #include "chrome/browser/download/download_request_limiter.h"
10 #include "chrome/browser/download/download_util.h" 12 #include "chrome/browser/download/download_util.h"
13 #include "chrome/browser/prefs/pref_service.h"
11 #include "chrome/browser/profiles/profile.h" 14 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/blocked_content/blocked_content_tab_helper.h" 15 #include "chrome/browser/ui/blocked_content/blocked_content_tab_helper.h"
13 #include "chrome/browser/ui/blocked_content/blocked_content_tab_helper_delegate. h" 16 #include "chrome/browser/ui/blocked_content/blocked_content_tab_helper_delegate. h"
14 #include "chrome/browser/ui/download/download_tab_helper_delegate.h" 17 #include "chrome/browser/ui/download/download_tab_helper_delegate.h"
15 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" 18 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
19 #include "chrome/common/chrome_paths.h"
20 #include "chrome/common/pref_names.h"
16 #include "chrome/common/render_messages.h" 21 #include "chrome/common/render_messages.h"
17 #include "content/browser/tab_contents/tab_contents.h" 22 #include "content/browser/tab_contents/tab_contents.h"
18 #include "content/common/view_messages.h" 23 #include "content/common/view_messages.h"
19 24
20 DownloadTabHelper::DownloadTabHelper(TabContentsWrapper* tab_contents) 25 DownloadTabHelper::DownloadTabHelper(TabContentsWrapper* tab_contents)
21 : TabContentsObserver(tab_contents->tab_contents()), 26 : TabContentsObserver(tab_contents->tab_contents()),
22 tab_contents_wrapper_(tab_contents), 27 tab_contents_wrapper_(tab_contents),
23 delegate_(NULL) { 28 delegate_(NULL) {
24 DCHECK(tab_contents); 29 DCHECK(tab_contents);
25 } 30 }
(...skipping 21 matching lines...) Expand all
47 // another thread. 52 // another thread.
48 save_package_ = new SavePackage(tab_contents_wrapper_); 53 save_package_ = new SavePackage(tab_contents_wrapper_);
49 save_package_->GetSaveInfo(); 54 save_package_->GetSaveInfo();
50 } 55 }
51 56
52 void DownloadTabHelper::OnSaveURL(const GURL& url) { 57 void DownloadTabHelper::OnSaveURL(const GURL& url) {
53 DownloadManager* dlm = tab_contents()->profile()->GetDownloadManager(); 58 DownloadManager* dlm = tab_contents()->profile()->GetDownloadManager();
54 dlm->DownloadUrl(url, tab_contents()->GetURL(), "", tab_contents()); 59 dlm->DownloadUrl(url, tab_contents()->GetURL(), "", tab_contents());
55 } 60 }
56 61
57 // Used in automated testing to bypass prompting the user for file names.
58 // Instead, the names and paths are hard coded rather than running them through
59 // file name sanitation and extension / mime checking.
Randy Smith (Not in Mondays) 2011/06/02 19:13:57 Why delete this comment? I think it's still valid
haraken1 2011/06/03 06:50:27 Oops. I restored this comment download_tab_helper.
60 bool DownloadTabHelper::SavePage(const FilePath& main_file, 62 bool DownloadTabHelper::SavePage(const FilePath& main_file,
61 const FilePath& dir_path, 63 const FilePath& dir_path,
62 SavePackage::SavePackageType save_type) { 64 SavePackage::SavePackageType save_type) {
63 // Stop the page from navigating. 65 // Stop the page from navigating.
64 tab_contents()->Stop(); 66 tab_contents()->Stop();
65 67
66 save_package_ = 68 save_package_ =
67 new SavePackage(tab_contents_wrapper_, save_type, main_file, dir_path); 69 new SavePackage(tab_contents_wrapper_, save_type, main_file, dir_path);
70 // Skips GetSaveInfo() and directly calls Init().
71 // We do not have to explicitly disable the select file dialog
72 // since we skip the process that can show the dialog.
68 return save_package_->Init(); 73 return save_package_->Init();
69 } 74 }
70 75
76 string16 DownloadTabHelper::SavePageWithoutDialog() {
77 tab_contents()->Stop();
78
79 save_package_ = new SavePackage(tab_contents_wrapper_);
80 // Disables the select file dialog.
81 save_package_->SetShouldPromptUser(false);
82 // This GetSaveInfo() calls save_package_->Init() in the background.
83 save_package_->GetSaveInfo();
84 return tab_contents()->GetTitle();
85 }
86
87 void DownloadTabHelper::ChangeSaveDirectoryPrefs(
88 const FilePath& website_save_dir,
89 const FilePath& download_save_dir,
90 const FilePath& default_downloads_dir,
91 const SavePackage::SavePackageType save_type) {
92 PrefService* prefs = tab_contents()->profile()->GetPrefs();
93
94 DCHECK(prefs->FindPreference(prefs::kDownloadDefaultDirectory));
95 prev_download_save_dir_ = prefs->GetFilePath(
96 prefs::kDownloadDefaultDirectory);
97
98 // Check whether the preference has the default folder for saving HTML.
99 // If not, initialize it with the default folder for downloaded files.
100 if (!prefs->FindPreference(prefs::kSaveFileDefaultDirectory)) {
101 prefs->RegisterFilePathPref(prefs::kSaveFileDefaultDirectory,
102 prev_download_save_dir_,
103 PrefService::UNSYNCABLE_PREF);
104 }
105 prev_website_save_dir_ = prefs->GetFilePath(
106 prefs::kSaveFileDefaultDirectory);
107
108 if (!PathService::Get(
109 chrome::DIR_DEFAULT_DOWNLOADS, &prev_default_downloads_dir_)) {
110 LOG(ERROR) << "Cannot find the user's \"Downloads\" folder.";
111 prev_default_downloads_dir_.clear();
112 }
113
114 DownloadPrefs* download_prefs =
115 tab_contents()->profile()->GetDownloadManager()->download_prefs();
116 prev_save_type_ =
117 static_cast<SavePackage::SavePackageType>
118 (download_prefs->save_file_type());
119
120 prefs->SetFilePath(prefs::kSaveFileDefaultDirectory, website_save_dir);
121 prefs->SetFilePath(prefs::kDownloadDefaultDirectory, download_save_dir);
122 PathService::Set(chrome::DIR_DEFAULT_DOWNLOADS, default_downloads_dir);
123 prefs->SetInteger(prefs::kSaveFileType, save_type);
Randy Smith (Not in Mondays) 2011/06/02 19:13:57 It looks to me as if everything in this function c
haraken1 2011/06/03 06:50:27 Makes sense. Done.
124 }
125
126 void DownloadTabHelper::RestoreSaveDirectoryPrefs() {
127 PrefService* prefs = tab_contents()->profile()->GetPrefs();
128 prefs->SetFilePath(prefs::kSaveFileDefaultDirectory, prev_website_save_dir_);
129 prefs->SetFilePath(prefs::kDownloadDefaultDirectory, prev_download_save_dir_);
130 // Restores DIR_DEFAULT_DOWNLOADS only when we could get
131 // |prev_default_downloads_dir_| successfully in ChangeSaveDirectoryPrefs().
132 if (!prev_default_downloads_dir_.empty())
133 PathService::Set(
134 chrome::DIR_DEFAULT_DOWNLOADS, prev_default_downloads_dir_);
135 prefs->SetInteger(prefs::kSaveFileType, prev_save_type_);
136 }
137
71 bool DownloadTabHelper::CanDownload(int request_id) { 138 bool DownloadTabHelper::CanDownload(int request_id) {
72 if (delegate_) 139 if (delegate_)
73 return delegate_->CanDownload(request_id); 140 return delegate_->CanDownload(request_id);
74 return true; 141 return true;
75 } 142 }
76 143
77 void DownloadTabHelper::OnStartDownload(DownloadItem* download) { 144 void DownloadTabHelper::OnStartDownload(DownloadItem* download) {
78 DCHECK(download); 145 DCHECK(download);
79 146
80 BlockedContentTabHelperDelegate* blocked_content_delegate = 147 BlockedContentTabHelperDelegate* blocked_content_delegate =
(...skipping 21 matching lines...) Expand all
102 169
103 return handled; 170 return handled;
104 } 171 }
105 172
106 void DownloadTabHelper::DidGetUserGesture() { 173 void DownloadTabHelper::DidGetUserGesture() {
107 DownloadRequestLimiter* limiter = 174 DownloadRequestLimiter* limiter =
108 g_browser_process->download_request_limiter(); 175 g_browser_process->download_request_limiter();
109 if (limiter) 176 if (limiter)
110 limiter->OnUserGesture(tab_contents()); 177 limiter->OnUserGesture(tab_contents());
111 } 178 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698