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

Side by Side Diff: content/browser/download/base_file.cc

Issue 10905284: Use the user's preferred downloads directory for creating the initial download file. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 3 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) 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/base_file.h" 5 #include "content/browser/download/base_file.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/file_util.h" 8 #include "base/file_util.h"
9 #include "base/format_macros.h" 9 #include "base/format_macros.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/pickle.h" 11 #include "base/pickle.h"
12 #include "base/stringprintf.h" 12 #include "base/stringprintf.h"
13 #include "base/threading/thread_restrictions.h" 13 #include "base/threading/thread_restrictions.h"
14 #include "base/utf_string_conversions.h" 14 #include "base/utf_string_conversions.h"
15 #include "content/browser/download/download_net_log_parameters.h" 15 #include "content/browser/download/download_net_log_parameters.h"
16 #include "content/browser/download/download_stats.h" 16 #include "content/browser/download/download_stats.h"
17 #include "content/public/browser/browser_thread.h" 17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/content_browser_client.h"
19 #include "crypto/secure_hash.h" 18 #include "crypto/secure_hash.h"
20 #include "net/base/file_stream.h" 19 #include "net/base/file_stream.h"
21 #include "net/base/net_errors.h" 20 #include "net/base/net_errors.h"
22 21
23 #if defined(OS_WIN) 22 #if defined(OS_WIN)
24 #include <windows.h> 23 #include <windows.h>
25 #include <shellapi.h> 24 #include <shellapi.h>
26 25
27 #include "content/browser/safe_util_win.h" 26 #include "content/browser/safe_util_win.h"
28 #elif defined(OS_MACOSX) 27 #elif defined(OS_MACOSX)
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 return MapShFileOperationCodes(result); 195 return MapShFileOperationCodes(result);
197 } 196 }
198 197
199 #endif 198 #endif
200 199
201 } // namespace 200 } // namespace
202 201
203 // This will initialize the entire array to zero. 202 // This will initialize the entire array to zero.
204 const unsigned char BaseFile::kEmptySha256Hash[] = { 0 }; 203 const unsigned char BaseFile::kEmptySha256Hash[] = { 0 };
205 204
206 BaseFile::BaseFile(const FilePath& full_path, 205 BaseFile::BaseFile(const FilePath& download_directory,
206 const FilePath& full_path,
207 const GURL& source_url, 207 const GURL& source_url,
208 const GURL& referrer_url, 208 const GURL& referrer_url,
209 int64 received_bytes, 209 int64 received_bytes,
210 bool calculate_hash, 210 bool calculate_hash,
211 const std::string& hash_state, 211 const std::string& hash_state,
212 const linked_ptr<net::FileStream>& file_stream, 212 const linked_ptr<net::FileStream>& file_stream,
213 const net::BoundNetLog& bound_net_log) 213 const net::BoundNetLog& bound_net_log)
214 : full_path_(full_path), 214 : download_directory_(download_directory),
215 full_path_(full_path),
215 source_url_(source_url), 216 source_url_(source_url),
216 referrer_url_(referrer_url), 217 referrer_url_(referrer_url),
217 file_stream_(file_stream), 218 file_stream_(file_stream),
218 bytes_so_far_(received_bytes), 219 bytes_so_far_(received_bytes),
219 start_tick_(base::TimeTicks::Now()), 220 start_tick_(base::TimeTicks::Now()),
220 calculate_hash_(calculate_hash), 221 calculate_hash_(calculate_hash),
221 detached_(false), 222 detached_(false),
222 bound_net_log_(bound_net_log) { 223 bound_net_log_(bound_net_log) {
223 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 224 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
224 memcpy(sha256_hash_, kEmptySha256Hash, kSha256HashLen); 225 memcpy(sha256_hash_, kEmptySha256Hash, kSha256HashLen);
(...skipping 16 matching lines...) Expand all
241 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 242 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
242 if (detached_) 243 if (detached_)
243 Close(); 244 Close();
244 else 245 else
245 Cancel(); // Will delete the file. 246 Cancel(); // Will delete the file.
246 } 247 }
247 248
248 net::Error BaseFile::Initialize() { 249 net::Error BaseFile::Initialize() {
249 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 250 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
250 DCHECK(!detached_); 251 DCHECK(!detached_);
252 DCHECK(!full_path_.empty() || !download_directory_.empty());
251 253
252 if (full_path_.empty()) { 254 if (full_path_.empty()) {
253 FilePath temp_file; 255 FilePath temp_file;
254 FilePath download_dir = 256 if (!file_util::CreateTemporaryFileInDir(download_directory_, &temp_file) &&
255 content::GetContentClient()->browser()->GetDefaultDownloadDirectory();
256 if (!file_util::CreateTemporaryFileInDir(download_dir, &temp_file) &&
257 !file_util::CreateTemporaryFile(&temp_file)) { 257 !file_util::CreateTemporaryFile(&temp_file)) {
258 return LOG_ERROR("unable to create", net::ERR_FILE_NOT_FOUND); 258 return LOG_ERROR("unable to create", net::ERR_FILE_NOT_FOUND);
259 } 259 }
260 full_path_ = temp_file; 260 full_path_ = temp_file;
261 } 261 }
262 262
263 return Open(); 263 return Open();
264 } 264 }
265 265
266 net::Error BaseFile::AppendDataToFile(const char* data, size_t data_len) { 266 net::Error BaseFile::AppendDataToFile(const char* data, size_t data_len) {
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after
551 base::TimeDelta diff = current_time - start_tick_; 551 base::TimeDelta diff = current_time - start_tick_;
552 int64 diff_ms = diff.InMilliseconds(); 552 int64 diff_ms = diff.InMilliseconds();
553 return diff_ms == 0 ? 0 : bytes_so_far() * 1000 / diff_ms; 553 return diff_ms == 0 ? 0 : bytes_so_far() * 1000 / diff_ms;
554 } 554 }
555 555
556 int64 BaseFile::CurrentSpeed() const { 556 int64 BaseFile::CurrentSpeed() const {
557 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 557 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
558 return CurrentSpeedAtTime(base::TimeTicks::Now()); 558 return CurrentSpeedAtTime(base::TimeTicks::Now());
559 } 559 }
560 560
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698