Save errno for logging before potentially overwriting it.
[chromium-blink-merge.git] / content / browser / renderer_host / media / web_contents_tracker.cc
blob3a75080cb46d45d8adfe73de4fb16ebdaf6658e1
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/renderer_host/media/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_process_host.h"
10 #include "content/public/browser/render_view_host.h"
11 #include "content/public/browser/web_contents.h"
13 namespace content {
15 WebContentsTracker::WebContentsTracker() {}
17 WebContentsTracker::~WebContentsTracker() {
18 DCHECK(!web_contents()) << "BUG: Still observering!";
21 void WebContentsTracker::Start(int render_process_id, int render_view_id,
22 const ChangeCallback& callback) {
23 DCHECK(!message_loop_.get() || message_loop_->BelongsToCurrentThread());
25 message_loop_ = base::MessageLoopProxy::current();
26 DCHECK(message_loop_.get());
27 callback_ = callback;
29 BrowserThread::PostTask(
30 BrowserThread::UI, FROM_HERE,
31 base::Bind(&WebContentsTracker::LookUpAndObserveWebContents, this,
32 render_process_id, render_view_id));
35 void WebContentsTracker::Stop() {
36 DCHECK(message_loop_->BelongsToCurrentThread());
38 callback_.Reset();
40 BrowserThread::PostTask(
41 BrowserThread::UI, FROM_HERE,
42 base::Bind(&WebContentsTracker::Observe, this,
43 static_cast<WebContents*>(NULL)));
46 void WebContentsTracker::OnWebContentsChangeEvent() {
47 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
49 WebContents* const wc = web_contents();
50 RenderViewHost* const rvh = wc ? wc->GetRenderViewHost() : NULL;
51 RenderProcessHost* const rph = rvh ? rvh->GetProcess() : NULL;
53 const int render_process_id = rph ? rph->GetID() : MSG_ROUTING_NONE;
54 const int render_view_id = rvh ? rvh->GetRoutingID() : MSG_ROUTING_NONE;
56 message_loop_->PostTask(FROM_HERE,
57 base::Bind(&WebContentsTracker::MaybeDoCallback, this,
58 render_process_id, render_view_id));
61 void WebContentsTracker::MaybeDoCallback(int render_process_id,
62 int render_view_id) {
63 DCHECK(message_loop_->BelongsToCurrentThread());
65 if (!callback_.is_null())
66 callback_.Run(render_process_id, render_view_id);
69 void WebContentsTracker::LookUpAndObserveWebContents(int render_process_id,
70 int render_view_id) {
71 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
73 RenderViewHost* const rvh =
74 RenderViewHost::FromID(render_process_id, render_view_id);
75 DVLOG_IF(1, !rvh) << "RenderViewHost::FromID("
76 << render_process_id << ", " << render_view_id
77 << ") returned NULL.";
78 Observe(rvh ? WebContents::FromRenderViewHost(rvh) : NULL);
79 DVLOG_IF(1, !web_contents())
80 << "WebContents::FromRenderViewHost(" << rvh << ") returned NULL.";
82 OnWebContentsChangeEvent();
85 void WebContentsTracker::RenderViewReady() {
86 OnWebContentsChangeEvent();
89 void WebContentsTracker::AboutToNavigateRenderView(RenderViewHost* rvh) {
90 OnWebContentsChangeEvent();
93 void WebContentsTracker::DidNavigateMainFrame(
94 const LoadCommittedDetails& details, const FrameNavigateParams& params) {
95 OnWebContentsChangeEvent();
98 void WebContentsTracker::WebContentsDestroyed(WebContents* web_contents) {
99 OnWebContentsChangeEvent();
102 } // namespace content