Home | History | Annotate | Download | only in media
      1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 //
      5 // Given a starting render_process_id and render_view_id, the WebContentsTracker
      6 // tracks RenderViewHost instance swapping during the lifetime of a WebContents
      7 // instance.  This is used when mirroring tab video and audio so that user
      8 // navigations, crashes, etc., during a tab's lifetime allow the capturing code
      9 // to remain active on the current/latest RenderView.
     10 //
     11 // Threading issues: Start(), Stop() and the ChangeCallback are invoked on the
     12 // same thread.  This can be any thread, and the decision is locked-in by
     13 // WebContentsTracker when Start() is called.
     14 
     15 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_WEB_CONTENTS_TRACKER_H_
     16 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_WEB_CONTENTS_TRACKER_H_
     17 
     18 #include "base/callback.h"
     19 #include "base/memory/ref_counted.h"
     20 #include "content/common/content_export.h"
     21 #include "content/public/browser/web_contents_observer.h"
     22 
     23 namespace base {
     24 class MessageLoopProxy;
     25 }
     26 
     27 namespace content {
     28 
     29 class CONTENT_EXPORT WebContentsTracker
     30     : public base::RefCountedThreadSafe<WebContentsTracker>,
     31       public WebContentsObserver {
     32  public:
     33   WebContentsTracker();
     34 
     35   // Callback for whenever the target is swapped.  The callback is also invoked
     36   // with both arguments set to MSG_ROUTING_NONE to indicate tracking will not
     37   // continue (i.e., the WebContents instance was not found or has been
     38   // destroyed).
     39   typedef base::Callback<void(int render_process_id, int render_view_id)>
     40       ChangeCallback;
     41 
     42   // Start tracking.  The last-known |render_process_id| and |render_view_id|
     43   // are provided, and the given callback is invoked asynchronously one or more
     44   // times.  The callback will be invoked on the same thread calling Start().
     45   virtual void Start(int render_process_id, int render_view_id,
     46                      const ChangeCallback& callback);
     47 
     48   // Stop tracking.  Once this method returns, the callback is guaranteed not to
     49   // be invoked again.
     50   virtual void Stop();
     51 
     52  protected:
     53   friend class base::RefCountedThreadSafe<WebContentsTracker>;
     54   virtual ~WebContentsTracker();
     55 
     56  private:
     57   // Reads the render_process_id/render_view_id from the current WebContents
     58   // instance and then invokes the callback.
     59   void OnWebContentsChangeEvent();
     60 
     61   // Called on the thread that Start()/Stop() are called on, check whether the
     62   // callback is still valid and, if so, invoke it.
     63   void MaybeDoCallback(int render_process_id, int render_view_id);
     64 
     65   // Look-up the current WebContents instance associated with the given
     66   // |render_process_id| and |render_view_id| and begin observing it.
     67   void LookUpAndObserveWebContents(int render_process_id,
     68                                    int render_view_id);
     69 
     70   // WebContentsObserver overrides to react to events of interest.
     71   virtual void RenderViewReady() OVERRIDE;
     72   virtual void AboutToNavigateRenderView(RenderViewHost* render_view_host)
     73       OVERRIDE;
     74   virtual void DidNavigateMainFrame(const LoadCommittedDetails& details,
     75                                     const FrameNavigateParams& params) OVERRIDE;
     76   virtual void WebContentsDestroyed(WebContents* web_contents) OVERRIDE;
     77 
     78   scoped_refptr<base::MessageLoopProxy> message_loop_;
     79   ChangeCallback callback_;
     80 
     81   DISALLOW_COPY_AND_ASSIGN(WebContentsTracker);
     82 };
     83 
     84 }  // namespace content
     85 
     86 #endif  // CONTENT_BROWSER_RENDERER_HOST_MEDIA_WEB_CONTENTS_TRACKER_H_
     87