Use weak pointer instead of manual bookkeeping

This commit is contained in:
Cheng Zhao 2016-02-16 10:44:10 +08:00
parent ed1966ac76
commit 66bb6a8534
3 changed files with 16 additions and 62 deletions

View file

@ -1072,11 +1072,9 @@ void WebContents::BeginFrameSubscription(
const FrameSubscriber::FrameCaptureCallback& callback) { const FrameSubscriber::FrameCaptureCallback& callback) {
const auto view = web_contents()->GetRenderWidgetHostView(); const auto view = web_contents()->GetRenderWidgetHostView();
if (view) { if (view) {
FrameSubscriber* frame_subscriber = new FrameSubscriber( scoped_ptr<FrameSubscriber> frame_subscriber(new FrameSubscriber(
isolate(), view->GetVisibleViewportSize(), callback); isolate(), view->GetVisibleViewportSize(), callback));
scoped_ptr<FrameSubscriber::Subscriber> del_frame_subscriber( view->BeginFrameSubscription(frame_subscriber.Pass());
frame_subscriber->GetSubscriber());
view->BeginFrameSubscription(del_frame_subscriber.Pass());
} }
} }

View file

@ -13,58 +13,28 @@ namespace atom {
namespace api { namespace api {
using Subscriber = FrameSubscriber::Subscriber;
FrameSubscriber::FrameSubscriber(v8::Isolate* isolate, FrameSubscriber::FrameSubscriber(v8::Isolate* isolate,
const gfx::Size& size, const gfx::Size& size,
const FrameCaptureCallback& callback) const FrameCaptureCallback& callback)
: isolate_(isolate), size_(size), callback_(callback), pending_frames(0) { : isolate_(isolate), size_(size), callback_(callback), weak_factory_(this) {
subscriber_ = new Subscriber(this);
} }
Subscriber::Subscriber( bool FrameSubscriber::ShouldCaptureFrame(
FrameSubscriber* frame_subscriber) : frame_subscriber_(frame_subscriber) {
}
Subscriber::~Subscriber() {
frame_subscriber_->subscriber_ = NULL;
frame_subscriber_->RequestDestruct();
}
bool Subscriber::ShouldCaptureFrame(
const gfx::Rect& damage_rect, const gfx::Rect& damage_rect,
base::TimeTicks present_time, base::TimeTicks present_time,
scoped_refptr<media::VideoFrame>* storage, scoped_refptr<media::VideoFrame>* storage,
DeliverFrameCallback* callback) { DeliverFrameCallback* callback) {
*storage = media::VideoFrame::CreateFrame( *storage = media::VideoFrame::CreateFrame(
media::PIXEL_FORMAT_YV12, media::PIXEL_FORMAT_YV12,
frame_subscriber_->size_, gfx::Rect(frame_subscriber_->size_), size_, gfx::Rect(size_), size_, base::TimeDelta());
frame_subscriber_->size_, base::TimeDelta());
*callback = base::Bind(&FrameSubscriber::OnFrameDelivered, *callback = base::Bind(&FrameSubscriber::OnFrameDelivered,
base::Unretained(frame_subscriber_), *storage); weak_factory_.GetWeakPtr(), *storage);
frame_subscriber_->pending_frames++;
return true; return true;
} }
Subscriber* FrameSubscriber::GetSubscriber() {
return subscriber_;
}
bool FrameSubscriber::RequestDestruct() {
bool deletable = (subscriber_ == NULL && pending_frames == 0);
// Destruct FrameSubscriber if we're not waiting for frames and the
// subscription has ended
if (deletable)
delete this;
return deletable;
}
void FrameSubscriber::OnFrameDelivered( void FrameSubscriber::OnFrameDelivered(
scoped_refptr<media::VideoFrame> frame, base::TimeTicks, bool result) { scoped_refptr<media::VideoFrame> frame, base::TimeTicks, bool result) {
pending_frames--; if (!result)
if (RequestDestruct() || subscriber_ == NULL || !result)
return; return;
v8::Locker locker(isolate_); v8::Locker locker(isolate_);

View file

@ -6,6 +6,7 @@
#define ATOM_BROWSER_API_FRAME_SUBSCRIBER_H_ #define ATOM_BROWSER_API_FRAME_SUBSCRIBER_H_
#include "base/callback.h" #include "base/callback.h"
#include "base/memory/weak_ptr.h"
#include "content/public/browser/render_widget_host_view_frame_subscriber.h" #include "content/public/browser/render_widget_host_view_frame_subscriber.h"
#include "ui/gfx/geometry/size.h" #include "ui/gfx/geometry/size.h"
#include "v8/include/v8.h" #include "v8/include/v8.h"
@ -14,43 +15,28 @@ namespace atom {
namespace api { namespace api {
class FrameSubscriber { class FrameSubscriber : public content::RenderWidgetHostViewFrameSubscriber {
public: public:
using FrameCaptureCallback = base::Callback<void(v8::Local<v8::Value>)>; using FrameCaptureCallback = base::Callback<void(v8::Local<v8::Value>)>;
// Inner class that is the actual subscriber sent to chromium
class Subscriber :
public content::RenderWidgetHostViewFrameSubscriber {
public:
explicit Subscriber(FrameSubscriber* frame_subscriber);
bool ShouldCaptureFrame(const gfx::Rect& damage_rect,
base::TimeTicks present_time,
scoped_refptr<media::VideoFrame>* storage,
DeliverFrameCallback* callback) override;
~Subscriber();
private:
FrameSubscriber* frame_subscriber_;
};
FrameSubscriber(v8::Isolate* isolate, FrameSubscriber(v8::Isolate* isolate,
const gfx::Size& size, const gfx::Size& size,
const FrameCaptureCallback& callback); const FrameCaptureCallback& callback);
Subscriber* GetSubscriber(); bool ShouldCaptureFrame(const gfx::Rect& damage_rect,
base::TimeTicks present_time,
scoped_refptr<media::VideoFrame>* storage,
DeliverFrameCallback* callback) override;
private: private:
void OnFrameDelivered( void OnFrameDelivered(
scoped_refptr<media::VideoFrame> frame, base::TimeTicks, bool); scoped_refptr<media::VideoFrame> frame, base::TimeTicks, bool);
bool RequestDestruct();
v8::Isolate* isolate_; v8::Isolate* isolate_;
gfx::Size size_; gfx::Size size_;
FrameCaptureCallback callback_; FrameCaptureCallback callback_;
Subscriber* subscriber_;
int pending_frames; base::WeakPtrFactory<FrameSubscriber> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(FrameSubscriber); DISALLOW_COPY_AND_ASSIGN(FrameSubscriber);
}; };