MockWebNotificationProvider.mm 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (C) 2012 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
  14. * its contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  18. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #import "config.h"
  29. #import "MockWebNotificationProvider.h"
  30. #import <WebKit/WebSecurityOriginPrivate.h>
  31. @implementation MockWebNotificationProvider
  32. + (MockWebNotificationProvider *)shared
  33. {
  34. static MockWebNotificationProvider *provider = [[MockWebNotificationProvider alloc] init];
  35. return provider;
  36. }
  37. - (id)init
  38. {
  39. if (!(self = [super init]))
  40. return nil;
  41. _permissions = adoptNS([[NSMutableDictionary alloc] init]);
  42. return self;
  43. }
  44. - (void)registerWebView:(WebView *)webView
  45. {
  46. ASSERT(!_registeredWebViews.contains(webView));
  47. _registeredWebViews.add(webView);
  48. }
  49. - (void)unregisterWebView:(WebView *)webView
  50. {
  51. ASSERT(_registeredWebViews.contains(webView));
  52. _registeredWebViews.remove(webView);
  53. }
  54. - (void)showNotification:(WebNotification *)notification fromWebView:(WebView *)webView
  55. {
  56. ASSERT(_registeredWebViews.contains(webView));
  57. uint64_t notificationID = [notification notificationID];
  58. _notifications.add(notificationID, notification);
  59. _notificationViewMap.add(notificationID, webView);
  60. [webView _notificationDidShow:notificationID];
  61. }
  62. - (void)cancelNotification:(WebNotification *)notification
  63. {
  64. uint64_t notificationID = [notification notificationID];
  65. ASSERT(_notifications.contains(notificationID));
  66. [_notificationViewMap.get(notificationID) _notificationsDidClose:[NSArray arrayWithObject:[NSNumber numberWithUnsignedLongLong:notificationID]]];
  67. }
  68. - (void)notificationDestroyed:(WebNotification *)notification
  69. {
  70. _notifications.remove([notification notificationID]);
  71. _notificationViewMap.remove([notification notificationID]);
  72. }
  73. - (void)clearNotifications:(NSArray *)notificationIDs
  74. {
  75. for (NSNumber *notificationID in notificationIDs) {
  76. uint64_t id = [notificationID unsignedLongLongValue];
  77. RetainPtr<WebNotification> notification = _notifications.take(id);
  78. _notificationViewMap.remove(id);
  79. }
  80. }
  81. - (void)webView:(WebView *)webView didShowNotification:(uint64_t)notificationID
  82. {
  83. [_notifications.get(notificationID).get() dispatchShowEvent];
  84. }
  85. - (void)webView:(WebView *)webView didClickNotification:(uint64_t)notificationID
  86. {
  87. [_notifications.get(notificationID).get() dispatchClickEvent];
  88. }
  89. - (void)webView:(WebView *)webView didCloseNotifications:(NSArray *)notificationIDs
  90. {
  91. for (NSNumber *notificationID in notificationIDs) {
  92. uint64_t id = [notificationID unsignedLongLongValue];
  93. NotificationIDMap::iterator it = _notifications.find(id);
  94. ASSERT(it != _notifications.end());
  95. [it->value.get() dispatchCloseEvent];
  96. _notifications.remove(it);
  97. _notificationViewMap.remove(id);
  98. }
  99. }
  100. - (void)simulateWebNotificationClick:(uint64_t)notificationID
  101. {
  102. ASSERT(_notifications.contains(notificationID));
  103. [_notificationViewMap.get(notificationID) _notificationDidClick:notificationID];
  104. }
  105. - (WebNotificationPermission)policyForOrigin:(WebSecurityOrigin *)origin
  106. {
  107. NSNumber *permission = [_permissions.get() objectForKey:[origin stringValue]];
  108. if (!permission)
  109. return WebNotificationPermissionNotAllowed;
  110. if ([permission boolValue])
  111. return WebNotificationPermissionAllowed;
  112. return WebNotificationPermissionDenied;
  113. }
  114. - (void)setWebNotificationOrigin:(NSString *)origin permission:(BOOL)allowed
  115. {
  116. [_permissions.get() setObject:[NSNumber numberWithBool:allowed] forKey:origin];
  117. }
  118. - (void)removeAllWebNotificationPermissions
  119. {
  120. [_permissions.get() removeAllObjects];
  121. }
  122. - (void)reset
  123. {
  124. _notifications.clear();
  125. _notificationViewMap.clear();
  126. [self removeAllWebNotificationPermissions];
  127. }
  128. @end