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 #include "content/browser/media/capture/web_contents_tracker.h" | 5 #include "content/browser/media/capture/web_contents_tracker.h" |
6 | 6 |
7 #include "base/thread_task_runner_handle.h" | 7 #include "base/thread_task_runner_handle.h" |
8 #include "content/browser/frame_host/render_frame_host_impl.h" | 8 #include "content/browser/frame_host/render_frame_host_impl.h" |
9 #include "content/browser/renderer_host/render_widget_host_impl.h" | 9 #include "content/browser/renderer_host/render_widget_host_impl.h" |
10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
(...skipping 26 matching lines...) Expand all Loading... | |
37 BrowserThread::UI, FROM_HERE, | 37 BrowserThread::UI, FROM_HERE, |
38 base::Bind(&WebContentsTracker::StartObservingWebContents, this, | 38 base::Bind(&WebContentsTracker::StartObservingWebContents, this, |
39 render_process_id, main_render_frame_id)); | 39 render_process_id, main_render_frame_id)); |
40 } | 40 } |
41 } | 41 } |
42 | 42 |
43 void WebContentsTracker::Stop() { | 43 void WebContentsTracker::Stop() { |
44 DCHECK(task_runner_->BelongsToCurrentThread()); | 44 DCHECK(task_runner_->BelongsToCurrentThread()); |
45 | 45 |
46 callback_.Reset(); | 46 callback_.Reset(); |
47 resize_callback_.Reset(); | |
47 | 48 |
48 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 49 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
49 WebContentsObserver::Observe(NULL); | 50 WebContentsObserver::Observe(NULL); |
50 } else { | 51 } else { |
51 BrowserThread::PostTask( | 52 BrowserThread::PostTask( |
52 BrowserThread::UI, FROM_HERE, | 53 BrowserThread::UI, FROM_HERE, |
53 base::Bind(&WebContentsTracker::Observe, this, | 54 base::Bind(&WebContentsTracker::Observe, this, |
54 static_cast<WebContents*>(NULL))); | 55 static_cast<WebContents*>(NULL))); |
55 } | 56 } |
56 } | 57 } |
(...skipping 14 matching lines...) Expand all Loading... | |
71 if (!rwh) { | 72 if (!rwh) { |
72 RenderFrameHostImpl* const rfh = | 73 RenderFrameHostImpl* const rfh = |
73 static_cast<RenderFrameHostImpl*>(wc->GetMainFrame()); | 74 static_cast<RenderFrameHostImpl*>(wc->GetMainFrame()); |
74 if (rfh) | 75 if (rfh) |
75 rwh = rfh->GetRenderWidgetHost(); | 76 rwh = rfh->GetRenderWidgetHost(); |
76 } | 77 } |
77 | 78 |
78 return rwh; | 79 return rwh; |
79 } | 80 } |
80 | 81 |
82 void WebContentsTracker::SetResizeChangeCallback( | |
83 const ChangeCallback& callback) { | |
84 DCHECK(!task_runner_.get() || task_runner_->BelongsToCurrentThread()); | |
85 resize_callback_ = callback; | |
86 } | |
87 | |
81 void WebContentsTracker::OnPossibleTargetChange(bool force_callback_run) { | 88 void WebContentsTracker::OnPossibleTargetChange(bool force_callback_run) { |
82 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 89 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
83 | 90 |
84 RenderWidgetHost* const rwh = GetTargetRenderWidgetHost(); | 91 RenderWidgetHost* const rwh = GetTargetRenderWidgetHost(); |
85 if (rwh == last_target_ && !force_callback_run) | 92 if (rwh == last_target_ && !force_callback_run) |
86 return; | 93 return; |
87 DVLOG(1) << "Will report target change from RenderWidgetHost@" << last_target_ | 94 DVLOG(1) << "Will report target change from RenderWidgetHost@" << last_target_ |
88 << " to RenderWidgetHost@" << rwh; | 95 << " to RenderWidgetHost@" << rwh; |
89 last_target_ = rwh; | 96 last_target_ = rwh; |
90 | 97 |
91 if (task_runner_->BelongsToCurrentThread()) { | 98 if (task_runner_->BelongsToCurrentThread()) { |
92 MaybeDoCallback(rwh); | 99 MaybeDoCallback(rwh); |
93 } else { | 100 } else { |
94 task_runner_->PostTask( | 101 task_runner_->PostTask( |
95 FROM_HERE, | 102 FROM_HERE, |
96 base::Bind(&WebContentsTracker::MaybeDoCallback, this, rwh)); | 103 base::Bind(&WebContentsTracker::MaybeDoCallback, this, rwh)); |
97 } | 104 } |
98 } | 105 } |
99 | 106 |
100 void WebContentsTracker::MaybeDoCallback(RenderWidgetHost* rwh) { | 107 void WebContentsTracker::MaybeDoCallback(RenderWidgetHost* rwh) { |
101 DCHECK(task_runner_->BelongsToCurrentThread()); | 108 DCHECK(task_runner_->BelongsToCurrentThread()); |
102 | 109 |
103 if (!callback_.is_null()) | 110 if (!callback_.is_null()) |
104 callback_.Run(rwh); | 111 callback_.Run(rwh); |
112 if (rwh) | |
113 MaybeDoResizeCallback(rwh); | |
114 } | |
115 | |
116 void WebContentsTracker::MaybeDoResizeCallback(RenderWidgetHost* rwh) { | |
117 DCHECK(task_runner_->BelongsToCurrentThread()); | |
118 | |
119 if (!resize_callback_.is_null()) | |
120 resize_callback_.Run(rwh); | |
105 } | 121 } |
106 | 122 |
107 void WebContentsTracker::StartObservingWebContents(int render_process_id, | 123 void WebContentsTracker::StartObservingWebContents(int render_process_id, |
108 int main_render_frame_id) { | 124 int main_render_frame_id) { |
109 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 125 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
110 | 126 |
111 Observe(WebContents::FromRenderFrameHost(RenderFrameHost::FromID( | 127 Observe(WebContents::FromRenderFrameHost(RenderFrameHost::FromID( |
112 render_process_id, main_render_frame_id))); | 128 render_process_id, main_render_frame_id))); |
113 DVLOG_IF(1, !web_contents()) | 129 DVLOG_IF(1, !web_contents()) |
114 << "Could not find WebContents associated with main RenderFrameHost " | 130 << "Could not find WebContents associated with main RenderFrameHost " |
115 << "referenced by render_process_id=" << render_process_id | 131 << "referenced by render_process_id=" << render_process_id |
116 << ", routing_id=" << main_render_frame_id; | 132 << ", routing_id=" << main_render_frame_id; |
117 | 133 |
118 OnPossibleTargetChange(true); | 134 OnPossibleTargetChange(true); |
119 } | 135 } |
120 | 136 |
121 void WebContentsTracker::RenderFrameDeleted( | 137 void WebContentsTracker::RenderFrameDeleted( |
122 RenderFrameHost* render_frame_host) { | 138 RenderFrameHost* render_frame_host) { |
123 OnPossibleTargetChange(false); | 139 OnPossibleTargetChange(false); |
124 } | 140 } |
125 | 141 |
126 void WebContentsTracker::RenderFrameHostChanged(RenderFrameHost* old_host, | 142 void WebContentsTracker::RenderFrameHostChanged(RenderFrameHost* old_host, |
127 RenderFrameHost* new_host) { | 143 RenderFrameHost* new_host) { |
128 OnPossibleTargetChange(false); | 144 OnPossibleTargetChange(false); |
129 } | 145 } |
130 | 146 |
147 void WebContentsTracker::MainFrameWasResized(bool width_changed) { | |
148 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
149 | |
150 RenderWidgetHost* const rwh = GetTargetRenderWidgetHost(); | |
151 if (!rwh) | |
152 return; | |
153 | |
154 if (task_runner_->BelongsToCurrentThread()) { | |
Wez
2015/05/13 19:05:09
So the task runner is sometimes UI thread and some
miu
2015/05/14 01:21:33
tl;dr: This is the pattern decided upon, based on
Wez
2015/05/14 01:44:55
Understood. The BelongsToCurrentThread() test and
Wez
2015/05/14 01:44:55
Also, is there any way that MainFrameWasResized()
miu
2015/05/14 21:12:26
This seems like a matter of preference. I like th
miu
2015/05/14 21:12:26
No. It is set exactly once, when Start() is calle
Wez
2015/05/15 00:55:42
I prefer early-exit because you could in principle
miu
2015/05/15 21:14:00
Done. Feels like we're bike shedding here. ;-)
| |
155 MaybeDoResizeCallback(rwh); | |
156 } else { | |
157 task_runner_->PostTask( | |
158 FROM_HERE, | |
159 base::Bind(&WebContentsTracker::MaybeDoResizeCallback, this, rwh)); | |
Wez
2015/05/14 01:44:55
What guarantees that the |rwh| will remain valid b
miu
2015/05/14 21:12:26
Excellent point. I looked again at the other code
Wez
2015/05/15 00:55:42
Given the usage by the two consumers, could we ins
miu
2015/05/15 21:14:00
Done, but I got rid of the pointer altogether. I
Wez
2015/05/15 23:31:31
Nice!
| |
160 } | |
161 | |
162 } | |
163 | |
131 void WebContentsTracker::WebContentsDestroyed() { | 164 void WebContentsTracker::WebContentsDestroyed() { |
132 Observe(NULL); | 165 Observe(NULL); |
133 OnPossibleTargetChange(false); | 166 OnPossibleTargetChange(false); |
134 } | 167 } |
135 | 168 |
136 void WebContentsTracker::DidShowFullscreenWidget(int routing_id) { | 169 void WebContentsTracker::DidShowFullscreenWidget(int routing_id) { |
137 OnPossibleTargetChange(false); | 170 OnPossibleTargetChange(false); |
138 } | 171 } |
139 | 172 |
140 void WebContentsTracker::DidDestroyFullscreenWidget(int routing_id) { | 173 void WebContentsTracker::DidDestroyFullscreenWidget(int routing_id) { |
141 OnPossibleTargetChange(false); | 174 OnPossibleTargetChange(false); |
142 } | 175 } |
143 | 176 |
144 } // namespace content | 177 } // namespace content |
OLD | NEW |