2010-10-16 21:10:11 +02:00
|
|
|
// Copyright (c) 2010 The Chromium Embedded Framework Authors. All rights
|
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
// can be found in the LICENSE file.
|
|
|
|
|
2011-01-29 23:52:34 +01:00
|
|
|
#include "include/cef_runnable.h"
|
2010-10-16 21:10:11 +02:00
|
|
|
#include "download_handler.h"
|
|
|
|
#include "util.h"
|
2010-10-28 22:38:27 +02:00
|
|
|
#include <sstream>
|
2010-12-07 02:30:26 +01:00
|
|
|
#include <stdio.h>
|
2010-10-28 22:38:27 +02:00
|
|
|
#include <vector>
|
|
|
|
|
2011-05-20 16:42:25 +02:00
|
|
|
#if defined(OS_WIN)
|
2010-10-16 21:10:11 +02:00
|
|
|
#include <windows.h>
|
|
|
|
#include <shlobj.h>
|
|
|
|
#include <shlwapi.h>
|
2011-05-20 16:42:25 +02:00
|
|
|
#endif // OS_WIN
|
2010-10-28 22:38:27 +02:00
|
|
|
|
2010-10-16 21:10:11 +02:00
|
|
|
// Implementation of the CefDownloadHandler interface.
|
2011-05-20 16:42:25 +02:00
|
|
|
class ClientDownloadHandler : public CefDownloadHandler
|
2010-10-16 21:10:11 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
ClientDownloadHandler(CefRefPtr<DownloadListener> listener,
|
2010-11-22 18:49:46 +01:00
|
|
|
const CefString& fileName)
|
2010-10-16 21:10:11 +02:00
|
|
|
: listener_(listener), filename_(fileName), file_(NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~ClientDownloadHandler()
|
|
|
|
{
|
|
|
|
ASSERT(pending_data_.empty());
|
|
|
|
ASSERT(file_ == NULL);
|
|
|
|
|
|
|
|
if(!pending_data_.empty()) {
|
|
|
|
// Delete remaining pending data.
|
|
|
|
std::vector<std::vector<char>*>::iterator it = pending_data_.begin();
|
|
|
|
for(; it != pending_data_.end(); ++it)
|
|
|
|
delete (*it);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(file_) {
|
|
|
|
// Close the dangling file pointer on the FILE thread.
|
2011-01-29 23:52:34 +01:00
|
|
|
CefPostTask(TID_FILE,
|
|
|
|
NewCefRunnableFunction(&ClientDownloadHandler::CloseDanglingFile,
|
|
|
|
file_));
|
2010-10-16 21:10:11 +02:00
|
|
|
|
|
|
|
// Notify the listener that the download failed.
|
|
|
|
listener_->NotifyDownloadError(filename_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------
|
|
|
|
// The following methods are called on the UI thread.
|
|
|
|
// --------------------------------------------------
|
|
|
|
|
2011-01-29 23:52:34 +01:00
|
|
|
void Initialize()
|
|
|
|
{
|
|
|
|
// Open the file on the FILE thread.
|
|
|
|
CefPostTask(TID_FILE,
|
|
|
|
NewCefRunnableMethod(this, &ClientDownloadHandler::OnOpen));
|
|
|
|
}
|
|
|
|
|
2010-10-16 21:10:11 +02:00
|
|
|
// A portion of the file contents have been received. This method will be
|
|
|
|
// called multiple times until the download is complete. Return |true| to
|
|
|
|
// continue receiving data and |false| to cancel.
|
|
|
|
virtual bool ReceivedData(void* data, int data_size)
|
|
|
|
{
|
2011-01-29 02:42:59 +01:00
|
|
|
REQUIRE_UI_THREAD();
|
2010-10-16 21:10:11 +02:00
|
|
|
|
|
|
|
if(data_size == 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Create a new vector for the data.
|
|
|
|
std::vector<char>* buffer = new std::vector<char>(data_size);
|
|
|
|
memcpy(&(*buffer)[0], data, data_size);
|
|
|
|
|
|
|
|
// Add the new data vector to the pending data queue.
|
2011-05-20 16:42:25 +02:00
|
|
|
{
|
|
|
|
AutoLock lock_scope(this);
|
|
|
|
pending_data_.push_back(buffer);
|
|
|
|
}
|
2010-10-16 21:10:11 +02:00
|
|
|
|
|
|
|
// Write data to file on the FILE thread.
|
2011-01-29 23:52:34 +01:00
|
|
|
CefPostTask(TID_FILE,
|
|
|
|
NewCefRunnableMethod(this, &ClientDownloadHandler::OnReceivedData));
|
2010-10-16 21:10:11 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The download is complete.
|
|
|
|
virtual void Complete()
|
|
|
|
{
|
2011-01-29 02:42:59 +01:00
|
|
|
REQUIRE_UI_THREAD();
|
2010-10-16 21:10:11 +02:00
|
|
|
|
|
|
|
// Flush and close the file on the FILE thread.
|
2011-01-29 23:52:34 +01:00
|
|
|
CefPostTask(TID_FILE,
|
|
|
|
NewCefRunnableMethod(this, &ClientDownloadHandler::OnComplete));
|
2010-10-16 21:10:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------
|
|
|
|
// The following methods are called on the FILE thread.
|
|
|
|
// ----------------------------------------------------
|
|
|
|
|
|
|
|
void OnOpen()
|
|
|
|
{
|
2011-01-29 02:42:59 +01:00
|
|
|
REQUIRE_FILE_THREAD();
|
2010-10-16 21:10:11 +02:00
|
|
|
|
|
|
|
if(file_)
|
|
|
|
return;
|
|
|
|
|
2011-05-20 16:42:25 +02:00
|
|
|
#if defined(OS_WIN)
|
2010-10-16 21:10:11 +02:00
|
|
|
TCHAR szFolderPath[MAX_PATH];
|
|
|
|
|
|
|
|
// Save the file in the user's "My Documents" folder.
|
|
|
|
if(SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL|CSIDL_FLAG_CREATE,
|
|
|
|
NULL, 0, szFolderPath))) {
|
2010-11-22 18:49:46 +01:00
|
|
|
std::wstring fileNameStr = filename_;
|
|
|
|
LPWSTR name = PathFindFileName(fileNameStr.c_str());
|
|
|
|
LPWSTR ext = PathFindExtension(fileNameStr.c_str());
|
2010-10-16 21:10:11 +02:00
|
|
|
int ct = 0;
|
|
|
|
std::wstringstream ss;
|
|
|
|
|
|
|
|
if(ext) {
|
|
|
|
name[ext-name] = 0;
|
|
|
|
ext++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the file name is unique.
|
|
|
|
do {
|
|
|
|
if(ct > 0)
|
|
|
|
ss.str(L"");
|
|
|
|
ss << szFolderPath << L"\\" << name;
|
|
|
|
if(ct > 0)
|
|
|
|
ss << L" (" << ct << L")";
|
|
|
|
if(ext)
|
|
|
|
ss << L"." << ext;
|
|
|
|
ct++;
|
|
|
|
} while(PathFileExists(ss.str().c_str()));
|
|
|
|
|
2011-05-20 16:42:25 +02:00
|
|
|
{
|
|
|
|
AutoLock lock_scope(this);
|
|
|
|
filename_ = ss.str();
|
|
|
|
}
|
2010-10-16 21:10:11 +02:00
|
|
|
|
|
|
|
file_ = _wfopen(ss.str().c_str(), L"wb");
|
|
|
|
ASSERT(file_ != NULL);
|
|
|
|
}
|
2010-11-15 16:39:56 +01:00
|
|
|
#else
|
|
|
|
// TODO(port): Implement this.
|
|
|
|
ASSERT(false); // Not implemented
|
|
|
|
#endif
|
2010-10-16 21:10:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void OnComplete()
|
|
|
|
{
|
2011-01-29 02:42:59 +01:00
|
|
|
REQUIRE_FILE_THREAD();
|
2010-10-16 21:10:11 +02:00
|
|
|
|
|
|
|
if(!file_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Make sure any pending data is written.
|
|
|
|
OnReceivedData();
|
|
|
|
|
|
|
|
fclose(file_);
|
|
|
|
file_ = NULL;
|
|
|
|
|
|
|
|
// Notify the listener that the download completed.
|
|
|
|
listener_->NotifyDownloadComplete(filename_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnReceivedData()
|
|
|
|
{
|
2011-01-29 02:42:59 +01:00
|
|
|
REQUIRE_FILE_THREAD();
|
2010-10-16 21:10:11 +02:00
|
|
|
|
|
|
|
std::vector<std::vector<char>*> data;
|
|
|
|
|
|
|
|
// Remove all data from the pending data queue.
|
2011-05-20 16:42:25 +02:00
|
|
|
{
|
|
|
|
AutoLock lock_scope(this);
|
|
|
|
if(!pending_data_.empty()) {
|
|
|
|
data = pending_data_;
|
|
|
|
pending_data_.clear();
|
|
|
|
}
|
2010-10-16 21:10:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(data.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Write all pending data to file.
|
|
|
|
std::vector<std::vector<char>*>::iterator it = data.begin();
|
|
|
|
for(; it != data.end(); ++it) {
|
|
|
|
std::vector<char>* buffer = *it;
|
|
|
|
if(file_)
|
|
|
|
fwrite(&(*buffer)[0], buffer->size(), 1, file_);
|
|
|
|
delete buffer;
|
|
|
|
}
|
|
|
|
data.clear();
|
|
|
|
}
|
|
|
|
|
2011-01-29 23:52:34 +01:00
|
|
|
static void CloseDanglingFile(FILE *file)
|
|
|
|
{
|
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
|
2010-10-16 21:10:11 +02:00
|
|
|
private:
|
|
|
|
CefRefPtr<DownloadListener> listener_;
|
2010-11-22 18:49:46 +01:00
|
|
|
CefString filename_;
|
2010-10-16 21:10:11 +02:00
|
|
|
FILE* file_;
|
|
|
|
std::vector<std::vector<char>*> pending_data_;
|
2011-05-20 16:42:25 +02:00
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(ClientDownloadHandler);
|
|
|
|
IMPLEMENT_LOCKING(ClientDownloadHandler);
|
2010-10-16 21:10:11 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
CefRefPtr<CefDownloadHandler> CreateDownloadHandler(
|
2010-11-22 18:49:46 +01:00
|
|
|
CefRefPtr<DownloadListener> listener, const CefString& fileName)
|
2010-10-16 21:10:11 +02:00
|
|
|
{
|
2011-01-29 23:52:34 +01:00
|
|
|
CefRefPtr<ClientDownloadHandler> handler =
|
|
|
|
new ClientDownloadHandler(listener, fileName);
|
|
|
|
handler->Initialize();
|
|
|
|
return handler.get();
|
2010-10-16 21:10:11 +02:00
|
|
|
}
|