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 @@ ...@@ -25,6 +25,7 @@
#include "content/public/common/content_features.h" #include "content/public/common/content_features.h"
#include "services/network/public/cpp/resource_request_body.h" #include "services/network/public/cpp/resource_request_body.h"
#include "storage/browser/blob/blob_data_builder.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_memory_controller.h"
#include "storage/browser/blob/blob_storage_context.h" #include "storage/browser/blob/blob_storage_context.h"
...@@ -68,6 +69,14 @@ class BlobHandleImpl : public BlobHandle { ...@@ -68,6 +69,14 @@ class BlobHandleImpl : public BlobHandle {
std::string GetUUID() override { return handle_->uuid(); } 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: private:
std::unique_ptr<storage::BlobDataHandle> handle_; std::unique_ptr<storage::BlobDataHandle> handle_;
}; };
......
...@@ -40,10 +40,6 @@ void ReleaseSharedMemoryPixels(void* addr, void* context) { ...@@ -40,10 +40,6 @@ void ReleaseSharedMemoryPixels(void* addr, void* context) {
DCHECK_EQ(MOJO_RESULT_OK, result); 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 } // namespace
ClipboardHostImpl::ClipboardHostImpl( ClipboardHostImpl::ClipboardHostImpl(
...@@ -185,12 +181,7 @@ void ClipboardHostImpl::ReadAndEncodeImage(const SkBitmap& bitmap, ...@@ -185,12 +181,7 @@ void ClipboardHostImpl::ReadAndEncodeImage(const SkBitmap& bitmap,
} }
} }
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::BindOnce( base::BindOnce(std::move(callback), nullptr));
[](ReadImageCallback callback) {
std::move(callback).Run(std::string(),
std::string(), -1);
},
std::move(callback)));
} }
void ClipboardHostImpl::OnReadAndEncodeImageFinished( void ClipboardHostImpl::OnReadAndEncodeImageFinished(
...@@ -198,36 +189,21 @@ void ClipboardHostImpl::OnReadAndEncodeImageFinished( ...@@ -198,36 +189,21 @@ void ClipboardHostImpl::OnReadAndEncodeImageFinished(
ReadImageCallback callback) { ReadImageCallback callback) {
// |blob_storage_context_| must be accessed only on the IO thread. // |blob_storage_context_| must be accessed only on the IO thread.
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
std::string blob_uuid; blink::mojom::SerializedBlobPtr blob;
std::string mime_type;
int64_t size = -1;
if (png_data.size() < std::numeric_limits<uint32_t>::max()) { if (png_data.size() < std::numeric_limits<uint32_t>::max()) {
std::unique_ptr<content::BlobHandle> blob_handle = std::unique_ptr<content::BlobHandle> blob_handle =
blob_storage_context_->CreateMemoryBackedBlob( blob_storage_context_->CreateMemoryBackedBlob(
reinterpret_cast<char*>(png_data.data()), png_data.size(), ""); reinterpret_cast<char*>(png_data.data()), png_data.size(), "");
if (blob_handle) { if (blob_handle) {
blob_uuid = blob_handle->GetUUID(); std::string blob_uuid = blob_handle->GetUUID();
mime_type = ui::Clipboard::kMimeTypePNG; blob = blink::mojom::SerializedBlob::New(
size = static_cast<int64_t>(png_data.size()); blob_uuid, ui::Clipboard::kMimeTypePNG,
// Give the renderer a minute to pick up a reference to the blob before static_cast<int64_t>(png_data.size()),
// giving up. blob_handle->PassBlob().PassInterface());
// 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));
} }
} }
BrowserThread::PostTask( BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
BrowserThread::UI, FROM_HERE, base::BindOnce(std::move(callback), std::move(blob)));
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));
} }
void ClipboardHostImpl::ReadCustomData(ui::ClipboardType clipboard_type, void ClipboardHostImpl::ReadCustomData(ui::ClipboardType clipboard_type,
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#define CONTENT_PUBLIC_BROWSER_BLOB_HANDLE_H_ #define CONTENT_PUBLIC_BROWSER_BLOB_HANDLE_H_
#include <string> #include <string>
#include "third_party/WebKit/common/blob/blob.mojom.h"
namespace content { namespace content {
...@@ -15,6 +16,7 @@ class BlobHandle { ...@@ -15,6 +16,7 @@ class BlobHandle {
public: public:
virtual ~BlobHandle() {} virtual ~BlobHandle() {}
virtual std::string GetUUID() = 0; virtual std::string GetUUID() = 0;
virtual blink::mojom::BlobPtr PassBlob() = 0;
protected: protected:
BlobHandle() {} BlobHandle() {}
......
...@@ -131,10 +131,8 @@ File* DataObjectItem::GetAsFile() const { ...@@ -131,10 +131,8 @@ File* DataObjectItem::GetAsFile() const {
mojom::ClipboardBuffer::kStandard); mojom::ClipboardBuffer::kStandard);
if (blob_info.size() < 0) if (blob_info.size() < 0)
return nullptr; return nullptr;
return File::Create( return File::Create("image.png", CurrentTimeMS(),
"image.png", CurrentTimeMS(), blob_info.GetBlobHandle());
BlobDataHandle::Create(blob_info.Uuid(), blob_info.GetType(),
blob_info.size()));
} }
return nullptr; return nullptr;
......
...@@ -153,13 +153,12 @@ WebBlobInfo WebClipboardImpl::ReadImage(mojom::ClipboardBuffer buffer) { ...@@ -153,13 +153,12 @@ WebBlobInfo WebClipboardImpl::ReadImage(mojom::ClipboardBuffer buffer) {
if (!IsValidBufferType(buffer)) if (!IsValidBufferType(buffer))
return WebBlobInfo(); return WebBlobInfo();
WTF::String blob_uuid; mojom::blink::SerializedBlobPtr blob;
WTF::String type; clipboard_->ReadImage(buffer, &blob);
int64_t size = -1; if (!blob)
clipboard_->ReadImage(buffer, &blob_uuid, &type, &size);
if (size < 0)
return WebBlobInfo(); 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, WebString WebClipboardImpl::ReadCustomData(mojom::ClipboardBuffer buffer,
......
...@@ -45,3 +45,14 @@ interface Blob { ...@@ -45,3 +45,14 @@ interface Blob {
// identify the blob. // identify the blob.
GetInternalUUID() => (string uuid); 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 @@ ...@@ -5,6 +5,7 @@
module blink.mojom; module blink.mojom;
import "mojo/common/string16.mojom"; import "mojo/common/string16.mojom";
import "third_party/WebKit/common/blob/blob.mojom";
import "ui/gfx/geometry/mojo/geometry.mojom"; import "ui/gfx/geometry/mojo/geometry.mojom";
import "url/mojo/url.mojom"; import "url/mojo/url.mojom";
...@@ -51,9 +52,7 @@ interface ClipboardHost { ...@@ -51,9 +52,7 @@ interface ClipboardHost {
ReadRtf(blink.mojom.ClipboardBuffer buffer) => (string result); ReadRtf(blink.mojom.ClipboardBuffer buffer) => (string result);
[Sync] [Sync]
ReadImage(blink.mojom.ClipboardBuffer buffer) => (string blob_uuid, ReadImage(blink.mojom.ClipboardBuffer buffer) => (SerializedBlob? blob);
string mime_type,
int64 size);
[Sync] [Sync]
ReadCustomData(blink.mojom.ClipboardBuffer buffer, ReadCustomData(blink.mojom.ClipboardBuffer buffer,
......
...@@ -37,13 +37,3 @@ struct TransferableMessage { ...@@ -37,13 +37,3 @@ struct TransferableMessage {
array<handle<message_pipe>> ports; 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