Finish removing layer-violating code from Pasteboard (just for Mac during this first...
[WebKit-https.git] / Source / WebCore / platform / mac / DragDataMac.mm
1 /*
2  * Copyright (C) 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  * 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 COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #import "config.h"
27 #import "DragData.h"
28
29 #if ENABLE(DRAG_SUPPORT)
30 #import "Document.h"
31 #import "DocumentFragment.h"
32 #import "DOMDocumentFragment.h"
33 #import "DOMDocumentFragmentInternal.h"
34 #import "Editor.h"
35 #import "EditorClient.h"
36 #import "Frame.h"
37 #import "MIMETypeRegistry.h"
38 #import "Pasteboard.h"
39 #import "PasteboardStrategy.h"
40 #import "PlatformStrategies.h"
41 #import "Range.h"
42
43 namespace WebCore {
44
45 DragData::DragData(DragDataRef data, const IntPoint& clientPosition, const IntPoint& globalPosition, 
46     DragOperation sourceOperationMask, DragApplicationFlags flags)
47     : m_clientPosition(clientPosition)
48     , m_globalPosition(globalPosition)
49     , m_platformDragData(data)
50     , m_draggingSourceOperationMask(sourceOperationMask)
51     , m_applicationFlags(flags)
52     , m_pasteboardName([[m_platformDragData draggingPasteboard] name])
53 {
54     ASSERT([data conformsToProtocol:@protocol(NSDraggingInfo)]);
55 }
56
57 DragData::DragData(const String& dragStorageName, const IntPoint& clientPosition, const IntPoint& globalPosition,
58     DragOperation sourceOperationMask, DragApplicationFlags flags)
59     : m_clientPosition(clientPosition)
60     , m_globalPosition(globalPosition)
61     , m_platformDragData(0)
62     , m_draggingSourceOperationMask(sourceOperationMask)
63     , m_applicationFlags(flags)
64     , m_pasteboardName(dragStorageName)
65 {
66 }
67     
68 bool DragData::canSmartReplace() const
69 {
70     return Pasteboard(m_pasteboardName).canSmartReplace();
71 }
72
73 bool DragData::containsColor() const
74 {
75     Vector<String> types;
76     platformStrategies()->pasteboardStrategy()->getTypes(types, m_pasteboardName);
77     return types.contains(String(NSColorPboardType));
78 }
79
80 bool DragData::containsFiles() const
81 {
82     Vector<String> types;
83     platformStrategies()->pasteboardStrategy()->getTypes(types, m_pasteboardName);
84     return types.contains(String(NSFilenamesPboardType));
85 }
86
87 unsigned DragData::numberOfFiles() const
88 {
89     Vector<String> files;
90     platformStrategies()->pasteboardStrategy()->getPathnamesForType(files, String(NSFilenamesPboardType), m_pasteboardName);
91     return files.size();
92 }
93
94 void DragData::asFilenames(Vector<String>& result) const
95 {
96     platformStrategies()->pasteboardStrategy()->getPathnamesForType(result, String(NSFilenamesPboardType), m_pasteboardName);
97 }
98
99 bool DragData::containsPlainText() const
100 {
101     Vector<String> types;
102     platformStrategies()->pasteboardStrategy()->getTypes(types, m_pasteboardName);
103
104     return types.contains(String(NSStringPboardType))
105         || types.contains(String(NSRTFDPboardType))
106         || types.contains(String(NSRTFPboardType))
107         || types.contains(String(NSFilenamesPboardType))
108         || platformStrategies()->pasteboardStrategy()->stringForType(String(NSURLPboardType), m_pasteboardName).length();
109 }
110
111 String DragData::asPlainText(Frame *frame) const
112 {
113     return frame->editor().readPlainTextFromPasteboard(*Pasteboard::create(m_pasteboardName));
114 }
115
116 Color DragData::asColor() const
117 {
118     return platformStrategies()->pasteboardStrategy()->color(m_pasteboardName);
119 }
120
121 bool DragData::containsCompatibleContent() const
122 {
123     Vector<String> types;
124     platformStrategies()->pasteboardStrategy()->getTypes(types, m_pasteboardName);
125     return types.contains(String(WebArchivePboardType))
126         || types.contains(String(NSHTMLPboardType))
127         || types.contains(String(NSFilenamesPboardType))
128         || types.contains(String(NSTIFFPboardType))
129         || types.contains(String(NSPDFPboardType))
130         || types.contains(String(NSURLPboardType))
131         || types.contains(String(NSRTFDPboardType))
132         || types.contains(String(NSRTFPboardType))
133         || types.contains(String(NSStringPboardType))
134         || types.contains(String(NSColorPboardType))
135         || types.contains(String(kUTTypePNG));
136 }
137     
138 bool DragData::containsURL(Frame* frame, FilenameConversionPolicy filenamePolicy) const
139 {
140     return !asURL(frame, filenamePolicy).isEmpty();
141 }
142
143 String DragData::asURL(Frame* frame, FilenameConversionPolicy, String* title) const
144 {
145     // FIXME: Use filenamePolicy.
146
147     if (title) {
148         String URLTitleString = platformStrategies()->pasteboardStrategy()->stringForType(String(WebURLNamePboardType), m_pasteboardName);
149         if (!URLTitleString.isEmpty())
150             *title = URLTitleString;
151     }
152     
153     Vector<String> types;
154     platformStrategies()->pasteboardStrategy()->getTypes(types, m_pasteboardName);
155     
156     // FIXME: using the editorClient to call into WebKit, for now, since 
157     // calling webkit_canonicalize from WebCore involves migrating a sizable amount of 
158     // helper code that should either be done in a separate patch or figured out in another way.
159     
160     if (types.contains(String(NSURLPboardType))) {
161         NSURL *URLFromPasteboard = [NSURL URLWithString:platformStrategies()->pasteboardStrategy()->stringForType(String(NSURLPboardType), m_pasteboardName)];
162         NSString *scheme = [URLFromPasteboard scheme];
163         // Cannot drop other schemes unless <rdar://problem/10562662> and <rdar://problem/11187315> are fixed.
164         if ([scheme isEqualToString:@"http"] || [scheme isEqualToString:@"https"])
165             return [frame->editor().client()->canonicalizeURL(URLFromPasteboard) absoluteString];
166     }
167     
168     if (types.contains(String(NSStringPboardType))) {
169         NSURL *URLFromPasteboard = [NSURL URLWithString:platformStrategies()->pasteboardStrategy()->stringForType(String(NSStringPboardType), m_pasteboardName)];
170         NSString *scheme = [URLFromPasteboard scheme];
171         // Pasteboard content is not trusted, because JavaScript code can modify it. We can sanitize it for URLs and other typed content, but not for strings.
172         // The result of this function is used to initiate navigation, so we shouldn't allow arbitrary file URLs.
173         // FIXME: Should we allow only http family schemes, or anything non-local?
174         if ([scheme isEqualToString:@"http"] || [scheme isEqualToString:@"https"])
175             return [frame->editor().client()->canonicalizeURL(URLFromPasteboard) absoluteString];
176     }
177     
178     if (types.contains(String(NSFilenamesPboardType))) {
179         Vector<String> files;
180         platformStrategies()->pasteboardStrategy()->getPathnamesForType(files, String(NSFilenamesPboardType), m_pasteboardName);
181         if (files.size() == 1) {
182             BOOL isDirectory;
183             if ([[NSFileManager defaultManager] fileExistsAtPath:files[0] isDirectory:&isDirectory] && isDirectory)
184                 return String();
185             return [frame->editor().client()->canonicalizeURL([NSURL fileURLWithPath:files[0]]) absoluteString];
186         }
187     }
188     
189     return String();        
190 }
191
192 PassRefPtr<DocumentFragment> DragData::asFragment(Frame* frame, PassRefPtr<Range> range, bool allowPlainText, bool& chosePlainText) const
193 {
194     Pasteboard pasteboard(m_pasteboardName);
195     return frame->editor().webContentFromPasteboard(pasteboard, *range, allowPlainText, chosePlainText);
196 }
197     
198 } // namespace WebCore
199
200 #endif // ENABLE(DRAG_SUPPORT)