Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(237)

Side by Side Diff: chrome/browser/download/download_danger_prompt.cc

Issue 16924017: A few minor changes to the chrome.downloads extension API (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: @r214130 Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/download/download_danger_prompt.h" 5 #include "chrome/browser/download/download_danger_prompt.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "chrome/browser/chrome_notification_types.h"
8 #include "chrome/browser/download/chrome_download_manager_delegate.h" 9 #include "chrome/browser/download/chrome_download_manager_delegate.h"
9 #include "chrome/browser/ui/tab_modal_confirm_dialog.h" 10 #include "chrome/browser/ui/tab_modal_confirm_dialog.h"
10 #include "chrome/browser/ui/tab_modal_confirm_dialog_delegate.h" 11 #include "chrome/browser/ui/tab_modal_confirm_dialog_delegate.h"
11 #include "content/public/browser/download_danger_type.h" 12 #include "content/public/browser/download_danger_type.h"
12 #include "content/public/browser/download_item.h" 13 #include "content/public/browser/download_item.h"
13 #include "grit/generated_resources.h" 14 #include "grit/generated_resources.h"
14 #include "ui/base/l10n/l10n_util.h" 15 #include "ui/base/l10n/l10n_util.h"
15 16
16 namespace { 17 namespace {
17 18
18 // Implements DownloadDangerPrompt using a TabModalConfirmDialog. 19 // Implements DownloadDangerPrompt using a TabModalConfirmDialog.
19 class DownloadDangerPromptImpl 20 class DownloadDangerPromptImpl
20 : public DownloadDangerPrompt, 21 : public DownloadDangerPrompt,
21 public content::DownloadItem::Observer, 22 public content::DownloadItem::Observer,
22 public TabModalConfirmDialogDelegate { 23 public TabModalConfirmDialogDelegate {
23 public: 24 public:
24 DownloadDangerPromptImpl(content::DownloadItem* item, 25 DownloadDangerPromptImpl(content::DownloadItem* item,
25 bool show_context, 26 bool show_context,
26 const base::Closure& accepted, 27 const OnDone& done);
27 const base::Closure& canceled);
28 virtual ~DownloadDangerPromptImpl(); 28 virtual ~DownloadDangerPromptImpl();
29 29
30 // DownloadDangerPrompt 30 // DownloadDangerPrompt
31 virtual void InvokeActionForTesting(Action action) OVERRIDE; 31 virtual void InvokeActionForTesting(Action action) OVERRIDE;
32 32
33 private: 33 private:
34 // content::DownloadItem::Observer 34 // content::DownloadItem::Observer
35 virtual void OnDownloadUpdated(content::DownloadItem* download) OVERRIDE; 35 virtual void OnDownloadUpdated(content::DownloadItem* download) OVERRIDE;
36 virtual void OnDownloadOpened(content::DownloadItem* download) OVERRIDE;
37 36
38 // TabModalConfirmDialogDelegate 37 // TabModalConfirmDialogDelegate
39 virtual string16 GetTitle() OVERRIDE; 38 virtual string16 GetTitle() OVERRIDE;
40 virtual string16 GetMessage() OVERRIDE; 39 virtual string16 GetMessage() OVERRIDE;
41 virtual string16 GetAcceptButtonTitle() OVERRIDE; 40 virtual string16 GetAcceptButtonTitle() OVERRIDE;
42 virtual void OnAccepted() OVERRIDE; 41 virtual void OnAccepted() OVERRIDE;
43 virtual void OnCanceled() OVERRIDE; 42 virtual void OnCanceled() OVERRIDE;
44 43
45 // Runs |callback|. PrepareToClose() is called beforehand. Doing so prevents 44 void RunDone(Action action);
46 // this object from responding to state changes in |download_| that might
47 // result from invoking the callback. |callback| must refer to either
48 // |accepted_| or |canceled_|.
49 void RunCallback(const base::Closure& callback);
50
51 // Resets |accepted_|, |canceled_| and removes the observer from |download_|,
52 // in preparation for closing the prompt.
53 void PrepareToClose();
54 45
55 content::DownloadItem* download_; 46 content::DownloadItem* download_;
56 bool show_context_; 47 bool show_context_;
57 base::Closure accepted_; 48 OnDone done_;
58 base::Closure canceled_;
59 49
60 DISALLOW_COPY_AND_ASSIGN(DownloadDangerPromptImpl); 50 DISALLOW_COPY_AND_ASSIGN(DownloadDangerPromptImpl);
61 }; 51 };
62 52
63 DownloadDangerPromptImpl::DownloadDangerPromptImpl( 53 DownloadDangerPromptImpl::DownloadDangerPromptImpl(
64 content::DownloadItem* download, 54 content::DownloadItem* download,
65 bool show_context, 55 bool show_context,
66 const base::Closure& accepted, 56 const OnDone& done)
67 const base::Closure& canceled)
68 : download_(download), 57 : download_(download),
69 show_context_(show_context), 58 show_context_(show_context),
70 accepted_(accepted), 59 done_(done) {
71 canceled_(canceled) { 60 DCHECK(!done_.is_null());
72 DCHECK(!accepted_.is_null());
73 // canceled_ is allowed to be null.
74 DCHECK(download_);
75 download_->AddObserver(this); 61 download_->AddObserver(this);
76 } 62 }
77 63
78 DownloadDangerPromptImpl::~DownloadDangerPromptImpl() { 64 DownloadDangerPromptImpl::~DownloadDangerPromptImpl() {
79 // |this| might be deleted without invoking any callbacks. E.g. pressing Esc 65 // |this| might be deleted without invoking any callbacks. E.g. pressing Esc
80 // on GTK or if the user navigates away from the page showing the prompt. 66 // on GTK or if the user navigates away from the page showing the prompt.
81 PrepareToClose(); 67 RunDone(DISMISS);
82 } 68 }
83 69
84 void DownloadDangerPromptImpl::InvokeActionForTesting(Action action) { 70 void DownloadDangerPromptImpl::InvokeActionForTesting(Action action) {
85 if (action == ACCEPT) 71 switch (action) {
86 Accept(); 72 case ACCEPT: Accept(); break;
87 else 73 case CANCEL: Cancel(); break;
88 Cancel(); 74 case DISMISS:
75 RunDone(DISMISS);
76 Cancel();
77 break;
78 }
89 } 79 }
90 80
91 void DownloadDangerPromptImpl::OnDownloadUpdated( 81 void DownloadDangerPromptImpl::OnDownloadUpdated(
92 content::DownloadItem* download) { 82 content::DownloadItem* download) {
93 // If the download is nolonger dangerous (accepted externally) or the download 83 // If the download is nolonger dangerous (accepted externally) or the download
94 // is in a terminal state, then the download danger prompt is no longer 84 // is in a terminal state, then the download danger prompt is no longer
95 // necessary. 85 // necessary.
96 if (!download->IsDangerous() || download->IsDone()) 86 if (!download->IsDangerous() || download->IsDone()) {
87 RunDone(DISMISS);
97 Cancel(); 88 Cancel();
98 } 89 }
99
100 void DownloadDangerPromptImpl::OnDownloadOpened(
101 content::DownloadItem* download) {
102 } 90 }
103 91
104 string16 DownloadDangerPromptImpl::GetTitle() { 92 string16 DownloadDangerPromptImpl::GetTitle() {
105 return l10n_util::GetStringUTF16(IDS_CONFIRM_KEEP_DANGEROUS_DOWNLOAD_TITLE); 93 return l10n_util::GetStringUTF16(IDS_CONFIRM_KEEP_DANGEROUS_DOWNLOAD_TITLE);
106 } 94 }
107 95
108 string16 DownloadDangerPromptImpl::GetMessage() { 96 string16 DownloadDangerPromptImpl::GetMessage() {
109 if (!show_context_) 97 if (!show_context_)
110 return l10n_util::GetStringUTF16( 98 return l10n_util::GetStringUTF16(
111 IDS_PROMPT_CONFIRM_KEEP_DANGEROUS_DOWNLOAD); 99 IDS_PROMPT_CONFIRM_KEEP_DANGEROUS_DOWNLOAD);
(...skipping 25 matching lines...) Expand all
137 NOTREACHED(); 125 NOTREACHED();
138 return string16(); 126 return string16();
139 } 127 }
140 128
141 string16 DownloadDangerPromptImpl::GetAcceptButtonTitle() { 129 string16 DownloadDangerPromptImpl::GetAcceptButtonTitle() {
142 return l10n_util::GetStringUTF16( 130 return l10n_util::GetStringUTF16(
143 show_context_ ? IDS_CONFIRM_DOWNLOAD : IDS_CONFIRM_DOWNLOAD_AGAIN); 131 show_context_ ? IDS_CONFIRM_DOWNLOAD : IDS_CONFIRM_DOWNLOAD_AGAIN);
144 } 132 }
145 133
146 void DownloadDangerPromptImpl::OnAccepted() { 134 void DownloadDangerPromptImpl::OnAccepted() {
147 RunCallback(accepted_); 135 RunDone(ACCEPT);
148 } 136 }
149 137
150 void DownloadDangerPromptImpl::OnCanceled() { 138 void DownloadDangerPromptImpl::OnCanceled() {
151 RunCallback(canceled_); 139 RunDone(CANCEL);
152 } 140 }
153 141
154 void DownloadDangerPromptImpl::RunCallback(const base::Closure& callback) { 142 void DownloadDangerPromptImpl::RunDone(Action action) {
155 // Invoking the callback can cause the download item state to change or cause 143 // Invoking the callback can cause the download item state to change or cause
156 // the constrained window to close, and |callback| refers to a member 144 // the constrained window to close, and |callback| refers to a member
157 // variable. 145 // variable.
158 base::Closure callback_copy = callback; 146 OnDone done = done_;
159 PrepareToClose(); 147 done_.Reset();
160 if (!callback_copy.is_null())
161 callback_copy.Run();
162 }
163
164 void DownloadDangerPromptImpl::PrepareToClose() {
165 accepted_.Reset();
166 canceled_.Reset();
167 if (download_ != NULL) { 148 if (download_ != NULL) {
168 download_->RemoveObserver(this); 149 download_->RemoveObserver(this);
169 download_ = NULL; 150 download_ = NULL;
170 } 151 }
152 if (!done.is_null())
153 done.Run(action);
171 } 154 }
172 155
173 } // namespace 156 } // namespace
174 157
175 // static 158 // static
176 DownloadDangerPrompt* DownloadDangerPrompt::Create( 159 DownloadDangerPrompt* DownloadDangerPrompt::Create(
177 content::DownloadItem* item, 160 content::DownloadItem* item,
178 content::WebContents* web_contents, 161 content::WebContents* web_contents,
179 bool show_context, 162 bool show_context,
180 const base::Closure& accepted, 163 const OnDone& done) {
181 const base::Closure& canceled) {
182 DownloadDangerPromptImpl* prompt = new DownloadDangerPromptImpl( 164 DownloadDangerPromptImpl* prompt = new DownloadDangerPromptImpl(
183 item, show_context, accepted, canceled); 165 item, show_context, done);
184 // |prompt| will be deleted when the dialog is done. 166 // |prompt| will be deleted when the dialog is done.
185 TabModalConfirmDialog::Create(prompt, web_contents); 167 TabModalConfirmDialog::Create(prompt, web_contents);
186 return prompt; 168 return prompt;
187 } 169 }
OLDNEW
« no previous file with comments | « chrome/browser/download/download_danger_prompt.h ('k') | chrome/browser/download/download_danger_prompt_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698