~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/WebCore/platform/chromium/PasteboardChromium.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 "ClipboardChromium.h"
 
35
#include "ClipboardUtilitiesChromium.h"
 
36
#include "Document.h"
 
37
#include "DocumentFragment.h"
 
38
#include "Element.h"
 
39
#include "Frame.h"
 
40
#include "HTMLNames.h"
 
41
#include "HTMLParserIdioms.h"
 
42
#include "Image.h"
 
43
#include "KURL.h"
 
44
#include "NativeImageSkia.h"
 
45
#include "Range.h"
 
46
#include "RenderImage.h"
 
47
#include "markup.h"
 
48
 
 
49
#if ENABLE(SVG)
 
50
#include "SVGNames.h"
 
51
#include "XLinkNames.h"
 
52
#endif
 
53
 
 
54
#include <public/Platform.h>
 
55
#include <public/WebClipboard.h>
 
56
#include <public/WebDragData.h>
 
57
 
 
58
namespace WebCore {
 
59
 
 
60
Pasteboard* Pasteboard::generalPasteboard()
 
61
{
 
62
    static Pasteboard* pasteboard = new Pasteboard;
 
63
    return pasteboard;
 
64
}
 
65
 
 
66
Pasteboard::Pasteboard()
 
67
    : m_selectionMode(false)
 
68
{
 
69
}
 
70
 
 
71
void Pasteboard::clear()
 
72
{
 
73
    // The ScopedClipboardWriter class takes care of clearing the clipboard's
 
74
    // previous contents.
 
75
}
 
76
 
 
77
bool Pasteboard::isSelectionMode() const
 
78
{
 
79
    return m_selectionMode;
 
80
}
 
81
 
 
82
void Pasteboard::setSelectionMode(bool selectionMode)
 
83
{
 
84
    m_selectionMode = selectionMode;
 
85
}
 
86
 
 
87
void Pasteboard::writeSelection(Range* selectedRange, bool canSmartCopyOrDelete, Frame* frame)
 
88
{
 
89
    String html = createMarkup(selectedRange, 0, AnnotateForInterchange, false, ResolveNonLocalURLs);
 
90
    ExceptionCode ec = 0;
 
91
    KURL url = selectedRange->startContainer(ec)->document()->url();
 
92
    String plainText = frame->editor()->selectedText();
 
93
#if OS(WINDOWS)
 
94
    replaceNewlinesWithWindowsStyleNewlines(plainText);
 
95
#endif
 
96
    replaceNBSPWithSpace(plainText);
 
97
 
 
98
    WebKit::Platform::current()->clipboard()->writeHTML(html, url, plainText, canSmartCopyOrDelete);
 
99
}
 
100
 
 
101
void Pasteboard::writePlainText(const String& text, SmartReplaceOption)
 
102
{
 
103
    // FIXME: add support for smart replace
 
104
#if OS(WINDOWS)
 
105
    String plainText(text);
 
106
    replaceNewlinesWithWindowsStyleNewlines(plainText);
 
107
    WebKit::Platform::current()->clipboard()->writePlainText(plainText);
 
108
#else
 
109
    WebKit::Platform::current()->clipboard()->writePlainText(text);
 
110
#endif
 
111
}
 
112
 
 
113
void Pasteboard::writeURL(const KURL& url, const String& titleStr, Frame* frame)
 
114
{
 
115
    ASSERT(!url.isEmpty());
 
116
 
 
117
    String title(titleStr);
 
118
    if (title.isEmpty()) {
 
119
        title = url.lastPathComponent();
 
120
        if (title.isEmpty())
 
121
            title = url.host();
 
122
    }
 
123
 
 
124
    WebKit::Platform::current()->clipboard()->writeURL(url, title);
 
125
}
 
126
 
 
127
void Pasteboard::writeImage(Node* node, const KURL&, const String& title)
 
128
{
 
129
    ASSERT(node);
 
130
 
 
131
    if (!(node->renderer() && node->renderer()->isImage()))
 
132
        return;
 
133
 
 
134
    RenderImage* renderer = toRenderImage(node->renderer());
 
135
    CachedImage* cachedImage = renderer->cachedImage();
 
136
    if (!cachedImage || cachedImage->errorOccurred())
 
137
        return;
 
138
    Image* image = cachedImage->imageForRenderer(renderer);
 
139
    ASSERT(image);
 
140
 
 
141
    NativeImagePtr bitmap = image->nativeImageForCurrentFrame();
 
142
    if (!bitmap)
 
143
        return;
 
144
 
 
145
    // If the image is wrapped in a link, |url| points to the target of the
 
146
    // link.  This isn't useful to us, so get the actual image URL.
 
147
    AtomicString urlString;
 
148
    if (node->hasTagName(HTMLNames::imgTag) || node->hasTagName(HTMLNames::inputTag))
 
149
        urlString = static_cast<Element*>(node)->getAttribute(HTMLNames::srcAttr);
 
150
#if ENABLE(SVG)
 
151
    else if (node->hasTagName(SVGNames::imageTag))
 
152
        urlString = static_cast<Element*>(node)->getAttribute(XLinkNames::hrefAttr);
 
153
#endif
 
154
    else if (node->hasTagName(HTMLNames::embedTag) || node->hasTagName(HTMLNames::objectTag)) {
 
155
        Element* element = static_cast<Element*>(node);
 
156
        urlString = element->getAttribute(element->imageSourceAttributeName());
 
157
    }
 
158
    KURL url = urlString.isEmpty() ? KURL() : node->document()->completeURL(stripLeadingAndTrailingHTMLSpaces(urlString));
 
159
    WebKit::WebImage webImage = bitmap->bitmap();
 
160
    WebKit::Platform::current()->clipboard()->writeImage(webImage, WebKit::WebURL(url), WebKit::WebString(title));
 
161
}
 
162
 
 
163
void Pasteboard::writeClipboard(Clipboard* clipboard)
 
164
{
 
165
    WebKit::WebDragData dragData = static_cast<ClipboardChromium*>(clipboard)->dataObject();
 
166
    WebKit::Platform::current()->clipboard()->writeDataObject(dragData);
 
167
}
 
168
 
 
169
bool Pasteboard::canSmartReplace()
 
170
{
 
171
    return WebKit::Platform::current()->clipboard()->isFormatAvailable(WebKit::WebClipboard::FormatSmartPaste, m_selectionMode ? WebKit::WebClipboard::BufferSelection : WebKit::WebClipboard::BufferStandard);
 
172
}
 
173
 
 
174
String Pasteboard::plainText(Frame* frame)
 
175
{
 
176
    return WebKit::Platform::current()->clipboard()->readPlainText(m_selectionMode ? WebKit::WebClipboard::BufferSelection : WebKit::WebClipboard::BufferStandard);
 
177
}
 
178
 
 
179
PassRefPtr<DocumentFragment> Pasteboard::documentFragment(Frame* frame, PassRefPtr<Range> context, bool allowPlainText, bool& chosePlainText)
 
180
{
 
181
    chosePlainText = false;
 
182
    WebKit::WebClipboard::Buffer buffer = m_selectionMode ? WebKit::WebClipboard::BufferSelection : WebKit::WebClipboard::BufferStandard;
 
183
 
 
184
    if (WebKit::Platform::current()->clipboard()->isFormatAvailable(WebKit::WebClipboard::FormatHTML, buffer)) {
 
185
        WebKit::WebString markup;
 
186
        unsigned fragmentStart = 0;
 
187
        unsigned fragmentEnd = 0;
 
188
        WebKit::WebURL url;
 
189
        markup = WebKit::Platform::current()->clipboard()->readHTML(buffer, &url, &fragmentStart, &fragmentEnd);
 
190
        if (!markup.isEmpty()) {
 
191
          RefPtr<DocumentFragment> fragment =
 
192
              createFragmentFromMarkupWithContext(frame->document(), markup, fragmentStart, fragmentEnd, KURL(url), DisallowScriptingContent);
 
193
          if (fragment)
 
194
              return fragment.release();
 
195
        }
 
196
    }
 
197
 
 
198
    if (allowPlainText) {
 
199
        String markup = WebKit::Platform::current()->clipboard()->readPlainText(buffer);
 
200
        if (!markup.isEmpty()) {
 
201
            chosePlainText = true;
 
202
 
 
203
            RefPtr<DocumentFragment> fragment =
 
204
                createFragmentFromText(context.get(), markup);
 
205
            if (fragment)
 
206
                return fragment.release();
 
207
        }
 
208
    }
 
209
 
 
210
    return 0;
 
211
}
 
212
 
 
213
} // namespace WebCore