Add remoting and PPAPI tests to GN build
[chromium-blink-merge.git] / content / browser / download / drag_download_util.cc
blobf259af22c6c2c4f895aea139896885519d9bc298
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"
7 #include <string>
9 #include "base/bind.h"
10 #include "base/files/file.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 "url/gurl.h"
20 namespace content {
22 bool ParseDownloadMetadata(const base::string16& metadata,
23 base::string16* mime_type,
24 base::FilePath* file_name,
25 GURL* url) {
26 const base::char16 separator = L':';
28 size_t mime_type_end_pos = metadata.find(separator);
29 if (mime_type_end_pos == base::string16::npos)
30 return false;
32 size_t file_name_end_pos = metadata.find(separator, mime_type_end_pos + 1);
33 if (file_name_end_pos == base::string16::npos)
34 return false;
36 GURL parsed_url = GURL(metadata.substr(file_name_end_pos + 1));
37 if (!parsed_url.is_valid())
38 return false;
40 if (mime_type)
41 *mime_type = metadata.substr(0, mime_type_end_pos);
42 if (file_name) {
43 base::string16 file_name_str = metadata.substr(
44 mime_type_end_pos + 1, file_name_end_pos - mime_type_end_pos - 1);
45 #if defined(OS_WIN)
46 *file_name = base::FilePath(file_name_str);
47 #else
48 *file_name = base::FilePath(base::UTF16ToUTF8(file_name_str));
49 #endif
51 if (url)
52 *url = parsed_url;
54 return true;
57 base::File CreateFileForDrop(base::FilePath* file_path) {
58 DCHECK(file_path && !file_path->empty());
60 const int kMaxSeq = 99;
61 for (int seq = 0; seq <= kMaxSeq; seq++) {
62 base::FilePath new_file_path;
63 if (seq == 0) {
64 new_file_path = *file_path;
65 } else {
66 #if defined(OS_WIN)
67 base::string16 suffix =
68 base::ASCIIToUTF16("-") + base::IntToString16(seq);
69 #else
70 std::string suffix = std::string("-") + base::IntToString(seq);
71 #endif
72 new_file_path = file_path->InsertBeforeExtension(suffix);
75 // http://crbug.com/110709
76 base::ThreadRestrictions::ScopedAllowIO allow_io;
78 base::File file(
79 new_file_path, base::File::FLAG_CREATE | base::File::FLAG_WRITE);
80 if (file.IsValid()) {
81 *file_path = new_file_path;
82 return file.Pass();
86 return base::File();
89 PromiseFileFinalizer::PromiseFileFinalizer(
90 DragDownloadFile* drag_file_downloader)
91 : drag_file_downloader_(drag_file_downloader) {
94 void PromiseFileFinalizer::OnDownloadCompleted(
95 const base::FilePath& file_path) {
96 BrowserThread::PostTask(
97 BrowserThread::UI, FROM_HERE,
98 base::Bind(&PromiseFileFinalizer::Cleanup, this));
101 void PromiseFileFinalizer::OnDownloadAborted() {
102 BrowserThread::PostTask(
103 BrowserThread::UI, FROM_HERE,
104 base::Bind(&PromiseFileFinalizer::Cleanup, this));
107 PromiseFileFinalizer::~PromiseFileFinalizer() {}
109 void PromiseFileFinalizer::Cleanup() {
110 if (drag_file_downloader_.get())
111 drag_file_downloader_ = NULL;
114 } // namespace content