• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
generalPasteboard()55 Pasteboard* Pasteboard::generalPasteboard()
56 {
57     static Pasteboard* pasteboard = new Pasteboard;
58     return pasteboard;
59 }
60 
Pasteboard()61 Pasteboard::Pasteboard()
62 {
63 }
64 
clear()65 void Pasteboard::clear()
66 {
67     // The ScopedClipboardWriter class takes care of clearing the clipboard's
68     // previous contents.
69 }
70 
writeSelection(Range * selectedRange,bool canSmartCopyOrDelete,Frame * frame)71 void Pasteboard::writeSelection(Range* selectedRange, bool canSmartCopyOrDelete, Frame* frame)
72 {
73     String html = createMarkup(selectedRange, 0, AnnotateForInterchange);
74 #if PLATFORM(DARWIN)
75     html = String("<meta charset='utf-8'>") + html;
76 #endif
77     ExceptionCode ec = 0;
78     KURL url = selectedRange->startContainer(ec)->document()->url();
79     String plainText = frame->selectedText();
80 #if PLATFORM(WIN_OS)
81     replaceNewlinesWithWindowsStyleNewlines(plainText);
82 #endif
83     replaceNBSPWithSpace(plainText);
84 
85     ChromiumBridge::clipboardWriteSelection(html, url, plainText, canSmartCopyOrDelete);
86 }
87 
writeURL(const KURL & url,const String & titleStr,Frame * frame)88 void Pasteboard::writeURL(const KURL& url, const String& titleStr, Frame* frame)
89 {
90     ASSERT(!url.isEmpty());
91 
92     String title(titleStr);
93     if (title.isEmpty()) {
94         title = url.lastPathComponent();
95         if (title.isEmpty())
96             title = url.host();
97     }
98 
99     ChromiumBridge::clipboardWriteURL(url, title);
100 }
101 
writeImage(Node * node,const KURL &,const String & title)102 void Pasteboard::writeImage(Node* node, const KURL&, const String& title)
103 {
104     ASSERT(node);
105     ASSERT(node->renderer());
106     ASSERT(node->renderer()->isImage());
107     RenderImage* renderer = toRenderImage(node->renderer());
108     CachedImage* cachedImage = renderer->cachedImage();
109     ASSERT(cachedImage);
110     Image* image = cachedImage->image();
111     ASSERT(image);
112 
113     // If the image is wrapped in a link, |url| points to the target of the
114     // link.  This isn't useful to us, so get the actual image URL.
115     AtomicString urlString;
116     if (node->hasTagName(HTMLNames::imgTag) || node->hasTagName(HTMLNames::inputTag))
117         urlString = static_cast<Element*>(node)->getAttribute(HTMLNames::srcAttr);
118 #if ENABLE(SVG)
119     else if (node->hasTagName(SVGNames::imageTag))
120         urlString = static_cast<Element*>(node)->getAttribute(XLinkNames::hrefAttr);
121 #endif
122     else if (node->hasTagName(HTMLNames::embedTag) || node->hasTagName(HTMLNames::objectTag)) {
123         Element* element = static_cast<Element*>(node);
124         urlString = element->getAttribute(element->imageSourceAttributeName());
125     }
126     KURL url = urlString.isEmpty() ? KURL() : node->document()->completeURL(deprecatedParseURL(urlString));
127 
128     NativeImageSkia* bitmap = 0;
129 #if !PLATFORM(CG)
130     bitmap = image->nativeImageForCurrentFrame();
131 #endif
132     ChromiumBridge::clipboardWriteImage(bitmap, url, title);
133 }
134 
canSmartReplace()135 bool Pasteboard::canSmartReplace()
136 {
137     return ChromiumBridge::clipboardIsFormatAvailable(
138         PasteboardPrivate::WebSmartPasteFormat);
139 }
140 
plainText(Frame * frame)141 String Pasteboard::plainText(Frame* frame)
142 {
143     return ChromiumBridge::clipboardReadPlainText();
144 }
145 
documentFragment(Frame * frame,PassRefPtr<Range> context,bool allowPlainText,bool & chosePlainText)146 PassRefPtr<DocumentFragment> Pasteboard::documentFragment(Frame* frame, PassRefPtr<Range> context, bool allowPlainText, bool& chosePlainText)
147 {
148     chosePlainText = false;
149 
150     if (ChromiumBridge::clipboardIsFormatAvailable(PasteboardPrivate::HTMLFormat)) {
151         String markup;
152         KURL srcURL;
153         ChromiumBridge::clipboardReadHTML(&markup, &srcURL);
154 
155         RefPtr<DocumentFragment> fragment =
156             createFragmentFromMarkup(frame->document(), markup, srcURL);
157         if (fragment)
158             return fragment.release();
159     }
160 
161     if (allowPlainText) {
162         String markup = ChromiumBridge::clipboardReadPlainText();
163         if (!markup.isEmpty()) {
164             chosePlainText = true;
165 
166             RefPtr<DocumentFragment> fragment =
167                 createFragmentFromText(context.get(), markup);
168             if (fragment)
169                 return fragment.release();
170         }
171     }
172 
173     return 0;
174 }
175 
176 } // namespace WebCore
177