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

Side by Side Diff: chrome/browser/notifications/desktop_notification_service.cc

Issue 432005: Make notifications permission available in the extension manifest and hook up... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 11 years, 1 month 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) 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"
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
184 bool action_taken_; 184 bool action_taken_;
185 185
186 DISALLOW_COPY_AND_ASSIGN(NotificationPermissionInfoBarDelegate); 186 DISALLOW_COPY_AND_ASSIGN(NotificationPermissionInfoBarDelegate);
187 }; 187 };
188 188
189 DesktopNotificationService::DesktopNotificationService(Profile* profile, 189 DesktopNotificationService::DesktopNotificationService(Profile* profile,
190 NotificationUIManager* ui_manager) 190 NotificationUIManager* ui_manager)
191 : profile_(profile), 191 : profile_(profile),
192 ui_manager_(ui_manager) { 192 ui_manager_(ui_manager) {
193 InitPrefs(); 193 InitPrefs();
194
195 // Listen for new extension installations so we can cache permissions.
196 notification_registrar_.Add(this,
197 NotificationType::EXTENSION_LOADED,
198 Source<Profile>(profile_));
194 } 199 }
195 200
196 DesktopNotificationService::~DesktopNotificationService() { 201 DesktopNotificationService::~DesktopNotificationService() {
197 } 202 }
198 203
199 // Initialize the cache with the allowed and denied origins, or 204 // Initialize the cache with the allowed and denied origins, or
200 // create the preferences if they don't exist yet. 205 // create the preferences if they don't exist yet.
201 void DesktopNotificationService::InitPrefs() { 206 void DesktopNotificationService::InitPrefs() {
202 PrefService* prefs = profile_->GetPrefs(); 207 PrefService* prefs = profile_->GetPrefs();
203 const ListValue* allowed_sites; 208 const ListValue* allowed_sites;
204 const ListValue* denied_sites; 209 const ListValue* denied_sites;
205 210
206 if (!prefs->FindPreference(prefs::kDesktopNotificationAllowedOrigins)) 211 if (!prefs->FindPreference(prefs::kDesktopNotificationAllowedOrigins))
207 prefs->RegisterListPref(prefs::kDesktopNotificationAllowedOrigins); 212 prefs->RegisterListPref(prefs::kDesktopNotificationAllowedOrigins);
208 allowed_sites = prefs->GetList(prefs::kDesktopNotificationAllowedOrigins); 213 allowed_sites = prefs->GetList(prefs::kDesktopNotificationAllowedOrigins);
209 214
210 if (!prefs->FindPreference(prefs::kDesktopNotificationDeniedOrigins)) 215 if (!prefs->FindPreference(prefs::kDesktopNotificationDeniedOrigins))
211 prefs->RegisterListPref(prefs::kDesktopNotificationDeniedOrigins); 216 prefs->RegisterListPref(prefs::kDesktopNotificationDeniedOrigins);
212 denied_sites = prefs->GetList(prefs::kDesktopNotificationDeniedOrigins); 217 denied_sites = prefs->GetList(prefs::kDesktopNotificationDeniedOrigins);
213 218
214 prefs_cache_ = new NotificationsPrefsCache(allowed_sites, denied_sites); 219 prefs_cache_ = new NotificationsPrefsCache(allowed_sites, denied_sites);
220
221 ExtensionsService* ext_service = profile_->GetExtensionsService();
222 if (ext_service) {
223 const ExtensionList* extensions = ext_service->extensions();
224 for (ExtensionList::const_iterator iter = extensions->begin();
225 iter != extensions->end(); ++iter) {
226 if ((*iter)->HasApiPermission(Extension::kNotificationPermission)) {
227 prefs_cache_->CacheAllowedOrigin((*iter)->url());
Aaron Boodman 2009/11/24 20:38:14 Why do you need to store this state again? It is a
John Gregg 2009/11/24 20:52:44 I need this state on the IO thread to respond to a
228 }
229 }
230 }
231 }
232
233 void DesktopNotificationService::Observe(NotificationType type,
234 const NotificationSource& source,
235 const NotificationDetails& details) {
236 if (type != NotificationType::EXTENSION_LOADED) {
237 NOTREACHED();
238 return;
239 }
240
241 Details<Extension> extension = static_cast<Details<Extension> >(details);
242 if (extension->HasApiPermission(Extension::kNotificationPermission)) {
243 GrantPermission(extension->url());
244 }
215 } 245 }
216 246
217 void DesktopNotificationService::GrantPermission(const GURL& origin) { 247 void DesktopNotificationService::GrantPermission(const GURL& origin) {
218 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); 248 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
219 PrefService* prefs = profile_->GetPrefs(); 249 PrefService* prefs = profile_->GetPrefs();
220 ListValue* allowed_sites = 250 ListValue* allowed_sites =
221 prefs->GetMutableList(prefs::kDesktopNotificationAllowedOrigins); 251 prefs->GetMutableList(prefs::kDesktopNotificationAllowedOrigins);
222 ListValue* denied_sites = 252 ListValue* denied_sites =
223 prefs->GetMutableList(prefs::kDesktopNotificationDeniedOrigins); 253 prefs->GetMutableList(prefs::kDesktopNotificationDeniedOrigins);
224 // Remove from the black-list and add to the white-list. 254 // Remove from the black-list and add to the white-list.
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
285 scoped_refptr<NotificationObjectProxy> proxy( 315 scoped_refptr<NotificationObjectProxy> proxy(
286 new NotificationObjectProxy(process_id, route_id, notification_id, 316 new NotificationObjectProxy(process_id, route_id, notification_id,
287 false)); 317 false));
288 Notification notif(GURL(), GURL(), L"", proxy); 318 Notification notif(GURL(), GURL(), L"", proxy);
289 return ui_manager_->Cancel(notif); 319 return ui_manager_->Cancel(notif);
290 } 320 }
291 321
292 322
293 bool DesktopNotificationService::ShowDesktopNotification( 323 bool DesktopNotificationService::ShowDesktopNotification(
294 const GURL& origin, const GURL& url, int process_id, int route_id, 324 const GURL& origin, const GURL& url, int process_id, int route_id,
295 NotificationSource source, int notification_id) { 325 DesktopNotificationSource source, int notification_id) {
296 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); 326 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
297 NotificationObjectProxy* proxy = 327 NotificationObjectProxy* proxy =
298 new NotificationObjectProxy(process_id, route_id, 328 new NotificationObjectProxy(process_id, route_id,
299 notification_id, 329 notification_id,
300 source == WorkerNotification); 330 source == WorkerNotification);
301 Notification notif(origin, url, DisplayNameForOrigin(origin), proxy); 331 Notification notif(origin, url, DisplayNameForOrigin(origin), proxy);
302 ShowNotification(notif); 332 ShowNotification(notif);
303 return true; 333 return true;
304 } 334 }
305 335
306 bool DesktopNotificationService::ShowDesktopNotificationText( 336 bool DesktopNotificationService::ShowDesktopNotificationText(
307 const GURL& origin, const GURL& icon, const string16& title, 337 const GURL& origin, const GURL& icon, const string16& title,
308 const string16& text, int process_id, int route_id, 338 const string16& text, int process_id, int route_id,
309 NotificationSource source, int notification_id) { 339 DesktopNotificationSource source, int notification_id) {
310 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); 340 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
311 NotificationObjectProxy* proxy = 341 NotificationObjectProxy* proxy =
312 new NotificationObjectProxy(process_id, route_id, 342 new NotificationObjectProxy(process_id, route_id,
313 notification_id, 343 notification_id,
314 source == WorkerNotification); 344 source == WorkerNotification);
315 // "upconvert" the string parameters to a data: URL. 345 // "upconvert" the string parameters to a data: URL.
316 string16 data_url = CreateDataUrl(icon, title, text); 346 string16 data_url = CreateDataUrl(icon, title, text);
317 Notification notif( 347 Notification notif(
318 origin, GURL(data_url), DisplayNameForOrigin(origin), proxy); 348 origin, GURL(data_url), DisplayNameForOrigin(origin), proxy);
319 ShowNotification(notif); 349 ShowNotification(notif);
320 return true; 350 return true;
321 } 351 }
322 352
323 std::wstring DesktopNotificationService::DisplayNameForOrigin( 353 std::wstring DesktopNotificationService::DisplayNameForOrigin(
324 const GURL& origin) { 354 const GURL& origin) {
325 // If the source is an extension, lookup the display name. 355 // If the source is an extension, lookup the display name.
326 if (origin.SchemeIs(chrome::kExtensionScheme)) { 356 if (origin.SchemeIs(chrome::kExtensionScheme)) {
327 ExtensionsService* ext_service = profile_->GetExtensionsService(); 357 ExtensionsService* ext_service = profile_->GetExtensionsService();
328 if (ext_service) { 358 if (ext_service) {
329 Extension* extension = ext_service->GetExtensionByURL(origin); 359 Extension* extension = ext_service->GetExtensionByURL(origin);
330 if (extension) 360 if (extension)
331 return ASCIIToWide(extension->name()); 361 return ASCIIToWide(extension->name());
332 } 362 }
333 } 363 }
334 return UTF8ToWide(origin.spec()); 364 return UTF8ToWide(origin.spec());
335 } 365 }
OLDNEW
« no previous file with comments | « chrome/browser/notifications/desktop_notification_service.h ('k') | chrome/common/extensions/extension.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698