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 #include "content/browser/media/capture/web_contents_tracker.h"
7 #include "base/message_loop/message_loop_proxy.h"
8 #include "content/public/browser/browser_thread.h"
9 #include "content/public/browser/render_frame_host.h"
10 #include "content/public/browser/render_process_host.h"
11 #include "content/public/browser/render_view_host.h"
12 #include "content/public/browser/web_contents.h"
16 WebContentsTracker::WebContentsTracker() {}
18 WebContentsTracker::~WebContentsTracker() {
19 DCHECK(!web_contents()) << "BUG: Still observering!";
22 void WebContentsTracker::Start(int render_process_id
, int main_render_frame_id
,
23 const ChangeCallback
& callback
) {
24 DCHECK(!message_loop_
.get() || message_loop_
->BelongsToCurrentThread());
26 message_loop_
= base::MessageLoopProxy::current();
27 DCHECK(message_loop_
.get());
30 BrowserThread::PostTask(
31 BrowserThread::UI
, FROM_HERE
,
32 base::Bind(&WebContentsTracker::LookUpAndObserveWebContents
, this,
33 render_process_id
, main_render_frame_id
));
36 void WebContentsTracker::Stop() {
37 DCHECK(message_loop_
->BelongsToCurrentThread());
41 BrowserThread::PostTask(
42 BrowserThread::UI
, FROM_HERE
,
43 base::Bind(&WebContentsTracker::Observe
, this,
44 static_cast<WebContents
*>(NULL
)));
47 void WebContentsTracker::OnWebContentsChangeEvent() {
48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
50 WebContents
* const wc
= web_contents();
51 RenderViewHost
* const rvh
= wc
? wc
->GetRenderViewHost() : NULL
;
52 RenderProcessHost
* const rph
= rvh
? rvh
->GetProcess() : NULL
;
54 const int render_process_id
= rph
? rph
->GetID() : MSG_ROUTING_NONE
;
55 const int render_view_id
= rvh
? rvh
->GetRoutingID() : MSG_ROUTING_NONE
;
57 message_loop_
->PostTask(FROM_HERE
,
58 base::Bind(&WebContentsTracker::MaybeDoCallback
, this,
59 render_process_id
, render_view_id
));
62 void WebContentsTracker::MaybeDoCallback(int render_process_id
,
64 DCHECK(message_loop_
->BelongsToCurrentThread());
66 if (!callback_
.is_null())
67 callback_
.Run(render_process_id
, render_view_id
);
70 void WebContentsTracker::LookUpAndObserveWebContents(int render_process_id
,
71 int main_render_frame_id
) {
72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
74 Observe(WebContents::FromRenderFrameHost(RenderFrameHost::FromID(
75 render_process_id
, main_render_frame_id
)));
76 DVLOG_IF(1, !web_contents())
77 << "Could not find WebContents associated with main RenderFrameHost "
78 << "referenced by render_process_id=" << render_process_id
79 << ", routing_id=" << main_render_frame_id
;
81 OnWebContentsChangeEvent();
84 void WebContentsTracker::RenderViewReady() {
85 OnWebContentsChangeEvent();
88 void WebContentsTracker::AboutToNavigateRenderView(RenderViewHost
* rvh
) {
89 OnWebContentsChangeEvent();
92 void WebContentsTracker::DidNavigateMainFrame(
93 const LoadCommittedDetails
& details
, const FrameNavigateParams
& params
) {
94 OnWebContentsChangeEvent();
97 void WebContentsTracker::WebContentsDestroyed() {
98 OnWebContentsChangeEvent();
101 } // namespace content