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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/download/drag_download_util.h"
6
7 #include "base/string_util.h"
8 #include "base/file_path.h"
9 #include "base/file_util.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/task.h"
12 #include "base/string_number_conversions.h"
13 #include "base/utf_string_conversions.h"
14 #include "content/browser/browser_thread.h"
15 #include "googleurl/src/gurl.h"
16 #include "net/base/file_stream.h"
17 #include "net/base/net_errors.h"
18
19 using net::FileStream;
20
21 namespace drag_download_util {
22
ParseDownloadMetadata(const string16 & metadata,string16 * mime_type,FilePath * file_name,GURL * url)23 bool ParseDownloadMetadata(const string16& metadata,
24 string16* mime_type,
25 FilePath* file_name,
26 GURL* url) {
27 const char16 separator = L':';
28
29 size_t mime_type_end_pos = metadata.find(separator);
30 if (mime_type_end_pos == string16::npos)
31 return false;
32
33 size_t file_name_end_pos = metadata.find(separator, mime_type_end_pos + 1);
34 if (file_name_end_pos == string16::npos)
35 return false;
36
37 GURL parsed_url = GURL(metadata.substr(file_name_end_pos + 1));
38 if (!parsed_url.is_valid())
39 return false;
40
41 if (mime_type)
42 *mime_type = metadata.substr(0, mime_type_end_pos);
43 if (file_name) {
44 string16 file_name_str = metadata.substr(
45 mime_type_end_pos + 1, file_name_end_pos - mime_type_end_pos - 1);
46 #if defined(OS_WIN)
47 *file_name = FilePath(file_name_str);
48 #else
49 *file_name = FilePath(UTF16ToUTF8(file_name_str));
50 #endif
51 }
52 if (url)
53 *url = parsed_url;
54
55 return true;
56 }
57
CreateFileStreamForDrop(FilePath * file_path)58 FileStream* CreateFileStreamForDrop(FilePath* file_path) {
59 DCHECK(file_path && !file_path->empty());
60
61 scoped_ptr<FileStream> file_stream(new FileStream);
62 const int kMaxSeq = 99;
63 for (int seq = 0; seq <= kMaxSeq; seq++) {
64 FilePath new_file_path;
65 if (seq == 0) {
66 new_file_path = *file_path;
67 } else {
68 #if defined(OS_WIN)
69 string16 suffix = ASCIIToUTF16("-") + base::IntToString16(seq);
70 #else
71 std::string suffix = std::string("-") + base::IntToString(seq);
72 #endif
73 new_file_path = file_path->InsertBeforeExtension(suffix);
74 }
75
76 // Explicitly (and redundantly check) for file -- despite the fact that our
77 // open won't overwrite -- just to avoid log spew.
78 if (!file_util::PathExists(new_file_path) &&
79 file_stream->Open(new_file_path, base::PLATFORM_FILE_CREATE |
80 base::PLATFORM_FILE_WRITE) == net::OK) {
81 *file_path = new_file_path;
82 return file_stream.release();
83 }
84 }
85
86 return NULL;
87 }
88
PromiseFileFinalizer(DragDownloadFile * drag_file_downloader)89 PromiseFileFinalizer::PromiseFileFinalizer(
90 DragDownloadFile* drag_file_downloader)
91 : drag_file_downloader_(drag_file_downloader) {
92 }
93
~PromiseFileFinalizer()94 PromiseFileFinalizer::~PromiseFileFinalizer() {}
95
Cleanup()96 void PromiseFileFinalizer::Cleanup() {
97 if (drag_file_downloader_.get())
98 drag_file_downloader_ = NULL;
99 }
100
OnDownloadCompleted(const FilePath & file_path)101 void PromiseFileFinalizer::OnDownloadCompleted(const FilePath& file_path) {
102 BrowserThread::PostTask(
103 BrowserThread::UI, FROM_HERE,
104 NewRunnableMethod(this, &PromiseFileFinalizer::Cleanup));
105 }
106
OnDownloadAborted()107 void PromiseFileFinalizer::OnDownloadAborted() {
108 BrowserThread::PostTask(
109 BrowserThread::UI, FROM_HERE,
110 NewRunnableMethod(this, &PromiseFileFinalizer::Cleanup));
111 }
112
113 } // namespace drag_download_util
114