webengine/osswebengine/WebKit/Misc/WebNSPasteboardExtras.mm
changeset 0 dd21522fd290
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2  * Copyright (C) 2005, 2006, 2007 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 "WebNSPasteboardExtras.h"
       
    30 
       
    31 #import "WebArchive.h"
       
    32 #import "WebFrameBridge.h"
       
    33 #import "WebFrameInternal.h"
       
    34 #import "WebHTMLViewInternal.h"
       
    35 #import "WebNSURLExtras.h"
       
    36 #import "WebResourcePrivate.h"
       
    37 #import "WebURLsWithTitles.h"
       
    38 #import "WebViewPrivate.h"
       
    39 #import <JavaScriptCore/Assertions.h>
       
    40 #import <WebCore/Element.h>
       
    41 #import <WebCore/MIMETypeRegistry.h>
       
    42 #import <WebCore/RenderImage.h>
       
    43 #import <WebKit/DOMExtensions.h>
       
    44 #import <WebKit/DOMPrivate.h>
       
    45 #import <wtf/RetainPtr.h>
       
    46 #import <WebKitSystemInterface.h>
       
    47 
       
    48 @interface NSFilePromiseDragSource : NSObject
       
    49 - initWithSource:(id)draggingSource;
       
    50 - (void)setTypes:(NSArray *)types onPasteboard:(NSPasteboard *)pboard;
       
    51 @end
       
    52 
       
    53 using namespace WebCore;
       
    54 
       
    55 NSString *WebURLPboardType = @"public.url";
       
    56 NSString *WebURLNamePboardType = @"public.url-name";
       
    57 
       
    58 @implementation NSPasteboard (WebExtras)
       
    59 
       
    60 + (NSArray *)_web_writableTypesForURL
       
    61 {
       
    62     static RetainPtr<NSArray> types;
       
    63     if (!types) {
       
    64         types = [[NSArray alloc] initWithObjects:
       
    65             WebURLsWithTitlesPboardType,
       
    66             NSURLPboardType,
       
    67             WebURLPboardType,
       
    68             WebURLNamePboardType,
       
    69             NSStringPboardType,
       
    70             nil];
       
    71     }
       
    72     return types.get();
       
    73 }
       
    74 
       
    75 static NSArray *_writableTypesForImageWithoutArchive (void)
       
    76 {
       
    77     static RetainPtr<NSMutableArray> types;
       
    78     if (types == nil) {
       
    79         types = [[NSMutableArray alloc] initWithObjects:NSTIFFPboardType, nil];
       
    80         [types.get() addObjectsFromArray:[NSPasteboard _web_writableTypesForURL]];
       
    81     }
       
    82     return types.get();
       
    83 }
       
    84 
       
    85 static NSArray *_writableTypesForImageWithArchive (void)
       
    86 {
       
    87     static RetainPtr<NSMutableArray> types;
       
    88     if (types == nil) {
       
    89         types = [_writableTypesForImageWithoutArchive() mutableCopy];
       
    90         [types.get() addObject:NSRTFDPboardType];
       
    91         [types.get() addObject:WebArchivePboardType];
       
    92     }
       
    93     return types.get();
       
    94 }
       
    95 
       
    96 + (NSArray *)_web_writableTypesForImageIncludingArchive:(BOOL)hasArchive
       
    97 {
       
    98     return hasArchive 
       
    99         ? _writableTypesForImageWithArchive()
       
   100         : _writableTypesForImageWithoutArchive();
       
   101 }
       
   102 
       
   103 + (NSArray *)_web_dragTypesForURL
       
   104 {
       
   105     return [NSArray arrayWithObjects:
       
   106         WebURLsWithTitlesPboardType,
       
   107         NSURLPboardType,
       
   108         WebURLPboardType,
       
   109         WebURLNamePboardType,
       
   110         NSStringPboardType,
       
   111         NSFilenamesPboardType,
       
   112         nil];
       
   113 }
       
   114 
       
   115 - (NSURL *)_web_bestURL
       
   116 {
       
   117     NSArray *types = [self types];
       
   118 
       
   119     if ([types containsObject:NSURLPboardType]) {
       
   120         NSURL *URLFromPasteboard = [NSURL URLFromPasteboard:self];
       
   121         NSString *scheme = [URLFromPasteboard scheme];
       
   122         if ([scheme isEqualToString:@"http"] || [scheme isEqualToString:@"https"]) {
       
   123             return [URLFromPasteboard _webkit_canonicalize];
       
   124         }
       
   125     }
       
   126 
       
   127     if ([types containsObject:NSStringPboardType]) {
       
   128         NSString *URLString = [self stringForType:NSStringPboardType];
       
   129         if ([URLString _webkit_looksLikeAbsoluteURL]) {
       
   130             NSURL *URL = [[NSURL _web_URLWithUserTypedString:URLString] _webkit_canonicalize];
       
   131             if (URL) {
       
   132                 return URL;
       
   133             }
       
   134         }
       
   135     }
       
   136 
       
   137     if ([types containsObject:NSFilenamesPboardType]) {
       
   138         NSArray *files = [self propertyListForType:NSFilenamesPboardType];
       
   139         if ([files count] == 1) {
       
   140             NSString *file = [files objectAtIndex:0];
       
   141             BOOL isDirectory;
       
   142             if([[NSFileManager defaultManager] fileExistsAtPath:file isDirectory:&isDirectory] && !isDirectory){
       
   143                 return [[NSURL fileURLWithPath:file] _webkit_canonicalize];
       
   144             }
       
   145         }
       
   146     }
       
   147 
       
   148     return nil;
       
   149 }
       
   150 
       
   151 - (void)_web_writeURL:(NSURL *)URL andTitle:(NSString *)title types:(NSArray *)types
       
   152 {
       
   153     ASSERT(URL);
       
   154 
       
   155     if ([title length] == 0) {
       
   156         title = [[URL path] lastPathComponent];
       
   157         if ([title length] == 0)
       
   158             title = [URL _web_userVisibleString];
       
   159     }
       
   160     
       
   161     if ([types containsObject:NSURLPboardType])
       
   162         [URL writeToPasteboard:self];
       
   163     if ([types containsObject:WebURLPboardType])
       
   164         [self setString:[URL _web_originalDataAsString] forType:WebURLPboardType];
       
   165     if ([types containsObject:WebURLNamePboardType])
       
   166         [self setString:title forType:WebURLNamePboardType];
       
   167     if ([types containsObject:NSStringPboardType])
       
   168         [self setString:[URL _web_userVisibleString] forType:NSStringPboardType];
       
   169     if ([types containsObject:WebURLsWithTitlesPboardType])
       
   170         [WebURLsWithTitles writeURLs:[NSArray arrayWithObject:URL] andTitles:[NSArray arrayWithObject:title] toPasteboard:self];
       
   171 }
       
   172 
       
   173 + (int)_web_setFindPasteboardString:(NSString *)string withOwner:(id)owner
       
   174 {
       
   175     NSPasteboard *findPasteboard = [NSPasteboard pasteboardWithName:NSFindPboard];
       
   176     [findPasteboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:owner];
       
   177     [findPasteboard setString:string forType:NSStringPboardType];
       
   178     return [findPasteboard changeCount];
       
   179 }
       
   180 
       
   181 - (void)_web_writeFileWrapperAsRTFDAttachment:(NSFileWrapper *)wrapper
       
   182 {
       
   183     NSTextAttachment *attachment = [[NSTextAttachment alloc] initWithFileWrapper:wrapper];
       
   184     
       
   185     NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attachment];
       
   186     [attachment release];
       
   187     
       
   188     NSData *RTFDData = [string RTFDFromRange:NSMakeRange(0, [string length]) documentAttributes:nil];
       
   189     [self setData:RTFDData forType:NSRTFDPboardType];
       
   190 }
       
   191 
       
   192 
       
   193 - (void)_web_writePromisedRTFDFromArchive:(WebArchive*)archive containsImage:(BOOL)containsImage
       
   194 {
       
   195     ASSERT(archive);
       
   196     // This image data is either the only subresource of an archive (HTML image case)
       
   197     // or the main resource (standalone image case).
       
   198     NSArray *subresources = [archive subresources];
       
   199     WebResource *resource = [archive mainResource];
       
   200     if (containsImage && [subresources count] > 0 
       
   201         && MIMETypeRegistry::isSupportedImageResourceMIMEType([[subresources objectAtIndex:0] MIMEType]))
       
   202         resource = (WebResource *)[subresources objectAtIndex:0];
       
   203     ASSERT(resource != nil);
       
   204     
       
   205     ASSERT(!containsImage || MIMETypeRegistry::isSupportedImageResourceMIMEType([resource MIMEType]));
       
   206     if (!containsImage || MIMETypeRegistry::isSupportedImageResourceMIMEType([resource MIMEType]))
       
   207         [self _web_writeFileWrapperAsRTFDAttachment:[resource _fileWrapperRepresentation]];
       
   208     
       
   209 }
       
   210 
       
   211 CachedImage* imageFromElement(DOMElement *domElement) {
       
   212     Element* element = core(domElement);
       
   213     if (!element)
       
   214         return 0;
       
   215     
       
   216     RenderObject* renderer = element->renderer();
       
   217     RenderImage* imageRenderer = static_cast<RenderImage*>(renderer);
       
   218     if (!imageRenderer->cachedImage() || imageRenderer->cachedImage()->errorOccurred()) 
       
   219         return 0;        
       
   220     return imageRenderer->cachedImage();
       
   221 }
       
   222 
       
   223 - (void)_web_writeImage:(NSImage *)image
       
   224                 element:(DOMElement *)element
       
   225                     URL:(NSURL *)URL 
       
   226                   title:(NSString *)title
       
   227                 archive:(WebArchive *)archive
       
   228                   types:(NSArray *)types
       
   229                  source:(WebHTMLView *)source
       
   230 {
       
   231     ASSERT(image || element);
       
   232     ASSERT(URL);
       
   233 
       
   234     [self _web_writeURL:URL andTitle:title types:types];
       
   235     
       
   236     if ([types containsObject:NSTIFFPboardType]) {
       
   237         if (image)
       
   238             [self setData:[image TIFFRepresentation] forType:NSTIFFPboardType];
       
   239         else if (source && element)
       
   240             [source setPromisedDragTIFFDataSource:imageFromElement(element)];
       
   241         else if (element)
       
   242             [self setData:[element _imageTIFFRepresentation] forType:NSTIFFPboardType];
       
   243     }
       
   244     
       
   245     if (archive)
       
   246         if ([types containsObject:WebArchivePboardType])
       
   247             [self setData:[archive data] forType:WebArchivePboardType];
       
   248     else {
       
   249         // We should not have declared types that we aren't going to write (4031826).
       
   250         ASSERT(![types containsObject:NSRTFDPboardType]);
       
   251         ASSERT(![types containsObject:WebArchivePboardType]);
       
   252     }
       
   253 }
       
   254 
       
   255 - (id)_web_declareAndWriteDragImageForElement:(DOMElement *)element
       
   256                                        URL:(NSURL *)URL 
       
   257                                      title:(NSString *)title
       
   258                                    archive:(WebArchive *)archive
       
   259                                     source:(WebHTMLView *)source
       
   260 {
       
   261     ASSERT(self == [NSPasteboard pasteboardWithName:NSDragPboard]);
       
   262 
       
   263     NSMutableArray *types = [[NSMutableArray alloc] initWithObjects:NSFilesPromisePboardType, nil];
       
   264     [types addObjectsFromArray:[NSPasteboard _web_writableTypesForImageIncludingArchive:(archive != nil)]];
       
   265     [self declareTypes:types owner:source];    
       
   266     [self _web_writeImage:nil element:element URL:URL title:title archive:archive types:types source:source];
       
   267     [types release];
       
   268 
       
   269     NSString *extension = @"";
       
   270     if (RenderObject* renderer = core(element)->renderer())
       
   271         if (renderer->isImage())
       
   272             if (CachedImage* image = static_cast<RenderImage*>(renderer)->cachedImage())
       
   273                 extension = WKGetPreferredExtensionForMIMEType(image->response().mimeType());
       
   274 
       
   275     NSArray *extensions = [[NSArray alloc] initWithObjects:extension, nil];
       
   276     [self setPropertyList:extensions forType:NSFilesPromisePboardType];
       
   277     [extensions release];
       
   278 
       
   279     return source;
       
   280 }
       
   281 
       
   282 @end