2
* Copyright (C) 2012 Apple Inc. All rights reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions
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.
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.
30
#import "MockWebNotificationProvider.h"
32
#import <WebKit/WebSecurityOriginPrivate.h>
34
@implementation MockWebNotificationProvider
36
+ (MockWebNotificationProvider *)shared
38
static MockWebNotificationProvider *provider = [[MockWebNotificationProvider alloc] init];
44
if (!(self = [super init]))
46
_permissions.adoptNS([[NSMutableDictionary alloc] init]);
50
- (void)registerWebView:(WebView *)webView
52
ASSERT(!_registeredWebViews.contains(webView));
53
_registeredWebViews.add(webView);
56
- (void)unregisterWebView:(WebView *)webView
58
ASSERT(_registeredWebViews.contains(webView));
59
_registeredWebViews.remove(webView);
62
- (void)showNotification:(WebNotification *)notification fromWebView:(WebView *)webView
64
ASSERT(_registeredWebViews.contains(webView));
66
uint64_t notificationID = [notification notificationID];
67
_notifications.add(notificationID, notification);
68
_notificationViewMap.add(notificationID, webView);
70
[webView _notificationDidShow:notificationID];
73
- (void)cancelNotification:(WebNotification *)notification
75
uint64_t notificationID = [notification notificationID];
76
ASSERT(_notifications.contains(notificationID));
78
[_notificationViewMap.get(notificationID) _notificationsDidClose:[NSArray arrayWithObject:[NSNumber numberWithUnsignedLongLong:notificationID]]];
81
- (void)notificationDestroyed:(WebNotification *)notification
83
_notifications.remove([notification notificationID]);
84
_notificationViewMap.remove([notification notificationID]);
87
- (void)clearNotifications:(NSArray *)notificationIDs
89
for (NSNumber *notificationID in notificationIDs) {
90
uint64_t id = [notificationID unsignedLongLongValue];
91
RetainPtr<WebNotification> notification = _notifications.take(id);
92
_notificationViewMap.remove(id);
96
- (void)webView:(WebView *)webView didShowNotification:(uint64_t)notificationID
98
[_notifications.get(notificationID).get() dispatchShowEvent];
101
- (void)webView:(WebView *)webView didClickNotification:(uint64_t)notificationID
103
[_notifications.get(notificationID).get() dispatchClickEvent];
106
- (void)webView:(WebView *)webView didCloseNotifications:(NSArray *)notificationIDs
108
for (NSNumber *notificationID in notificationIDs) {
109
uint64_t id = [notificationID unsignedLongLongValue];
110
NotificationIDMap::iterator it = _notifications.find(id);
111
ASSERT(it != _notifications.end());
112
[it->value.get() dispatchCloseEvent];
113
_notifications.remove(it);
114
_notificationViewMap.remove(id);
118
- (void)simulateWebNotificationClick:(uint64_t)notificationID
120
ASSERT(_notifications.contains(notificationID));
121
[_notificationViewMap.get(notificationID) _notificationDidClick:notificationID];
124
- (WebNotificationPermission)policyForOrigin:(WebSecurityOrigin *)origin
126
NSNumber *permission = [_permissions.get() objectForKey:[origin stringValue]];
128
return WebNotificationPermissionNotAllowed;
129
if ([permission boolValue])
130
return WebNotificationPermissionAllowed;
131
return WebNotificationPermissionDenied;
134
- (void)setWebNotificationOrigin:(NSString *)origin permission:(BOOL)allowed
136
[_permissions.get() setObject:[NSNumber numberWithBool:allowed] forKey:origin];
139
- (void)removeAllWebNotificationPermissions
141
[_permissions.get() removeAllObjects];
146
_notifications.clear();
147
_notificationViewMap.clear();
148
[self removeAllWebNotificationPermissions];