OSDN Git Service

Merge WebKit at r80534: Intial merge by Git
[android-x86/external-webkit.git] / Source / WebKit2 / WebProcess / WebCoreSupport / mac / WebDragClientMac.mm
1 /*
2  * Copyright (C) 2011 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  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #import "config.h"
27 #import "WebDragClient.h"
28
29 #import "PasteboardTypes.h"
30 #import "ShareableBitmap.h"
31 #import "WebCoreArgumentCoders.h"
32 #import "WebPage.h"
33 #import "WebPageProxyMessages.h"
34 #import <WebCore/CachedImage.h>
35 #import <WebCore/DOMPrivate.h>
36 #import <WebCore/DOMElementInternal.h>
37 #import <WebCore/FrameView.h>
38 #import <WebCore/GraphicsContext.h>
39 #import <WebCore/LegacyWebArchive.h>
40 #import <WebCore/RenderImage.h>
41 #import <WebCore/StringTruncator.h>
42 #import <wtf/StdLibExtras.h>
43 #import <WebKit/WebKitNSStringExtras.h>
44 #import <WebKit/WebNSURLExtras.h>
45
46 using namespace WebCore;
47
48 namespace WebKit {
49
50 using namespace WebCore;
51    
52 void WebDragClient::startDrag(DragImageRef dragImage, const IntPoint& at, const IntPoint& eventPos, Clipboard* clipboard, Frame* frame, bool linkDrag)
53 {
54     if (!frame)
55         return;
56     ASSERT(clipboard);
57     
58     NSImage *dragNSImage = dragImage.get();
59     RefPtr<ShareableBitmap> dragShareableImage = ShareableBitmap::createShareable(IntSize([dragNSImage size]));
60     OwnPtr<GraphicsContext> graphicsContext = dragShareableImage->createGraphicsContext();
61
62     [NSGraphicsContext saveGraphicsState];
63     NSGraphicsContext* bitmapContext = [NSGraphicsContext graphicsContextWithGraphicsPort:graphicsContext->platformContext() flipped:YES];
64     [NSGraphicsContext setCurrentContext: bitmapContext];
65     
66     [dragNSImage drawInRect:NSMakeRect(0, 0, [dragNSImage size].width , [dragNSImage size].height) fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1 respectFlipped:YES hints:nil];
67     [NSGraphicsContext restoreGraphicsState];
68  
69     SharedMemory::Handle handle;
70     if (!dragShareableImage->createHandle(handle))
71         return;
72     IntPoint clientPoint(at);
73     m_page->send(Messages::WebPageProxy::SetDragImage(clientPoint, IntSize([dragNSImage size]), handle, linkDrag));
74 }
75
76 static void writeURL(NSPasteboard* pasteboard, NSURL* URL, NSString* title, NSArray* types)
77 {
78     ASSERT(URL);
79     
80     if (![title length]) {
81         title = [[URL path] lastPathComponent];
82         if (![title length])
83             title = [URL _web_userVisibleString];
84     }
85     
86     if ([types containsObject:NSURLPboardType])
87         [URL writeToPasteboard:pasteboard];
88     if ([types containsObject:PasteboardTypes::WebURLPboardType])
89         [pasteboard setString:[URL _web_originalDataAsString] forType:PasteboardTypes::WebURLPboardType];
90     if ([types containsObject:PasteboardTypes::WebURLNamePboardType])
91         [pasteboard setString:title forType:PasteboardTypes::WebURLNamePboardType];
92     if ([types containsObject:NSStringPboardType])
93         [pasteboard setString:[URL _web_userVisibleString] forType:NSStringPboardType];
94     if ([types containsObject:PasteboardTypes::WebURLsWithTitlesPboardType]) {
95         NSArray* URLs = [NSArray arrayWithObject:URL];
96         unsigned count = [URLs count];
97         
98         if (!count || [pasteboard availableTypeFromArray:[NSArray arrayWithObject:PasteboardTypes::WebURLsWithTitlesPboardType]] == nil)
99             return;
100
101         NSArray* titles = [NSArray arrayWithObject:title];
102         
103         if (count != [titles count])
104             titles = nil;
105         
106         NSMutableArray* URLStrings = [NSMutableArray arrayWithCapacity:count];
107         NSMutableArray* titlesOrEmptyStrings = [NSMutableArray arrayWithCapacity:count];
108         for (unsigned index = 0; index < count; ++index) {
109             [URLStrings addObject:[[URLs objectAtIndex:index] _web_originalDataAsString]];
110             [titlesOrEmptyStrings addObject:(titles == nil) ? @"" : [[titles objectAtIndex:index] _webkit_stringByTrimmingWhitespace]];
111         }
112         
113         [pasteboard setPropertyList:[NSArray arrayWithObjects:URLStrings, titlesOrEmptyStrings, nil]
114                             forType:PasteboardTypes::WebURLsWithTitlesPboardType];
115     }
116 }
117     
118 static void writeImage(NSPasteboard* pasteboard, NSImage *image, DOMElement* element, NSURL* URL, NSString* title, LegacyWebArchive* archive, NSArray* types)
119 {
120     ASSERT(image || element);
121     ASSERT(URL);
122     
123     writeURL(pasteboard, URL, title, types);
124     
125     if ([types containsObject:NSTIFFPboardType]) {
126         // FIXME: we should add handling of promised types.
127         if (image)
128             [pasteboard setData:[image TIFFRepresentation] forType:NSTIFFPboardType];
129         else if (element)
130             [pasteboard setData:[element _imageTIFFRepresentation] forType:NSTIFFPboardType];
131     }
132     
133     if (archive && [types containsObject:PasteboardTypes::WebArchivePboardType])
134         [pasteboard setData:[[(NSData *)archive->rawDataRepresentation().get() retain] autorelease] forType:PasteboardTypes::WebArchivePboardType];
135 }
136     
137 void WebDragClient::declareAndWriteDragImage(NSPasteboard* pasteboard, DOMElement* element, NSURL* URL, NSString* title, WebCore::Frame*)
138 {
139     ASSERT(element);
140     ASSERT(pasteboard && pasteboard == [NSPasteboard pasteboardWithName:NSDragPboard]);
141     
142     NSString *extension = @"";
143     if (RenderObject* renderer = core(element)->renderer()) {
144         if (renderer->isImage()) {
145             if (CachedImage* image = toRenderImage(renderer)->cachedImage()) {
146                 extension = image->image()->filenameExtension();
147                 if (![extension length])
148                     return;
149             }
150         }
151     }
152     
153     RefPtr<LegacyWebArchive> archive = LegacyWebArchive::create(core(element));
154     NSMutableArray *types = [[NSMutableArray alloc] initWithObjects:NSFilesPromisePboardType, nil];
155     [types addObjectsFromArray:(archive) ? PasteboardTypes::forImagesWithArchive() : PasteboardTypes::forImages()];
156     [pasteboard declareTypes:types owner:nil];    
157     writeImage(pasteboard, nil, element, URL, title, archive.get(), types);
158     [types release];
159     
160     NSArray *extensions = [[NSArray alloc] initWithObjects:extension, nil];
161     [pasteboard setPropertyList:extensions forType:NSFilesPromisePboardType];
162     [extensions release];
163 }
164
165 } // namespace WebKit