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
3 // found in the LICENSE file.
5 #include "content/browser/download/drag_download_util.h"
10 #include "base/file_util.h"
11 #include "base/files/file_path.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_util.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "base/threading/thread_restrictions.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "net/base/file_stream.h"
19 #include "net/base/net_errors.h"
22 using net::FileStream
;
26 bool ParseDownloadMetadata(const base::string16
& metadata
,
27 base::string16
* mime_type
,
28 base::FilePath
* file_name
,
30 const base::char16 separator
= L
':';
32 size_t mime_type_end_pos
= metadata
.find(separator
);
33 if (mime_type_end_pos
== base::string16::npos
)
36 size_t file_name_end_pos
= metadata
.find(separator
, mime_type_end_pos
+ 1);
37 if (file_name_end_pos
== base::string16::npos
)
40 GURL parsed_url
= GURL(metadata
.substr(file_name_end_pos
+ 1));
41 if (!parsed_url
.is_valid())
45 *mime_type
= metadata
.substr(0, mime_type_end_pos
);
47 base::string16 file_name_str
= metadata
.substr(
48 mime_type_end_pos
+ 1, file_name_end_pos
- mime_type_end_pos
- 1);
50 *file_name
= base::FilePath(file_name_str
);
52 *file_name
= base::FilePath(base::UTF16ToUTF8(file_name_str
));
61 FileStream
* CreateFileStreamForDrop(base::FilePath
* file_path
,
62 net::NetLog
* net_log
) {
63 DCHECK(file_path
&& !file_path
->empty());
65 scoped_ptr
<FileStream
> file_stream(new FileStream(net_log
));
66 const int kMaxSeq
= 99;
67 for (int seq
= 0; seq
<= kMaxSeq
; seq
++) {
68 base::FilePath new_file_path
;
70 new_file_path
= *file_path
;
73 base::string16 suffix
=
74 base::ASCIIToUTF16("-") + base::IntToString16(seq
);
76 std::string suffix
= std::string("-") + base::IntToString(seq
);
78 new_file_path
= file_path
->InsertBeforeExtension(suffix
);
81 // http://crbug.com/110709
82 base::ThreadRestrictions::ScopedAllowIO allow_io
;
84 // Explicitly (and redundantly check) for file -- despite the fact that our
85 // open won't overwrite -- just to avoid log spew.
86 if (!base::PathExists(new_file_path
) &&
87 file_stream
->OpenSync(new_file_path
, base::PLATFORM_FILE_CREATE
|
88 base::PLATFORM_FILE_WRITE
) == net::OK
) {
89 *file_path
= new_file_path
;
90 return file_stream
.release();
97 PromiseFileFinalizer::PromiseFileFinalizer(
98 DragDownloadFile
* drag_file_downloader
)
99 : drag_file_downloader_(drag_file_downloader
) {
102 void PromiseFileFinalizer::OnDownloadCompleted(
103 const base::FilePath
& file_path
) {
104 BrowserThread::PostTask(
105 BrowserThread::UI
, FROM_HERE
,
106 base::Bind(&PromiseFileFinalizer::Cleanup
, this));
109 void PromiseFileFinalizer::OnDownloadAborted() {
110 BrowserThread::PostTask(
111 BrowserThread::UI
, FROM_HERE
,
112 base::Bind(&PromiseFileFinalizer::Cleanup
, this));
115 PromiseFileFinalizer::~PromiseFileFinalizer() {}
117 void PromiseFileFinalizer::Cleanup() {
118 if (drag_file_downloader_
.get())
119 drag_file_downloader_
= NULL
;
122 } // namespace content