Skip to content

Commit

Permalink
[Async Clipboard API] Add histogram for empty clipboard.
Browse files Browse the repository at this point in the history
Record when an empty clipboard is encountered during `read()`.

Gathering data for w3c/clipboard-apis#179.

Bug: 1500440

Change-Id: I7c9c162ec7e17ea18a42683c9720330cbac57b3d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5029806
Reviewed-by: Evan Stade <estade@chromium.org>
Commit-Queue: Anupam Snigdha <snianu@microsoft.com>
Cr-Commit-Position: refs/heads/main@{#1225837}
  • Loading branch information
snianu authored and Chromium LUCI CQ committed Nov 17, 2023
1 parent 6dffcd3 commit 2c14659
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
39 changes: 39 additions & 0 deletions content/browser/renderer_host/clipboard_host_impl_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "base/base_paths.h"
#include "base/files/file_util.h"
#include "base/path_service.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/threading/thread_restrictions.h"
#include "content/public/browser/browser_context.h"
Expand Down Expand Up @@ -181,4 +182,42 @@ IN_PROC_BROWSER_TEST_P(ClipboardDocUrlBrowserTestP, HtmlUrl) {
EXPECT_EQ(src_url, main_url.spec());
}

class ClipboardBrowserTest : public ClipboardHostImplBrowserTest {
public:
ClipboardBrowserTest() = default;
};

IN_PROC_BROWSER_TEST_F(ClipboardBrowserTest, EmptyClipboard) {
base::HistogramTester histogram_tester;
GURL main_url(embedded_test_server()->GetURL("/title1.html"));
ASSERT_TRUE(NavigateToURL(shell(), main_url));
PermissionController* permission_controller =
GetRenderFrameHost()->GetBrowserContext()->GetPermissionController();
url::Origin origin = url::Origin::Create(main_url);
SetPermissionControllerOverrideForDevTools(
permission_controller, origin,
blink::PermissionType::CLIPBOARD_READ_WRITE,
blink::mojom::PermissionStatus::GRANTED);
ui::Clipboard::GetForCurrentThread()->Clear(ui::ClipboardBuffer::kCopyPaste);
// Currently we throw an exception if the clipboard is empty.
ASSERT_FALSE(ExecJs(shell(), " navigator.clipboard.read()"));
SetPermissionControllerOverrideForDevTools(
permission_controller, origin,
blink::PermissionType::CLIPBOARD_SANITIZED_WRITE,
blink::mojom::PermissionStatus::GRANTED);
ASSERT_TRUE(ExecJs(
shell(),
" const format1 = 'text/html';"
" const textInput = '<p>Hello</p>';"
" const blobInput1 = new Blob([textInput], {type: format1});"
" const clipboardItemInput = new ClipboardItem({[format1]: blobInput1});"
" navigator.clipboard.write([clipboardItemInput]);"));
ASSERT_TRUE(ExecJs(shell(), " navigator.clipboard.read()"));
content::FetchHistogramsFromChildProcesses();
histogram_tester.ExpectBucketCount("Blink.Clipboard.Read.NumberOfFormats", 0,
1);
histogram_tester.ExpectBucketCount("Blink.Clipboard.Read.NumberOfFormats", 1,
1);
}

} // namespace content
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <memory>
#include <utility>

#include "base/metrics/histogram_functions.h"
#include "base/task/single_thread_task_runner.h"
#include "mojo/public/cpp/base/big_buffer.h"
#include "third_party/blink/public/common/features.h"
Expand Down Expand Up @@ -351,6 +352,8 @@ void ClipboardPromise::ResolveRead() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(GetExecutionContext());

base::UmaHistogramCounts100("Blink.Clipboard.Read.NumberOfFormats",
clipboard_item_data_.size());
if (!clipboard_item_data_.size()) {
script_promise_resolver_->Reject(MakeGarbageCollected<DOMException>(
DOMExceptionCode::kDataError, "No valid data on clipboard."));
Expand Down
11 changes: 11 additions & 0 deletions tools/metrics/histograms/metadata/blink/histograms.xml
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,17 @@ chromium-metrics-reviews@google.com.
</summary>
</histogram>

<histogram name="Blink.Clipboard.Read.NumberOfFormats" units="count"
expires_after="2024-03-31">
<owner>estade@chromium.org</owner>
<owner>storage-dev@chromium.org</owner>
<owner>snianu@microsoft.com</owner>
<summary>
Reports the number of formats read from the system clipboard when
navigator.clipboard.read() is called.
</summary>
</histogram>

<histogram name="Blink.ColorGamut.Destination" enum="Gamut" expires_after="M85">
<owner>brianosman@chromium.org</owner>
<owner>mcasas@google.com</owner>
Expand Down

0 comments on commit 2c14659

Please sign in to comment.