DumpRenderTreePasteboard.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  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>
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  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.
  18. *
  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.
  29. */
  30. #import "config.h"
  31. #import "DumpRenderTreeMac.h"
  32. #import "DumpRenderTreePasteboard.h"
  33. #import <WebKit/WebTypesInternal.h>
  34. @interface LocalPasteboard : NSPasteboard
  35. {
  36. NSMutableArray *typesArray;
  37. NSMutableSet *typesSet;
  38. NSMutableDictionary *dataByType;
  39. NSInteger changeCount;
  40. NSString *pasteboardName;
  41. }
  42. -(id)initWithName:(NSString *)name;
  43. @end
  44. static NSMutableDictionary *localPasteboards;
  45. @implementation DumpRenderTreePasteboard
  46. // Return a local pasteboard so we don't disturb the real pasteboards when running tests.
  47. + (NSPasteboard *)_pasteboardWithName:(NSString *)name
  48. {
  49. static int number = 0;
  50. if (!name)
  51. name = [NSString stringWithFormat:@"LocalPasteboard%d", ++number];
  52. if (!localPasteboards)
  53. localPasteboards = [[NSMutableDictionary alloc] init];
  54. LocalPasteboard *pasteboard = [localPasteboards objectForKey:name];
  55. if (pasteboard)
  56. return pasteboard;
  57. pasteboard = [[LocalPasteboard alloc] initWithName:name];
  58. [localPasteboards setObject:pasteboard forKey:name];
  59. [pasteboard release];
  60. return pasteboard;
  61. }
  62. + (void)releaseLocalPasteboards
  63. {
  64. [localPasteboards release];
  65. localPasteboards = nil;
  66. }
  67. // Convenience method for JS so that it doesn't have to try and create a NSArray on the objc side instead
  68. // of the usual WebScriptObject that is passed around
  69. - (NSInteger)declareType:(NSString *)type owner:(id)newOwner
  70. {
  71. return [self declareTypes:[NSArray arrayWithObject:type] owner:newOwner];
  72. }
  73. @end
  74. @implementation LocalPasteboard
  75. + (id)alloc
  76. {
  77. return NSAllocateObject(self, 0, 0);
  78. }
  79. - (id)initWithName:(NSString *)name
  80. {
  81. typesArray = [[NSMutableArray alloc] init];
  82. typesSet = [[NSMutableSet alloc] init];
  83. dataByType = [[NSMutableDictionary alloc] init];
  84. pasteboardName = [name copy];
  85. return self;
  86. }
  87. - (void)dealloc
  88. {
  89. [typesArray release];
  90. [typesSet release];
  91. [dataByType release];
  92. [pasteboardName release];
  93. [super dealloc];
  94. }
  95. - (NSString *)name
  96. {
  97. return pasteboardName;
  98. }
  99. - (void)releaseGlobally
  100. {
  101. }
  102. - (NSInteger)declareTypes:(NSArray *)newTypes owner:(id)newOwner
  103. {
  104. [typesArray removeAllObjects];
  105. [typesSet removeAllObjects];
  106. [dataByType removeAllObjects];
  107. return [self addTypes:newTypes owner:newOwner];
  108. }
  109. - (NSInteger)addTypes:(NSArray *)newTypes owner:(id)newOwner
  110. {
  111. unsigned count = [newTypes count];
  112. unsigned i;
  113. for (i = 0; i < count; ++i) {
  114. NSString *type = [newTypes objectAtIndex:i];
  115. NSString *setType = [typesSet member:type];
  116. if (!setType) {
  117. setType = [type copy];
  118. [typesArray addObject:setType];
  119. [typesSet addObject:setType];
  120. [setType release];
  121. }
  122. if (newOwner && [newOwner respondsToSelector:@selector(pasteboard:provideDataForType:)])
  123. [newOwner pasteboard:self provideDataForType:setType];
  124. }
  125. return ++changeCount;
  126. }
  127. - (NSInteger)changeCount
  128. {
  129. return changeCount;
  130. }
  131. - (NSArray *)types
  132. {
  133. return typesArray;
  134. }
  135. - (NSString *)availableTypeFromArray:(NSArray *)types
  136. {
  137. unsigned count = [types count];
  138. unsigned i;
  139. for (i = 0; i < count; ++i) {
  140. NSString *type = [types objectAtIndex:i];
  141. NSString *setType = [typesSet member:type];
  142. if (setType)
  143. return setType;
  144. }
  145. return nil;
  146. }
  147. - (BOOL)setData:(NSData *)data forType:(NSString *)dataType
  148. {
  149. if (data == nil)
  150. data = [NSData data];
  151. if (![typesSet containsObject:dataType])
  152. return NO;
  153. [dataByType setObject:data forKey:dataType];
  154. ++changeCount;
  155. return YES;
  156. }
  157. - (NSData *)dataForType:(NSString *)dataType
  158. {
  159. return [dataByType objectForKey:dataType];
  160. }
  161. - (BOOL)setPropertyList:(id)propertyList forType:(NSString *)dataType
  162. {
  163. CFDataRef data = NULL;
  164. if (propertyList)
  165. data = CFPropertyListCreateXMLData(NULL, propertyList);
  166. BOOL result = [self setData:(NSData *)data forType:dataType];
  167. if (data)
  168. CFRelease(data);
  169. return result;
  170. }
  171. - (BOOL)setString:(NSString *)string forType:(NSString *)dataType
  172. {
  173. CFDataRef data = NULL;
  174. if (string) {
  175. if ([string length] == 0)
  176. data = CFDataCreate(NULL, NULL, 0);
  177. else
  178. data = CFStringCreateExternalRepresentation(NULL, (CFStringRef)string, kCFStringEncodingUTF8, 0);
  179. }
  180. BOOL result = [self setData:(NSData *)data forType:dataType];
  181. if (data)
  182. CFRelease(data);
  183. return result;
  184. }
  185. @end