~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Tools/DumpRenderTree/mac/MockWebNotificationProvider.mm

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
29
#import "config.h"
 
30
#import "MockWebNotificationProvider.h"
 
31
 
 
32
#import <WebKit/WebSecurityOriginPrivate.h>
 
33
 
 
34
@implementation MockWebNotificationProvider
 
35
 
 
36
+ (MockWebNotificationProvider *)shared
 
37
{
 
38
    static MockWebNotificationProvider *provider = [[MockWebNotificationProvider alloc] init];
 
39
    return provider;
 
40
}
 
41
 
 
42
- (id)init
 
43
{
 
44
    if (!(self = [super init]))
 
45
        return nil;
 
46
    _permissions.adoptNS([[NSMutableDictionary alloc] init]);
 
47
    return self;
 
48
}
 
49
 
 
50
- (void)registerWebView:(WebView *)webView
 
51
{
 
52
    ASSERT(!_registeredWebViews.contains(webView));
 
53
    _registeredWebViews.add(webView);
 
54
}
 
55
 
 
56
- (void)unregisterWebView:(WebView *)webView
 
57
{
 
58
    ASSERT(_registeredWebViews.contains(webView));
 
59
    _registeredWebViews.remove(webView);
 
60
}
 
61
 
 
62
- (void)showNotification:(WebNotification *)notification fromWebView:(WebView *)webView
 
63
{
 
64
    ASSERT(_registeredWebViews.contains(webView));
 
65
 
 
66
    uint64_t notificationID = [notification notificationID];
 
67
    _notifications.add(notificationID, notification);
 
68
    _notificationViewMap.add(notificationID, webView);
 
69
 
 
70
    [webView _notificationDidShow:notificationID];
 
71
}
 
72
 
 
73
- (void)cancelNotification:(WebNotification *)notification
 
74
{
 
75
    uint64_t notificationID = [notification notificationID];
 
76
    ASSERT(_notifications.contains(notificationID));
 
77
 
 
78
    [_notificationViewMap.get(notificationID) _notificationsDidClose:[NSArray arrayWithObject:[NSNumber numberWithUnsignedLongLong:notificationID]]];
 
79
}
 
80
 
 
81
- (void)notificationDestroyed:(WebNotification *)notification
 
82
{
 
83
    _notifications.remove([notification notificationID]);
 
84
    _notificationViewMap.remove([notification notificationID]);
 
85
}
 
86
 
 
87
- (void)clearNotifications:(NSArray *)notificationIDs
 
88
{
 
89
    for (NSNumber *notificationID in notificationIDs) {
 
90
        uint64_t id = [notificationID unsignedLongLongValue];
 
91
        RetainPtr<WebNotification> notification = _notifications.take(id);
 
92
        _notificationViewMap.remove(id);
 
93
    }
 
94
}
 
95
 
 
96
- (void)webView:(WebView *)webView didShowNotification:(uint64_t)notificationID
 
97
{
 
98
    [_notifications.get(notificationID).get() dispatchShowEvent];
 
99
}
 
100
 
 
101
- (void)webView:(WebView *)webView didClickNotification:(uint64_t)notificationID
 
102
{
 
103
    [_notifications.get(notificationID).get() dispatchClickEvent];
 
104
}
 
105
 
 
106
- (void)webView:(WebView *)webView didCloseNotifications:(NSArray *)notificationIDs
 
107
{
 
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);
 
115
    }
 
116
}
 
117
 
 
118
- (void)simulateWebNotificationClick:(uint64_t)notificationID
 
119
{
 
120
    ASSERT(_notifications.contains(notificationID));
 
121
    [_notificationViewMap.get(notificationID) _notificationDidClick:notificationID];
 
122
}
 
123
 
 
124
- (WebNotificationPermission)policyForOrigin:(WebSecurityOrigin *)origin
 
125
{
 
126
    NSNumber *permission = [_permissions.get() objectForKey:[origin stringValue]];
 
127
    if (!permission)
 
128
        return WebNotificationPermissionNotAllowed;
 
129
    if ([permission boolValue])
 
130
        return WebNotificationPermissionAllowed;
 
131
    return WebNotificationPermissionDenied;
 
132
}
 
133
 
 
134
- (void)setWebNotificationOrigin:(NSString *)origin permission:(BOOL)allowed
 
135
{
 
136
    [_permissions.get() setObject:[NSNumber numberWithBool:allowed] forKey:origin];
 
137
}
 
138
 
 
139
- (void)removeAllWebNotificationPermissions
 
140
{
 
141
    [_permissions.get() removeAllObjects];
 
142
}
 
143
 
 
144
- (void)reset
 
145
{
 
146
    _notifications.clear();
 
147
    _notificationViewMap.clear();
 
148
    [self removeAllWebNotificationPermissions];
 
149
}
 
150
 
 
151
@end