Commit 6e14efc1 authored by Krzysztof Olczyk's avatar Krzysztof Olczyk Committed by Commit Bot

Move content::ScopedWebCallbacks to blink platform.

The content::ScopedWebCallbacks class is a helper for blink::WebCallbacks
and does not do anything strictly related to //content.
Moving it to //third_party/blink/public/platform makes it possible
to use it in all blink platform consumer, also the ones outside of //content,
such as //media/blink.
One example is //media/blink/webmediacapabilitiesclient_impl.cc which before
had to use specialized local copy of it.

This also adds two modifications to ScopedWebCallbacks:
* Make make_scoped_web_callbacks take a unique_ptr to WebCallbacks,
  instead of raw pointer that gets silently WrapUnique'ed inside,
  to make ownership passing more explicit,
* Modernize it to operate on OnceCallback, instead of Callback,
* Use default move constructor and move assignment.

Bug: 847211
Change-Id: I3d9b8a660dbfd66cf8721331777f36ea42c04bc8
Reviewed-on: https://chromium-review.googlesource.com/1104356
Commit-Queue: Chrome Cunningham <chcunningham@chromium.org>
Reviewed-by: default avatarChrome Cunningham <chcunningham@chromium.org>
Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Cr-Commit-Position: refs/heads/master@{#568572}
parent 66f0831c
......@@ -67,7 +67,6 @@ target(link_target_type, "child") {
"runtime_features.h",
"scoped_child_process_reference.cc",
"scoped_child_process_reference.h",
"scoped_web_callbacks.h",
"service_factory.cc",
"service_factory.h",
"thread_safe_sender.cc",
......
......@@ -115,7 +115,7 @@ ImageCaptureFrameGrabber::~ImageCaptureFrameGrabber() {
void ImageCaptureFrameGrabber::GrabFrame(
blink::WebMediaStreamTrack* track,
WebImageCaptureGrabFrameCallbacks* callbacks) {
std::unique_ptr<blink::WebImageCaptureGrabFrameCallbacks> callbacks) {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(!!callbacks);
......@@ -128,8 +128,8 @@ void ImageCaptureFrameGrabber::GrabFrame(
return;
}
ScopedWebCallbacks<WebImageCaptureGrabFrameCallbacks> scoped_callbacks =
make_scoped_web_callbacks(callbacks, base::Bind(&OnError));
auto scoped_callbacks = blink::MakeScopedWebCallbacks(
std::move(callbacks), base::BindOnce(&OnError));
// A SingleShotFrameHandler is bound and given to the Track to guarantee that
// only one VideoFrame is converted and delivered to OnSkImage(), otherwise
......@@ -149,7 +149,8 @@ void ImageCaptureFrameGrabber::GrabFrame(
}
void ImageCaptureFrameGrabber::OnSkImage(
ScopedWebCallbacks<blink::WebImageCaptureGrabFrameCallbacks> callbacks,
blink::ScopedWebCallbacks<blink::WebImageCaptureGrabFrameCallbacks>
callbacks,
sk_sp<SkImage> image) {
DCHECK(thread_checker_.CalledOnValidThread());
......
......@@ -5,13 +5,15 @@
#ifndef CONTENT_RENDERER_IMAGE_CAPTURE_IMAGE_CAPTURE_FRAME_GRABBER_H_
#define CONTENT_RENDERER_IMAGE_CAPTURE_IMAGE_CAPTURE_FRAME_GRABBER_H_
#include <memory>
#include "base/compiler_specific.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/threading/thread_checker.h"
#include "content/child/scoped_web_callbacks.h"
#include "content/common/content_export.h"
#include "content/public/renderer/media_stream_video_sink.h"
#include "third_party/blink/public/platform/scoped_web_callbacks.h"
#include "third_party/blink/public/platform/web_image_capture_frame_grabber.h"
namespace blink {
......@@ -36,15 +38,16 @@ class CONTENT_EXPORT ImageCaptureFrameGrabber final
// blink::WebImageCaptureFrameGrabber implementation.
void GrabFrame(blink::WebMediaStreamTrack* track,
blink::WebImageCaptureGrabFrameCallbacks* callbacks) override;
std::unique_ptr<blink::WebImageCaptureGrabFrameCallbacks>
callbacks) override;
private:
// Internal class to receive, convert and forward one frame.
class SingleShotFrameHandler;
void OnSkImage(
ScopedWebCallbacks<blink::WebImageCaptureGrabFrameCallbacks> callbacks,
sk_sp<SkImage> image);
void OnSkImage(blink::ScopedWebCallbacks<
blink::WebImageCaptureGrabFrameCallbacks> callbacks,
sk_sp<SkImage> image);
// Flag to indicate that there is a frame grabbing in progress.
bool frame_grab_in_progress_;
......
......@@ -13,7 +13,6 @@
#include "base/strings/string_tokenizer.h"
#include "base/strings/string_util.h"
#include "base/sys_info.h"
#include "content/child/scoped_web_callbacks.h"
#include "content/renderer/media/stream/media_stream_audio_track.h"
#include "content/renderer/media/stream/media_stream_track.h"
#include "content/renderer/media/webrtc/webrtc_uma_histograms.h"
......@@ -27,6 +26,7 @@
#include "media/base/video_frame.h"
#include "media/muxers/webm_muxer.h"
#include "third_party/blink/public/platform/modules/media_capabilities/web_media_configuration.h"
#include "third_party/blink/public/platform/scoped_web_callbacks.h"
#include "third_party/blink/public/platform/web_media_recorder_handler_client.h"
#include "third_party/blink/public/platform/web_media_stream_source.h"
#include "third_party/blink/public/platform/web_string.h"
......@@ -350,9 +350,8 @@ void MediaRecorderHandler::EncodingInfo(
DCHECK(configuration.video_configuration ||
configuration.audio_configuration);
ScopedWebCallbacks<WebMediaCapabilitiesQueryCallbacks> scoped_callbacks =
make_scoped_web_callbacks(callbacks.release(),
base::Bind(&OnEncodingInfoError));
auto scoped_callbacks = blink::MakeScopedWebCallbacks(
std::move(callbacks), base::BindOnce(&OnEncodingInfoError));
std::unique_ptr<blink::WebMediaCapabilitiesInfo> info(
new blink::WebMediaCapabilitiesInfo());
......
......@@ -22,6 +22,7 @@
#include "third_party/blink/public/platform/modules/media_capabilities/web_media_configuration.h"
#include "third_party/blink/public/platform/modules/media_capabilities/web_video_configuration.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/public/platform/scoped_web_callbacks.h"
namespace media {
......@@ -141,30 +142,21 @@ WebMediaCapabilitiesClientImpl::WebMediaCapabilitiesClientImpl() = default;
WebMediaCapabilitiesClientImpl::~WebMediaCapabilitiesClientImpl() = default;
namespace {
// This structs wraps WebMediaCapabilitiesQueryCallbacks and makes sure
// they are called even when the mojo response is not received
// (connection error) as there is a pending promise waiting for the callback.
// See https://crbug.com/847211
struct CallbacksHolder {
std::unique_ptr<blink::WebMediaCapabilitiesQueryCallbacks> callbacks;
CallbacksHolder(CallbacksHolder&&) = default;
~CallbacksHolder() {
if (callbacks) {
callbacks->OnError();
}
}
};
void VideoPerfInfoCallback(
CallbacksHolder callbacks_holder,
blink::ScopedWebCallbacks<blink::WebMediaCapabilitiesQueryCallbacks>
scoped_callbacks,
std::unique_ptr<blink::WebMediaCapabilitiesInfo> info,
bool is_smooth,
bool is_power_efficient) {
DCHECK(info->supported);
info->smooth = is_smooth;
info->power_efficient = is_power_efficient;
std::move(callbacks_holder.callbacks)->OnSuccess(std::move(info));
scoped_callbacks.PassCallbacks()->OnSuccess(std::move(info));
}
void OnGetPerfInfoError(
std::unique_ptr<blink::WebMediaCapabilitiesQueryCallbacks> callbacks) {
callbacks->OnError();
}
} // namespace
......@@ -226,8 +218,11 @@ void WebMediaCapabilitiesClientImpl::DecodingInfo(
decode_history_ptr_->GetPerfInfo(
std::move(features),
base::BindOnce(&VideoPerfInfoCallback,
CallbacksHolder{std::move(callbacks)}, std::move(info)));
base::BindOnce(
&VideoPerfInfoCallback,
blink::MakeScopedWebCallbacks(std::move(callbacks),
base::BindOnce(&OnGetPerfInfoError)),
std::move(info)));
}
void WebMediaCapabilitiesClientImpl::BindVideoDecodePerfHistoryForTests(
......
......@@ -195,6 +195,7 @@ source_set("blink_headers") {
"platform/scheduler/web_main_thread_scheduler.h",
"platform/scheduler/web_render_widget_scheduling_state.h",
"platform/scheduler/web_thread_scheduler.h",
"platform/scoped_web_callbacks.h",
"platform/shape_properties.h",
"platform/task_type.h",
"platform/url_conversion.h",
......
// Copyright 2015 The Chromium Authors. All rights reserved.
// Copyright 2018 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.
#ifndef CONTENT_CHILD_SCOPED_WEB_CALLBACKS_H_
#define CONTENT_CHILD_SCOPED_WEB_CALLBACKS_H_
#ifndef THIRD_PARTY_BLINK_PUBLIC_PLATFORM_SCOPED_WEB_CALLBACKS_H_
#define THIRD_PARTY_BLINK_PUBLIC_PLATFORM_SCOPED_WEB_CALLBACKS_H_
#include <memory>
#include <utility>
#include "base/callback.h"
#include "base/macros.h"
#include "base/memory/ptr_util.h"
#include "third_party/blink/public/platform/web_callbacks.h"
namespace blink {
// A ScopedWebCallbacks is a move-only scoper which helps manage the lifetime of
// a blink::WebCallbacks object. This is particularly useful when you're
// simultaneously dealing with the following two conditions:
......@@ -48,14 +48,14 @@
// }
//
// // Blink client implementation
// void FooClientImpl::doMagic(FooCallbacks* callbacks) {
// void FooClientImpl::doMagic(std::unique_ptr<FooCallbacks> callbacks) {
// auto scoped_callbacks = make_scoped_web_callbacks(
// callbacks, base::Bind(&OnCallbacksDropped));
// std::move(callbacks), base::BindOnce(&OnCallbacksDropped));
//
// // Call to some lower-level service which may never run the callback we
// // give it.
// foo_service_->DoMagic(base::Bind(&RespondWithSuccess,
// base::Passed(&scoped_callbacks)));
// foo_service_->DoMagic(base::BindOnce(&RespondWithSuccess,
// std::move(scoped_callbacks)));
// }
//
// If the bound RespondWithSuccess callback actually runs, PassCallbacks() will
......@@ -69,44 +69,43 @@ template <typename CallbacksType>
class ScopedWebCallbacks {
public:
using DestructionCallback =
base::Callback<void(std::unique_ptr<CallbacksType> callbacks)>;
base::OnceCallback<void(std::unique_ptr<CallbacksType> callbacks)>;
ScopedWebCallbacks(std::unique_ptr<CallbacksType> callbacks,
const DestructionCallback& destruction_callback)
DestructionCallback destruction_callback)
: callbacks_(std::move(callbacks)),
destruction_callback_(destruction_callback) {}
destruction_callback_(std::move(destruction_callback)) {}
~ScopedWebCallbacks() {
if (callbacks_)
destruction_callback_.Run(std::move(callbacks_));
if (destruction_callback_)
std::move(destruction_callback_).Run(std::move(callbacks_));
}
ScopedWebCallbacks(ScopedWebCallbacks&& other) { *this = std::move(other); }
ScopedWebCallbacks(ScopedWebCallbacks&& other) = default;
ScopedWebCallbacks(const ScopedWebCallbacks& other) = delete;
ScopedWebCallbacks& operator=(ScopedWebCallbacks&& other) {
callbacks_ = std::move(other.callbacks_);
destruction_callback_ = other.destruction_callback_;
return *this;
}
ScopedWebCallbacks& operator=(ScopedWebCallbacks&& other) = default;
ScopedWebCallbacks& operator=(const ScopedWebCallbacks& other) = delete;
std::unique_ptr<CallbacksType> PassCallbacks() {
destruction_callback_ = DestructionCallback();
return std::move(callbacks_);
}
private:
std::unique_ptr<CallbacksType> callbacks_;
DestructionCallback destruction_callback_;
DISALLOW_COPY_AND_ASSIGN(ScopedWebCallbacks);
};
template <typename CallbacksType>
ScopedWebCallbacks<CallbacksType> make_scoped_web_callbacks(
CallbacksType* callbacks,
const typename ScopedWebCallbacks<CallbacksType>::DestructionCallback&
ScopedWebCallbacks<CallbacksType> MakeScopedWebCallbacks(
std::unique_ptr<CallbacksType> callbacks,
typename ScopedWebCallbacks<CallbacksType>::DestructionCallback
destruction_callback) {
return ScopedWebCallbacks<CallbacksType>(base::WrapUnique(callbacks),
destruction_callback);
return ScopedWebCallbacks<CallbacksType>(std::move(callbacks),
std::move(destruction_callback));
}
#endif // CONTENT_CHILD_SCOPED_WEB_CALLBACKS_H_
} // namespace blink
#endif // THIRD_PARTY_BLINK_PUBLIC_PLATFORM_SCOPED_WEB_CALLBACKS_H_
......@@ -5,6 +5,8 @@
#ifndef THIRD_PARTY_BLINK_PUBLIC_PLATFORM_WEB_IMAGE_CAPTURE_FRAME_GRABBER_H_
#define THIRD_PARTY_BLINK_PUBLIC_PLATFORM_WEB_IMAGE_CAPTURE_FRAME_GRABBER_H_
#include <memory>
#include "third_party/blink/public/platform/web_callbacks.h"
#include "third_party/blink/public/platform/web_common.h"
#include "third_party/skia/include/core/SkRefCnt.h"
......@@ -22,8 +24,9 @@ class WebImageCaptureFrameGrabber {
public:
virtual ~WebImageCaptureFrameGrabber() = default;
virtual void GrabFrame(WebMediaStreamTrack*,
WebImageCaptureGrabFrameCallbacks*) = 0;
virtual void GrabFrame(
WebMediaStreamTrack*,
std::unique_ptr<WebImageCaptureGrabFrameCallbacks> callbacks) = 0;
};
} // namespace blink
......
......@@ -313,8 +313,9 @@ ScriptPromise ImageCapture::grabFrame(ScriptState* script_state) {
// The platform does not know about MediaStreamTrack, so we wrap it up.
WebMediaStreamTrack track(stream_track_->Component());
frame_grabber_->GrabFrame(
&track, new CallbackPromiseAdapter<ImageBitmap, void>(resolver));
auto resolver_callback_adapter =
std::make_unique<CallbackPromiseAdapter<ImageBitmap, void>>(resolver);
frame_grabber_->GrabFrame(&track, std::move(resolver_callback_adapter));
return promise;
}
......
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