1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/download/download_file.h"
#include <string>
#include "base/file_util.h"
#include "base/stringprintf.h"
#include "content/browser/download/download_create_info.h"
#include "content/browser/download/download_manager.h"
#include "content/public/browser/browser_thread.h"
using content::BrowserThread;
namespace {
// The maximum number of 'uniquified' files we will try to create.
// This is used when the filename we're trying to download is already in use,
// so we create a new unique filename by appending " (nnn)" before the
// extension, where 1 <= nnn <= kMaxUniqueFiles.
// Also used by code that cleans up said files.
static const int kMaxUniqueFiles = 100;
}
DownloadFile::DownloadFile(const DownloadCreateInfo* info,
DownloadRequestHandleInterface* request_handle,
DownloadManager* download_manager)
: BaseFile(info->save_info.file_path,
info->url(),
info->referrer_url,
info->received_bytes,
info->save_info.file_stream),
id_(info->download_id),
request_handle_(request_handle),
download_manager_(download_manager) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
}
DownloadFile::~DownloadFile() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
}
void DownloadFile::CancelDownloadRequest() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
request_handle_->CancelRequest();
}
DownloadManager* DownloadFile::GetDownloadManager() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
return download_manager_.get();
}
std::string DownloadFile::DebugString() const {
return base::StringPrintf("{"
" id_ = " "%d"
" request_handle = %s"
" Base File = %s"
" }",
id_.local(),
request_handle_->DebugString().c_str(),
BaseFile::DebugString().c_str());
}
void DownloadFile::AppendNumberToPath(FilePath* path, int number) {
*path = path->InsertBeforeExtensionASCII(StringPrintf(" (%d)", number));
}
int DownloadFile::GetUniquePathNumber(const FilePath& path) {
if (!file_util::PathExists(path))
return 0;
FilePath new_path;
for (int count = 1; count <= kMaxUniqueFiles; ++count) {
new_path = FilePath(path);
AppendNumberToPath(&new_path, count);
if (!file_util::PathExists(new_path))
return count;
}
return -1;
}
FilePath DownloadFile::AppendSuffixToPath(
const FilePath& path,
const FilePath::StringType& suffix) {
FilePath::StringType file_name;
base::SStringPrintf(
&file_name, PRFilePathLiteral PRFilePathLiteral, path.value().c_str(),
suffix.c_str());
return FilePath(file_name);
}
int DownloadFile::GetUniquePathNumberWithSuffix(
const FilePath& path,
const FilePath::StringType& suffix) {
if (!file_util::PathExists(path) &&
!file_util::PathExists(AppendSuffixToPath(path, suffix)))
return 0;
FilePath new_path;
for (int count = 1; count <= kMaxUniqueFiles; ++count) {
new_path = FilePath(path);
AppendNumberToPath(&new_path, count);
if (!file_util::PathExists(new_path) &&
!file_util::PathExists(AppendSuffixToPath(new_path, suffix)))
return count;
}
return -1;
}
|