1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // This file declares the ScopedClipboardWriter class, a wrapper around
6 // the Clipboard class which simplifies writing data to the system clipboard.
7 // Upon deletion the class atomically writes all data to the clipboard,
8 // avoiding any potential race condition with other processes that are also
9 // writing to the system clipboard.
11 #ifndef UI_BASE_CLIPBOARD_SCOPED_CLIPBOARD_WRITER_H_
12 #define UI_BASE_CLIPBOARD_SCOPED_CLIPBOARD_WRITER_H_
16 #include "base/strings/string16.h"
17 #include "third_party/skia/include/core/SkBitmap.h"
18 #include "ui/base/clipboard/clipboard.h"
19 #include "ui/base/ui_base_export.h"
27 // This class is a wrapper for |Clipboard| that handles packing data
28 // into a Clipboard::ObjectMap.
29 class UI_BASE_EXPORT ScopedClipboardWriter
{
31 // Create an instance that is a simple wrapper around the clipboard of the
33 explicit ScopedClipboardWriter(ClipboardType type
);
35 ~ScopedClipboardWriter();
37 // Converts |text| to UTF-8 and adds it to the clipboard.
38 void WriteText(const base::string16
& text
);
40 // Converts the text of the URL to UTF-8 and adds it to the clipboard, then
41 // notifies the Clipboard that we just wrote a URL.
42 void WriteURL(const base::string16
& text
);
44 // Adds HTML to the clipboard. The url parameter is optional, but especially
45 // useful if the HTML fragment contains relative links.
46 void WriteHTML(const base::string16
& markup
, const std::string
& source_url
);
48 // Adds RTF to the clipboard.
49 void WriteRTF(const std::string
& rtf_data
);
51 // Adds a bookmark to the clipboard.
52 void WriteBookmark(const base::string16
& bookmark_title
,
53 const std::string
& url
);
55 // Adds an html hyperlink (<a href>) to the clipboard. |anchor_text| and
56 // |url| will be escaped as needed.
57 void WriteHyperlink(const base::string16
& anchor_text
,
58 const std::string
& url
);
60 // Used by WebKit to determine whether WebKit wrote the clipboard last
61 void WriteWebSmartPaste();
63 // Adds arbitrary pickled data to clipboard.
64 void WritePickledData(const base::Pickle
& pickle
,
65 const Clipboard::FormatType
& format
);
67 void WriteImage(const SkBitmap
& bitmap
);
69 // Removes all objects that would be written to the clipboard.
73 // Converts |text| to UTF-8 and adds it to the clipboard. If it's a URL, we
74 // also notify the clipboard of that fact.
75 void WriteTextOrURL(const base::string16
& text
, bool is_url
);
77 // We accumulate the data passed to the various targets in the |objects_|
78 // vector, and pass it to Clipboard::WriteObjects() during object destruction.
79 Clipboard::ObjectMap objects_
;
80 const ClipboardType type_
;
84 // We keep around the UTF-8 text of the URL in order to pass it to
85 // Clipboard::DidWriteURL().
86 std::string url_text_
;
88 DISALLOW_COPY_AND_ASSIGN(ScopedClipboardWriter
);
93 #endif // UI_BASE_CLIPBOARD_SCOPED_CLIPBOARD_WRITER_H_