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 the 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_base_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. 26class UI_BASE_EXPORT ScopedClipboardWriter { 27 public: 28 // Create an instance that is a simple wrapper around the clipboard of the 29 // given type. 30 explicit ScopedClipboardWriter(ClipboardType type); 31 32 ~ScopedClipboardWriter(); 33 34 // Converts |text| to UTF-8 and adds it to the clipboard. 35 void WriteText(const base::string16& text); 36 37 // Converts the text of the URL to UTF-8 and adds it to the clipboard, then 38 // notifies the Clipboard that we just wrote a URL. 39 void WriteURL(const base::string16& text); 40 41 // Adds HTML to the clipboard. The url parameter is optional, but especially 42 // useful if the HTML fragment contains relative links. 43 void WriteHTML(const base::string16& markup, const std::string& source_url); 44 45 // Adds RTF to the clipboard. 46 void WriteRTF(const std::string& rtf_data); 47 48 // Adds a bookmark to the clipboard. 49 void WriteBookmark(const base::string16& bookmark_title, 50 const std::string& url); 51 52 // Adds an html hyperlink (<a href>) to the clipboard. |anchor_text| and 53 // |url| will be escaped as needed. 54 void WriteHyperlink(const base::string16& anchor_text, 55 const std::string& url); 56 57 // Used by WebKit to determine whether WebKit wrote the clipboard last 58 void WriteWebSmartPaste(); 59 60 // Adds arbitrary pickled data to clipboard. 61 void WritePickledData(const Pickle& pickle, 62 const Clipboard::FormatType& format); 63 64 // Removes all objects that would be written to the clipboard. 65 void Reset(); 66 67 protected: 68 // Converts |text| to UTF-8 and adds it to the clipboard. If it's a URL, we 69 // also notify the clipboard of that fact. 70 void WriteTextOrURL(const base::string16& text, bool is_url); 71 72 // We accumulate the data passed to the various targets in the |objects_| 73 // vector, and pass it to Clipboard::WriteObjects() during object destruction. 74 Clipboard::ObjectMap objects_; 75 const ClipboardType type_; 76 77 // We keep around the UTF-8 text of the URL in order to pass it to 78 // Clipboard::DidWriteURL(). 79 std::string url_text_; 80 81 private: 82 DISALLOW_COPY_AND_ASSIGN(ScopedClipboardWriter); 83}; 84 85} // namespace ui 86 87#endif // UI_BASE_CLIPBOARD_SCOPED_CLIPBOARD_WRITER_H_ 88 89