| Index: chrome/browser/download/download_danger_prompt.cc
|
| diff --git a/chrome/browser/download/download_danger_prompt.cc b/chrome/browser/download/download_danger_prompt.cc
|
| index bdd75cc4d58c2eb7c6cc8cabcdf19b80154d5bef..45d5524432211cc08c2d1674d28c650d052a58f9 100644
|
| --- a/chrome/browser/download/download_danger_prompt.cc
|
| +++ b/chrome/browser/download/download_danger_prompt.cc
|
| @@ -8,6 +8,7 @@
|
| #include "chrome/browser/download/chrome_download_manager_delegate.h"
|
| #include "chrome/browser/ui/tab_modal_confirm_dialog.h"
|
| #include "chrome/browser/ui/tab_modal_confirm_dialog_delegate.h"
|
| +#include "chrome/browser/chrome_notification_types.h"
|
| #include "content/public/browser/download_danger_type.h"
|
| #include "content/public/browser/download_item.h"
|
| #include "grit/generated_resources.h"
|
| @@ -24,8 +25,7 @@ class DownloadDangerPromptImpl
|
| DownloadDangerPromptImpl(content::DownloadItem* item,
|
| content::WebContents* web_contents,
|
| bool show_context,
|
| - const base::Closure& accepted,
|
| - const base::Closure& canceled);
|
| + const OnDone& done);
|
| virtual ~DownloadDangerPromptImpl();
|
|
|
| // DownloadDangerPrompt
|
| @@ -34,7 +34,6 @@ class DownloadDangerPromptImpl
|
| private:
|
| // content::DownloadItem::Observer
|
| virtual void OnDownloadUpdated(content::DownloadItem* download) OVERRIDE;
|
| - virtual void OnDownloadOpened(content::DownloadItem* download) OVERRIDE;
|
|
|
| // TabModalConfirmDialogDelegate
|
| virtual string16 GetTitle() OVERRIDE;
|
| @@ -43,20 +42,17 @@ class DownloadDangerPromptImpl
|
| virtual void OnAccepted() OVERRIDE;
|
| virtual void OnCanceled() OVERRIDE;
|
|
|
| - // Runs |callback|. PrepareToClose() is called beforehand. Doing so prevents
|
| - // this object from responding to state changes in |download_| that might
|
| - // result from invoking the callback. |callback| must refer to either
|
| - // |accepted_| or |canceled_|.
|
| - void RunCallback(const base::Closure& callback);
|
| + // content::NotificationObserver implementation.
|
| + virtual void Observe(
|
| + int type,
|
| + const content::NotificationSource& source,
|
| + const content::NotificationDetails& details) OVERRIDE;
|
|
|
| - // Resets |accepted_|, |canceled_| and removes the observer from |download_|,
|
| - // in preparation for closing the prompt.
|
| - void PrepareToClose();
|
| + void RunDone(Action action);
|
|
|
| content::DownloadItem* download_;
|
| bool show_context_;
|
| - base::Closure accepted_;
|
| - base::Closure canceled_;
|
| + OnDone done_;
|
|
|
| DISALLOW_COPY_AND_ASSIGN(DownloadDangerPromptImpl);
|
| };
|
| @@ -65,30 +61,30 @@ DownloadDangerPromptImpl::DownloadDangerPromptImpl(
|
| content::DownloadItem* download,
|
| content::WebContents* web_contents,
|
| bool show_context,
|
| - const base::Closure& accepted,
|
| - const base::Closure& canceled)
|
| + const OnDone& done)
|
| : TabModalConfirmDialogDelegate(web_contents),
|
| download_(download),
|
| show_context_(show_context),
|
| - accepted_(accepted),
|
| - canceled_(canceled) {
|
| - DCHECK(!accepted_.is_null());
|
| - // canceled_ is allowed to be null.
|
| - DCHECK(download_);
|
| + done_(done) {
|
| + DCHECK(!done_.is_null());
|
| download_->AddObserver(this);
|
| }
|
|
|
| DownloadDangerPromptImpl::~DownloadDangerPromptImpl() {
|
| // |this| might be deleted without invoking any callbacks. E.g. pressing Esc
|
| // on GTK or if the user navigates away from the page showing the prompt.
|
| - PrepareToClose();
|
| + RunDone(DISMISS);
|
| }
|
|
|
| void DownloadDangerPromptImpl::InvokeActionForTesting(Action action) {
|
| - if (action == ACCEPT)
|
| - Accept();
|
| - else
|
| - Cancel();
|
| + switch (action) {
|
| + case ACCEPT: Accept(); break;
|
| + case CANCEL: Cancel(); break;
|
| + case DISMISS:
|
| + RunDone(DISMISS);
|
| + Cancel();
|
| + break;
|
| + }
|
| }
|
|
|
| void DownloadDangerPromptImpl::OnDownloadUpdated(
|
| @@ -96,12 +92,10 @@ void DownloadDangerPromptImpl::OnDownloadUpdated(
|
| // If the download is nolonger dangerous (accepted externally) or the download
|
| // is in a terminal state, then the download danger prompt is no longer
|
| // necessary.
|
| - if (!download->IsDangerous() || download->IsDone())
|
| + if (!download->IsDangerous() || download->IsDone()) {
|
| + RunDone(DISMISS);
|
| Cancel();
|
| -}
|
| -
|
| -void DownloadDangerPromptImpl::OnDownloadOpened(
|
| - content::DownloadItem* download) {
|
| + }
|
| }
|
|
|
| string16 DownloadDangerPromptImpl::GetTitle() {
|
| @@ -139,30 +133,35 @@ string16 DownloadDangerPromptImpl::GetAcceptButtonTitle() {
|
| }
|
|
|
| void DownloadDangerPromptImpl::OnAccepted() {
|
| - RunCallback(accepted_);
|
| + RunDone(ACCEPT);
|
| }
|
|
|
| void DownloadDangerPromptImpl::OnCanceled() {
|
| - RunCallback(canceled_);
|
| + RunDone(CANCEL);
|
| +}
|
| +
|
| +void DownloadDangerPromptImpl::Observe(
|
| + int type,
|
| + const content::NotificationSource& source,
|
| + const content::NotificationDetails& details) {
|
| + DCHECK(type == content::NOTIFICATION_LOAD_START ||
|
| + type == chrome::NOTIFICATION_TAB_CLOSING);
|
| + RunDone(DISMISS);
|
| + Cancel();
|
| }
|
|
|
| -void DownloadDangerPromptImpl::RunCallback(const base::Closure& callback) {
|
| +void DownloadDangerPromptImpl::RunDone(Action action) {
|
| // Invoking the callback can cause the download item state to change or cause
|
| // the constrained window to close, and |callback| refers to a member
|
| // variable.
|
| - base::Closure callback_copy = callback;
|
| - PrepareToClose();
|
| - if (!callback_copy.is_null())
|
| - callback_copy.Run();
|
| -}
|
| -
|
| -void DownloadDangerPromptImpl::PrepareToClose() {
|
| - accepted_.Reset();
|
| - canceled_.Reset();
|
| + OnDone done = done_;
|
| + done_.Reset();
|
| if (download_ != NULL) {
|
| download_->RemoveObserver(this);
|
| download_ = NULL;
|
| }
|
| + if (!done.is_null())
|
| + done.Run(action);
|
| }
|
|
|
| } // namespace
|
| @@ -172,10 +171,9 @@ DownloadDangerPrompt* DownloadDangerPrompt::Create(
|
| content::DownloadItem* item,
|
| content::WebContents* web_contents,
|
| bool show_context,
|
| - const base::Closure& accepted,
|
| - const base::Closure& canceled) {
|
| + const OnDone& done) {
|
| DownloadDangerPromptImpl* prompt = new DownloadDangerPromptImpl(
|
| - item, web_contents, show_context, accepted, canceled);
|
| + item, web_contents, show_context, done);
|
| // |prompt| will be deleted when the dialog is done.
|
| TabModalConfirmDialog::Create(prompt, web_contents);
|
| return prompt;
|
|
|