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. 4 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 |clipboard_|, 8// avoiding any potential race condition with other processes that are also 9// writing to the system clipboard. 10 11#ifndef UI_BASE_CLIPBOARD_SCOPED_CLIPBOARD_WRITER_H_ 12#define UI_BASE_CLIPBOARD_SCOPED_CLIPBOARD_WRITER_H_ 13 14#include <string> 15 16#include "base/strings/string16.h" 17#include "ui/base/clipboard/clipboard.h" 18#include "ui/base/ui_export.h" 19 20class Pickle; 21 22namespace ui { 23 24// This class is a wrapper for |Clipboard| that handles packing data 25// into a Clipboard::ObjectMap. 26// NB: You should probably NOT be using this class if you include 27// webkit_glue.h. Use ScopedClipboardWriterGlue instead. 28class UI_EXPORT ScopedClipboardWriter { 29 public: 30 // Create an instance that is a simple wrapper around clipboard. 31 ScopedClipboardWriter(Clipboard* clipboard, Clipboard::Buffer buffer); 32 33 ~ScopedClipboardWriter(); 34 35 // Converts |text| to UTF-8 and adds it to the clipboard. 36 void WriteText(const string16& text); 37 38 // Converts the text of the URL to UTF-8 and adds it to the clipboard, then 39 // notifies the Clipboard that we just wrote a URL. 40 void WriteURL(const string16& text); 41 42 // Adds HTML to the clipboard. The url parameter is optional, but especially 43 // useful if the HTML fragment contains relative links. 44 void WriteHTML(const string16& markup, const std::string& source_url); 45 46 // Adds RTF to the clipboard. 47 void WriteRTF(const std::string& rtf_data); 48 49 // Adds a bookmark to the clipboard. 50 void WriteBookmark(const string16& bookmark_title, 51 const std::string& url); 52 53 // Adds an html hyperlink (<a href>) to the clipboard. |anchor_text| should 54 // be escaped prior to being passed in. 55 void WriteHyperlink(const string16& anchor_text, const std::string& url); 56 57 // Used by WebKit to determine whether WebKit wrote the clipboard last 58 void WriteWebSmartPaste(); 59 60 // Adds a bitmap to the clipboard 61 // Pixel format is assumed to be 32-bit BI_RGB. 62 void WriteBitmapFromPixels(const void* pixels, const gfx::Size& size); 63 64 // Adds arbitrary pickled data to clipboard. 65 void WritePickledData(const Pickle& pickle, 66 const Clipboard::FormatType& format); 67 68 // Removes all objects that would be written to the clipboard. 69 void Reset(); 70 71 protected: 72 // Converts |text| to UTF-8 and adds it to the clipboard. If it's a URL, we 73 // also notify the clipboard of that fact. 74 void WriteTextOrURL(const string16& text, bool is_url); 75 76 // We accumulate the data passed to the various targets in the |objects_| 77 // vector, and pass it to Clipboard::WriteObjects() during object destruction. 78 Clipboard::ObjectMap objects_; 79 Clipboard* clipboard_; 80 Clipboard::Buffer buffer_; 81 82 // We keep around the UTF-8 text of the URL in order to pass it to 83 // Clipboard::DidWriteURL(). 84 std::string url_text_; 85 86 private: 87 DISALLOW_COPY_AND_ASSIGN(ScopedClipboardWriter); 88}; 89 90} // namespace ui 91 92#endif // UI_BASE_CLIPBOARD_SCOPED_CLIPBOARD_WRITER_H_ 93 94