2011-08-02 18:50:06 +02:00
|
|
|
// 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.
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
#include "libcef/cef_thread.h"
|
|
|
|
#include "libcef/drag_download_util.h"
|
2011-08-02 18:50:06 +02:00
|
|
|
|
2012-02-17 15:51:20 +01:00
|
|
|
#include "base/bind.h"
|
2011-08-02 18:50:06 +02:00
|
|
|
#include "base/string_util.h"
|
|
|
|
#include "base/file_path.h"
|
|
|
|
#include "base/file_util.h"
|
|
|
|
#include "base/memory/scoped_ptr.h"
|
|
|
|
#include "base/string_number_conversions.h"
|
|
|
|
#include "base/utf_string_conversions.h"
|
|
|
|
#include "googleurl/src/gurl.h"
|
|
|
|
#include "net/base/file_stream.h"
|
|
|
|
#include "net/base/net_errors.h"
|
|
|
|
|
|
|
|
using net::FileStream;
|
|
|
|
|
|
|
|
namespace drag_download_util {
|
|
|
|
|
|
|
|
bool ParseDownloadMetadata(const string16& metadata,
|
|
|
|
string16* mime_type,
|
|
|
|
FilePath* file_name,
|
|
|
|
GURL* url) {
|
|
|
|
const char16 separator = L':';
|
|
|
|
|
|
|
|
size_t mime_type_end_pos = metadata.find(separator);
|
|
|
|
if (mime_type_end_pos == string16::npos)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
size_t file_name_end_pos = metadata.find(separator, mime_type_end_pos + 1);
|
|
|
|
if (file_name_end_pos == string16::npos)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
GURL parsed_url = GURL(metadata.substr(file_name_end_pos + 1));
|
|
|
|
if (!parsed_url.is_valid())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (mime_type)
|
|
|
|
*mime_type = metadata.substr(0, mime_type_end_pos);
|
|
|
|
if (file_name) {
|
|
|
|
string16 file_name_str = metadata.substr(
|
|
|
|
mime_type_end_pos + 1, file_name_end_pos - mime_type_end_pos - 1);
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
*file_name = FilePath(file_name_str);
|
|
|
|
#else
|
|
|
|
*file_name = FilePath(UTF16ToUTF8(file_name_str));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
if (url)
|
|
|
|
*url = parsed_url;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileStream* CreateFileStreamForDrop(FilePath* file_path) {
|
|
|
|
DCHECK(file_path && !file_path->empty());
|
|
|
|
|
2012-02-18 01:19:52 +01:00
|
|
|
scoped_ptr<FileStream> file_stream(new FileStream(NULL));
|
2011-08-02 18:50:06 +02:00
|
|
|
const int kMaxSeq = 99;
|
|
|
|
for (int seq = 0; seq <= kMaxSeq; seq++) {
|
|
|
|
FilePath new_file_path;
|
|
|
|
if (seq == 0) {
|
|
|
|
new_file_path = *file_path;
|
|
|
|
} else {
|
2012-01-10 00:46:23 +01:00
|
|
|
#if defined(OS_WIN)
|
2011-08-02 18:50:06 +02:00
|
|
|
string16 suffix = ASCIIToUTF16("-") + base::IntToString16(seq);
|
2012-01-10 00:46:23 +01:00
|
|
|
#else
|
2011-08-02 18:50:06 +02:00
|
|
|
std::string suffix = std::string("-") + base::IntToString(seq);
|
2012-01-10 00:46:23 +01:00
|
|
|
#endif
|
2011-08-02 18:50:06 +02:00
|
|
|
new_file_path = file_path->InsertBeforeExtension(suffix);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Explicitly (and redundantly check) for file -- despite the fact that our
|
|
|
|
// open won't overwrite -- just to avoid log spew.
|
|
|
|
if (!file_util::PathExists(new_file_path) &&
|
2012-02-18 01:19:52 +01:00
|
|
|
file_stream->OpenSync(new_file_path, base::PLATFORM_FILE_CREATE |
|
2011-08-02 18:50:06 +02:00
|
|
|
base::PLATFORM_FILE_WRITE) == net::OK) {
|
|
|
|
*file_path = new_file_path;
|
|
|
|
return file_stream.release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
PromiseFileFinalizer::PromiseFileFinalizer(
|
|
|
|
DragDownloadFile* drag_file_downloader)
|
|
|
|
: drag_file_downloader_(drag_file_downloader) {
|
|
|
|
}
|
|
|
|
|
|
|
|
PromiseFileFinalizer::~PromiseFileFinalizer() {}
|
|
|
|
|
|
|
|
void PromiseFileFinalizer::Cleanup() {
|
|
|
|
if (drag_file_downloader_.get())
|
|
|
|
drag_file_downloader_ = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PromiseFileFinalizer::OnDownloadCompleted(const FilePath& file_path) {
|
|
|
|
CefThread::PostTask(
|
|
|
|
CefThread::UI, FROM_HERE,
|
2012-02-17 15:51:20 +01:00
|
|
|
base::Bind(&PromiseFileFinalizer::Cleanup, this));
|
2011-08-02 18:50:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void PromiseFileFinalizer::OnDownloadAborted() {
|
|
|
|
CefThread::PostTask(
|
|
|
|
CefThread::UI, FROM_HERE,
|
2012-02-17 15:51:20 +01:00
|
|
|
base::Bind(&PromiseFileFinalizer::Cleanup, this));
|
2011-08-02 18:50:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace drag_download_util
|