OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/download/notification/download_notification_manager.h" | |
6 | |
7 #include "base/strings/string_number_conversions.h" | |
8 #include "chrome/browser/download/download_item_model.h" | |
9 #include "chrome/grit/chromium_strings.h" | |
10 #include "content/public/browser/download_item.h" | |
11 #include "grit/theme_resources.h" | |
12 #include "ui/base/l10n/l10n_util.h" | |
13 #include "ui/base/resource/resource_bundle.h" | |
14 #include "ui/message_center/message_center.h" | |
15 #include "ui/message_center/notification.h" | |
16 #include "ui/message_center/notification_delegate.h" | |
17 | |
18 using message_center::Notification; | |
19 | |
20 namespace { | |
21 | |
22 const char kDownloadNotificationNotifierId[] = | |
23 "chrome://settings/display/notification/id-notifier"; | |
24 const char kDownloadNotificationIdBase[] = | |
25 "chrome://settings/display/notification/id-"; | |
26 | |
27 class DownloadNotificationItem | |
28 : public message_center::NotificationDelegate, | |
29 public content::DownloadItem::Observer { | |
30 public: | |
31 explicit DownloadNotificationItem(content::DownloadItem* item) : | |
32 openable_(false), image_resource_id_(0) { | |
33 item_ = item; | |
34 item->AddObserver(this); | |
35 | |
36 message_center_ = message_center::MessageCenter::Get(); | |
37 | |
38 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); | |
39 | |
40 const base::string16 timeout_message = | |
41 l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_CRX_INSTALL_RUNNING); | |
42 const base::string16 message = | |
43 l10n_util::GetStringUTF16(IDS_PROMPT_MALICIOUS_DOWNLOAD_URL); | |
44 | |
45 std::string id(kDownloadNotificationIdBase); | |
46 id += base::UintToString(item->GetId()); | |
47 | |
48 message_center::RichNotificationData data; | |
49 notification_.reset(new Notification( | |
50 message_center::NOTIFICATION_TYPE_PROGRESS, | |
51 id, | |
52 message, | |
53 timeout_message, | |
54 bundle.GetImageNamed(IDR_DOWNLOAD_NOTIFICATION_DOWNLOADING), | |
55 base::string16() /* display_source */, | |
56 message_center::NotifierId( | |
57 message_center::NotifierId::SYSTEM_COMPONENT, | |
58 kDownloadNotificationNotifierId), | |
59 data, | |
60 this)); | |
61 | |
62 notification_->set_progress(0); | |
63 notification_->set_never_timeout(false); | |
64 | |
65 SetNotificationData(); | |
66 | |
67 scoped_ptr<Notification> notification(new Notification(*notification_)); | |
68 message_center_->AddNotification(notification.Pass()); | |
69 } | |
70 | |
71 protected: | |
72 virtual ~DownloadNotificationItem() { | |
73 item_->RemoveObserver(this); | |
74 } | |
75 | |
76 private: | |
77 // message_center::NotificationDelegate overrides: | |
78 void Close(bool by_user) override { | |
79 delete this; | |
80 } | |
81 | |
82 void Click() override { | |
83 if (openable_) | |
84 item_->OpenDownload(); | |
85 if (item_->IsDone()) { | |
86 message_center_->RemoveNotification(notification_->id(), true); | |
87 } | |
88 } | |
89 | |
90 bool HasClickedListener() override { return true; } | |
91 | |
92 void ButtonClick(int button_index) override { | |
93 if (button_index < 0 || | |
94 static_cast<size_t>(button_index) >= button_actions_->size()) { | |
95 // Out of boundary. Do nothing. | |
96 return; | |
97 } | |
98 | |
99 switch (button_actions_->at(button_index).type()) { | |
100 case DownloadAction::OPEN: | |
101 NOTREACHED(); | |
102 break; | |
103 case DownloadAction::PAUSE: | |
104 item_->Pause(); | |
105 break; | |
106 case DownloadAction::RESUME: | |
107 case DownloadAction::RETRY: | |
108 item_->Resume(); | |
109 break; | |
110 case DownloadAction::VIEW_IN_FOLDER: | |
111 item_->ShowDownloadInShell(); | |
112 break; | |
113 case DownloadAction::DISCARD: | |
114 item_->Cancel(true); | |
115 break; | |
116 } | |
117 } | |
118 | |
119 // DownloadItem::Observer methods | |
120 void OnDownloadUpdated(content::DownloadItem* item) override { | |
121 DCHECK_EQ(item, item_); | |
122 | |
123 SetNotificationData(); | |
124 | |
125 // Updates notification. | |
126 scoped_ptr<Notification> notification(new Notification(*notification_)); | |
127 message_center_->UpdateNotification( | |
128 notification->id(), | |
129 notification.Pass()); | |
130 } | |
131 | |
132 void SetNotificationData() { | |
133 DownloadItemModel* model = new DownloadItemModel(item_); | |
134 | |
135 if (item_->IsDangerous()) { | |
136 notification_->set_type(message_center::NOTIFICATION_TYPE_SIMPLE); | |
137 notification_->set_title(model->GetTitle()); | |
138 notification_->set_message(model->GetWarningTextLong()); | |
139 | |
140 // Show icon. | |
141 | |
142 switch (item_->GetDangerType()) { | |
143 case content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS: | |
144 case content::DOWNLOAD_DANGER_TYPE_USER_VALIDATED: | |
145 break; | |
146 default: | |
147 SetImageToNotification(IDR_DOWNLOAD_NOTIFICATION_MALICIOUS); | |
148 break; | |
149 } | |
150 } else { | |
151 notification_->set_title(model->GetTitle()); | |
152 notification_->set_message(model->GetStatusText()); | |
153 | |
154 switch (item_->GetState()) { | |
155 case content::DownloadItem::IN_PROGRESS: | |
156 notification_->set_type(message_center::NOTIFICATION_TYPE_PROGRESS); | |
157 notification_->set_progress(item_->PercentComplete()); | |
158 SetImageToNotification(IDR_DOWNLOAD_NOTIFICATION_DOWNLOADING); | |
159 break; | |
160 case content::DownloadItem::COMPLETE: | |
161 notification_->set_type(message_center::NOTIFICATION_TYPE_SIMPLE); | |
162 SetImageToNotification(IDR_DOWNLOAD_NOTIFICATION_DOWNLOADING); | |
163 break; | |
164 case content::DownloadItem::CANCELLED: | |
165 notification_->set_type(message_center::NOTIFICATION_TYPE_SIMPLE); | |
166 SetImageToNotification(IDR_DOWNLOAD_NOTIFICATION_WARNING); | |
167 break; | |
168 case content::DownloadItem::INTERRUPTED: | |
169 notification_->set_type(message_center::NOTIFICATION_TYPE_SIMPLE); | |
170 SetImageToNotification(IDR_DOWNLOAD_NOTIFICATION_WARNING); | |
171 break; | |
172 case content::DownloadItem::MAX_DOWNLOAD_STATE: // sentinel | |
173 NOTREACHED(); | |
174 } | |
175 } | |
176 | |
177 std::vector<message_center::ButtonInfo> notification_actions; | |
178 scoped_ptr<std::vector<DownloadAction> > | |
179 actions(model->GetPossibleActions().Pass()); | |
180 | |
181 openable_ = false; | |
182 button_actions_.reset(new std::vector<DownloadAction>); | |
183 for (auto it = actions->begin(); it != actions->end(); it++) { | |
184 switch (it->type()) { | |
185 case DownloadAction::OPEN: | |
186 openable_ = true; | |
187 break; | |
188 default: | |
189 button_actions_->push_back(*it); | |
190 notification_actions.push_back( | |
191 message_center::ButtonInfo(it->text())); | |
192 break; | |
193 } | |
194 } | |
195 notification_->set_buttons(notification_actions); | |
196 | |
197 if (item_->IsDone()) { | |
198 // TODO(yoshiki): If the downloaded file is an image, show the thumbnail. | |
199 } | |
200 } | |
201 | |
202 void OnDownloadOpened(content::DownloadItem* item) override { | |
203 DCHECK_EQ(item, item_); | |
204 // Do nothing. | |
205 } | |
206 | |
207 void OnDownloadDestroyed(content::DownloadItem* item) override { | |
208 DCHECK_EQ(item, item_); | |
209 | |
210 delete this; | |
211 | |
212 // TODO(yoshiki): close the notification if necessary. | |
213 } | |
214 | |
215 void SetImageToNotification(int resource_id) { | |
216 if (image_resource_id_ == resource_id) | |
217 return; | |
218 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); | |
219 image_resource_id_ = resource_id; | |
220 notification_->set_small_image(bundle.GetImageNamed(image_resource_id_)); | |
221 } | |
222 | |
223 scoped_ptr<Notification> notification_; | |
224 scoped_ptr<std::vector<DownloadAction> > button_actions_; | |
225 | |
226 bool has_timeout_; | |
227 bool openable_; | |
228 int image_resource_id_; | |
229 message_center::MessageCenter* message_center_; | |
230 content::DownloadItem* item_; | |
231 | |
232 DISALLOW_COPY_AND_ASSIGN(DownloadNotificationItem); | |
233 }; | |
234 | |
235 } // namespace | |
236 | |
237 DownloadNotificationManager::DownloadNotificationManager(Profile* profile) | |
asanka
2015/01/23 19:43:53
What is the behavior going to be for multi-profile
yoshiki
2015/01/29 20:20:46
I checked and it works well. Since download IDs an
| |
238 : profile_(profile) { | |
239 profile_ = profile; | |
240 } | |
241 | |
242 DownloadNotificationManager::~DownloadNotificationManager() { | |
243 } | |
244 | |
245 void DownloadNotificationManager::OnNewDownloadReady( | |
246 content::DownloadItem* item) { | |
247 new DownloadNotificationItem(item); | |
248 } | |
OLD | NEW |