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") { ...@@ -67,7 +67,6 @@ target(link_target_type, "child") {
"runtime_features.h", "runtime_features.h",
"scoped_child_process_reference.cc", "scoped_child_process_reference.cc",
"scoped_child_process_reference.h", "scoped_child_process_reference.h",
"scoped_web_callbacks.h",
"service_factory.cc", "service_factory.cc",
"service_factory.h", "service_factory.h",
"thread_safe_sender.cc", "thread_safe_sender.cc",
......
...@@ -115,7 +115,7 @@ ImageCaptureFrameGrabber::~ImageCaptureFrameGrabber() { ...@@ -115,7 +115,7 @@ ImageCaptureFrameGrabber::~ImageCaptureFrameGrabber() {
void ImageCaptureFrameGrabber::GrabFrame( void ImageCaptureFrameGrabber::GrabFrame(
blink::WebMediaStreamTrack* track, blink::WebMediaStreamTrack* track,
WebImageCaptureGrabFrameCallbacks* callbacks) { std::unique_ptr<blink::WebImageCaptureGrabFrameCallbacks> callbacks) {
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(!!callbacks); DCHECK(!!callbacks);
...@@ -128,8 +128,8 @@ void ImageCaptureFrameGrabber::GrabFrame( ...@@ -128,8 +128,8 @@ void ImageCaptureFrameGrabber::GrabFrame(
return; return;
} }
ScopedWebCallbacks<WebImageCaptureGrabFrameCallbacks> scoped_callbacks = auto scoped_callbacks = blink::MakeScopedWebCallbacks(
make_scoped_web_callbacks(callbacks, base::Bind(&OnError)); std::move(callbacks), base::BindOnce(&OnError));
// A SingleShotFrameHandler is bound and given to the Track to guarantee that // A SingleShotFrameHandler is bound and given to the Track to guarantee that
// only one VideoFrame is converted and delivered to OnSkImage(), otherwise // only one VideoFrame is converted and delivered to OnSkImage(), otherwise
...@@ -149,7 +149,8 @@ void ImageCaptureFrameGrabber::GrabFrame( ...@@ -149,7 +149,8 @@ void ImageCaptureFrameGrabber::GrabFrame(
} }
void ImageCaptureFrameGrabber::OnSkImage( void ImageCaptureFrameGrabber::OnSkImage(
ScopedWebCallbacks<blink::WebImageCaptureGrabFrameCallbacks> callbacks, blink::ScopedWebCallbacks<blink::WebImageCaptureGrabFrameCallbacks>
callbacks,
sk_sp<SkImage> image) { sk_sp<SkImage> image) {
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK(thread_checker_.CalledOnValidThread());
......
...@@ -5,13 +5,15 @@ ...@@ -5,13 +5,15 @@
#ifndef CONTENT_RENDERER_IMAGE_CAPTURE_IMAGE_CAPTURE_FRAME_GRABBER_H_ #ifndef CONTENT_RENDERER_IMAGE_CAPTURE_IMAGE_CAPTURE_FRAME_GRABBER_H_
#define 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/compiler_specific.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/threading/thread_checker.h" #include "base/threading/thread_checker.h"
#include "content/child/scoped_web_callbacks.h"
#include "content/common/content_export.h" #include "content/common/content_export.h"
#include "content/public/renderer/media_stream_video_sink.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" #include "third_party/blink/public/platform/web_image_capture_frame_grabber.h"
namespace blink { namespace blink {
...@@ -36,14 +38,15 @@ class CONTENT_EXPORT ImageCaptureFrameGrabber final ...@@ -36,14 +38,15 @@ class CONTENT_EXPORT ImageCaptureFrameGrabber final
// blink::WebImageCaptureFrameGrabber implementation. // blink::WebImageCaptureFrameGrabber implementation.
void GrabFrame(blink::WebMediaStreamTrack* track, void GrabFrame(blink::WebMediaStreamTrack* track,
blink::WebImageCaptureGrabFrameCallbacks* callbacks) override; std::unique_ptr<blink::WebImageCaptureGrabFrameCallbacks>
callbacks) override;
private: private:
// Internal class to receive, convert and forward one frame. // Internal class to receive, convert and forward one frame.
class SingleShotFrameHandler; class SingleShotFrameHandler;
void OnSkImage( void OnSkImage(blink::ScopedWebCallbacks<
ScopedWebCallbacks<blink::WebImageCaptureGrabFrameCallbacks> callbacks, blink::WebImageCaptureGrabFrameCallbacks> callbacks,
sk_sp<SkImage> image); sk_sp<SkImage> image);
// Flag to indicate that there is a frame grabbing in progress. // Flag to indicate that there is a frame grabbing in progress.
......
...@@ -13,7 +13,6 @@ ...@@ -13,7 +13,6 @@
#include "base/strings/string_tokenizer.h" #include "base/strings/string_tokenizer.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/sys_info.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_audio_track.h"
#include "content/renderer/media/stream/media_stream_track.h" #include "content/renderer/media/stream/media_stream_track.h"
#include "content/renderer/media/webrtc/webrtc_uma_histograms.h" #include "content/renderer/media/webrtc/webrtc_uma_histograms.h"
...@@ -27,6 +26,7 @@ ...@@ -27,6 +26,7 @@
#include "media/base/video_frame.h" #include "media/base/video_frame.h"
#include "media/muxers/webm_muxer.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/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_recorder_handler_client.h"
#include "third_party/blink/public/platform/web_media_stream_source.h" #include "third_party/blink/public/platform/web_media_stream_source.h"
#include "third_party/blink/public/platform/web_string.h" #include "third_party/blink/public/platform/web_string.h"
...@@ -350,9 +350,8 @@ void MediaRecorderHandler::EncodingInfo( ...@@ -350,9 +350,8 @@ void MediaRecorderHandler::EncodingInfo(
DCHECK(configuration.video_configuration || DCHECK(configuration.video_configuration ||
configuration.audio_configuration); configuration.audio_configuration);
ScopedWebCallbacks<WebMediaCapabilitiesQueryCallbacks> scoped_callbacks = auto scoped_callbacks = blink::MakeScopedWebCallbacks(
make_scoped_web_callbacks(callbacks.release(), std::move(callbacks), base::BindOnce(&OnEncodingInfoError));
base::Bind(&OnEncodingInfoError));
std::unique_ptr<blink::WebMediaCapabilitiesInfo> info( std::unique_ptr<blink::WebMediaCapabilitiesInfo> info(
new blink::WebMediaCapabilitiesInfo()); new blink::WebMediaCapabilitiesInfo());
......
...@@ -22,6 +22,7 @@ ...@@ -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_media_configuration.h"
#include "third_party/blink/public/platform/modules/media_capabilities/web_video_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/platform.h"
#include "third_party/blink/public/platform/scoped_web_callbacks.h"
namespace media { namespace media {
...@@ -141,30 +142,21 @@ WebMediaCapabilitiesClientImpl::WebMediaCapabilitiesClientImpl() = default; ...@@ -141,30 +142,21 @@ WebMediaCapabilitiesClientImpl::WebMediaCapabilitiesClientImpl() = default;
WebMediaCapabilitiesClientImpl::~WebMediaCapabilitiesClientImpl() = default; WebMediaCapabilitiesClientImpl::~WebMediaCapabilitiesClientImpl() = default;
namespace { 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( void VideoPerfInfoCallback(
CallbacksHolder callbacks_holder, blink::ScopedWebCallbacks<blink::WebMediaCapabilitiesQueryCallbacks>
scoped_callbacks,
std::unique_ptr<blink::WebMediaCapabilitiesInfo> info, std::unique_ptr<blink::WebMediaCapabilitiesInfo> info,
bool is_smooth, bool is_smooth,
bool is_power_efficient) { bool is_power_efficient) {
DCHECK(info->supported); DCHECK(info->supported);
info->smooth = is_smooth; info->smooth = is_smooth;
info->power_efficient = is_power_efficient; 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 } // namespace
...@@ -226,8 +218,11 @@ void WebMediaCapabilitiesClientImpl::DecodingInfo( ...@@ -226,8 +218,11 @@ void WebMediaCapabilitiesClientImpl::DecodingInfo(
decode_history_ptr_->GetPerfInfo( decode_history_ptr_->GetPerfInfo(
std::move(features), std::move(features),
base::BindOnce(&VideoPerfInfoCallback, base::BindOnce(
CallbacksHolder{std::move(callbacks)}, std::move(info))); &VideoPerfInfoCallback,
blink::MakeScopedWebCallbacks(std::move(callbacks),
base::BindOnce(&OnGetPerfInfoError)),
std::move(info)));
} }
void WebMediaCapabilitiesClientImpl::BindVideoDecodePerfHistoryForTests( void WebMediaCapabilitiesClientImpl::BindVideoDecodePerfHistoryForTests(
......
...@@ -195,6 +195,7 @@ source_set("blink_headers") { ...@@ -195,6 +195,7 @@ source_set("blink_headers") {
"platform/scheduler/web_main_thread_scheduler.h", "platform/scheduler/web_main_thread_scheduler.h",
"platform/scheduler/web_render_widget_scheduling_state.h", "platform/scheduler/web_render_widget_scheduling_state.h",
"platform/scheduler/web_thread_scheduler.h", "platform/scheduler/web_thread_scheduler.h",
"platform/scoped_web_callbacks.h",
"platform/shape_properties.h", "platform/shape_properties.h",
"platform/task_type.h", "platform/task_type.h",
"platform/url_conversion.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 // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#ifndef CONTENT_CHILD_SCOPED_WEB_CALLBACKS_H_ #ifndef THIRD_PARTY_BLINK_PUBLIC_PLATFORM_SCOPED_WEB_CALLBACKS_H_
#define CONTENT_CHILD_SCOPED_WEB_CALLBACKS_H_ #define THIRD_PARTY_BLINK_PUBLIC_PLATFORM_SCOPED_WEB_CALLBACKS_H_
#include <memory> #include <memory>
#include <utility> #include <utility>
#include "base/callback.h" #include "base/callback.h"
#include "base/macros.h"
#include "base/memory/ptr_util.h"
#include "third_party/blink/public/platform/web_callbacks.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 ScopedWebCallbacks is a move-only scoper which helps manage the lifetime of
// a blink::WebCallbacks object. This is particularly useful when you're // a blink::WebCallbacks object. This is particularly useful when you're
// simultaneously dealing with the following two conditions: // simultaneously dealing with the following two conditions:
...@@ -48,14 +48,14 @@ ...@@ -48,14 +48,14 @@
// } // }
// //
// // Blink client implementation // // Blink client implementation
// void FooClientImpl::doMagic(FooCallbacks* callbacks) { // void FooClientImpl::doMagic(std::unique_ptr<FooCallbacks> callbacks) {
// auto scoped_callbacks = make_scoped_web_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 // // Call to some lower-level service which may never run the callback we
// // give it. // // give it.
// foo_service_->DoMagic(base::Bind(&RespondWithSuccess, // foo_service_->DoMagic(base::BindOnce(&RespondWithSuccess,
// base::Passed(&scoped_callbacks))); // std::move(scoped_callbacks)));
// } // }
// //
// If the bound RespondWithSuccess callback actually runs, PassCallbacks() will // If the bound RespondWithSuccess callback actually runs, PassCallbacks() will
...@@ -69,44 +69,43 @@ template <typename CallbacksType> ...@@ -69,44 +69,43 @@ template <typename CallbacksType>
class ScopedWebCallbacks { class ScopedWebCallbacks {
public: public:
using DestructionCallback = using DestructionCallback =
base::Callback<void(std::unique_ptr<CallbacksType> callbacks)>; base::OnceCallback<void(std::unique_ptr<CallbacksType> callbacks)>;
ScopedWebCallbacks(std::unique_ptr<CallbacksType> callbacks, ScopedWebCallbacks(std::unique_ptr<CallbacksType> callbacks,
const DestructionCallback& destruction_callback) DestructionCallback destruction_callback)
: callbacks_(std::move(callbacks)), : callbacks_(std::move(callbacks)),
destruction_callback_(destruction_callback) {} destruction_callback_(std::move(destruction_callback)) {}
~ScopedWebCallbacks() { ~ScopedWebCallbacks() {
if (callbacks_) if (destruction_callback_)
destruction_callback_.Run(std::move(callbacks_)); 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) { ScopedWebCallbacks& operator=(ScopedWebCallbacks&& other) = default;
callbacks_ = std::move(other.callbacks_); ScopedWebCallbacks& operator=(const ScopedWebCallbacks& other) = delete;
destruction_callback_ = other.destruction_callback_;
return *this;
}
std::unique_ptr<CallbacksType> PassCallbacks() { std::unique_ptr<CallbacksType> PassCallbacks() {
destruction_callback_ = DestructionCallback();
return std::move(callbacks_); return std::move(callbacks_);
} }
private: private:
std::unique_ptr<CallbacksType> callbacks_; std::unique_ptr<CallbacksType> callbacks_;
DestructionCallback destruction_callback_; DestructionCallback destruction_callback_;
DISALLOW_COPY_AND_ASSIGN(ScopedWebCallbacks);
}; };
template <typename CallbacksType> template <typename CallbacksType>
ScopedWebCallbacks<CallbacksType> make_scoped_web_callbacks( ScopedWebCallbacks<CallbacksType> MakeScopedWebCallbacks(
CallbacksType* callbacks, std::unique_ptr<CallbacksType> callbacks,
const typename ScopedWebCallbacks<CallbacksType>::DestructionCallback& typename ScopedWebCallbacks<CallbacksType>::DestructionCallback
destruction_callback) { destruction_callback) {
return ScopedWebCallbacks<CallbacksType>(base::WrapUnique(callbacks), return ScopedWebCallbacks<CallbacksType>(std::move(callbacks),
destruction_callback); 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 @@ ...@@ -5,6 +5,8 @@
#ifndef THIRD_PARTY_BLINK_PUBLIC_PLATFORM_WEB_IMAGE_CAPTURE_FRAME_GRABBER_H_ #ifndef THIRD_PARTY_BLINK_PUBLIC_PLATFORM_WEB_IMAGE_CAPTURE_FRAME_GRABBER_H_
#define 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_callbacks.h"
#include "third_party/blink/public/platform/web_common.h" #include "third_party/blink/public/platform/web_common.h"
#include "third_party/skia/include/core/SkRefCnt.h" #include "third_party/skia/include/core/SkRefCnt.h"
...@@ -22,8 +24,9 @@ class WebImageCaptureFrameGrabber { ...@@ -22,8 +24,9 @@ class WebImageCaptureFrameGrabber {
public: public:
virtual ~WebImageCaptureFrameGrabber() = default; virtual ~WebImageCaptureFrameGrabber() = default;
virtual void GrabFrame(WebMediaStreamTrack*, virtual void GrabFrame(
WebImageCaptureGrabFrameCallbacks*) = 0; WebMediaStreamTrack*,
std::unique_ptr<WebImageCaptureGrabFrameCallbacks> callbacks) = 0;
}; };
} // namespace blink } // namespace blink
......
...@@ -313,8 +313,9 @@ ScriptPromise ImageCapture::grabFrame(ScriptState* script_state) { ...@@ -313,8 +313,9 @@ ScriptPromise ImageCapture::grabFrame(ScriptState* script_state) {
// The platform does not know about MediaStreamTrack, so we wrap it up. // The platform does not know about MediaStreamTrack, so we wrap it up.
WebMediaStreamTrack track(stream_track_->Component()); WebMediaStreamTrack track(stream_track_->Component());
frame_grabber_->GrabFrame( auto resolver_callback_adapter =
&track, new CallbackPromiseAdapter<ImageBitmap, void>(resolver)); std::make_unique<CallbackPromiseAdapter<ImageBitmap, void>>(resolver);
frame_grabber_->GrabFrame(&track, std::move(resolver_callback_adapter));
return promise; 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