diff options
author | estade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-24 18:33:49 +0000 |
---|---|---|
committer | estade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-24 18:33:49 +0000 |
commit | b0f16b6c2d73f55512dade4545d448daa9bb57c0 (patch) | |
tree | 57dec9ab1d0a4026b60612383fadb28eb46aabf5 /chrome/browser | |
parent | 143cb01067b26440ad710a9db8b24982f0196c47 (diff) | |
download | chromium_src-b0f16b6c2d73f55512dade4545d448daa9bb57c0.zip chromium_src-b0f16b6c2d73f55512dade4545d448daa9bb57c0.tar.gz chromium_src-b0f16b6c2d73f55512dade4545d448daa9bb57c0.tar.bz2 |
Download request manager refactoring.
Pull out the dialog delegate to a separate file so we can swap it out with other implementations on other platforms.
Also, build download_request_manager.cc on POSIX.
Review URL: http://codereview.chromium.org/27062
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@10270 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser')
9 files changed, 275 insertions, 192 deletions
diff --git a/chrome/browser/browser.scons b/chrome/browser/browser.scons index c1e1dc4..61a3d3a 100644 --- a/chrome/browser/browser.scons +++ b/chrome/browser/browser.scons @@ -479,6 +479,7 @@ input_files = ChromeFileList([ 'download/download_item_model.h', 'download/download_manager.cc', 'download/download_manager.h', + 'download/download_request_dialog_delegate.h', 'download/download_request_manager.cc', 'download/download_request_manager.h', 'download/download_util.cc', @@ -684,7 +685,6 @@ if not env.Bit('windows'): 'dom_ui/new_tab_ui.cc', 'download/download_exe.cc', 'download/download_util.cc', - 'download/download_request_manager.cc', 'drag_utils.cc', 'encoding_menu_controller_delegate.cc', 'external_protocol_handler.cc', diff --git a/chrome/browser/browser.vcproj b/chrome/browser/browser.vcproj index 1636017..e8ee21e 100644 --- a/chrome/browser/browser.vcproj +++ b/chrome/browser/browser.vcproj @@ -1798,6 +1798,18 @@ > </File> <File + RelativePath=".\download\download_request_dialog_delegate.h" + > + </File> + <File + RelativePath=".\download\download_request_dialog_delegate_win.cc" + > + </File> + <File + RelativePath=".\download\download_request_dialog_delegate_win.h" + > + </File> + <File RelativePath=".\download\download_request_manager.cc" > </File> diff --git a/chrome/browser/download/download_request_dialog_delegate.h b/chrome/browser/download/download_request_dialog_delegate.h new file mode 100644 index 0000000..034107a --- /dev/null +++ b/chrome/browser/download/download_request_dialog_delegate.h @@ -0,0 +1,58 @@ +// Copyright (c) 2006-2009 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. + +#ifndef CHROME_BROWSER_DOWNLOAD_DOWNLOAD_REQUEST_DIALOG_DELEGATE_H_ +#define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_REQUEST_DIALOG_DELEGATE_H_ + +#include "base/basictypes.h" + +#include "chrome/browser/download/download_request_manager.h" + +// DownloadRequestDialogDelegate is the dialog implementation used to prompt the +// the user as to whether they want to allow multiple downloads. +// DownloadRequestDialogDelegate delegates the allow/cancel methods to the +// TabDownloadState. +// +// TabDownloadState does not directly act as a dialog delegate because +// the dialog may outlive the TabDownloadState object. +class DownloadRequestDialogDelegate { + public: + // This factory method constructs a DownloadRequestDialogDelegate in a + // platform-specific way. + static DownloadRequestDialogDelegate* Create(TabContents* tab, + DownloadRequestManager::TabDownloadState* host); + + void set_host(DownloadRequestManager::TabDownloadState* host) { + host_ = host; + } + + // Closes the prompt. + virtual void CloseWindow() = 0; + + protected: + explicit DownloadRequestDialogDelegate( + DownloadRequestManager::TabDownloadState* host) : host_(host) { } + + virtual ~DownloadRequestDialogDelegate() { } + + virtual bool DoCancel() { + if (host_) + host_->Cancel(); + return true; + } + + virtual bool DoAccept() { + if (host_) + host_->Accept(); + return true; + } + + // The TabDownloadState we're displaying the dialog for. May be null. + DownloadRequestManager::TabDownloadState* host_; + + DISALLOW_COPY_AND_ASSIGN(DownloadRequestDialogDelegate); +}; + +#endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_REQUEST_DIALOG_DELEGATE_H_ + diff --git a/chrome/browser/download/download_request_dialog_delegate_win.cc b/chrome/browser/download/download_request_dialog_delegate_win.cc new file mode 100644 index 0000000..62d23d5 --- /dev/null +++ b/chrome/browser/download/download_request_dialog_delegate_win.cc @@ -0,0 +1,60 @@ +// Copyright (c) 2006-2009 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 "chrome/browser/download/download_request_dialog_delegate_win.h" + +#include "chrome/browser/tab_contents/constrained_window.h" +#include "chrome/browser/tab_contents/tab_contents.h" +#include "chrome/common/l10n_util.h" +#include "chrome/views/message_box_view.h" +#include "grit/generated_resources.h" + +// static +DownloadRequestDialogDelegate* DownloadRequestDialogDelegate::Create( + TabContents* tab, + DownloadRequestManager::TabDownloadState* host) { + return new DownloadRequestDialogDelegateWin(tab, host); +} + +DownloadRequestDialogDelegateWin::DownloadRequestDialogDelegateWin( + TabContents* tab, + DownloadRequestManager::TabDownloadState* host) + : DownloadRequestDialogDelegate(host) { + message_view_ = new MessageBoxView( + MessageBoxView::kIsConfirmMessageBox, + l10n_util::GetString(IDS_MULTI_DOWNLOAD_WARNING), + std::wstring()); + window_ = tab->CreateConstrainedDialog(this, message_view_); +} + +void DownloadRequestDialogDelegateWin::CloseWindow() { + window_->CloseConstrainedWindow(); +} + +bool DownloadRequestDialogDelegateWin::Cancel() { + return DoCancel(); +} + +bool DownloadRequestDialogDelegateWin::Accept() { + return DoAccept(); +} + +views::View* DownloadRequestDialogDelegateWin::GetContentsView() { + return message_view_; +} + +std::wstring DownloadRequestDialogDelegateWin::GetDialogButtonLabel( + DialogButton button) const { + if (button == DIALOGBUTTON_OK) + return l10n_util::GetString(IDS_MULTI_DOWNLOAD_WARNING_ALLOW); + if (button == DIALOGBUTTON_CANCEL) + return l10n_util::GetString(IDS_MULTI_DOWNLOAD_WARNING_DENY); + return std::wstring(); +} + +void DownloadRequestDialogDelegateWin::WindowClosing() { + DCHECK(!host_); + delete this; +} + diff --git a/chrome/browser/download/download_request_dialog_delegate_win.h b/chrome/browser/download/download_request_dialog_delegate_win.h new file mode 100644 index 0000000..b901483 --- /dev/null +++ b/chrome/browser/download/download_request_dialog_delegate_win.h @@ -0,0 +1,52 @@ +// Copyright (c) 2006-2009 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. + +#ifndef CHROME_BROWSER_DOWNLOAD_DOWNLOAD_REQUEST_DIALOG_DELEGATE_WIN_H_ +#define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_REQUEST_DIALOG_DELEGATE_WIN_H_ + +#include "base/basictypes.h" + +#include "chrome/browser/download/download_request_dialog_delegate.h" +#include "chrome/browser/download/download_request_manager.h" +#include "chrome/views/dialog_delegate.h" + +class ConstrainedWindow; +class MessageBoxView; +class TabContents; + +class DownloadRequestDialogDelegateWin : public DownloadRequestDialogDelegate, + public views::DialogDelegate { + public: + DownloadRequestDialogDelegateWin(TabContents* tab, + DownloadRequestManager::TabDownloadState* host); + + void set_host(DownloadRequestManager::TabDownloadState* host) { + host_ = host; + } + + private: + // DownloadRequestDialogDelegate methods. + virtual void CloseWindow(); + // DialogDelegate methods. + virtual bool Cancel(); + virtual bool Accept(); + virtual views::View* GetContentsView(); + virtual std::wstring GetDialogButtonLabel(DialogButton button) const; + virtual int GetDefaultDialogButton() const { + return DIALOGBUTTON_CANCEL; + } + virtual void WindowClosing(); + + // The TabDownloadState we're displaying the dialog for. May be null. + DownloadRequestManager::TabDownloadState* host_; + + MessageBoxView* message_view_; + + ConstrainedWindow* window_; + + DISALLOW_COPY_AND_ASSIGN(DownloadRequestDialogDelegateWin); +}; + +#endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_REQUEST_DIALOG_DELEGATE_WIN_H_ + diff --git a/chrome/browser/download/download_request_manager.cc b/chrome/browser/download/download_request_manager.cc index 55b48fe..c124af3 100644 --- a/chrome/browser/download/download_request_manager.cc +++ b/chrome/browser/download/download_request_manager.cc @@ -6,151 +6,16 @@ #include "base/message_loop.h" #include "base/thread.h" -#include "chrome/browser/tab_contents/constrained_window.h" +#include "chrome/browser/download/download_request_dialog_delegate.h" #include "chrome/browser/tab_contents/navigation_controller.h" #include "chrome/browser/tab_contents/navigation_entry.h" #include "chrome/browser/tab_contents/tab_contents_delegate.h" #include "chrome/browser/tab_contents/tab_util.h" #include "chrome/browser/tab_contents/web_contents.h" -#include "chrome/common/l10n_util.h" -#include "chrome/common/notification_registrar.h" #include "chrome/common/notification_service.h" -#include "chrome/views/dialog_delegate.h" -#include "chrome/views/message_box_view.h" -#include "grit/generated_resources.h" - -namespace { - -// DialogDelegateImpl ---------------------------------------------------------- - -// DialogDelegateImpl is the DialogDelegate implementation used to prompt the -// the user as to whether they want to allow multiple downloads. -// DialogDelegateImpl delegates the allow/cancel methods to the -// TabDownloadState. -// -// TabDownloadState does not directly implement DialogDelegate, rather it is -// split into DialogDelegateImpl as TabDownloadState may be deleted before -// the dialog. - -class DialogDelegateImpl : public views::DialogDelegate { - public: - DialogDelegateImpl(TabContents* tab, - DownloadRequestManager::TabDownloadState* host); - - void set_host(DownloadRequestManager::TabDownloadState* host) { - host_ = host; - } - - // Closes the prompt. - void CloseWindow(); - - private: - // DialogDelegate methods; - virtual bool Cancel(); - virtual bool Accept(); - virtual views::View* GetContentsView() { return message_view_; } - virtual std::wstring GetDialogButtonLabel(DialogButton button) const; - virtual int GetDefaultDialogButton() const { - return DIALOGBUTTON_CANCEL; - } - virtual void WindowClosing(); - - // The TabDownloadState we're displaying the dialog for. May be null. - DownloadRequestManager::TabDownloadState* host_; - - MessageBoxView* message_view_; - - ConstrainedWindow* window_; - - DISALLOW_COPY_AND_ASSIGN(DialogDelegateImpl); -}; - -} // namespace // TabDownloadState ------------------------------------------------------------ -// TabDownloadState maintains the download state for a particular tab. -// TabDownloadState installs observers to update the download status -// appropriately. Additionally TabDownloadState prompts the user as necessary. -// TabDownloadState deletes itself (by invoking DownloadRequestManager::Remove) -// as necessary. - -class DownloadRequestManager::TabDownloadState : public NotificationObserver { - public: - // Creates a new TabDownloadState. |controller| is the controller the - // TabDownloadState tracks the state of and is the host for any dialogs that - // are displayed. |originating_controller| is used to determine the host of - // the initial download. If |originating_controller| is null, |controller| is - // used. |originating_controller| is typically null, but differs from - // |controller| in the case of a constrained popup requesting the download. - TabDownloadState(DownloadRequestManager* host, - NavigationController* controller, - NavigationController* originating_controller); - ~TabDownloadState(); - - // Status of the download. - void set_download_status(DownloadRequestManager::DownloadStatus status) { - status_ = status; - } - DownloadRequestManager::DownloadStatus download_status() const { - return status_; - } - - // Invoked when a user gesture occurs (mouse click, enter or space). This - // may result in invoking Remove on DownloadRequestManager. - void OnUserGesture(); - - // Asks the user if they really want to allow the download. - // See description above CanDownloadOnIOThread for details on lifetime of - // callback. - void PromptUserForDownload(TabContents* tab, - DownloadRequestManager::Callback* callback); - - // Are we showing a prompt to the user? - bool is_showing_prompt() const { return (dialog_delegate_ != NULL); } - - // NavigationController we're tracking. - NavigationController* controller() const { return controller_; } - - // Invoked from DialogDelegateImpl. Notifies the delegates and changes the - // status appropriately. - void Cancel(); - void Accept(); - - private: - // NotificationObserver method. - void Observe(NotificationType type, - const NotificationSource& source, - const NotificationDetails& details); - - // Notifies the callbacks as to whether the download is allowed or not. - // Updates status_ appropriately. - void NotifyCallbacks(bool allow); - - DownloadRequestManager* host_; - - NavigationController* controller_; - - // Host of the first page the download started on. This may be empty. - std::string initial_page_host_; - - DownloadRequestManager::DownloadStatus status_; - - // Callbacks we need to notify. This is only non-empty if we're showing a - // dialog. - // See description above CanDownloadOnIOThread for details on lifetime of - // callbacks. - std::vector<DownloadRequestManager::Callback*> callbacks_; - - // Used to remove observers installed on NavigationController. - NotificationRegistrar registrar_; - - // Handles showing the dialog to the user, may be null. - DialogDelegateImpl* dialog_delegate_; - - DISALLOW_COPY_AND_ASSIGN(TabDownloadState); -}; - DownloadRequestManager::TabDownloadState::TabDownloadState( DownloadRequestManager* host, NavigationController* controller, @@ -204,7 +69,7 @@ void DownloadRequestManager::TabDownloadState::PromptUserForDownload( if (DownloadRequestManager::delegate_) NotifyCallbacks(DownloadRequestManager::delegate_->ShouldAllowDownload()); else - dialog_delegate_ = new DialogDelegateImpl(tab, this); + dialog_delegate_ = DownloadRequestDialogDelegate::Create(tab, this); } void DownloadRequestManager::TabDownloadState::Cancel() { @@ -294,47 +159,6 @@ namespace { // DialogDelegateImpl ---------------------------------------------------------- -DialogDelegateImpl::DialogDelegateImpl( - TabContents* tab, - DownloadRequestManager::TabDownloadState* host) - : host_(host) { - message_view_ = new MessageBoxView( - MessageBoxView::kIsConfirmMessageBox, - l10n_util::GetString(IDS_MULTI_DOWNLOAD_WARNING), - std::wstring()); - window_ = tab->CreateConstrainedDialog(this, message_view_); -} - -void DialogDelegateImpl::CloseWindow() { - window_->CloseConstrainedWindow(); -} - -bool DialogDelegateImpl::Cancel() { - if (host_) - host_->Cancel(); - return true; -} - -bool DialogDelegateImpl::Accept() { - if (host_) - host_->Accept(); - return true; -} - -std::wstring DialogDelegateImpl::GetDialogButtonLabel( - DialogButton button) const { - if (button == DIALOGBUTTON_OK) - return l10n_util::GetString(IDS_MULTI_DOWNLOAD_WARNING_ALLOW); - if (button == DIALOGBUTTON_CANCEL) - return l10n_util::GetString(IDS_MULTI_DOWNLOAD_WARNING_DENY); - return std::wstring(); -} - -void DialogDelegateImpl::WindowClosing() { - DCHECK(!host_); - delete this; -} - } // namespace // DownloadRequestManager ------------------------------------------------------ diff --git a/chrome/browser/download/download_request_manager.h b/chrome/browser/download/download_request_manager.h index 90180f8..f9351c8 100644 --- a/chrome/browser/download/download_request_manager.h +++ b/chrome/browser/download/download_request_manager.h @@ -6,10 +6,14 @@ #define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_REQUEST_MANAGER_H_ #include <map> +#include <string> #include <vector> #include "base/ref_counted.h" +#include "chrome/common/notification_observer.h" +#include "chrome/common/notification_registrar.h" +class DownloadRequestDialogDelegate; class MessageLoop; class NavigationController; class TabContents; @@ -40,8 +44,6 @@ class TabContents; class DownloadRequestManager : public base::RefCountedThreadSafe<DownloadRequestManager> { public: - class TabDownloadState; - // Download status for a particular page. See class description for details. enum DownloadStatus { ALLOW_ONE_DOWNLOAD, @@ -50,9 +52,6 @@ class DownloadRequestManager : DOWNLOADS_NOT_ALLOWED }; - DownloadRequestManager(MessageLoop* io_loop, MessageLoop* ui_loop); - ~DownloadRequestManager(); - // The callback from CanDownloadOnIOThread. This is invoked on the io thread. class Callback { public: @@ -60,6 +59,90 @@ class DownloadRequestManager : virtual void CancelDownload() = 0; }; + // TabDownloadState maintains the download state for a particular tab. + // TabDownloadState installs observers to update the download status + // appropriately. Additionally TabDownloadState prompts the user as necessary. + // TabDownloadState deletes itself (by invoking + // DownloadRequestManager::Remove) as necessary. + class TabDownloadState : public NotificationObserver { + public: + // Creates a new TabDownloadState. |controller| is the controller the + // TabDownloadState tracks the state of and is the host for any dialogs that + // are displayed. |originating_controller| is used to determine the host of + // the initial download. If |originating_controller| is null, |controller| + // is used. |originating_controller| is typically null, but differs from + // |controller| in the case of a constrained popup requesting the download. + TabDownloadState(DownloadRequestManager* host, + NavigationController* controller, + NavigationController* originating_controller); + ~TabDownloadState(); + + // Status of the download. + void set_download_status(DownloadRequestManager::DownloadStatus status) { + status_ = status; + } + DownloadRequestManager::DownloadStatus download_status() const { + return status_; + } + + // Invoked when a user gesture occurs (mouse click, enter or space). This + // may result in invoking Remove on DownloadRequestManager. + void OnUserGesture(); + + // Asks the user if they really want to allow the download. + // See description above CanDownloadOnIOThread for details on lifetime of + // callback. + void PromptUserForDownload(TabContents* tab, + DownloadRequestManager::Callback* callback); + + // Are we showing a prompt to the user? + bool is_showing_prompt() const { return (dialog_delegate_ != NULL); } + + // NavigationController we're tracking. + NavigationController* controller() const { return controller_; } + + // Invoked from DownloadRequestDialogDelegate. Notifies the delegates and + // changes the status appropriately. + void Cancel(); + void Accept(); + + private: + // NotificationObserver method. + void Observe(NotificationType type, + const NotificationSource& source, + const NotificationDetails& details); + + // Notifies the callbacks as to whether the download is allowed or not. + // Updates status_ appropriately. + void NotifyCallbacks(bool allow); + + DownloadRequestManager* host_; + + NavigationController* controller_; + + // Host of the first page the download started on. This may be empty. + std::string initial_page_host_; + + DownloadRequestManager::DownloadStatus status_; + + // Callbacks we need to notify. This is only non-empty if we're showing a + // dialog. + // See description above CanDownloadOnIOThread for details on lifetime of + // callbacks. + std::vector<DownloadRequestManager::Callback*> callbacks_; + + // Used to remove observers installed on NavigationController. + NotificationRegistrar registrar_; + + // Handles showing the dialog to the user, may be null. + DownloadRequestDialogDelegate* dialog_delegate_; + + DISALLOW_COPY_AND_ASSIGN(TabDownloadState); + }; + + DownloadRequestManager(MessageLoop* io_loop, MessageLoop* ui_loop); + ~DownloadRequestManager(); + // Returns the download status for a page. This does not change the state in // anyway. DownloadStatus GetDownloadStatus(TabContents* tab); diff --git a/chrome/browser/renderer_host/download_throttling_resource_handler.h b/chrome/browser/renderer_host/download_throttling_resource_handler.h index f280841..3306475 100644 --- a/chrome/browser/renderer_host/download_throttling_resource_handler.h +++ b/chrome/browser/renderer_host/download_throttling_resource_handler.h @@ -7,16 +7,10 @@ #include <string> +#include "chrome/browser/download/download_request_manager.h" #include "chrome/browser/renderer_host/resource_handler.h" #include "googleurl/src/gurl.h" -#if defined(OS_WIN) -// TODO(port): Remove ifdef when downloads are ported. -#include "chrome/browser/download/download_request_manager.h" -#elif defined(OS_POSIX) -#include "chrome/common/temp_scaffolding_stubs.h" -#endif - class DownloadResourceHandler; class ResourceDispatcherHost; class URLRequest; diff --git a/chrome/browser/renderer_host/resource_dispatcher_host.cc b/chrome/browser/renderer_host/resource_dispatcher_host.cc index 80b2133..d746201 100644 --- a/chrome/browser/renderer_host/resource_dispatcher_host.cc +++ b/chrome/browser/renderer_host/resource_dispatcher_host.cc @@ -15,6 +15,7 @@ #include "chrome/browser/cross_site_request_manager.h" #include "chrome/browser/download/download_file.h" #include "chrome/browser/download/download_manager.h" +#include "chrome/browser/download/download_request_manager.h" #include "chrome/browser/download/save_file_manager.h" #include "chrome/browser/plugin_service.h" #include "chrome/browser/renderer_host/async_resource_handler.h" @@ -43,7 +44,6 @@ #if defined(OS_POSIX) #include "chrome/common/temp_scaffolding_stubs.h" #elif defined(OS_WIN) -#include "chrome/browser/download/download_request_manager.h" #include "chrome/browser/external_protocol_handler.h" #include "chrome/browser/login_prompt.h" #include "chrome/browser/renderer_host/render_view_host_delegate.h" |