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

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: @r211135 Created 7 years, 5 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/download/chrome_download_manager_delegate.h" 8 #include "chrome/browser/download/chrome_download_manager_delegate.h"
9 #include "chrome/browser/ui/tab_modal_confirm_dialog.h" 9 #include "chrome/browser/ui/tab_modal_confirm_dialog.h"
10 #include "chrome/browser/ui/tab_modal_confirm_dialog_delegate.h" 10 #include "chrome/browser/ui/tab_modal_confirm_dialog_delegate.h"
11 #include "chrome/common/chrome_notification_types.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 content::WebContents* web_contents, 26 content::WebContents* web_contents,
26 bool show_context, 27 bool show_context,
27 const base::Closure& accepted, 28 const OnDone& done);
28 const base::Closure& canceled);
29 virtual ~DownloadDangerPromptImpl(); 29 virtual ~DownloadDangerPromptImpl();
30 30
31 // DownloadDangerPrompt 31 // DownloadDangerPrompt
32 virtual void InvokeActionForTesting(Action action) OVERRIDE; 32 virtual void InvokeActionForTesting(Action action) OVERRIDE;
33 33
34 private: 34 private:
35 // content::DownloadItem::Observer 35 // content::DownloadItem::Observer
36 virtual void OnDownloadUpdated(content::DownloadItem* download) OVERRIDE; 36 virtual void OnDownloadUpdated(content::DownloadItem* download) OVERRIDE;
37 virtual void OnDownloadOpened(content::DownloadItem* download) OVERRIDE;
38 37
39 // TabModalConfirmDialogDelegate 38 // TabModalConfirmDialogDelegate
40 virtual string16 GetTitle() OVERRIDE; 39 virtual string16 GetTitle() OVERRIDE;
41 virtual string16 GetMessage() OVERRIDE; 40 virtual string16 GetMessage() OVERRIDE;
42 virtual string16 GetAcceptButtonTitle() OVERRIDE; 41 virtual string16 GetAcceptButtonTitle() OVERRIDE;
43 virtual void OnAccepted() OVERRIDE; 42 virtual void OnAccepted() OVERRIDE;
44 virtual void OnCanceled() OVERRIDE; 43 virtual void OnCanceled() OVERRIDE;
45 44
46 // Runs |callback|. PrepareToClose() is called beforehand. Doing so prevents 45 // content::NotificationObserver implementation.
47 // this object from responding to state changes in |download_| that might 46 virtual void Observe(
48 // result from invoking the callback. |callback| must refer to either 47 int type,
49 // |accepted_| or |canceled_|. 48 const content::NotificationSource& source,
50 void RunCallback(const base::Closure& callback); 49 const content::NotificationDetails& details) OVERRIDE;
51 50
52 // Resets |accepted_|, |canceled_| and removes the observer from |download_|, 51 void RunDone(Action action);
53 // in preparation for closing the prompt.
54 void PrepareToClose();
55 52
56 content::DownloadItem* download_; 53 content::DownloadItem* download_;
57 bool show_context_; 54 bool show_context_;
58 base::Closure accepted_; 55 OnDone done_;
59 base::Closure canceled_;
60 56
61 DISALLOW_COPY_AND_ASSIGN(DownloadDangerPromptImpl); 57 DISALLOW_COPY_AND_ASSIGN(DownloadDangerPromptImpl);
62 }; 58 };
63 59
64 DownloadDangerPromptImpl::DownloadDangerPromptImpl( 60 DownloadDangerPromptImpl::DownloadDangerPromptImpl(
65 content::DownloadItem* download, 61 content::DownloadItem* download,
66 content::WebContents* web_contents, 62 content::WebContents* web_contents,
67 bool show_context, 63 bool show_context,
68 const base::Closure& accepted, 64 const OnDone& done)
69 const base::Closure& canceled)
70 : TabModalConfirmDialogDelegate(web_contents), 65 : TabModalConfirmDialogDelegate(web_contents),
71 download_(download), 66 download_(download),
72 show_context_(show_context), 67 show_context_(show_context),
73 accepted_(accepted), 68 done_(done) {
74 canceled_(canceled) { 69 DCHECK(!done_.is_null());
75 DCHECK(!accepted_.is_null());
76 // canceled_ is allowed to be null.
77 DCHECK(download_);
78 download_->AddObserver(this); 70 download_->AddObserver(this);
79 } 71 }
80 72
81 DownloadDangerPromptImpl::~DownloadDangerPromptImpl() { 73 DownloadDangerPromptImpl::~DownloadDangerPromptImpl() {
82 // |this| might be deleted without invoking any callbacks. E.g. pressing Esc 74 // |this| might be deleted without invoking any callbacks. E.g. pressing Esc
83 // on GTK or if the user navigates away from the page showing the prompt. 75 // on GTK or if the user navigates away from the page showing the prompt.
84 PrepareToClose(); 76 RunDone(DISMISS);
85 } 77 }
86 78
87 void DownloadDangerPromptImpl::InvokeActionForTesting(Action action) { 79 void DownloadDangerPromptImpl::InvokeActionForTesting(Action action) {
88 if (action == ACCEPT) 80 switch (action) {
89 Accept(); 81 case ACCEPT: Accept(); break;
90 else 82 case CANCEL: Cancel(); break;
91 Cancel(); 83 case DISMISS:
84 RunDone(DISMISS);
85 Cancel();
86 break;
87 }
92 } 88 }
93 89
94 void DownloadDangerPromptImpl::OnDownloadUpdated( 90 void DownloadDangerPromptImpl::OnDownloadUpdated(
95 content::DownloadItem* download) { 91 content::DownloadItem* download) {
96 // If the download is nolonger dangerous (accepted externally) or the download 92 // If the download is nolonger dangerous (accepted externally) or the download
97 // is in a terminal state, then the download danger prompt is no longer 93 // is in a terminal state, then the download danger prompt is no longer
98 // necessary. 94 // necessary.
99 if (!download->IsDangerous() || download->IsDone()) 95 if (!download->IsDangerous() || download->IsDone()) {
96 RunDone(DISMISS);
100 Cancel(); 97 Cancel();
101 } 98 }
102
103 void DownloadDangerPromptImpl::OnDownloadOpened(
104 content::DownloadItem* download) {
105 } 99 }
106 100
107 string16 DownloadDangerPromptImpl::GetTitle() { 101 string16 DownloadDangerPromptImpl::GetTitle() {
108 return l10n_util::GetStringUTF16(IDS_CONFIRM_KEEP_DANGEROUS_DOWNLOAD_TITLE); 102 return l10n_util::GetStringUTF16(IDS_CONFIRM_KEEP_DANGEROUS_DOWNLOAD_TITLE);
109 } 103 }
110 104
111 string16 DownloadDangerPromptImpl::GetMessage() { 105 string16 DownloadDangerPromptImpl::GetMessage() {
112 if (!show_context_) 106 if (!show_context_)
113 return l10n_util::GetStringUTF16( 107 return l10n_util::GetStringUTF16(
114 IDS_PROMPT_CONFIRM_KEEP_DANGEROUS_DOWNLOAD); 108 IDS_PROMPT_CONFIRM_KEEP_DANGEROUS_DOWNLOAD);
(...skipping 17 matching lines...) Expand all
132 return string16(); 126 return string16();
133 } 127 }
134 } 128 }
135 129
136 string16 DownloadDangerPromptImpl::GetAcceptButtonTitle() { 130 string16 DownloadDangerPromptImpl::GetAcceptButtonTitle() {
137 return l10n_util::GetStringUTF16( 131 return l10n_util::GetStringUTF16(
138 show_context_ ? IDS_CONFIRM_DOWNLOAD : IDS_CONFIRM_DOWNLOAD_AGAIN); 132 show_context_ ? IDS_CONFIRM_DOWNLOAD : IDS_CONFIRM_DOWNLOAD_AGAIN);
139 } 133 }
140 134
141 void DownloadDangerPromptImpl::OnAccepted() { 135 void DownloadDangerPromptImpl::OnAccepted() {
142 RunCallback(accepted_); 136 RunDone(ACCEPT);
143 } 137 }
144 138
145 void DownloadDangerPromptImpl::OnCanceled() { 139 void DownloadDangerPromptImpl::OnCanceled() {
146 RunCallback(canceled_); 140 RunDone(CANCEL);
147 } 141 }
148 142
149 void DownloadDangerPromptImpl::RunCallback(const base::Closure& callback) { 143 void DownloadDangerPromptImpl::Observe(
144 int type,
145 const content::NotificationSource& source,
146 const content::NotificationDetails& details) {
147 DCHECK(type == content::NOTIFICATION_LOAD_START ||
148 type == chrome::NOTIFICATION_TAB_CLOSING);
149 RunDone(DISMISS);
150 Cancel();
151 }
152
153 void DownloadDangerPromptImpl::RunDone(Action action) {
150 // Invoking the callback can cause the download item state to change or cause 154 // Invoking the callback can cause the download item state to change or cause
151 // the constrained window to close, and |callback| refers to a member 155 // the constrained window to close, and |callback| refers to a member
152 // variable. 156 // variable.
153 base::Closure callback_copy = callback; 157 OnDone done = done_;
154 PrepareToClose(); 158 done_.Reset();
155 if (!callback_copy.is_null())
156 callback_copy.Run();
157 }
158
159 void DownloadDangerPromptImpl::PrepareToClose() {
160 accepted_.Reset();
161 canceled_.Reset();
162 if (download_ != NULL) { 159 if (download_ != NULL) {
163 download_->RemoveObserver(this); 160 download_->RemoveObserver(this);
164 download_ = NULL; 161 download_ = NULL;
165 } 162 }
163 if (!done.is_null())
164 done.Run(action);
166 } 165 }
167 166
168 } // namespace 167 } // namespace
169 168
170 // static 169 // static
171 DownloadDangerPrompt* DownloadDangerPrompt::Create( 170 DownloadDangerPrompt* DownloadDangerPrompt::Create(
172 content::DownloadItem* item, 171 content::DownloadItem* item,
173 content::WebContents* web_contents, 172 content::WebContents* web_contents,
174 bool show_context, 173 bool show_context,
175 const base::Closure& accepted, 174 const OnDone& done) {
176 const base::Closure& canceled) {
177 DownloadDangerPromptImpl* prompt = new DownloadDangerPromptImpl( 175 DownloadDangerPromptImpl* prompt = new DownloadDangerPromptImpl(
178 item, web_contents, show_context, accepted, canceled); 176 item, web_contents, show_context, done);
179 // |prompt| will be deleted when the dialog is done. 177 // |prompt| will be deleted when the dialog is done.
180 TabModalConfirmDialog::Create(prompt, web_contents); 178 TabModalConfirmDialog::Create(prompt, web_contents);
181 return prompt; 179 return prompt;
182 } 180 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698