- Mac: Add CefDownloadManagerDelegate::ChooseDownloadPath implementation (issue #634).

- Persist downloaded files after CEF exits (issue #634).
- Shutdown the DownloadManager when CEF exits.
- Don't show an error message when downloading files with cefclient.

git-svn-id: https://chromiumembedded.googlecode.com/svn/trunk@682 5089003a-bbd8-11dd-ad1f-f1f9622dbc98
This commit is contained in:
Marshall Greenblatt 2012-06-11 17:35:23 +00:00
parent f79d18d510
commit a782ae94df
7 changed files with 104 additions and 21 deletions

View File

@ -875,6 +875,7 @@
'<@(includes_win)',
'libcef/browser/browser_host_impl_win.cc',
'libcef/browser/browser_main_win.cc',
'libcef/browser/download_manager_delegate_win.cc',
'libcef/browser/javascript_dialog_win.cc',
'libcef/browser/menu_creator_runner_win.cc',
'libcef/browser/menu_creator_runner_win.h',
@ -897,6 +898,7 @@
'libcef/browser/application_mac.mm',
'libcef/browser/browser_host_impl_mac.mm',
'libcef/browser/browser_main_mac.mm',
'libcef/browser/download_manager_delegate_mac.mm',
'libcef/browser/javascript_dialog_mac.mm',
'libcef/browser/menu_creator_runner_mac.h',
'libcef/browser/menu_creator_runner_mac.mm',

View File

@ -200,6 +200,9 @@ CefBrowserContext::CefBrowserContext() {
}
CefBrowserContext::~CefBrowserContext() {
if (download_manager_.get())
download_manager_->Shutdown();
if (resource_context_.get()) {
BrowserThread::DeleteSoon(
BrowserThread::IO, FROM_HERE, resource_context_.release());

View File

@ -76,27 +76,8 @@ void CefDownloadManagerDelegate::ChooseDownloadPath(
const FilePath& suggested_path,
int32 download_id) {
FilePath result;
#if defined(OS_WIN) && !defined(USE_AURA)
std::wstring file_part = FilePath(suggested_path).BaseName().value();
wchar_t file_name[MAX_PATH];
base::wcslcpy(file_name, file_part.c_str(), arraysize(file_name));
OPENFILENAME save_as;
ZeroMemory(&save_as, sizeof(save_as));
save_as.lStructSize = sizeof(OPENFILENAME);
save_as.hwndOwner = web_contents->GetNativeView();
save_as.lpstrFile = file_name;
save_as.nMaxFile = arraysize(file_name);
std::wstring directory;
if (!suggested_path.empty())
directory = suggested_path.DirName().value();
save_as.lpstrInitialDir = directory.c_str();
save_as.Flags = OFN_OVERWRITEPROMPT | OFN_EXPLORER | OFN_ENABLESIZING |
OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST;
if (GetSaveFileName(&save_as))
result = FilePath(std::wstring(save_as.lpstrFile));
#if defined(OS_WIN) || defined(OS_MACOSX)
result = PlatformChooseDownloadPath(web_contents, suggested_path);
#else
NOTIMPLEMENTED();
#endif
@ -108,6 +89,12 @@ void CefDownloadManagerDelegate::ChooseDownloadPath(
}
}
void CefDownloadManagerDelegate::AddItemToPersistentStore(
content::DownloadItem* item) {
static int next_id;
download_manager_->OnItemAddedToPersistentStore(item->GetId(), ++next_id);
}
void CefDownloadManagerDelegate::GenerateFilename(
int32 download_id,
const FilePath& generated_name) {

View File

@ -30,6 +30,7 @@ class CefDownloadManagerDelegate
virtual void ChooseDownloadPath(content::WebContents* web_contents,
const FilePath& suggested_path,
int32 download_id) OVERRIDE;
virtual void AddItemToPersistentStore(content::DownloadItem* item) OVERRIDE;
private:
friend class base::RefCountedThreadSafe<CefDownloadManagerDelegate>;
@ -41,6 +42,9 @@ class CefDownloadManagerDelegate
void RestartDownload(int32 download_id,
const FilePath& suggested_path);
FilePath PlatformChooseDownloadPath(content::WebContents* web_contents,
const FilePath& suggested_path);
content::DownloadManager* download_manager_;
DISALLOW_COPY_AND_ASSIGN(CefDownloadManagerDelegate);

View File

@ -0,0 +1,41 @@
// Copyright (c) 2012 The Chromium Embedded Framework Authors.
// Portions 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.
#include "libcef/browser/download_manager_delegate.h"
#import <Cocoa/Cocoa.h>
#include "base/sys_string_conversions.h"
#include "content/public/browser/web_contents.h"
FilePath CefDownloadManagerDelegate::PlatformChooseDownloadPath(
content::WebContents* web_contents,
const FilePath& suggested_path) {
FilePath result;
NSSavePanel* savePanel = [NSSavePanel savePanel];
if (!suggested_path.BaseName().empty()) {
NSString* defaultName = base::SysUTF8ToNSString(
suggested_path.BaseName().value());
[savePanel setNameFieldStringValue:defaultName];
}
if (!suggested_path.DirName().empty()) {
NSString* defaultDir = base::SysUTF8ToNSString(
suggested_path.DirName().value());
[savePanel setDirectoryURL:[NSURL fileURLWithPath:defaultDir]];
}
NSView* view = web_contents->GetNativeView();
[savePanel beginSheetModalForWindow:[view window] completionHandler:nil];
if ([savePanel runModal] == NSFileHandlingPanelOKButton) {
NSURL * url = [savePanel URL];
NSString* path = [url path];
result = FilePath([path UTF8String]);
}
[NSApp endSheet:savePanel];
return result;
}

View File

@ -0,0 +1,42 @@
// Copyright (c) 2012 The Chromium Embedded Framework Authors.
// Portions 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.
#include "libcef/browser/download_manager_delegate.h"
#include <commdlg.h>
#include <windows.h>
#include "base/string_util.h"
#include "content/public/browser/web_contents.h"
FilePath CefDownloadManagerDelegate::PlatformChooseDownloadPath(
content::WebContents* web_contents,
const FilePath& suggested_path) {
FilePath result;
std::wstring file_part = FilePath(suggested_path).BaseName().value();
wchar_t file_name[MAX_PATH];
base::wcslcpy(file_name, file_part.c_str(), arraysize(file_name));
OPENFILENAME save_as;
ZeroMemory(&save_as, sizeof(save_as));
save_as.lStructSize = sizeof(OPENFILENAME);
save_as.hwndOwner = web_contents->GetNativeView();
save_as.lpstrFile = file_name;
save_as.nMaxFile = arraysize(file_name);
std::wstring directory;
if (!suggested_path.empty())
directory = suggested_path.DirName().value();
save_as.lpstrInitialDir = directory.c_str();
save_as.Flags = OFN_OVERWRITEPROMPT | OFN_EXPLORER | OFN_ENABLESIZING |
OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST;
if (GetSaveFileName(&save_as))
result = FilePath(std::wstring(save_as.lpstrFile));
return result;
}

View File

@ -270,6 +270,10 @@ void ClientHandler::OnLoadError(CefRefPtr<CefBrowser> browser,
const CefString& failedUrl) {
REQUIRE_UI_THREAD();
// Don't display an error for downloaded files.
if (errorCode == ERR_ABORTED)
return;
// Display a load error message.
std::stringstream ss;
ss << "<html><body><h2>Failed to load URL " << std::string(failedUrl) <<