Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / clipboard / Pasteboard.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 "core/clipboard/Pasteboard.h"
33
34 #include "core/clipboard/DataObject.h"
35 #include "platform/clipboard/ClipboardUtilities.h"
36 #include "platform/graphics/Image.h"
37 #include "platform/graphics/skia/NativeImageSkia.h"
38 #include "platform/weborigin/KURL.h"
39 #include "public/platform/Platform.h"
40 #include "public/platform/WebDragData.h"
41 #include "public/platform/WebString.h"
42 #include "public/platform/WebURL.h"
43 #include "wtf/PassRefPtr.h"
44 #include "wtf/RefPtr.h"
45
46 namespace WebCore {
47
48 Pasteboard* Pasteboard::generalPasteboard()
49 {
50     static Pasteboard* pasteboard = new Pasteboard;
51     return pasteboard;
52 }
53
54 Pasteboard::Pasteboard()
55     : m_buffer(blink::WebClipboard::BufferStandard)
56 {
57 }
58
59 bool Pasteboard::isSelectionMode() const
60 {
61     return m_buffer == blink::WebClipboard::BufferSelection;
62 }
63
64 void Pasteboard::setSelectionMode(bool selectionMode)
65 {
66     m_buffer = selectionMode ? blink::WebClipboard::BufferSelection : blink::WebClipboard::BufferStandard;
67 }
68
69 void Pasteboard::writePlainText(const String& text, SmartReplaceOption)
70 {
71     // FIXME: add support for smart replace
72 #if OS(WIN)
73     String plainText(text);
74     replaceNewlinesWithWindowsStyleNewlines(plainText);
75     blink::Platform::current()->clipboard()->writePlainText(plainText);
76 #else
77     blink::Platform::current()->clipboard()->writePlainText(text);
78 #endif
79 }
80
81 void Pasteboard::writeImage(Image* image, const KURL& url, const String& title)
82 {
83     ASSERT(image);
84
85     RefPtr<NativeImageSkia> bitmap = image->nativeImageForCurrentFrame();
86     if (!bitmap)
87         return;
88
89     blink::WebImage webImage = bitmap->bitmap();
90     blink::Platform::current()->clipboard()->writeImage(webImage, blink::WebURL(url), blink::WebString(title));
91 }
92
93 void Pasteboard::writeDataObject(PassRefPtr<DataObject> dataObject)
94 {
95     blink::Platform::current()->clipboard()->writeDataObject(dataObject);
96 }
97
98 bool Pasteboard::canSmartReplace()
99 {
100     return blink::Platform::current()->clipboard()->isFormatAvailable(blink::WebClipboard::FormatSmartPaste, m_buffer);
101 }
102
103 bool Pasteboard::isHTMLAvailable()
104 {
105     return blink::Platform::current()->clipboard()->isFormatAvailable(blink::WebClipboard::FormatHTML, m_buffer);
106 }
107
108 String Pasteboard::plainText()
109 {
110     return blink::Platform::current()->clipboard()->readPlainText(m_buffer);
111 }
112
113 String Pasteboard::readHTML(KURL& url, unsigned& fragmentStart, unsigned& fragmentEnd)
114 {
115     blink::WebURL webURL;
116     blink::WebString markup = blink::Platform::current()->clipboard()->readHTML(m_buffer, &webURL, &fragmentStart, &fragmentEnd);
117     if (!markup.isEmpty()) {
118         url = webURL;
119         // fragmentStart and fragmentEnd are populated by WebClipboard::readHTML.
120     } else {
121         url = KURL();
122         fragmentStart = 0;
123         fragmentEnd = 0;
124     }
125     return markup;
126 }
127
128 void Pasteboard::writeHTML(const String& markup, const KURL& documentURL, const String& plainText, bool canSmartCopyOrDelete)
129 {
130     String text = plainText;
131 #if OS(WIN)
132     replaceNewlinesWithWindowsStyleNewlines(text);
133 #endif
134     replaceNBSPWithSpace(text);
135
136     blink::Platform::current()->clipboard()->writeHTML(markup, documentURL, text, canSmartCopyOrDelete);
137 }
138
139 } // namespace WebCore