OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/notifications/desktop_notification_service.h" | 5 #include "chrome/browser/notifications/desktop_notification_service.h" |
6 | 6 |
7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
8 #include "app/resource_bundle.h" | 8 #include "app/resource_bundle.h" |
9 #include "base/string_piece.h" | 9 #include "base/string_piece.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
11 #include "base/thread.h" | 11 #include "base/thread.h" |
12 #include "chrome/browser/browser_list.h" | 12 #include "chrome/browser/browser_list.h" |
13 #include "chrome/browser/chrome_thread.h" | 13 #include "chrome/browser/chrome_thread.h" |
| 14 #include "chrome/browser/extensions/extensions_service.h" |
14 #include "chrome/browser/notifications/notification.h" | 15 #include "chrome/browser/notifications/notification.h" |
15 #include "chrome/browser/notifications/notification_object_proxy.h" | 16 #include "chrome/browser/notifications/notification_object_proxy.h" |
16 #include "chrome/browser/notifications/notification_ui_manager.h" | 17 #include "chrome/browser/notifications/notification_ui_manager.h" |
17 #include "chrome/browser/notifications/notifications_prefs_cache.h" | 18 #include "chrome/browser/notifications/notifications_prefs_cache.h" |
18 #include "chrome/browser/profile.h" | 19 #include "chrome/browser/profile.h" |
19 #include "chrome/browser/renderer_host/render_process_host.h" | 20 #include "chrome/browser/renderer_host/render_process_host.h" |
20 #include "chrome/browser/renderer_host/render_view_host.h" | 21 #include "chrome/browser/renderer_host/render_view_host.h" |
21 #include "chrome/browser/renderer_host/site_instance.h" | 22 #include "chrome/browser/renderer_host/site_instance.h" |
22 #include "chrome/browser/tab_contents/infobar_delegate.h" | 23 #include "chrome/browser/tab_contents/infobar_delegate.h" |
23 #include "chrome/browser/tab_contents/tab_contents.h" | 24 #include "chrome/browser/tab_contents/tab_contents.h" |
24 #include "chrome/browser/worker_host/worker_process_host.h" | 25 #include "chrome/browser/worker_host/worker_process_host.h" |
25 #include "chrome/common/child_process_host.h" | 26 #include "chrome/common/child_process_host.h" |
26 #include "chrome/common/pref_names.h" | 27 #include "chrome/common/pref_names.h" |
27 #include "chrome/common/pref_service.h" | 28 #include "chrome/common/pref_service.h" |
28 #include "chrome/common/render_messages.h" | 29 #include "chrome/common/render_messages.h" |
| 30 #include "chrome/common/url_constants.h" |
29 #include "grit/browser_resources.h" | 31 #include "grit/browser_resources.h" |
30 #include "grit/chromium_strings.h" | 32 #include "grit/chromium_strings.h" |
31 #include "grit/generated_resources.h" | 33 #include "grit/generated_resources.h" |
32 #include "grit/theme_resources.h" | 34 #include "grit/theme_resources.h" |
33 #include "webkit/api/public/WebNotificationPresenter.h" | 35 #include "webkit/api/public/WebNotificationPresenter.h" |
34 | 36 |
35 using WebKit::WebNotificationPresenter; | 37 using WebKit::WebNotificationPresenter; |
36 | 38 |
37 // Creates a data:xxxx URL which contains the full HTML for a notification | 39 // Creates a data:xxxx URL which contains the full HTML for a notification |
38 // using supplied icon, title, and text, run through a template which contains | 40 // using supplied icon, title, and text, run through a template which contains |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 int route_id_; | 95 int route_id_; |
94 int request_id_; | 96 int request_id_; |
95 }; | 97 }; |
96 | 98 |
97 // The delegate for the infobar shown when an origin requests notification | 99 // The delegate for the infobar shown when an origin requests notification |
98 // permissions. | 100 // permissions. |
99 class NotificationPermissionInfoBarDelegate : public ConfirmInfoBarDelegate { | 101 class NotificationPermissionInfoBarDelegate : public ConfirmInfoBarDelegate { |
100 public: | 102 public: |
101 NotificationPermissionInfoBarDelegate(TabContents* contents, | 103 NotificationPermissionInfoBarDelegate(TabContents* contents, |
102 const GURL& origin, | 104 const GURL& origin, |
| 105 const std::wstring& display_name, |
103 int process_id, | 106 int process_id, |
104 int route_id, | 107 int route_id, |
105 int callback_context) | 108 int callback_context) |
106 : ConfirmInfoBarDelegate(contents), | 109 : ConfirmInfoBarDelegate(contents), |
107 origin_(origin), | 110 origin_(origin), |
| 111 display_name_(display_name), |
108 profile_(contents->profile()), | 112 profile_(contents->profile()), |
109 process_id_(process_id), | 113 process_id_(process_id), |
110 route_id_(route_id), | 114 route_id_(route_id), |
111 callback_context_(callback_context), | 115 callback_context_(callback_context), |
112 action_taken_(false) { | 116 action_taken_(false) { |
113 } | 117 } |
114 | 118 |
115 // Overridden from ConfirmInfoBarDelegate: | 119 // Overridden from ConfirmInfoBarDelegate: |
116 virtual void InfoBarClosed() { | 120 virtual void InfoBarClosed() { |
117 if (!action_taken_) | 121 if (!action_taken_) |
118 UMA_HISTOGRAM_COUNTS("NotificationPermissionRequest.Ignored", 1); | 122 UMA_HISTOGRAM_COUNTS("NotificationPermissionRequest.Ignored", 1); |
119 | 123 |
120 ChromeThread::PostTask( | 124 ChromeThread::PostTask( |
121 ChromeThread::IO, FROM_HERE, | 125 ChromeThread::IO, FROM_HERE, |
122 new NotificationPermissionCallbackTask( | 126 new NotificationPermissionCallbackTask( |
123 process_id_, route_id_, callback_context_)); | 127 process_id_, route_id_, callback_context_)); |
124 | 128 |
125 delete this; | 129 delete this; |
126 } | 130 } |
127 | 131 |
128 virtual std::wstring GetMessageText() const { | 132 virtual std::wstring GetMessageText() const { |
129 return l10n_util::GetStringF(IDS_NOTIFICATION_PERMISSIONS, | 133 return l10n_util::GetStringF(IDS_NOTIFICATION_PERMISSIONS, display_name_); |
130 UTF8ToWide(origin_.spec())); | |
131 } | 134 } |
132 | 135 |
133 virtual SkBitmap* GetIcon() const { | 136 virtual SkBitmap* GetIcon() const { |
134 return ResourceBundle::GetSharedInstance().GetBitmapNamed( | 137 return ResourceBundle::GetSharedInstance().GetBitmapNamed( |
135 IDR_PRODUCT_ICON_32); | 138 IDR_PRODUCT_ICON_32); |
136 } | 139 } |
137 | 140 |
138 virtual int GetButtons() const { | 141 virtual int GetButtons() const { |
139 return BUTTON_OK | BUTTON_CANCEL | BUTTON_OK_DEFAULT; | 142 return BUTTON_OK | BUTTON_CANCEL | BUTTON_OK_DEFAULT; |
140 } | 143 } |
(...skipping 15 matching lines...) Expand all Loading... |
156 UMA_HISTOGRAM_COUNTS("NotificationPermissionRequest.Denied", 1); | 159 UMA_HISTOGRAM_COUNTS("NotificationPermissionRequest.Denied", 1); |
157 profile_->GetDesktopNotificationService()->DenyPermission(origin_); | 160 profile_->GetDesktopNotificationService()->DenyPermission(origin_); |
158 action_taken_ = true; | 161 action_taken_ = true; |
159 return true; | 162 return true; |
160 } | 163 } |
161 | 164 |
162 private: | 165 private: |
163 // The origin we are asking for permissions on. | 166 // The origin we are asking for permissions on. |
164 GURL origin_; | 167 GURL origin_; |
165 | 168 |
| 169 // The display name for the origin to be displayed. Will be different from |
| 170 // origin_ for extensions. |
| 171 std::wstring display_name_; |
| 172 |
166 // The Profile that we restore sessions from. | 173 // The Profile that we restore sessions from. |
167 Profile* profile_; | 174 Profile* profile_; |
168 | 175 |
169 // The callback information that tells us how to respond to javascript via | 176 // The callback information that tells us how to respond to javascript via |
170 // the correct RenderView. | 177 // the correct RenderView. |
171 int process_id_; | 178 int process_id_; |
172 int route_id_; | 179 int route_id_; |
173 int callback_context_; | 180 int callback_context_; |
174 | 181 |
175 // Whether the user clicked one of the buttons. | 182 // Whether the user clicked one of the buttons. |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | 260 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
254 // Show an info bar requesting permission. | 261 // Show an info bar requesting permission. |
255 Browser* browser = BrowserList::GetLastActive(); | 262 Browser* browser = BrowserList::GetLastActive(); |
256 if (!browser) { | 263 if (!browser) { |
257 // Reached during ui tests. | 264 // Reached during ui tests. |
258 return; | 265 return; |
259 } | 266 } |
260 TabContents* tab = browser->GetSelectedTabContents(); | 267 TabContents* tab = browser->GetSelectedTabContents(); |
261 if (!tab) | 268 if (!tab) |
262 return; | 269 return; |
| 270 |
| 271 std::wstring display_name = DisplayNameForOrigin(origin); |
| 272 |
263 tab->AddInfoBar(new NotificationPermissionInfoBarDelegate( | 273 tab->AddInfoBar(new NotificationPermissionInfoBarDelegate( |
264 tab, origin, process_id, route_id, callback_context)); | 274 tab, origin, display_name, process_id, route_id, callback_context)); |
265 } | 275 } |
266 | 276 |
267 void DesktopNotificationService::ShowNotification( | 277 void DesktopNotificationService::ShowNotification( |
268 const Notification& notification) { | 278 const Notification& notification) { |
269 ui_manager_->Add(notification, profile_); | 279 ui_manager_->Add(notification, profile_); |
270 } | 280 } |
271 | 281 |
272 bool DesktopNotificationService::CancelDesktopNotification( | 282 bool DesktopNotificationService::CancelDesktopNotification( |
273 int process_id, int route_id, int notification_id) { | 283 int process_id, int route_id, int notification_id) { |
274 scoped_refptr<NotificationObjectProxy> proxy( | 284 scoped_refptr<NotificationObjectProxy> proxy( |
275 new NotificationObjectProxy(process_id, route_id, notification_id, | 285 new NotificationObjectProxy(process_id, route_id, notification_id, |
276 false)); | 286 false)); |
277 Notification notif(GURL(), GURL(), proxy); | 287 Notification notif(GURL(), GURL(), L"", proxy); |
278 return ui_manager_->Cancel(notif); | 288 return ui_manager_->Cancel(notif); |
279 } | 289 } |
280 | 290 |
281 | 291 |
282 bool DesktopNotificationService::ShowDesktopNotification( | 292 bool DesktopNotificationService::ShowDesktopNotification( |
283 const GURL& origin, const GURL& url, int process_id, int route_id, | 293 const GURL& origin, const GURL& url, int process_id, int route_id, |
284 NotificationSource source, int notification_id) { | 294 NotificationSource source, int notification_id) { |
285 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | 295 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
286 NotificationObjectProxy* proxy = | 296 NotificationObjectProxy* proxy = |
287 new NotificationObjectProxy(process_id, route_id, | 297 new NotificationObjectProxy(process_id, route_id, |
288 notification_id, | 298 notification_id, |
289 source == WorkerNotification); | 299 source == WorkerNotification); |
290 Notification notif(origin, url, proxy); | 300 Notification notif(origin, url, DisplayNameForOrigin(origin), proxy); |
291 ShowNotification(notif); | 301 ShowNotification(notif); |
292 return true; | 302 return true; |
293 } | 303 } |
294 | 304 |
295 bool DesktopNotificationService::ShowDesktopNotificationText( | 305 bool DesktopNotificationService::ShowDesktopNotificationText( |
296 const GURL& origin, const GURL& icon, const string16& title, | 306 const GURL& origin, const GURL& icon, const string16& title, |
297 const string16& text, int process_id, int route_id, | 307 const string16& text, int process_id, int route_id, |
298 NotificationSource source, int notification_id) { | 308 NotificationSource source, int notification_id) { |
299 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | 309 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
300 NotificationObjectProxy* proxy = | 310 NotificationObjectProxy* proxy = |
301 new NotificationObjectProxy(process_id, route_id, | 311 new NotificationObjectProxy(process_id, route_id, |
302 notification_id, | 312 notification_id, |
303 source == WorkerNotification); | 313 source == WorkerNotification); |
304 // "upconvert" the string parameters to a data: URL. | 314 // "upconvert" the string parameters to a data: URL. |
305 string16 data_url = CreateDataUrl(icon, title, text); | 315 string16 data_url = CreateDataUrl(icon, title, text); |
306 Notification notif(origin, GURL(data_url), proxy); | 316 Notification notif( |
| 317 origin, GURL(data_url), DisplayNameForOrigin(origin), proxy); |
307 ShowNotification(notif); | 318 ShowNotification(notif); |
308 return true; | 319 return true; |
309 } | 320 } |
| 321 |
| 322 std::wstring DesktopNotificationService::DisplayNameForOrigin( |
| 323 const GURL& origin) { |
| 324 // If the source is an extension, lookup the display name. |
| 325 if (origin.SchemeIs(chrome::kExtensionScheme)) { |
| 326 ExtensionsService* ext_service = profile_->GetExtensionsService(); |
| 327 if (ext_service) { |
| 328 Extension* extension = ext_service->GetExtensionByURL(origin); |
| 329 if (extension) |
| 330 return ASCIIToWide(extension->name()); |
| 331 } |
| 332 } |
| 333 return UTF8ToWide(origin.spec()); |
| 334 } |
OLD | NEW |