Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // | 4 // |
| 5 // Given a starting render_process_id and main_render_frame_id, the | 5 // Given a starting render_process_id and main_render_frame_id, the |
| 6 // WebContentsTracker tracks changes to the active RenderFrameHost tree during | 6 // WebContentsTracker tracks changes to the active RenderFrameHost tree during |
| 7 // the lifetime of a WebContents instance. This is used when mirroring tab | 7 // the lifetime of a WebContents instance. This is used to maintain capture of |
| 8 // video and audio so that user navigations, crashes, iframes, etc., during a | 8 // the WebContents's video and audio across page transitions such as user |
| 9 // tab's lifetime allow the capturing code to remain active on the | 9 // navigations, crashes, iframes, etc.. |
| 10 // current/latest render frame tree. | |
| 11 // | 10 // |
| 12 // Threading issues: Start(), Stop() and the ChangeCallback are invoked on the | 11 // Threading issues: Start(), Stop() and the ChangeCallback must be invoked on |
| 13 // same thread. This can be any thread, and the decision is locked-in by | 12 // the same thread. This can be any thread, and the decision is locked-in once |
| 14 // WebContentsTracker when Start() is called. | 13 // WebContentsTracker::Start() is called. |
| 15 | 14 |
| 16 #ifndef CONTENT_BROWSER_MEDIA_CAPTURE_WEB_CONTENTS_TRACKER_H_ | 15 #ifndef CONTENT_BROWSER_MEDIA_CAPTURE_WEB_CONTENTS_TRACKER_H_ |
| 17 #define CONTENT_BROWSER_MEDIA_CAPTURE_WEB_CONTENTS_TRACKER_H_ | 16 #define CONTENT_BROWSER_MEDIA_CAPTURE_WEB_CONTENTS_TRACKER_H_ |
| 18 | 17 |
| 19 #include "base/callback.h" | 18 #include "base/callback.h" |
| 20 #include "base/macros.h" | 19 #include "base/macros.h" |
| 21 #include "base/memory/ref_counted.h" | 20 #include "base/memory/ref_counted.h" |
| 22 #include "content/common/content_export.h" | 21 #include "content/common/content_export.h" |
| 23 #include "content/public/browser/web_contents_observer.h" | 22 #include "content/public/browser/web_contents_observer.h" |
| 24 | 23 |
| 25 namespace base { | 24 namespace base { |
| 26 class SingleThreadTaskRunner; | 25 class SingleThreadTaskRunner; |
| 27 } | 26 } |
| 28 | 27 |
| 29 namespace content { | 28 namespace content { |
| 30 | 29 |
| 31 class RenderWidgetHost; | 30 class RenderWidgetHostView; |
| 32 | 31 |
| 33 class CONTENT_EXPORT WebContentsTracker | 32 class CONTENT_EXPORT WebContentsTracker |
| 34 : public base::RefCountedThreadSafe<WebContentsTracker>, | 33 : public base::RefCountedThreadSafe<WebContentsTracker>, |
| 35 public WebContentsObserver { | 34 public WebContentsObserver { |
| 36 public: | 35 public: |
| 37 // If |track_fullscreen_rwh| is true, the ChangeCallback will be run when a | 36 // If |track_fullscreen_rwhv| is true, the ChangeCallback will be run when a |
| 38 // WebContents shows/destroys a fullscreen RenderWidgetHost view. If false, | 37 // WebContents shows/destroys a fullscreen RenderWidgetHostView. If false, |
| 39 // fullscreen events are ignored. Specify true for video tab capture and | 38 // fullscreen events are ignored. Normally, specify true for video capture and |
| 40 // false for audio tab capture. | 39 // false for audio capture. |
| 41 explicit WebContentsTracker(bool track_fullscreen_rwh); | 40 explicit WebContentsTracker(bool track_fullscreen_rwhv); |
| 42 | 41 |
| 43 // Callback to indicate a new RenderWidgetHost should be targeted for capture. | 42 // Callback to indicate a new RenderWidgetHostView should be targeted for |
| 44 // This is also invoked with false to indicate tracking will not continue | 43 // capture. This is also invoked with false to indicate tracking will not |
| 45 // (i.e., the WebContents instance was not found or has been destroyed). | 44 // continue (i.e., the WebContents instance was not found or has been |
| 45 // destroyed). | |
| 46 typedef base::Callback<void(bool was_still_tracking)> ChangeCallback; | 46 typedef base::Callback<void(bool was_still_tracking)> ChangeCallback; |
| 47 | 47 |
| 48 // Start tracking. The last-known |render_process_id| and | 48 // Start tracking. The last-known |render_process_id| and |
| 49 // |main_render_frame_id| are provided, and |callback| will be run once to | 49 // |main_render_frame_id| are provided, and |callback| will be run once to |
| 50 // indicate whether tracking successfully started (this may occur during the | 50 // indicate whether tracking successfully started (this may occur during the |
| 51 // invocation of Start(), or in the future). The callback will be invoked on | 51 // invocation of Start(), or in the future). The callback will be invoked on |
| 52 // the same thread calling Start(). | 52 // the same thread calling Start(). |
| 53 virtual void Start(int render_process_id, int main_render_frame_id, | 53 virtual void Start(int render_process_id, int main_render_frame_id, |
| 54 const ChangeCallback& callback); | 54 const ChangeCallback& callback); |
| 55 | 55 |
| 56 // Stop tracking. Once this method returns, the callback is guaranteed not to | 56 // Stop tracking. Once this method returns, the callback is guaranteed not to |
| 57 // be invoked again. | 57 // be invoked again. |
| 58 virtual void Stop(); | 58 virtual void Stop(); |
| 59 | 59 |
| 60 // Current target. This must only be called on the UI BrowserThread. | 60 // Returns true if this tracker is still able to continue tracking changes. |
| 61 RenderWidgetHost* GetTargetRenderWidgetHost() const; | 61 // This must only be called on the UI BrowserThread. |
| 62 bool is_still_tracking() const { return !!web_contents(); } | |
|
xjz
2017/01/03 19:16:40
nit: DCHECK_CURRENTLY_ON(BrowserThread::UI);?
miu
2017/01/03 21:01:28
We generally don't add these for inline accessors.
| |
| 63 | |
| 64 // Current target view. May return nullptr during certain transient periods. | |
| 65 // This must only be called on the UI BrowserThread. | |
| 66 RenderWidgetHostView* GetTargetView() const; | |
| 62 | 67 |
| 63 // Set a callback that is run whenever the main frame of the WebContents is | 68 // Set a callback that is run whenever the main frame of the WebContents is |
| 64 // resized. This method must be called on the same thread that calls | 69 // resized. This method must be called on the same thread that calls |
| 65 // Start()/Stop(), and |callback| will be run on that same thread. Calling | 70 // Start()/Stop(), and |callback| will be run on that same thread. Calling |
| 66 // the Stop() method guarantees the callback will never be invoked again. | 71 // the Stop() method guarantees the callback will never be invoked again. |
| 67 void SetResizeChangeCallback(const base::Closure& callback); | 72 void SetResizeChangeCallback(const base::Closure& callback); |
| 68 | 73 |
| 69 protected: | 74 protected: |
| 70 friend class base::RefCountedThreadSafe<WebContentsTracker>; | 75 friend class base::RefCountedThreadSafe<WebContentsTracker>; |
| 71 ~WebContentsTracker() override; | 76 ~WebContentsTracker() override; |
| 72 | 77 |
| 73 private: | 78 private: |
| 74 // Determine the target RenderWidgetHost and, if different from that last | 79 // Determine the target RenderWidgetHostView and, if different from that last |
| 75 // reported, runs the ChangeCallback on the appropriate thread. If | 80 // reported, runs the ChangeCallback on the appropriate thread. If |
| 76 // |force_callback_run| is true, the ChangeCallback is run even if the | 81 // |force_callback_run| is true, the ChangeCallback is run even if the |
| 77 // RenderWidgetHost has not changed. | 82 // RenderWidgetHost has not changed. |
|
xjz
2017/01/03 19:16:40
nit: s/RenderWidgetHost/RenderWidgetHostView
miu
2017/01/03 21:01:28
Done. Good catch.
| |
| 78 void OnPossibleTargetChange(bool force_callback_run); | 83 void OnPossibleTargetChange(bool force_callback_run); |
| 79 | 84 |
| 80 // Called on the thread that Start()/Stop() are called on. Checks whether the | 85 // Called on the thread that Start()/Stop() are called on. Checks whether the |
| 81 // callback is still valid and, if so, runs it. | 86 // callback is still valid and, if so, runs it. |
| 82 void MaybeDoCallback(bool was_still_tracking); | 87 void MaybeDoCallback(bool was_still_tracking); |
| 83 | 88 |
| 84 // Called on the thread that Start()/Stop() are called on. Checks whether the | 89 // Called on the thread that Start()/Stop() are called on. Checks whether the |
| 85 // callback is still valid and, if so, runs it to indicate the main frame has | 90 // callback is still valid and, if so, runs it to indicate the main frame has |
| 86 // changed in size. | 91 // changed in size. |
| 87 void MaybeDoResizeCallback(); | 92 void MaybeDoResizeCallback(); |
| 88 | 93 |
| 89 // Look-up the current WebContents instance associated with the given | 94 // Look-up the current WebContents instance associated with the given |
| 90 // |render_process_id| and |main_render_frame_id| and begin observing it. | 95 // |render_process_id| and |main_render_frame_id| and begin observing it. |
| 91 void StartObservingWebContents(int render_process_id, | 96 void StartObservingWebContents(int render_process_id, |
| 92 int main_render_frame_id); | 97 int main_render_frame_id); |
| 93 | 98 |
| 94 // WebContentsObserver overrides: According to web_contents_observer.h, these | 99 // WebContentsObserver overrides: These events indicate that the view of the |
| 95 // two method overrides are all that is necessary to track the set of active | 100 // main frame may have changed.. |
|
xjz
2017/01/03 19:16:40
nit: s/../.
miu
2017/01/03 21:01:28
Done.
| |
| 96 // RenderFrameHosts. | 101 void RenderFrameCreated(RenderFrameHost* render_frame_host) final; |
| 97 void RenderFrameDeleted(RenderFrameHost* render_frame_host) override; | 102 void RenderFrameDeleted(RenderFrameHost* render_frame_host) final; |
| 98 void RenderFrameHostChanged(RenderFrameHost* old_host, | 103 void RenderFrameHostChanged(RenderFrameHost* old_host, |
| 99 RenderFrameHost* new_host) override; | 104 RenderFrameHost* new_host) final; |
| 100 | 105 |
| 101 // WebContentsObserver override to notify the client that the source size has | 106 // WebContentsObserver override to notify the client that the source size has |
| 102 // changed. | 107 // changed. |
| 103 void MainFrameWasResized(bool width_changed) override; | 108 void MainFrameWasResized(bool width_changed) final; |
| 104 | 109 |
| 105 // WebContentsObserver override to notify the client that the capture target | 110 // WebContentsObserver override to notify the client that the capture target |
| 106 // has been permanently lost. | 111 // has been permanently lost. |
| 107 void WebContentsDestroyed() override; | 112 void WebContentsDestroyed() final; |
| 108 | 113 |
| 109 // WebContentsObserver overrides to notify the client that the capture target | 114 // WebContentsObserver overrides to notify the client that the capture target |
| 110 // may have changed due to a separate fullscreen widget shown/destroyed. | 115 // may have changed due to a separate fullscreen widget shown/destroyed. |
| 111 void DidShowFullscreenWidget() override; | 116 void DidShowFullscreenWidget() final; |
| 112 void DidDestroyFullscreenWidget() override; | 117 void DidDestroyFullscreenWidget() final; |
| 113 | 118 |
| 114 // If true, the client is interested in the showing/destruction of fullscreen | 119 // If true, the client is interested in the showing/destruction of fullscreen |
| 115 // RenderWidgetHosts. | 120 // RenderWidgetHostViews. |
| 116 const bool track_fullscreen_rwh_; | 121 const bool track_fullscreen_rwhv_; |
| 122 | |
| 123 // Pointer to the RenderWidgetHostView provided in the last run of | |
| 124 // |callback_|. This is used to eliminate duplicate callback runs. | |
| 125 RenderWidgetHostView* last_target_view_; | |
| 117 | 126 |
| 118 // TaskRunner corresponding to the thread that called Start(). | 127 // TaskRunner corresponding to the thread that called Start(). |
| 119 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 128 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 120 | 129 |
| 121 // Callback to run when the target RenderWidgetHost has changed. | 130 // Callback to run when the target RenderWidgetHostView has changed. |
| 122 ChangeCallback callback_; | 131 ChangeCallback callback_; |
| 123 | 132 |
| 124 // Pointer to the RenderWidgetHost provided in the last run of |callback_|. | 133 // Callback to run when the target RenderWidgetHostView has resized. |
| 125 // This is used to eliminate duplicate callback runs. | |
| 126 RenderWidgetHost* last_target_; | |
| 127 | |
| 128 // Callback to run when the target RenderWidgetHost has resized. | |
| 129 base::Closure resize_callback_; | 134 base::Closure resize_callback_; |
| 130 | 135 |
| 131 DISALLOW_COPY_AND_ASSIGN(WebContentsTracker); | 136 DISALLOW_COPY_AND_ASSIGN(WebContentsTracker); |
| 132 }; | 137 }; |
| 133 | 138 |
| 134 } // namespace content | 139 } // namespace content |
| 135 | 140 |
| 136 #endif // CONTENT_BROWSER_MEDIA_CAPTURE_WEB_CONTENTS_TRACKER_H_ | 141 #endif // CONTENT_BROWSER_MEDIA_CAPTURE_WEB_CONTENTS_TRACKER_H_ |
| OLD | NEW |