blob: 82bf6526076609567e75e4b6126f94636275333b [file] [log] [blame]
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string>
#include "base/component_export.h"
#include "base/macros.h"
#include "base/strings/string16.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/base/clipboard/clipboard.h"
#include "ui/base/clipboard/clipboard_data_endpoint.h"
namespace base {
class Pickle;
namespace ui {
// |ScopedClipboardWriter|:
// - is a wrapper for |Clipboard|.
// - simplifies writing data to the system clipboard.
// - handles packing data into a Clipboard::ObjectMap.
// Upon deletion, the class atomically writes all data to the clipboard,
// avoiding any potential race condition with other processes that are also
// writing to the system clipboard.
// Create an instance that is a simple wrapper around the clipboard of the
// given buffer with an optional parameter indicating the source of the data.
// TODO( change its references to use
// ClipboardDataEndpoint, if possible.
explicit ScopedClipboardWriter(
ClipboardBuffer buffer,
std::unique_ptr<ClipboardDataEndpoint> src = nullptr);
// Converts |text| to UTF-8 and adds it to the clipboard.
void WriteText(const base::string16& text);
// Adds HTML to the clipboard. The url parameter is optional, but especially
// useful if the HTML fragment contains relative links.
void WriteHTML(const base::string16& markup, const std::string& source_url);
// Adds RTF to the clipboard.
void WriteRTF(const std::string& rtf_data);
// Adds a bookmark to the clipboard.
void WriteBookmark(const base::string16& bookmark_title,
const std::string& url);
// Adds an html hyperlink (<a href>) to the clipboard. |anchor_text| and
// |url| will be escaped as needed.
void WriteHyperlink(const base::string16& anchor_text,
const std::string& url);
// Used by WebKit to determine whether WebKit wrote the clipboard last
void WriteWebSmartPaste();
// Adds arbitrary pickled data to clipboard.
void WritePickledData(const base::Pickle& pickle,
const ClipboardFormatType& format);
// Data is written to the system clipboard in the same order as WriteData
// calls are received.
void WriteData(const base::string16& format, mojo_base::BigBuffer data);
void WriteImage(const SkBitmap& bitmap);
// Mark the data to be written as confidential.
void MarkAsConfidential();
// Removes all objects that would be written to the clipboard.
void Reset();
// We accumulate the data passed to the various targets in the |objects_|
// vector, and pass it to Clipboard::WritePortableRepresentations() during
// object destruction.
Clipboard::ObjectMap objects_;
std::vector<Clipboard::PlatformRepresentation> platform_representations_;
// The type is set at construction, and can be changed before committing.
const ClipboardBuffer buffer_;
SkBitmap bitmap_;
bool confidential_ = false;
// The source of the data written in ScopedClipboardWriter, nullptr means it's
// not set, or the source of the data can't be represented by
// ClipboardDataEndpoint.
std::unique_ptr<ClipboardDataEndpoint> data_src_ = nullptr;
} // namespace ui