2009-11-26 Kinuko Yasuda <kinuko@chromium.com>
[WebKit-https.git] / WebCore / platform / chromium / PasteboardChromium.cpp
1 /*
2  * Copyright (c) 2008, 2009, Google 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 are
6  * met:
7  * 
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  * 
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32 #include "Pasteboard.h"
33
34 #include "ChromiumBridge.h"
35 #include "ClipboardUtilitiesChromium.h"
36 #include "DocumentFragment.h"
37 #include "Document.h"
38 #include "Element.h"
39 #include "Frame.h"
40 #include "HTMLNames.h"
41 #include "Image.h"
42 #include "KURL.h"
43 #include "markup.h"
44 #include "NativeImageSkia.h"
45 #include "Range.h"
46 #include "RenderImage.h"
47
48 #if ENABLE(SVG)
49 #include "SVGNames.h"
50 #include "XLinkNames.h"
51 #endif
52
53 namespace WebCore {
54
55 Pasteboard* Pasteboard::generalPasteboard()
56 {
57     static Pasteboard* pasteboard = new Pasteboard;
58     return pasteboard;
59 }
60
61 Pasteboard::Pasteboard()
62     : m_selectionMode(false)
63 {
64 }
65
66 void Pasteboard::clear()
67 {
68     // The ScopedClipboardWriter class takes care of clearing the clipboard's
69     // previous contents.
70 }
71
72 bool Pasteboard::isSelectionMode() const
73 {
74     return m_selectionMode;
75 }
76
77 void Pasteboard::setSelectionMode(bool selectionMode)
78 {
79     m_selectionMode = selectionMode;
80 }
81
82 void Pasteboard::writeSelection(Range* selectedRange, bool canSmartCopyOrDelete, Frame* frame)
83 {
84     String html = createMarkup(selectedRange, 0, AnnotateForInterchange);
85 #if PLATFORM(DARWIN)
86     html = String("<meta charset='utf-8' id='webkit-interchange-charset'>") + html;
87 #endif
88     ExceptionCode ec = 0;
89     KURL url = selectedRange->startContainer(ec)->document()->url();
90     String plainText = frame->selectedText();
91 #if PLATFORM(WIN_OS)
92     replaceNewlinesWithWindowsStyleNewlines(plainText);
93 #endif
94     replaceNBSPWithSpace(plainText);
95
96     ChromiumBridge::clipboardWriteSelection(html, url, plainText, canSmartCopyOrDelete);
97 }
98
99 void Pasteboard::writePlainText(const String& text)
100 {
101 #if PLATFORM(WIN_OS)
102     String plainText(text);
103     replaceNewlinesWithWindowsStyleNewlines(plainText);
104     ChromiumBridge::clipboardWritePlainText(plainText);
105 #else
106     ChromiumBridge::clipboardWritePlainText(text);
107 #endif
108 }
109
110 void Pasteboard::writeURL(const KURL& url, const String& titleStr, Frame* frame)
111 {
112     ASSERT(!url.isEmpty());
113
114     String title(titleStr);
115     if (title.isEmpty()) {
116         title = url.lastPathComponent();
117         if (title.isEmpty())
118             title = url.host();
119     }
120
121     ChromiumBridge::clipboardWriteURL(url, title);
122 }
123
124 void Pasteboard::writeImage(Node* node, const KURL&, const String& title)
125 {
126     ASSERT(node);
127     ASSERT(node->renderer());
128     ASSERT(node->renderer()->isImage());
129     RenderImage* renderer = toRenderImage(node->renderer());
130     CachedImage* cachedImage = renderer->cachedImage();
131     ASSERT(cachedImage);
132     Image* image = cachedImage->image();
133     ASSERT(image);
134
135     // If the image is wrapped in a link, |url| points to the target of the
136     // link.  This isn't useful to us, so get the actual image URL.
137     AtomicString urlString;
138     if (node->hasTagName(HTMLNames::imgTag) || node->hasTagName(HTMLNames::inputTag))
139         urlString = static_cast<Element*>(node)->getAttribute(HTMLNames::srcAttr);
140 #if ENABLE(SVG)
141     else if (node->hasTagName(SVGNames::imageTag))
142         urlString = static_cast<Element*>(node)->getAttribute(XLinkNames::hrefAttr);
143 #endif
144     else if (node->hasTagName(HTMLNames::embedTag) || node->hasTagName(HTMLNames::objectTag)) {
145         Element* element = static_cast<Element*>(node);
146         urlString = element->getAttribute(element->imageSourceAttributeName());
147     }
148     KURL url = urlString.isEmpty() ? KURL() : node->document()->completeURL(deprecatedParseURL(urlString));
149
150     NativeImagePtr bitmap = image->nativeImageForCurrentFrame();
151     ChromiumBridge::clipboardWriteImage(bitmap, url, title);
152 }
153
154 bool Pasteboard::canSmartReplace()
155 {
156     return ChromiumBridge::clipboardIsFormatAvailable(PasteboardPrivate::WebSmartPasteFormat, m_selectionMode ? PasteboardPrivate::SelectionBuffer : PasteboardPrivate::StandardBuffer);
157 }
158
159 String Pasteboard::plainText(Frame* frame)
160 {
161     return ChromiumBridge::clipboardReadPlainText(m_selectionMode ? PasteboardPrivate::SelectionBuffer : PasteboardPrivate::StandardBuffer);
162 }
163
164 PassRefPtr<DocumentFragment> Pasteboard::documentFragment(Frame* frame, PassRefPtr<Range> context, bool allowPlainText, bool& chosePlainText)
165 {
166     chosePlainText = false;
167     PasteboardPrivate::ClipboardBuffer buffer = m_selectionMode ? PasteboardPrivate::SelectionBuffer : PasteboardPrivate::StandardBuffer;
168
169     if (ChromiumBridge::clipboardIsFormatAvailable(PasteboardPrivate::HTMLFormat, buffer)) {
170         String markup;
171         KURL srcURL;
172         ChromiumBridge::clipboardReadHTML(buffer, &markup, &srcURL);
173 #if PLATFORM(DARWIN)
174         DEFINE_STATIC_LOCAL(const String, forceUtf8String, ("<meta charset='utf-8' id='webkit-interchange-charset'>"));
175         if (markup.startsWith(forceUtf8String))
176             markup = markup.substring(forceUtf8String.length());
177 #endif
178
179         RefPtr<DocumentFragment> fragment =
180             createFragmentFromMarkup(frame->document(), markup, srcURL);
181         if (fragment)
182             return fragment.release();
183     }
184
185     if (allowPlainText) {
186         String markup = ChromiumBridge::clipboardReadPlainText(buffer);
187         if (!markup.isEmpty()) {
188             chosePlainText = true;
189
190             RefPtr<DocumentFragment> fragment =
191                 createFragmentFromText(context.get(), markup);
192             if (fragment)
193                 return fragment.release();
194         }
195     }
196
197     return 0;
198 }
199
200 } // namespace WebCore