Commit 46c0324e authored by Darwin Huang's avatar Darwin Huang Committed by Commit Bot

Windows Clipboard: Replace base::LazyInstance<T>::Leaky with base::NoDestructor<T>.

static initialization is thread-safe, since C++11, and base::LazyInstance is
deprecated[1]. Therefore, remove use of base::LazyInstance. This also provides
the opportunity to remove a preprocessor macro. This CL also fixes
TestURLExchangeFormatsViaCOM, which was using an incorrect FORMATETC argument.

[1]: https://cs.chromium.org/chromium/src/base/lazy_instance.h?rcl=82bf98cd15cedc12558f475feb6770f0c9d903ea&l=5

Bug: 800760
Change-Id: Ib902ca5f7660c609f74c2681a9bf96cb828be52c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1764890
Commit-Queue: Darwin Huang <huangdarwin@chromium.org>
Auto-Submit: Darwin Huang <huangdarwin@chromium.org>
Reviewed-by: default avatarScott Violet <sky@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarVictor Costan <pwnall@chromium.org>
Cr-Commit-Position: refs/heads/master@{#690442}
parent 48104db6
......@@ -6,9 +6,9 @@
#include <shlobj.h>
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/no_destructor.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
......@@ -66,101 +66,95 @@ ClipboardFormatType ClipboardFormatType::GetType(
}
// The following formats can be referenced by ClipboardUtilWin::GetPlainText.
// For reasons (COM), they must be initialized in a thread-safe manner.
// Clipboard formats are initialized in a thread-safe manner, using static
// initialization. COM requires this thread-safe initialization.
// TODO(dcheng): We probably need to make static initialization of "known"
// ClipboardFormatTypes thread-safe on all platforms.
#define CR_STATIC_UI_CLIPBOARD_FORMAT_TYPE(name, ...) \
struct ClipboardFormatTypeArgumentForwarder : public ClipboardFormatType { \
ClipboardFormatTypeArgumentForwarder() \
: ClipboardFormatType(__VA_ARGS__) {} \
}; \
static base::LazyInstance<ClipboardFormatTypeArgumentForwarder>::Leaky \
name = LAZY_INSTANCE_INITIALIZER
// static
const ClipboardFormatType& ClipboardFormatType::GetUrlType() {
CR_STATIC_UI_CLIPBOARD_FORMAT_TYPE(type,
::RegisterClipboardFormat(CFSTR_INETURLA));
return type.Get();
static base::NoDestructor<ClipboardFormatType> format(
::RegisterClipboardFormat(CFSTR_INETURLA));
return *format;
}
// static
const ClipboardFormatType& ClipboardFormatType::GetUrlWType() {
CR_STATIC_UI_CLIPBOARD_FORMAT_TYPE(type,
::RegisterClipboardFormat(CFSTR_INETURLW));
return type.Get();
static base::NoDestructor<ClipboardFormatType> format(
::RegisterClipboardFormat(CFSTR_INETURLW));
return *format;
}
// static
const ClipboardFormatType& ClipboardFormatType::GetMozUrlType() {
CR_STATIC_UI_CLIPBOARD_FORMAT_TYPE(
type, ::RegisterClipboardFormat(L"text/x-moz-url"));
return type.Get();
static base::NoDestructor<ClipboardFormatType> format(
::RegisterClipboardFormat(L"text/x-moz-url"));
return *format;
}
// static
const ClipboardFormatType& ClipboardFormatType::GetPlainTextType() {
CR_STATIC_UI_CLIPBOARD_FORMAT_TYPE(type, CF_TEXT);
return type.Get();
static base::NoDestructor<ClipboardFormatType> format(CF_TEXT);
return *format;
}
// static
const ClipboardFormatType& ClipboardFormatType::GetPlainTextWType() {
CR_STATIC_UI_CLIPBOARD_FORMAT_TYPE(type, CF_UNICODETEXT);
return type.Get();
static base::NoDestructor<ClipboardFormatType> format(CF_UNICODETEXT);
return *format;
}
// static
const ClipboardFormatType& ClipboardFormatType::GetFilenameType() {
CR_STATIC_UI_CLIPBOARD_FORMAT_TYPE(
type, ::RegisterClipboardFormat(CFSTR_FILENAMEA));
return type.Get();
static base::NoDestructor<ClipboardFormatType> format(
::RegisterClipboardFormat(CFSTR_FILENAMEA));
return *format;
}
// static
const ClipboardFormatType& ClipboardFormatType::GetFilenameWType() {
CR_STATIC_UI_CLIPBOARD_FORMAT_TYPE(
type, ::RegisterClipboardFormat(CFSTR_FILENAMEW));
return type.Get();
static base::NoDestructor<ClipboardFormatType> format(
::RegisterClipboardFormat(CFSTR_FILENAMEW));
return *format;
}
// MS HTML Format
// static
const ClipboardFormatType& ClipboardFormatType::GetHtmlType() {
CR_STATIC_UI_CLIPBOARD_FORMAT_TYPE(type,
::RegisterClipboardFormat(L"HTML Format"));
return type.Get();
static base::NoDestructor<ClipboardFormatType> format(
::RegisterClipboardFormat(L"HTML Format"));
return *format;
}
// MS RTF Format
// static
const ClipboardFormatType& ClipboardFormatType::GetRtfType() {
CR_STATIC_UI_CLIPBOARD_FORMAT_TYPE(
type, ::RegisterClipboardFormat(L"Rich Text Format"));
return type.Get();
static base::NoDestructor<ClipboardFormatType> format(
::RegisterClipboardFormat(L"Rich Text Format"));
return *format;
}
// static
const ClipboardFormatType& ClipboardFormatType::GetBitmapType() {
CR_STATIC_UI_CLIPBOARD_FORMAT_TYPE(type, CF_BITMAP);
return type.Get();
static base::NoDestructor<ClipboardFormatType> format(CF_BITMAP);
return *format;
}
// Firefox text/html
// static
const ClipboardFormatType& ClipboardFormatType::GetTextHtmlType() {
CR_STATIC_UI_CLIPBOARD_FORMAT_TYPE(type,
::RegisterClipboardFormat(L"text/html"));
return type.Get();
static base::NoDestructor<ClipboardFormatType> format(
::RegisterClipboardFormat(L"text/html"));
return *format;
}
// static
const ClipboardFormatType& ClipboardFormatType::GetCFHDropType() {
CR_STATIC_UI_CLIPBOARD_FORMAT_TYPE(type, CF_HDROP);
return type.Get();
static base::NoDestructor<ClipboardFormatType> format(CF_HDROP);
return *format;
}
// Nothing prevents the drag source app from using the CFSTR_FILEDESCRIPTORA
......@@ -168,16 +162,16 @@ const ClipboardFormatType& ClipboardFormatType::GetCFHDropType() {
// register both the ANSI and Unicode file group descriptors.
// static
const ClipboardFormatType& ClipboardFormatType::GetFileDescriptorType() {
CR_STATIC_UI_CLIPBOARD_FORMAT_TYPE(
type, ::RegisterClipboardFormat(CFSTR_FILEDESCRIPTORA));
return type.Get();
static base::NoDestructor<ClipboardFormatType> format(
::RegisterClipboardFormat(CFSTR_FILEDESCRIPTORA));
return *format;
}
// static
const ClipboardFormatType& ClipboardFormatType::GetFileDescriptorWType() {
CR_STATIC_UI_CLIPBOARD_FORMAT_TYPE(
type, ::RegisterClipboardFormat(CFSTR_FILEDESCRIPTORW));
return type.Get();
static base::NoDestructor<ClipboardFormatType> format(
::RegisterClipboardFormat(CFSTR_FILEDESCRIPTORW));
return *format;
}
// static
......@@ -189,9 +183,9 @@ const ClipboardFormatType& ClipboardFormatType::GetFileContentZeroType() {
// TODO(https://crbug.com/950756): Should TYMED_ISTREAM / TYMED_ISTORAGE be
// used instead of TYMED_HGLOBAL in
// OSExchangeDataProviderWin::SetFileContents.
CR_STATIC_UI_CLIPBOARD_FORMAT_TYPE(
type, ::RegisterClipboardFormat(CFSTR_FILECONTENTS), 0);
return type.Get();
static base::NoDestructor<ClipboardFormatType> format(
::RegisterClipboardFormat(CFSTR_FILECONTENTS));
return *format;
}
// static
......@@ -216,32 +210,31 @@ const ClipboardFormatType& ClipboardFormatType::GetFileContentAtIndexType(
// static
const ClipboardFormatType& ClipboardFormatType::GetIDListType() {
CR_STATIC_UI_CLIPBOARD_FORMAT_TYPE(
type, ::RegisterClipboardFormat(CFSTR_SHELLIDLIST));
return type.Get();
static base::NoDestructor<ClipboardFormatType> format(
::RegisterClipboardFormat(CFSTR_SHELLIDLIST));
return *format;
}
// static
const ClipboardFormatType& ClipboardFormatType::GetWebKitSmartPasteType() {
CR_STATIC_UI_CLIPBOARD_FORMAT_TYPE(
type, ::RegisterClipboardFormat(L"WebKit Smart Paste Format"));
return type.Get();
static base::NoDestructor<ClipboardFormatType> format(
::RegisterClipboardFormat(L"WebKit Smart Paste Format"));
return *format;
}
// static
const ClipboardFormatType& ClipboardFormatType::GetWebCustomDataType() {
// TODO(dcheng): This name is temporary. See http://crbug.com/106449.
CR_STATIC_UI_CLIPBOARD_FORMAT_TYPE(
type, ::RegisterClipboardFormat(L"Chromium Web Custom MIME Data Format"));
return type.Get();
// TODO(http://crbug.com/106449): Standardize this name.
static base::NoDestructor<ClipboardFormatType> format(
::RegisterClipboardFormat(L"Chromium Web Custom MIME Data Format"));
return *format;
}
// static
const ClipboardFormatType& ClipboardFormatType::GetPepperCustomDataType() {
CR_STATIC_UI_CLIPBOARD_FORMAT_TYPE(
type, ::RegisterClipboardFormat(L"Chromium Pepper MIME Data Format"));
return type.Get();
static base::NoDestructor<ClipboardFormatType> format(
::RegisterClipboardFormat(L"Chromium Pepper MIME Data Format"));
return *format;
}
#undef CR_STATIC_UI_CLIPBOARD_FORMAT_TYPE
} // namespace ui
......@@ -312,8 +312,8 @@ TEST_F(OSExchangeDataWinTest, TestURLExchangeFormatsViaCOM) {
{
CLIPFORMAT cfstr_file_contents =
RegisterClipboardFormat(CFSTR_FILECONTENTS);
FORMATETC format_etc =
{ cfstr_file_contents, NULL, DVASPECT_CONTENT, 0, TYMED_HGLOBAL };
FORMATETC format_etc = {cfstr_file_contents, nullptr, DVASPECT_CONTENT, -1,
TYMED_HGLOBAL};
EXPECT_EQ(S_OK, com_data->QueryGetData(&format_etc));
STGMEDIUM medium;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment