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 RenderViewHost instance swapping during the | 6 // WebContentsTracker tracks changes to the active RenderFrameHost tree during |
7 // lifetime of a WebContents instance. This is used when mirroring tab video | 7 // the lifetime of a WebContents instance. This is used when mirroring tab |
8 // and audio so that user navigations, crashes, etc., during a tab's lifetime | 8 // video and audio so that user navigations, crashes, iframes, etc., during a |
9 // allow the capturing code to remain active on the current/latest RenderView. | 9 // tab's lifetime allow the capturing code to remain active on the |
10 // | 10 // current/latest render frame tree. |
11 // TODO(miu): In a soon upcoming change, the cross-site isolation migration of | |
12 // this code will be completed such that the main RenderFrameHost is tracked | |
13 // instead of the RenderViewHost. | |
14 // | 11 // |
15 // Threading issues: Start(), Stop() and the ChangeCallback are invoked on the | 12 // Threading issues: Start(), Stop() and the ChangeCallback are invoked on the |
16 // same thread. This can be any thread, and the decision is locked-in by | 13 // same thread. This can be any thread, and the decision is locked-in by |
17 // WebContentsTracker when Start() is called. | 14 // WebContentsTracker when Start() is called. |
18 | 15 |
19 #ifndef CONTENT_BROWSER_MEDIA_CAPTURE_WEB_CONTENTS_TRACKER_H_ | 16 #ifndef CONTENT_BROWSER_MEDIA_CAPTURE_WEB_CONTENTS_TRACKER_H_ |
20 #define CONTENT_BROWSER_MEDIA_CAPTURE_WEB_CONTENTS_TRACKER_H_ | 17 #define CONTENT_BROWSER_MEDIA_CAPTURE_WEB_CONTENTS_TRACKER_H_ |
21 | 18 |
22 #include "base/callback.h" | 19 #include "base/callback.h" |
23 #include "base/memory/ref_counted.h" | 20 #include "base/memory/ref_counted.h" |
24 #include "content/common/content_export.h" | 21 #include "content/common/content_export.h" |
25 #include "content/public/browser/web_contents_observer.h" | 22 #include "content/public/browser/web_contents_observer.h" |
26 | 23 |
27 namespace base { | 24 namespace base { |
28 class MessageLoopProxy; | 25 class MessageLoopProxy; |
29 } | 26 } |
30 | 27 |
31 namespace content { | 28 namespace content { |
32 | 29 |
| 30 class RenderWidgetHost; |
| 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 WebContentsTracker(); | 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); |
38 | 41 |
39 // Callback for whenever the target is swapped. The callback is also invoked | 42 // Callback to indicate a new RenderWidgetHost should be targeted for capture. |
40 // with both arguments set to MSG_ROUTING_NONE to indicate tracking will not | 43 // This is also invoked with NULL to indicate tracking will not continue |
41 // continue (i.e., the WebContents instance was not found or has been | 44 // (i.e., the WebContents instance was not found or has been destroyed). |
42 // destroyed). | 45 typedef base::Callback<void(RenderWidgetHost* rwh)> ChangeCallback; |
43 typedef base::Callback<void(int render_process_id, int render_view_id)> | |
44 ChangeCallback; | |
45 | 46 |
46 // Start tracking. The last-known |render_process_id| and | 47 // Start tracking. The last-known |render_process_id| and |
47 // |main_render_frame_id| are provided, and the given callback is invoked | 48 // |main_render_frame_id| are provided, and |callback| will be run once to |
48 // asynchronously one or more times. The callback will be invoked on the same | 49 // indicate the current capture target (this may occur during the invocation |
| 50 // of Start(), or in the future). The callback will be invoked on the same |
49 // thread calling Start(). | 51 // thread calling Start(). |
50 virtual void Start(int render_process_id, int main_render_frame_id, | 52 virtual void Start(int render_process_id, int main_render_frame_id, |
51 const ChangeCallback& callback); | 53 const ChangeCallback& callback); |
52 | 54 |
53 // Stop tracking. Once this method returns, the callback is guaranteed not to | 55 // Stop tracking. Once this method returns, the callback is guaranteed not to |
54 // be invoked again. | 56 // be invoked again. |
55 virtual void Stop(); | 57 virtual void Stop(); |
56 | 58 |
| 59 // Current target. This must only be called on the UI BrowserThread. |
| 60 RenderWidgetHost* GetTargetRenderWidgetHost() const; |
| 61 |
57 protected: | 62 protected: |
58 friend class base::RefCountedThreadSafe<WebContentsTracker>; | 63 friend class base::RefCountedThreadSafe<WebContentsTracker>; |
59 virtual ~WebContentsTracker(); | 64 virtual ~WebContentsTracker(); |
60 | 65 |
61 private: | 66 private: |
62 // Reads the render_process_id/render_view_id from the current WebContents | 67 // Determine the target RenderWidgetHost and, if different from that last |
63 // instance and then invokes the callback. | 68 // reported, runs the ChangeCallback on the appropriate thread. If |
64 void OnWebContentsChangeEvent(); | 69 // |force_callback_run| is true, the ChangeCallback is run even if the |
| 70 // RenderWidgetHost has not changed. |
| 71 void OnPossibleTargetChange(bool force_callback_run); |
65 | 72 |
66 // Called on the thread that Start()/Stop() are called on, check whether the | 73 // Called on the thread that Start()/Stop() are called on. Checks whether the |
67 // callback is still valid and, if so, invoke it. | 74 // callback is still valid and, if so, runs it. |
68 void MaybeDoCallback(int render_process_id, int render_view_id); | 75 void MaybeDoCallback(RenderWidgetHost* rwh); |
69 | 76 |
70 // Look-up the current WebContents instance associated with the given | 77 // Look-up the current WebContents instance associated with the given |
71 // |render_process_id| and |main_render_frame_id| and begin observing it. | 78 // |render_process_id| and |main_render_frame_id| and begin observing it. |
72 void LookUpAndObserveWebContents(int render_process_id, | 79 void StartObservingWebContents(int render_process_id, |
73 int main_render_frame_id); | 80 int main_render_frame_id); |
74 | 81 |
75 // WebContentsObserver overrides to react to events of interest. | 82 // WebContentsObserver overrides: According to web_contents_observer.h, these |
76 virtual void RenderViewReady() OVERRIDE; | 83 // two method overrides are all that is necessary to track the set of active |
77 virtual void AboutToNavigateRenderView(RenderViewHost* render_view_host) | 84 // RenderFrameHosts. |
78 OVERRIDE; | 85 virtual void RenderFrameDeleted(RenderFrameHost* render_frame_host) OVERRIDE; |
79 virtual void DidNavigateMainFrame(const LoadCommittedDetails& details, | 86 virtual void RenderFrameHostChanged(RenderFrameHost* old_host, |
80 const FrameNavigateParams& params) OVERRIDE; | 87 RenderFrameHost* new_host) OVERRIDE; |
| 88 |
| 89 // WebContentsObserver override to notify the client that the capture target |
| 90 // has been permanently lost. |
81 virtual void WebContentsDestroyed() OVERRIDE; | 91 virtual void WebContentsDestroyed() OVERRIDE; |
82 | 92 |
| 93 // WebContentsObserver overrides to notify the client that the capture target |
| 94 // may have changed due to a separate fullscreen widget shown/destroyed. |
| 95 virtual void DidShowFullscreenWidget(int routing_id) OVERRIDE; |
| 96 virtual void DidDestroyFullscreenWidget(int routing_id) OVERRIDE; |
| 97 |
| 98 // If true, the client is interested in the showing/destruction of fullscreen |
| 99 // RenderWidgetHosts. |
| 100 const bool track_fullscreen_rwh_; |
| 101 |
| 102 // MessageLoop corresponding to the thread that called Start(). |
83 scoped_refptr<base::MessageLoopProxy> message_loop_; | 103 scoped_refptr<base::MessageLoopProxy> message_loop_; |
| 104 |
| 105 // Callback to run when the target RenderWidgetHost has changed. |
84 ChangeCallback callback_; | 106 ChangeCallback callback_; |
85 | 107 |
| 108 // Pointer to the RenderWidgetHost provided in the last run of |callback_|. |
| 109 // This is used to eliminate duplicate callback runs. |
| 110 RenderWidgetHost* last_target_; |
| 111 |
86 DISALLOW_COPY_AND_ASSIGN(WebContentsTracker); | 112 DISALLOW_COPY_AND_ASSIGN(WebContentsTracker); |
87 }; | 113 }; |
88 | 114 |
89 } // namespace content | 115 } // namespace content |
90 | 116 |
91 #endif // CONTENT_BROWSER_MEDIA_CAPTURE_WEB_CONTENTS_TRACKER_H_ | 117 #endif // CONTENT_BROWSER_MEDIA_CAPTURE_WEB_CONTENTS_TRACKER_H_ |
OLD | NEW |