From a782ae94df38fbad737b77bb76f8831a23feb889 Mon Sep 17 00:00:00 2001 From: Marshall Greenblatt Date: Mon, 11 Jun 2012 17:35:23 +0000 Subject: [PATCH] - 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 --- cef.gyp | 2 + libcef/browser/browser_context.cc | 3 ++ libcef/browser/download_manager_delegate.cc | 29 ++++--------- libcef/browser/download_manager_delegate.h | 4 ++ .../browser/download_manager_delegate_mac.mm | 41 ++++++++++++++++++ .../browser/download_manager_delegate_win.cc | 42 +++++++++++++++++++ tests/cefclient/client_handler.cpp | 4 ++ 7 files changed, 104 insertions(+), 21 deletions(-) create mode 100644 libcef/browser/download_manager_delegate_mac.mm create mode 100644 libcef/browser/download_manager_delegate_win.cc diff --git a/cef.gyp b/cef.gyp index a1db7b114..428731490 100644 --- a/cef.gyp +++ b/cef.gyp @@ -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', diff --git a/libcef/browser/browser_context.cc b/libcef/browser/browser_context.cc index 4a89aaa56..e4757f130 100644 --- a/libcef/browser/browser_context.cc +++ b/libcef/browser/browser_context.cc @@ -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()); diff --git a/libcef/browser/download_manager_delegate.cc b/libcef/browser/download_manager_delegate.cc index 128f4c7b5..5e4dc0ef3 100644 --- a/libcef/browser/download_manager_delegate.cc +++ b/libcef/browser/download_manager_delegate.cc @@ -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) { diff --git a/libcef/browser/download_manager_delegate.h b/libcef/browser/download_manager_delegate.h index 7fa278555..b647daffe 100644 --- a/libcef/browser/download_manager_delegate.h +++ b/libcef/browser/download_manager_delegate.h @@ -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; @@ -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); diff --git a/libcef/browser/download_manager_delegate_mac.mm b/libcef/browser/download_manager_delegate_mac.mm new file mode 100644 index 000000000..1e6546329 --- /dev/null +++ b/libcef/browser/download_manager_delegate_mac.mm @@ -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 + +#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; +} diff --git a/libcef/browser/download_manager_delegate_win.cc b/libcef/browser/download_manager_delegate_win.cc new file mode 100644 index 000000000..85db4e6f3 --- /dev/null +++ b/libcef/browser/download_manager_delegate_win.cc @@ -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 +#include + +#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; +} diff --git a/tests/cefclient/client_handler.cpp b/tests/cefclient/client_handler.cpp index 4656af20b..5eb86b567 100644 --- a/tests/cefclient/client_handler.cpp +++ b/tests/cefclient/client_handler.cpp @@ -270,6 +270,10 @@ void ClientHandler::OnLoadError(CefRefPtr 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 << "

Failed to load URL " << std::string(failedUrl) <<