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/extensions/api/notifications/notifications_api.h" | 5 #include "chrome/browser/extensions/api/notifications/notifications_api.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/feature_list.h" | 12 #include "base/feature_list.h" |
13 #include "base/guid.h" | 13 #include "base/guid.h" |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
16 #include "base/metrics/histogram_macros.h" | 16 #include "base/metrics/histogram_macros.h" |
17 #include "base/rand_util.h" | 17 #include "base/rand_util.h" |
18 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
19 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
20 #include "base/time/time.h" | 20 #include "base/time/time.h" |
21 #include "build/build_config.h" | 21 #include "build/build_config.h" |
22 #include "chrome/browser/extensions/api/notifications/extension_notification_dis play_helper.h" | 22 #include "chrome/browser/extensions/api/notifications/extension_notification_dis play_helper.h" |
23 #include "chrome/browser/extensions/api/notifications/extension_notification_dis play_helper_factory.h" | 23 #include "chrome/browser/extensions/api/notifications/extension_notification_dis play_helper_factory.h" |
24 #include "chrome/browser/notifications/notification.h" | 24 #include "chrome/browser/notifications/notification.h" |
25 #include "chrome/browser/notifications/notifier_state_tracker.h" | 25 #include "chrome/browser/notifications/notifier_state_tracker.h" |
26 #include "chrome/browser/notifications/notifier_state_tracker_factory.h" | 26 #include "chrome/browser/notifications/notifier_state_tracker_factory.h" |
27 #include "chrome/browser/profiles/profile.h" | 27 #include "chrome/browser/profiles/profile.h" |
28 #include "chrome/common/chrome_features.h" | |
28 #include "chrome/common/extensions/api/notifications/notification_style.h" | 29 #include "chrome/common/extensions/api/notifications/notification_style.h" |
30 #include "chrome/common/features.h" | |
29 #include "components/keyed_service/content/browser_context_keyed_service_shutdow n_notifier_factory.h" | 31 #include "components/keyed_service/content/browser_context_keyed_service_shutdow n_notifier_factory.h" |
30 #include "components/keyed_service/core/keyed_service_shutdown_notifier.h" | 32 #include "components/keyed_service/core/keyed_service_shutdown_notifier.h" |
31 #include "content/public/browser/render_process_host.h" | 33 #include "content/public/browser/render_process_host.h" |
32 #include "content/public/browser/render_view_host.h" | 34 #include "content/public/browser/render_view_host.h" |
33 #include "content/public/browser/web_contents.h" | 35 #include "content/public/browser/web_contents.h" |
34 #include "extensions/browser/app_window/app_window.h" | 36 #include "extensions/browser/app_window/app_window.h" |
35 #include "extensions/browser/app_window/app_window_registry.h" | 37 #include "extensions/browser/app_window/app_window_registry.h" |
36 #include "extensions/browser/app_window/native_app_window.h" | 38 #include "extensions/browser/app_window/native_app_window.h" |
37 #include "extensions/browser/event_router.h" | 39 #include "extensions/browser/event_router.h" |
38 #include "extensions/browser/extension_system_provider.h" | 40 #include "extensions/browser/extension_system_provider.h" |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
197 ShutdownNotifierFactory() | 199 ShutdownNotifierFactory() |
198 : BrowserContextKeyedServiceShutdownNotifierFactory( | 200 : BrowserContextKeyedServiceShutdownNotifierFactory( |
199 "NotificationsApiDelegate") { | 201 "NotificationsApiDelegate") { |
200 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); | 202 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); |
201 } | 203 } |
202 ~ShutdownNotifierFactory() override {} | 204 ~ShutdownNotifierFactory() override {} |
203 | 205 |
204 DISALLOW_COPY_AND_ASSIGN(ShutdownNotifierFactory); | 206 DISALLOW_COPY_AND_ASSIGN(ShutdownNotifierFactory); |
205 }; | 207 }; |
206 | 208 |
207 class NotificationsApiDelegate : public NotificationDelegate { | 209 // Temporary native notification api delagate, it is only used |
210 // to extract the delegate id. | |
211 #if BUILDFLAG(ENABLE_NATIVE_NOTIFICATIONS) | |
212 class NativeNotificationApiDelegate : public NotificationDelegate { | |
208 public: | 213 public: |
209 NotificationsApiDelegate(ChromeAsyncExtensionFunction* api_function, | 214 NativeNotificationApiDelegate(const std::string& extension_id, |
210 Profile* profile, | 215 const std::string& notification_id) |
211 const std::string& extension_id, | 216 : scoped_notification_id_( |
212 const std::string& id) | 217 CreateScopedIdentifier(extension_id, notification_id)) {} |
218 | |
219 std::string id() const override { return scoped_notification_id_; } | |
220 | |
221 private: | |
222 ~NativeNotificationApiDelegate() override = default; | |
223 const std::string scoped_notification_id_; | |
224 | |
225 DISALLOW_COPY_AND_ASSIGN(NativeNotificationApiDelegate); | |
226 }; | |
227 #endif // BUILDFLAG(ENABLE_NATIVE_NOTIFICATIONS) | |
228 | |
229 // Message center based notification delegate with all the functionality. | |
230 class NotificationApiDelegate : public NotificationDelegate { | |
231 public: | |
232 NotificationApiDelegate(ChromeAsyncExtensionFunction* api_function, | |
233 Profile* profile, | |
234 const std::string& extension_id, | |
235 const std::string& id) | |
213 : api_function_(api_function), | 236 : api_function_(api_function), |
214 event_router_(EventRouter::Get(profile)), | 237 event_router_(EventRouter::Get(profile)), |
215 display_helper_( | 238 display_helper_( |
216 ExtensionNotificationDisplayHelperFactory::GetForProfile(profile)), | 239 ExtensionNotificationDisplayHelperFactory::GetForProfile(profile)), |
217 extension_id_(extension_id), | 240 extension_id_(extension_id), |
218 id_(id), | 241 id_(id), |
219 scoped_id_(CreateScopedIdentifier(extension_id, id)) { | 242 scoped_id_(CreateScopedIdentifier(extension_id, id)) { |
220 DCHECK(api_function_); | 243 DCHECK(api_function_); |
221 DCHECK(display_helper_); | 244 DCHECK(display_helper_); |
222 | 245 |
223 shutdown_notifier_subscription_ = | 246 shutdown_notifier_subscription_ = |
224 ShutdownNotifierFactory::GetInstance()->Get(profile)->Subscribe( | 247 ShutdownNotifierFactory::GetInstance()->Get(profile)->Subscribe( |
225 base::Bind(&NotificationsApiDelegate::Shutdown, | 248 base::Bind(&NotificationApiDelegate::Shutdown, |
226 base::Unretained(this))); | 249 base::Unretained(this))); |
227 } | 250 } |
228 | 251 |
229 void Close(bool by_user) override { | 252 void Close(bool by_user) override { |
230 EventRouter::UserGestureState gesture = | 253 EventRouter::UserGestureState gesture = |
231 by_user ? EventRouter::USER_GESTURE_ENABLED | 254 by_user ? EventRouter::USER_GESTURE_ENABLED |
232 : EventRouter::USER_GESTURE_NOT_ENABLED; | 255 : EventRouter::USER_GESTURE_NOT_ENABLED; |
233 std::unique_ptr<base::ListValue> args(CreateBaseEventArgs()); | 256 std::unique_ptr<base::ListValue> args(CreateBaseEventArgs()); |
234 args->AppendBoolean(by_user); | 257 args->AppendBoolean(by_user); |
235 SendEvent(events::NOTIFICATIONS_ON_CLOSED, | 258 SendEvent(events::NOTIFICATIONS_ON_CLOSED, |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
286 | 309 |
287 } | 310 } |
288 return enabled; | 311 return enabled; |
289 } | 312 } |
290 } | 313 } |
291 | 314 |
292 return false; | 315 return false; |
293 } | 316 } |
294 | 317 |
295 private: | 318 private: |
296 ~NotificationsApiDelegate() override {} | 319 ~NotificationApiDelegate() override {} |
297 | 320 |
298 void SendEvent(events::HistogramValue histogram_value, | 321 void SendEvent(events::HistogramValue histogram_value, |
299 const std::string& name, | 322 const std::string& name, |
300 EventRouter::UserGestureState user_gesture, | 323 EventRouter::UserGestureState user_gesture, |
301 std::unique_ptr<base::ListValue> args) { | 324 std::unique_ptr<base::ListValue> args) { |
302 if (!event_router_) | 325 if (!event_router_) |
303 return; | 326 return; |
304 | 327 |
305 std::unique_ptr<Event> event( | 328 std::unique_ptr<Event> event( |
306 new Event(histogram_value, name, std::move(args))); | 329 new Event(histogram_value, name, std::move(args))); |
(...skipping 21 matching lines...) Expand all Loading... | |
328 EventRouter* event_router_; | 351 EventRouter* event_router_; |
329 ExtensionNotificationDisplayHelper* display_helper_; | 352 ExtensionNotificationDisplayHelper* display_helper_; |
330 | 353 |
331 const std::string extension_id_; | 354 const std::string extension_id_; |
332 const std::string id_; | 355 const std::string id_; |
333 const std::string scoped_id_; | 356 const std::string scoped_id_; |
334 | 357 |
335 std::unique_ptr<KeyedServiceShutdownNotifier::Subscription> | 358 std::unique_ptr<KeyedServiceShutdownNotifier::Subscription> |
336 shutdown_notifier_subscription_; | 359 shutdown_notifier_subscription_; |
337 | 360 |
338 DISALLOW_COPY_AND_ASSIGN(NotificationsApiDelegate); | 361 DISALLOW_COPY_AND_ASSIGN(NotificationApiDelegate); |
339 }; | 362 }; |
340 | 363 |
341 } // namespace | 364 } // namespace |
342 | 365 |
343 bool NotificationsApiFunction::IsNotificationsApiAvailable() { | 366 bool NotificationsApiFunction::IsNotificationsApiAvailable() { |
344 // We need to check this explicitly rather than letting | 367 // We need to check this explicitly rather than letting |
345 // _permission_features.json enforce it, because we're sharing the | 368 // _permission_features.json enforce it, because we're sharing the |
346 // chrome.notifications permissions namespace with WebKit notifications. | 369 // chrome.notifications permissions namespace with WebKit notifications. |
347 return extension()->is_platform_app() || extension()->is_extension(); | 370 return extension()->is_platform_app() || extension()->is_extension(); |
348 } | 371 } |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
484 for (const NotificationItem& api_item : *options->items) { | 507 for (const NotificationItem& api_item : *options->items) { |
485 optional_fields.items.push_back(message_center::NotificationItem( | 508 optional_fields.items.push_back(message_center::NotificationItem( |
486 base::UTF8ToUTF16(api_item.title), | 509 base::UTF8ToUTF16(api_item.title), |
487 base::UTF8ToUTF16(api_item.message))); | 510 base::UTF8ToUTF16(api_item.message))); |
488 } | 511 } |
489 } | 512 } |
490 | 513 |
491 if (options->is_clickable.get()) | 514 if (options->is_clickable.get()) |
492 optional_fields.clickable = *options->is_clickable; | 515 optional_fields.clickable = *options->is_clickable; |
493 | 516 |
494 NotificationsApiDelegate* api_delegate(new NotificationsApiDelegate( | 517 // Create the notification api delegate. Ownership passed to the notification. |
495 this, GetProfile(), extension_->id(), id)); // ownership is passed to | 518 NotificationDelegate* api_delegate; |
496 // Notification | 519 #if BUILDFLAG(ENABLE_NATIVE_NOTIFICATIONS) |
Peter Beverloo
2017/05/15 15:56:52
Here and above: These #if-defs are ugly. Please be
Miguel Garcia
2017/05/16 11:01:34
Good point I provided a high level explanation in
| |
520 if (base::FeatureList::IsEnabled(features::kNativeNotifications)) | |
521 api_delegate = new NativeNotificationApiDelegate(extension_->id(), id); | |
522 else | |
523 api_delegate = | |
524 new NotificationApiDelegate(this, GetProfile(), extension_->id(), id); | |
Peter Beverloo
2017/05/15 15:56:52
if () {
} else {
}
Miguel Garcia
2017/05/16 11:01:34
Done.
| |
525 #else | |
526 api_delegate = | |
527 new NotificationApiDelegate(this, GetProfile(), extension_->id(), id); | |
528 #endif // BUILDFLAG(ENABLE_NATIVE_NOTIFICATIONS) | |
529 | |
497 Notification notification( | 530 Notification notification( |
498 type, title, message, icon, | 531 type, title, message, icon, |
499 message_center::NotifierId(message_center::NotifierId::APPLICATION, | 532 message_center::NotifierId(message_center::NotifierId::APPLICATION, |
500 extension_->id()), | 533 extension_->id()), |
501 base::UTF8ToUTF16(extension_->name()), extension_->url(), | 534 base::UTF8ToUTF16(extension_->name()), extension_->url(), |
502 api_delegate->id(), optional_fields, api_delegate); | 535 api_delegate->id(), optional_fields, api_delegate); |
503 | 536 |
504 // Apply the "requireInteraction" flag. The value defaults to false. | 537 // Apply the "requireInteraction" flag. The value defaults to false. |
505 notification.set_never_timeout(options->require_interaction && | 538 notification.set_never_timeout(options->require_interaction && |
506 *options->require_interaction); | 539 *options->require_interaction); |
(...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
830 : api::notifications::PERMISSION_LEVEL_DENIED; | 863 : api::notifications::PERMISSION_LEVEL_DENIED; |
831 | 864 |
832 SetResult( | 865 SetResult( |
833 base::MakeUnique<base::Value>(api::notifications::ToString(result))); | 866 base::MakeUnique<base::Value>(api::notifications::ToString(result))); |
834 SendResponse(true); | 867 SendResponse(true); |
835 | 868 |
836 return true; | 869 return true; |
837 } | 870 } |
838 | 871 |
839 } // namespace extensions | 872 } // namespace extensions |
OLD | NEW |