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 | |
88 namespace { | |
89 | |
90 // In debug builds, map a non-null RenderWidgetHost pointer to an invalid | |
91 // non-null pointer. This causes the app to crash if a client of | |
92 // WebContentsTracker later attempts to dereference the pointer when it should | |
93 // not. See comments for ChangeCallback in the header file. | |
94 RenderWidgetHost* InvalidatePointerInDebugBuilds(RenderWidgetHost* rwh) { | |
Wez
2015/05/15 00:55:42
See my other comments on how to avoid the need for
miu
2015/05/15 21:14:01
Acknowledged.
| |
95 #ifndef NDEBUG | |
96 if (rwh) | |
97 return static_cast<RenderWidgetHost*>(nullptr) + 1; | |
98 else | |
99 return nullptr; | |
100 #else | |
101 return rwh; | |
102 #endif | |
103 } | |
104 | |
105 } // namespace | |
106 | |
81 void WebContentsTracker::OnPossibleTargetChange(bool force_callback_run) { | 107 void WebContentsTracker::OnPossibleTargetChange(bool force_callback_run) { |
82 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 108 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
83 | 109 |
84 RenderWidgetHost* const rwh = GetTargetRenderWidgetHost(); | 110 RenderWidgetHost* const rwh = GetTargetRenderWidgetHost(); |
85 if (rwh == last_target_ && !force_callback_run) | 111 if (rwh == last_target_ && !force_callback_run) |
86 return; | 112 return; |
87 DVLOG(1) << "Will report target change from RenderWidgetHost@" << last_target_ | 113 DVLOG(1) << "Will report target change from RenderWidgetHost@" << last_target_ |
88 << " to RenderWidgetHost@" << rwh; | 114 << " to RenderWidgetHost@" << rwh; |
89 last_target_ = rwh; | 115 last_target_ = rwh; |
90 | 116 |
91 if (task_runner_->BelongsToCurrentThread()) { | 117 if (task_runner_->BelongsToCurrentThread()) { |
92 MaybeDoCallback(rwh); | 118 MaybeDoCallback(rwh); |
93 } else { | 119 } else { |
94 task_runner_->PostTask( | 120 task_runner_->PostTask( |
95 FROM_HERE, | 121 FROM_HERE, |
96 base::Bind(&WebContentsTracker::MaybeDoCallback, this, rwh)); | 122 base::Bind(&WebContentsTracker::MaybeDoCallback, |
123 this, | |
124 InvalidatePointerInDebugBuilds(rwh))); | |
97 } | 125 } |
98 } | 126 } |
99 | 127 |
100 void WebContentsTracker::MaybeDoCallback(RenderWidgetHost* rwh) { | 128 void WebContentsTracker::MaybeDoCallback(RenderWidgetHost* rwh) { |
101 DCHECK(task_runner_->BelongsToCurrentThread()); | 129 DCHECK(task_runner_->BelongsToCurrentThread()); |
102 | 130 |
103 if (!callback_.is_null()) | 131 if (!callback_.is_null()) |
104 callback_.Run(rwh); | 132 callback_.Run(rwh); |
133 if (rwh) | |
134 MaybeDoResizeCallback(rwh); | |
135 } | |
136 | |
137 void WebContentsTracker::MaybeDoResizeCallback(RenderWidgetHost* rwh) { | |
138 DCHECK(task_runner_->BelongsToCurrentThread()); | |
139 | |
140 if (!resize_callback_.is_null()) | |
141 resize_callback_.Run(rwh); | |
105 } | 142 } |
106 | 143 |
107 void WebContentsTracker::StartObservingWebContents(int render_process_id, | 144 void WebContentsTracker::StartObservingWebContents(int render_process_id, |
108 int main_render_frame_id) { | 145 int main_render_frame_id) { |
109 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 146 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
110 | 147 |
111 Observe(WebContents::FromRenderFrameHost(RenderFrameHost::FromID( | 148 Observe(WebContents::FromRenderFrameHost(RenderFrameHost::FromID( |
112 render_process_id, main_render_frame_id))); | 149 render_process_id, main_render_frame_id))); |
113 DVLOG_IF(1, !web_contents()) | 150 DVLOG_IF(1, !web_contents()) |
114 << "Could not find WebContents associated with main RenderFrameHost " | 151 << "Could not find WebContents associated with main RenderFrameHost " |
115 << "referenced by render_process_id=" << render_process_id | 152 << "referenced by render_process_id=" << render_process_id |
116 << ", routing_id=" << main_render_frame_id; | 153 << ", routing_id=" << main_render_frame_id; |
117 | 154 |
118 OnPossibleTargetChange(true); | 155 OnPossibleTargetChange(true); |
119 } | 156 } |
120 | 157 |
121 void WebContentsTracker::RenderFrameDeleted( | 158 void WebContentsTracker::RenderFrameDeleted( |
122 RenderFrameHost* render_frame_host) { | 159 RenderFrameHost* render_frame_host) { |
123 OnPossibleTargetChange(false); | 160 OnPossibleTargetChange(false); |
124 } | 161 } |
125 | 162 |
126 void WebContentsTracker::RenderFrameHostChanged(RenderFrameHost* old_host, | 163 void WebContentsTracker::RenderFrameHostChanged(RenderFrameHost* old_host, |
127 RenderFrameHost* new_host) { | 164 RenderFrameHost* new_host) { |
128 OnPossibleTargetChange(false); | 165 OnPossibleTargetChange(false); |
129 } | 166 } |
130 | 167 |
168 void WebContentsTracker::MainFrameWasResized(bool width_changed) { | |
169 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
170 | |
171 RenderWidgetHost* const rwh = GetTargetRenderWidgetHost(); | |
172 if (!rwh) | |
173 return; | |
174 | |
175 if (task_runner_->BelongsToCurrentThread()) { | |
176 MaybeDoResizeCallback(rwh); | |
177 } else { | |
178 task_runner_->PostTask( | |
179 FROM_HERE, | |
180 base::Bind(&WebContentsTracker::MaybeDoResizeCallback, | |
181 this, | |
182 InvalidatePointerInDebugBuilds(rwh))); | |
183 } | |
184 | |
185 } | |
186 | |
131 void WebContentsTracker::WebContentsDestroyed() { | 187 void WebContentsTracker::WebContentsDestroyed() { |
132 Observe(NULL); | 188 Observe(NULL); |
133 OnPossibleTargetChange(false); | 189 OnPossibleTargetChange(false); |
134 } | 190 } |
135 | 191 |
136 void WebContentsTracker::DidShowFullscreenWidget(int routing_id) { | 192 void WebContentsTracker::DidShowFullscreenWidget(int routing_id) { |
137 OnPossibleTargetChange(false); | 193 OnPossibleTargetChange(false); |
138 } | 194 } |
139 | 195 |
140 void WebContentsTracker::DidDestroyFullscreenWidget(int routing_id) { | 196 void WebContentsTracker::DidDestroyFullscreenWidget(int routing_id) { |
141 OnPossibleTargetChange(false); | 197 OnPossibleTargetChange(false); |
142 } | 198 } |
143 | 199 |
144 } // namespace content | 200 } // namespace content |
OLD | NEW |