Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / media / web_contents_video_capture_device.cc
index 95333fc..6e33465 100644 (file)
 #include "base/callback_helpers.h"
 #include "base/logging.h"
 #include "base/memory/scoped_ptr.h"
+#include "base/memory/weak_ptr.h"
 #include "base/message_loop/message_loop_proxy.h"
 #include "base/metrics/histogram.h"
 #include "base/sequenced_task_runner.h"
 #include "base/threading/thread.h"
 #include "base/threading/thread_checker.h"
 #include "base/time/time.h"
-#include "content/browser/renderer_host/media/video_capture_device_impl.h"
+#include "content/browser/renderer_host/media/content_video_capture_device_core.h"
 #include "content/browser/renderer_host/media/video_capture_oracle.h"
 #include "content/browser/renderer_host/media/web_contents_capture_util.h"
 #include "content/browser/renderer_host/render_widget_host_impl.h"
@@ -210,8 +211,7 @@ void RenderVideoFrame(const SkBitmap& input,
 // this seems disadvantageous.
 class WebContentsCaptureMachine
     : public VideoCaptureMachine,
-      public WebContentsObserver,
-      public base::SupportsWeakPtr<WebContentsCaptureMachine> {
+      public WebContentsObserver {
  public:
   WebContentsCaptureMachine(int render_process_id, int render_view_id);
   virtual ~WebContentsCaptureMachine();
@@ -309,6 +309,9 @@ class WebContentsCaptureMachine
   // oracle, and initiating captures accordingly.
   scoped_ptr<ContentCaptureSubscription> subscription_;
 
+  // Weak pointer factory used to invalidate callbacks.
+  base::WeakPtrFactory<WebContentsCaptureMachine> weak_ptr_factory_;
+
   DISALLOW_COPY_AND_ASSIGN(WebContentsCaptureMachine);
 };
 
@@ -559,7 +562,8 @@ WebContentsCaptureMachine::WebContentsCaptureMachine(int render_process_id,
                                                      int render_view_id)
     : initial_render_process_id_(render_process_id),
       initial_render_view_id_(render_view_id),
-      fullscreen_widget_id_(MSG_ROUTING_NONE) {}
+      fullscreen_widget_id_(MSG_ROUTING_NONE),
+      weak_ptr_factory_(this) {}
 
 WebContentsCaptureMachine::~WebContentsCaptureMachine() {
   BrowserThread::PostBlockingPoolTask(
@@ -601,6 +605,10 @@ void WebContentsCaptureMachine::Stop(const base::Closure& callback) {
     Observe(NULL);
   }
 
+  // Any callback that intend to use render_thread_ will not work after it is
+  // passed.
+  weak_ptr_factory_.InvalidateWeakPtrs();
+
   // The render thread cannot be stopped on the UI thread, so post a message
   // to the thread pool used for blocking operations.
   BrowserThread::PostBlockingPoolTask(
@@ -647,20 +655,23 @@ void WebContentsCaptureMachine::Capture(
     rwh->GetSnapshotFromRenderer(
         gfx::Rect(),
         base::Bind(&WebContentsCaptureMachine::DidCopyFromBackingStore,
-                   this->AsWeakPtr(), start_time, target, deliver_frame_cb));
+                   weak_ptr_factory_.GetWeakPtr(),
+                   start_time, target, deliver_frame_cb));
   } else if (view->CanCopyToVideoFrame()) {
     view->CopyFromCompositingSurfaceToVideoFrame(
         gfx::Rect(view_size),
         target,
         base::Bind(&WebContentsCaptureMachine::
                         DidCopyFromCompositingSurfaceToVideoFrame,
-                   this->AsWeakPtr(), start_time, deliver_frame_cb));
+                   weak_ptr_factory_.GetWeakPtr(),
+                   start_time, deliver_frame_cb));
   } else {
     rwh->CopyFromBackingStore(
         gfx::Rect(),
         fitted_size,  // Size here is a request not always honored.
         base::Bind(&WebContentsCaptureMachine::DidCopyFromBackingStore,
-                   this->AsWeakPtr(), start_time, target, deliver_frame_cb));
+                   weak_ptr_factory_.GetWeakPtr(),
+                   start_time, target, deliver_frame_cb));
   }
 }
 
@@ -684,7 +695,7 @@ bool WebContentsCaptureMachine::StartObservingWebContents() {
 
   WebContentsImpl* contents = static_cast<WebContentsImpl*>(web_contents());
   if (contents) {
-    contents->IncrementCapturerCount();
+    contents->IncrementCapturerCount(oracle_proxy_->GetCaptureSize());
     fullscreen_widget_id_ = contents->GetFullscreenWidgetRoutingID();
     RenewFrameSubscription();
     return true;
@@ -773,14 +784,15 @@ void WebContentsCaptureMachine::RenewFrameSubscription() {
     return;
 
   subscription_.reset(new ContentCaptureSubscription(*rwh, oracle_proxy_,
-      base::Bind(&WebContentsCaptureMachine::Capture, this->AsWeakPtr())));
+      base::Bind(&WebContentsCaptureMachine::Capture,
+                 weak_ptr_factory_.GetWeakPtr())));
 }
 
 }  // namespace
 
 WebContentsVideoCaptureDevice::WebContentsVideoCaptureDevice(
     int render_process_id, int render_view_id)
-    : impl_(new VideoCaptureDeviceImpl(scoped_ptr<VideoCaptureMachine>(
+    : core_(new ContentVideoCaptureDeviceCore(scoped_ptr<VideoCaptureMachine>(
         new WebContentsCaptureMachine(render_process_id, render_view_id)))) {}
 
 WebContentsVideoCaptureDevice::~WebContentsVideoCaptureDevice() {
@@ -805,11 +817,11 @@ void WebContentsVideoCaptureDevice::AllocateAndStart(
     const media::VideoCaptureParams& params,
     scoped_ptr<Client> client) {
   DVLOG(1) << "Allocating " << params.requested_format.frame_size.ToString();
-  impl_->AllocateAndStart(params, client.Pass());
+  core_->AllocateAndStart(params, client.Pass());
 }
 
 void WebContentsVideoCaptureDevice::StopAndDeAllocate() {
-  impl_->StopAndDeAllocate();
+  core_->StopAndDeAllocate();
 }
 
 }  // namespace content