2 * Copyright (C) 2005, 2006, 2007 Apple, Inc. All rights reserved.
3 * (C) 2007 Graham Dennis (graham.dennis@gmail.com)
4 * (C) 2007 Eric Seidel <eric@webkit.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
16 * its contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #import "DumpRenderTreeMac.h"
33 #import "DumpRenderTreePasteboard.h"
35 #import <WebKit/WebTypesInternal.h>
37 @interface LocalPasteboard : NSPasteboard
39 NSMutableArray *typesArray;
40 NSMutableSet *typesSet;
41 NSMutableDictionary *dataByType;
42 NSInteger changeCount;
46 static NSMutableDictionary *localPasteboards;
48 @implementation DumpRenderTreePasteboard
50 // Return a local pasteboard so we don't disturb the real pasteboards when running tests.
51 + (NSPasteboard *)_pasteboardWithName:(NSString *)name
53 static int number = 0;
55 name = [NSString stringWithFormat:@"LocalPasteboard%d", ++number];
56 if (!localPasteboards)
57 localPasteboards = [[NSMutableDictionary alloc] init];
58 LocalPasteboard *pasteboard = [localPasteboards objectForKey:name];
61 pasteboard = [[LocalPasteboard alloc] init];
62 [localPasteboards setObject:pasteboard forKey:name];
67 + (void)releaseLocalPasteboards
69 [localPasteboards release];
70 localPasteboards = nil;
73 // Convenience method for JS so that it doesn't have to try and create a NSArray on the objc side instead
74 // of the usual WebScriptObject that is passed around
75 - (NSInteger)declareType:(NSString *)type owner:(id)newOwner
77 return [self declareTypes:[NSArray arrayWithObject:type] owner:newOwner];
82 @implementation LocalPasteboard
86 return NSAllocateObject(self, 0, 0);
91 typesArray = [[NSMutableArray alloc] init];
92 typesSet = [[NSMutableSet alloc] init];
93 dataByType = [[NSMutableDictionary alloc] init];
101 [dataByType release];
110 - (void)releaseGlobally
114 - (NSInteger)declareTypes:(NSArray *)newTypes owner:(id)newOwner
116 [typesArray removeAllObjects];
117 [typesSet removeAllObjects];
118 [dataByType removeAllObjects];
119 return [self addTypes:newTypes owner:newOwner];
122 - (NSInteger)addTypes:(NSArray *)newTypes owner:(id)newOwner
124 unsigned count = [newTypes count];
126 for (i = 0; i < count; ++i) {
127 NSString *type = [newTypes objectAtIndex:i];
128 NSString *setType = [typesSet member:type];
130 setType = [type copy];
131 [typesArray addObject:setType];
132 [typesSet addObject:setType];
135 if (newOwner && [newOwner respondsToSelector:@selector(pasteboard:provideDataForType:)])
136 [newOwner pasteboard:self provideDataForType:setType];
138 return ++changeCount;
141 - (NSInteger)changeCount
151 - (NSString *)availableTypeFromArray:(NSArray *)types
153 unsigned count = [types count];
155 for (i = 0; i < count; ++i) {
156 NSString *type = [types objectAtIndex:i];
157 NSString *setType = [typesSet member:type];
164 - (BOOL)setData:(NSData *)data forType:(NSString *)dataType
167 data = [NSData data];
168 if (![typesSet containsObject:dataType])
170 [dataByType setObject:data forKey:dataType];
175 - (NSData *)dataForType:(NSString *)dataType
177 return [dataByType objectForKey:dataType];
180 - (BOOL)setPropertyList:(id)propertyList forType:(NSString *)dataType
182 CFDataRef data = NULL;
184 data = CFPropertyListCreateXMLData(NULL, propertyList);
185 BOOL result = [self setData:(NSData *)data forType:dataType];
191 - (BOOL)setString:(NSString *)string forType:(NSString *)dataType
193 CFDataRef data = NULL;
195 if ([string length] == 0)
196 data = CFDataCreate(NULL, NULL, 0);
198 data = CFStringCreateExternalRepresentation(NULL, (CFStringRef)string, kCFStringEncodingUTF8, 0);
200 BOOL result = [self setData:(NSData *)data forType:dataType];