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.
5 // Given a starting render_process_id and main_render_frame_id, the
6 // WebContentsTracker tracks changes to the active RenderFrameHost tree during
7 // the lifetime of a WebContents instance. This is used when mirroring tab
8 // video and audio so that user navigations, crashes, iframes, etc., during a
9 // tab's lifetime allow the capturing code to remain active on the
10 // current/latest render frame tree.
12 // Threading issues: Start(), Stop() and the ChangeCallback are invoked on the
13 // same thread. This can be any thread, and the decision is locked-in by
14 // WebContentsTracker when Start() is called.
16 #ifndef CONTENT_BROWSER_MEDIA_CAPTURE_WEB_CONTENTS_TRACKER_H_
17 #define CONTENT_BROWSER_MEDIA_CAPTURE_WEB_CONTENTS_TRACKER_H_
19 #include "base/callback.h"
20 #include "base/memory/ref_counted.h"
21 #include "content/common/content_export.h"
22 #include "content/public/browser/web_contents_observer.h"
25 class SingleThreadTaskRunner
;
30 class RenderWidgetHost
;
32 class CONTENT_EXPORT WebContentsTracker
33 : public base::RefCountedThreadSafe
<WebContentsTracker
>,
34 public WebContentsObserver
{
36 // If |track_fullscreen_rwh| is true, the ChangeCallback will be run when a
37 // WebContents shows/destroys a fullscreen RenderWidgetHost view. If false,
38 // fullscreen events are ignored. Specify true for video tab capture and
39 // false for audio tab capture.
40 explicit WebContentsTracker(bool track_fullscreen_rwh
);
42 // Callback to indicate a new RenderWidgetHost should be targeted for capture.
43 // This is also invoked with false to indicate tracking will not continue
44 // (i.e., the WebContents instance was not found or has been destroyed).
45 typedef base::Callback
<void(bool was_still_tracking
)> ChangeCallback
;
47 // Start tracking. The last-known |render_process_id| and
48 // |main_render_frame_id| are provided, and |callback| will be run once to
49 // indicate whether tracking successfully started (this may occur during the
50 // invocation of Start(), or in the future). The callback will be invoked on
51 // the same thread calling Start().
52 virtual void Start(int render_process_id
, int main_render_frame_id
,
53 const ChangeCallback
& callback
);
55 // Stop tracking. Once this method returns, the callback is guaranteed not to
59 // Current target. This must only be called on the UI BrowserThread.
60 RenderWidgetHost
* GetTargetRenderWidgetHost() const;
62 // Set a callback that is run whenever the main frame of the WebContents is
63 // resized. This method must be called on the same thread that calls
64 // Start()/Stop(), and |callback| will be run on that same thread. Calling
65 // the Stop() method guarantees the callback will never be invoked again.
66 void SetResizeChangeCallback(const base::Closure
& callback
);
69 friend class base::RefCountedThreadSafe
<WebContentsTracker
>;
70 ~WebContentsTracker() override
;
73 // Determine the target RenderWidgetHost and, if different from that last
74 // reported, runs the ChangeCallback on the appropriate thread. If
75 // |force_callback_run| is true, the ChangeCallback is run even if the
76 // RenderWidgetHost has not changed.
77 void OnPossibleTargetChange(bool force_callback_run
);
79 // Called on the thread that Start()/Stop() are called on. Checks whether the
80 // callback is still valid and, if so, runs it.
81 void MaybeDoCallback(bool was_still_tracking
);
83 // Called on the thread that Start()/Stop() are called on. Checks whether the
84 // callback is still valid and, if so, runs it to indicate the main frame has
86 void MaybeDoResizeCallback();
88 // Look-up the current WebContents instance associated with the given
89 // |render_process_id| and |main_render_frame_id| and begin observing it.
90 void StartObservingWebContents(int render_process_id
,
91 int main_render_frame_id
);
93 // WebContentsObserver overrides: According to web_contents_observer.h, these
94 // two method overrides are all that is necessary to track the set of active
96 void RenderFrameDeleted(RenderFrameHost
* render_frame_host
) override
;
97 void RenderFrameHostChanged(RenderFrameHost
* old_host
,
98 RenderFrameHost
* new_host
) override
;
100 // WebContentsObserver override to notify the client that the source size has
102 void MainFrameWasResized(bool width_changed
) override
;
104 // WebContentsObserver override to notify the client that the capture target
105 // has been permanently lost.
106 void WebContentsDestroyed() override
;
108 // WebContentsObserver overrides to notify the client that the capture target
109 // may have changed due to a separate fullscreen widget shown/destroyed.
110 void DidShowFullscreenWidget(int routing_id
) override
;
111 void DidDestroyFullscreenWidget(int routing_id
) override
;
113 // If true, the client is interested in the showing/destruction of fullscreen
114 // RenderWidgetHosts.
115 const bool track_fullscreen_rwh_
;
117 // TaskRunner corresponding to the thread that called Start().
118 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
120 // Callback to run when the target RenderWidgetHost has changed.
121 ChangeCallback callback_
;
123 // Pointer to the RenderWidgetHost provided in the last run of |callback_|.
124 // This is used to eliminate duplicate callback runs.
125 RenderWidgetHost
* last_target_
;
127 // Callback to run when the target RenderWidgetHost has resized.
128 base::Closure resize_callback_
;
130 DISALLOW_COPY_AND_ASSIGN(WebContentsTracker
);
133 } // namespace content
135 #endif // CONTENT_BROWSER_MEDIA_CAPTURE_WEB_CONTENTS_TRACKER_H_