Commit f4e2d638 authored by Marijn Kruisselbrink's avatar Marijn Kruisselbrink Committed by Commit Bot

Take advantage of mojo blobs for lifetime management in clipboard code.

Bug: 604800, 769024, 740744
Change-Id: I2267b1015e3c9ad0d3dfe019572ff328557e0112
Reviewed-on: https://chromium-review.googlesource.com/882483Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarJeremy Roman <jbroman@chromium.org>
Reviewed-by: default avatarDaniel Murphy <dmurph@chromium.org>
Reviewed-by: default avatarAvi Drissman <avi@chromium.org>
Commit-Queue: Marijn Kruisselbrink <mek@chromium.org>
Cr-Commit-Position: refs/heads/master@{#532042}
parent 359e47b2
......@@ -25,6 +25,7 @@
#include "content/public/common/content_features.h"
#include "services/network/public/cpp/resource_request_body.h"
#include "storage/browser/blob/blob_data_builder.h"
#include "storage/browser/blob/blob_impl.h"
#include "storage/browser/blob/blob_memory_controller.h"
#include "storage/browser/blob/blob_storage_context.h"
......@@ -68,6 +69,14 @@ class BlobHandleImpl : public BlobHandle {
std::string GetUUID() override { return handle_->uuid(); }
blink::mojom::BlobPtr PassBlob() override {
blink::mojom::BlobPtr result;
storage::BlobImpl::Create(
base::MakeUnique<storage::BlobDataHandle>(*handle_),
MakeRequest(&result));
return result;
}
private:
std::unique_ptr<storage::BlobDataHandle> handle_;
};
......
......@@ -40,10 +40,6 @@ void ReleaseSharedMemoryPixels(void* addr, void* context) {
DCHECK_EQ(MOJO_RESULT_OK, result);
}
// No-op helper for delayed cleanup of BlobHandles generated by reading
// clipboard images.
void CleanupReadImageBlob(std::unique_ptr<content::BlobHandle>) {}
} // namespace
ClipboardHostImpl::ClipboardHostImpl(
......@@ -185,12 +181,7 @@ void ClipboardHostImpl::ReadAndEncodeImage(const SkBitmap& bitmap,
}
}
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::BindOnce(
[](ReadImageCallback callback) {
std::move(callback).Run(std::string(),
std::string(), -1);
},
std::move(callback)));
base::BindOnce(std::move(callback), nullptr));
}
void ClipboardHostImpl::OnReadAndEncodeImageFinished(
......@@ -198,36 +189,21 @@ void ClipboardHostImpl::OnReadAndEncodeImageFinished(
ReadImageCallback callback) {
// |blob_storage_context_| must be accessed only on the IO thread.
DCHECK_CURRENTLY_ON(BrowserThread::IO);
std::string blob_uuid;
std::string mime_type;
int64_t size = -1;
blink::mojom::SerializedBlobPtr blob;
if (png_data.size() < std::numeric_limits<uint32_t>::max()) {
std::unique_ptr<content::BlobHandle> blob_handle =
blob_storage_context_->CreateMemoryBackedBlob(
reinterpret_cast<char*>(png_data.data()), png_data.size(), "");
if (blob_handle) {
blob_uuid = blob_handle->GetUUID();
mime_type = ui::Clipboard::kMimeTypePNG;
size = static_cast<int64_t>(png_data.size());
// Give the renderer a minute to pick up a reference to the blob before
// giving up.
// TODO(dmurph): There should be a better way of transferring ownership of
// a blob from the browser to the renderer, rather than relying on this
// timeout to clean up eventually. See https://crbug.com/604800.
BrowserThread::PostDelayedTask(
BrowserThread::IO, FROM_HERE,
base::BindOnce(&CleanupReadImageBlob, base::Passed(&blob_handle)),
base::TimeDelta::FromMinutes(1));
std::string blob_uuid = blob_handle->GetUUID();
blob = blink::mojom::SerializedBlob::New(
blob_uuid, ui::Clipboard::kMimeTypePNG,
static_cast<int64_t>(png_data.size()),
blob_handle->PassBlob().PassInterface());
}
}
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::BindOnce(
[](ReadImageCallback callback, const std::string& blob_uuid,
const std::string& mime_type, int64_t size) {
std::move(callback).Run(blob_uuid, mime_type, size);
},
std::move(callback), blob_uuid, mime_type, size));
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::BindOnce(std::move(callback), std::move(blob)));
}
void ClipboardHostImpl::ReadCustomData(ui::ClipboardType clipboard_type,
......
......@@ -6,6 +6,7 @@
#define CONTENT_PUBLIC_BROWSER_BLOB_HANDLE_H_
#include <string>
#include "third_party/WebKit/common/blob/blob.mojom.h"
namespace content {
......@@ -15,6 +16,7 @@ class BlobHandle {
public:
virtual ~BlobHandle() {}
virtual std::string GetUUID() = 0;
virtual blink::mojom::BlobPtr PassBlob() = 0;
protected:
BlobHandle() {}
......
......@@ -131,10 +131,8 @@ File* DataObjectItem::GetAsFile() const {
mojom::ClipboardBuffer::kStandard);
if (blob_info.size() < 0)
return nullptr;
return File::Create(
"image.png", CurrentTimeMS(),
BlobDataHandle::Create(blob_info.Uuid(), blob_info.GetType(),
blob_info.size()));
return File::Create("image.png", CurrentTimeMS(),
blob_info.GetBlobHandle());
}
return nullptr;
......
......@@ -153,13 +153,12 @@ WebBlobInfo WebClipboardImpl::ReadImage(mojom::ClipboardBuffer buffer) {
if (!IsValidBufferType(buffer))
return WebBlobInfo();
WTF::String blob_uuid;
WTF::String type;
int64_t size = -1;
clipboard_->ReadImage(buffer, &blob_uuid, &type, &size);
if (size < 0)
mojom::blink::SerializedBlobPtr blob;
clipboard_->ReadImage(buffer, &blob);
if (!blob)
return WebBlobInfo();
return WebBlobInfo(blob_uuid, type, size);
return WebBlobInfo(blob->uuid, blob->content_type, blob->size,
blob->blob.PassHandle());
}
WebString WebClipboardImpl::ReadCustomData(mojom::ClipboardBuffer buffer,
......
......@@ -45,3 +45,14 @@ interface Blob {
// identify the blob.
GetInternalUUID() => (string uuid);
};
// Struct wrapping a Blob interface pointer.
// TODO(mek): Once https://crbug.com/696107 is implemented all usage of this
// struct can be replaced with a simple Blob interface pointer with handle
// properties for the other attributes.
struct SerializedBlob {
string uuid;
string content_type;
uint64 size;
Blob blob;
};
......@@ -5,6 +5,7 @@
module blink.mojom;
import "mojo/common/string16.mojom";
import "third_party/WebKit/common/blob/blob.mojom";
import "ui/gfx/geometry/mojo/geometry.mojom";
import "url/mojo/url.mojom";
......@@ -51,9 +52,7 @@ interface ClipboardHost {
ReadRtf(blink.mojom.ClipboardBuffer buffer) => (string result);
[Sync]
ReadImage(blink.mojom.ClipboardBuffer buffer) => (string blob_uuid,
string mime_type,
int64 size);
ReadImage(blink.mojom.ClipboardBuffer buffer) => (SerializedBlob? blob);
[Sync]
ReadCustomData(blink.mojom.ClipboardBuffer buffer,
......
......@@ -37,13 +37,3 @@ struct TransferableMessage {
array<handle<message_pipe>> ports;
};
// Struct wrapping a Blob interface pointer.
// TODO(mek): Once https://crbug.com/696107 is implemented all usage of this
// struct can be replaced with a simple Blob interface pointer with handle
// properties for the other attributes.
struct SerializedBlob {
string uuid;
string content_type;
uint64 size;
Blob blob;
};
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