OLD | NEW |
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/notifications/desktop_notification_service.h" | 5 #include "chrome/browser/notifications/desktop_notification_service.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
9 #include "base/prefs/scoped_user_pref_update.h" | 9 #include "base/prefs/scoped_user_pref_update.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 base::string16(), | 95 base::string16(), |
96 replace_id, | 96 replace_id, |
97 message_center::RichNotificationData(), | 97 message_center::RichNotificationData(), |
98 delegate); | 98 delegate); |
99 g_browser_process->notification_ui_manager()->Add(notification, profile); | 99 g_browser_process->notification_ui_manager()->Add(notification, profile); |
100 return notification.delegate_id(); | 100 return notification.delegate_id(); |
101 } | 101 } |
102 | 102 |
103 DesktopNotificationService::DesktopNotificationService(Profile* profile) | 103 DesktopNotificationService::DesktopNotificationService(Profile* profile) |
104 : PermissionContextBase(profile, CONTENT_SETTINGS_TYPE_NOTIFICATIONS), | 104 : PermissionContextBase(profile, CONTENT_SETTINGS_TYPE_NOTIFICATIONS), |
105 profile_(profile), | 105 profile_(profile) |
106 #if defined(ENABLE_EXTENSIONS) | 106 #if defined(ENABLE_EXTENSIONS) |
107 extension_registry_observer_(this), | 107 , |
| 108 extension_registry_observer_(this) |
108 #endif | 109 #endif |
109 weak_factory_(this) { | 110 { |
110 OnStringListPrefChanged( | 111 OnStringListPrefChanged( |
111 prefs::kMessageCenterDisabledExtensionIds, &disabled_extension_ids_); | 112 prefs::kMessageCenterDisabledExtensionIds, &disabled_extension_ids_); |
112 OnStringListPrefChanged( | 113 OnStringListPrefChanged( |
113 prefs::kMessageCenterDisabledSystemComponentIds, | 114 prefs::kMessageCenterDisabledSystemComponentIds, |
114 &disabled_system_component_ids_); | 115 &disabled_system_component_ids_); |
115 disabled_extension_id_pref_.Init( | 116 disabled_extension_id_pref_.Init( |
116 prefs::kMessageCenterDisabledExtensionIds, | 117 prefs::kMessageCenterDisabledExtensionIds, |
117 profile_->GetPrefs(), | 118 profile_->GetPrefs(), |
118 base::Bind( | 119 base::Bind( |
119 &DesktopNotificationService::OnStringListPrefChanged, | 120 &DesktopNotificationService::OnStringListPrefChanged, |
(...skipping 15 matching lines...) Expand all Loading... |
135 } | 136 } |
136 | 137 |
137 DesktopNotificationService::~DesktopNotificationService() { | 138 DesktopNotificationService::~DesktopNotificationService() { |
138 } | 139 } |
139 | 140 |
140 void DesktopNotificationService::RequestNotificationPermission( | 141 void DesktopNotificationService::RequestNotificationPermission( |
141 content::WebContents* web_contents, | 142 content::WebContents* web_contents, |
142 const PermissionRequestID& request_id, | 143 const PermissionRequestID& request_id, |
143 const GURL& requesting_origin, | 144 const GURL& requesting_origin, |
144 bool user_gesture, | 145 bool user_gesture, |
145 const NotificationPermissionCallback& callback) { | 146 const base::Callback<void(bool)>& result_callback) { |
146 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 147 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
147 | 148 |
148 #if defined(ENABLE_EXTENSIONS) | 149 #if defined(ENABLE_EXTENSIONS) |
149 extensions::InfoMap* extension_info_map = | 150 extensions::InfoMap* extension_info_map = |
150 extensions::ExtensionSystem::Get(profile_)->info_map(); | 151 extensions::ExtensionSystem::Get(profile_)->info_map(); |
151 const extensions::Extension* extension = NULL; | 152 const extensions::Extension* extension = NULL; |
152 if (extension_info_map) { | 153 if (extension_info_map) { |
153 extensions::ExtensionSet extensions; | 154 extensions::ExtensionSet extensions; |
154 extension_info_map->GetExtensionsWithAPIPermissionForSecurityOrigin( | 155 extension_info_map->GetExtensionsWithAPIPermissionForSecurityOrigin( |
155 requesting_origin, | 156 requesting_origin, |
156 request_id.render_process_id(), | 157 request_id.render_process_id(), |
157 extensions::APIPermission::kNotifications, | 158 extensions::APIPermission::kNotifications, |
158 &extensions); | 159 &extensions); |
159 for (extensions::ExtensionSet::const_iterator iter = extensions.begin(); | 160 for (extensions::ExtensionSet::const_iterator iter = extensions.begin(); |
160 iter != extensions.end(); ++iter) { | 161 iter != extensions.end(); ++iter) { |
161 if (IsNotifierEnabled(NotifierId( | 162 if (IsNotifierEnabled(NotifierId( |
162 NotifierId::APPLICATION, (*iter)->id()))) { | 163 NotifierId::APPLICATION, (*iter)->id()))) { |
163 extension = iter->get(); | 164 extension = iter->get(); |
164 break; | 165 break; |
165 } | 166 } |
166 } | 167 } |
167 } | 168 } |
168 if (IsExtensionWithPermissionOrSuggestInConsole( | 169 if (IsExtensionWithPermissionOrSuggestInConsole( |
169 extensions::APIPermission::kNotifications, | 170 extensions::APIPermission::kNotifications, |
170 extension, | 171 extension, |
171 web_contents->GetRenderViewHost())) { | 172 web_contents->GetRenderViewHost())) { |
172 callback.Run(blink::WebNotificationPermissionAllowed); | 173 result_callback.Run(true); |
173 return; | 174 return; |
174 } | 175 } |
175 #endif | 176 #endif |
176 | 177 |
177 RequestPermission( | 178 RequestPermission(web_contents, |
178 web_contents, | 179 request_id, |
179 request_id, | 180 requesting_origin, |
180 requesting_origin, | 181 user_gesture, |
181 user_gesture, | 182 result_callback); |
182 base::Bind(&DesktopNotificationService::OnNotificationPermissionRequested, | |
183 weak_factory_.GetWeakPtr(), | |
184 callback)); | |
185 } | 183 } |
186 | 184 |
187 void DesktopNotificationService::ShowDesktopNotification( | 185 void DesktopNotificationService::ShowDesktopNotification( |
188 const content::ShowDesktopNotificationHostMsgParams& params, | 186 const content::ShowDesktopNotificationHostMsgParams& params, |
189 content::RenderFrameHost* render_frame_host, | 187 content::RenderFrameHost* render_frame_host, |
190 scoped_ptr<content::DesktopNotificationDelegate> delegate, | 188 scoped_ptr<content::DesktopNotificationDelegate> delegate, |
191 base::Closure* cancel_callback) { | 189 base::Closure* cancel_callback) { |
192 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 190 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
193 const GURL& origin = params.origin; | 191 const GURL& origin = params.origin; |
194 NotificationObjectProxy* proxy = new NotificationObjectProxy(delegate.Pass()); | 192 NotificationObjectProxy* proxy = new NotificationObjectProxy(delegate.Pass()); |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
350 const GURL& embedder_origin, | 348 const GURL& embedder_origin, |
351 bool allowed) { | 349 bool allowed) { |
352 if (allowed) { | 350 if (allowed) { |
353 DesktopNotificationProfileUtil::GrantPermission( | 351 DesktopNotificationProfileUtil::GrantPermission( |
354 profile_, requesting_origin); | 352 profile_, requesting_origin); |
355 } else { | 353 } else { |
356 DesktopNotificationProfileUtil::DenyPermission(profile_, requesting_origin); | 354 DesktopNotificationProfileUtil::DenyPermission(profile_, requesting_origin); |
357 } | 355 } |
358 } | 356 } |
359 | 357 |
360 void DesktopNotificationService::OnNotificationPermissionRequested( | |
361 const NotificationPermissionCallback& callback, bool allowed) { | |
362 blink::WebNotificationPermission permission = allowed ? | |
363 blink::WebNotificationPermissionAllowed : | |
364 blink::WebNotificationPermissionDenied; | |
365 | |
366 callback.Run(permission); | |
367 } | |
368 | |
369 void DesktopNotificationService::FirePermissionLevelChangedEvent( | 358 void DesktopNotificationService::FirePermissionLevelChangedEvent( |
370 const NotifierId& notifier_id, bool enabled) { | 359 const NotifierId& notifier_id, bool enabled) { |
371 #if defined(ENABLE_EXTENSIONS) | 360 #if defined(ENABLE_EXTENSIONS) |
372 DCHECK_EQ(NotifierId::APPLICATION, notifier_id.type); | 361 DCHECK_EQ(NotifierId::APPLICATION, notifier_id.type); |
373 extensions::api::notifications::PermissionLevel permission = | 362 extensions::api::notifications::PermissionLevel permission = |
374 enabled ? extensions::api::notifications::PERMISSION_LEVEL_GRANTED | 363 enabled ? extensions::api::notifications::PERMISSION_LEVEL_GRANTED |
375 : extensions::api::notifications::PERMISSION_LEVEL_DENIED; | 364 : extensions::api::notifications::PERMISSION_LEVEL_DENIED; |
376 scoped_ptr<base::ListValue> args(new base::ListValue()); | 365 scoped_ptr<base::ListValue> args(new base::ListValue()); |
377 args->Append(new base::StringValue( | 366 args->Append(new base::StringValue( |
378 extensions::api::notifications::ToString(permission))); | 367 extensions::api::notifications::ToString(permission))); |
379 scoped_ptr<extensions::Event> event(new extensions::Event( | 368 scoped_ptr<extensions::Event> event(new extensions::Event( |
380 extensions::api::notifications::OnPermissionLevelChanged::kEventName, | 369 extensions::api::notifications::OnPermissionLevelChanged::kEventName, |
381 args.Pass())); | 370 args.Pass())); |
382 extensions::EventRouter::Get(profile_) | 371 extensions::EventRouter::Get(profile_) |
383 ->DispatchEventToExtension(notifier_id.id, event.Pass()); | 372 ->DispatchEventToExtension(notifier_id.id, event.Pass()); |
384 | 373 |
385 // Tell the IO thread that this extension's permission for notifications | 374 // Tell the IO thread that this extension's permission for notifications |
386 // has changed. | 375 // has changed. |
387 extensions::InfoMap* extension_info_map = | 376 extensions::InfoMap* extension_info_map = |
388 extensions::ExtensionSystem::Get(profile_)->info_map(); | 377 extensions::ExtensionSystem::Get(profile_)->info_map(); |
389 BrowserThread::PostTask( | 378 BrowserThread::PostTask( |
390 BrowserThread::IO, FROM_HERE, | 379 BrowserThread::IO, FROM_HERE, |
391 base::Bind(&extensions::InfoMap::SetNotificationsDisabled, | 380 base::Bind(&extensions::InfoMap::SetNotificationsDisabled, |
392 extension_info_map, notifier_id.id, !enabled)); | 381 extension_info_map, notifier_id.id, !enabled)); |
393 #endif | 382 #endif |
394 } | 383 } |
OLD | NEW |