OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 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. | |
4 | |
5 #include "content/browser/renderer_host/media/web_contents_capture_util.h" | |
6 | |
7 #include "base/basictypes.h" | |
8 #include "base/string_number_conversions.h" | |
9 #include "base/string_piece.h" | |
10 #include "base/string_util.h" | |
11 #include "content/public/browser/browser_thread.h" | |
12 #include "content/public/browser/render_process_host.h" | |
13 #include "content/public/browser/render_view_host.h" | |
14 #include "content/public/browser/web_contents.h" | |
15 | |
16 namespace { | |
17 | |
18 const char kVirtualDeviceScheme[] = "virtual://"; | |
19 | |
20 } // namespace | |
21 | |
22 namespace content { | |
23 | |
24 // static | |
25 bool WebContentsCaptureUtil::ExtractTabCaptureTarget( | |
26 const std::string& device_id_param, | |
27 int* render_process_id, | |
28 int* render_view_id) { | |
29 const std::string device_id = device_id_param.substr( | |
30 arraysize(kVirtualDeviceScheme) - 1, device_id_param.length()); | |
31 | |
32 const size_t sep_pos = device_id.find(':'); | |
33 if (sep_pos == std::string::npos) | |
34 return false; | |
35 | |
36 const base::StringPiece component1(device_id.data(), sep_pos); | |
37 const base::StringPiece component2(device_id.data() + sep_pos + 1, | |
38 device_id.length() - sep_pos - 1); | |
39 | |
40 return (base::StringToInt(component1, render_process_id) && | |
41 base::StringToInt(component2, render_view_id)); | |
42 } | |
43 | |
44 WebContentsCaptureUtil::RenderViewTracker::RenderViewTracker() {} | |
45 | |
46 WebContentsCaptureUtil::RenderViewTracker::~RenderViewTracker() { | |
47 DCHECK(!web_contents()) << "BUG: Still observering!"; | |
48 } | |
49 | |
50 void WebContentsCaptureUtil::RenderViewTracker::Start( | |
51 int render_process_id, int render_view_id, | |
52 const ChangeCallback& callback) { | |
53 { | |
Alpha Left Google
2012/11/26 22:59:59
I still think this can be done without a lock:
St
miu
2012/11/28 05:05:01
Actually, that's not guaranteed. The reason is be
| |
54 base::AutoLock guard(lock_); | |
55 callback_ = callback; | |
56 } | |
57 BrowserThread::PostTask( | |
58 BrowserThread::UI, FROM_HERE, | |
59 base::Bind(&RenderViewTracker::LookUpAndObserveWebContents, this, | |
Alpha Left Google
2012/11/26 22:59:59
Use BindToLoop here such that LookUpAndObserveWebC
| |
60 render_process_id, render_view_id)); | |
61 } | |
62 | |
63 void WebContentsCaptureUtil::RenderViewTracker::Stop() { | |
64 { | |
65 base::AutoLock guard(lock_); | |
66 callback_.Reset(); | |
67 } | |
68 BrowserThread::PostTask( | |
69 BrowserThread::UI, FROM_HERE, | |
70 base::Bind(&RenderViewTracker::Observe, this, | |
71 static_cast<WebContents*>(NULL))); | |
72 } | |
73 | |
74 void WebContentsCaptureUtil::RenderViewTracker::OnWebContentsChangeEvent() { | |
75 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
76 | |
77 WebContents* const wc = web_contents(); | |
78 RenderViewHost* const rvh = wc ? wc->GetRenderViewHost() : NULL; | |
79 RenderProcessHost* const rph = rvh ? rvh->GetProcess() : NULL; | |
80 | |
81 const int render_process_id = rph ? rph->GetID() : MSG_ROUTING_NONE; | |
82 const int render_view_id = rvh ? rvh->GetRoutingID() : MSG_ROUTING_NONE; | |
83 | |
84 base::AutoLock guard(lock_); | |
85 if (!callback_.is_null()) { | |
86 callback_.Run(render_process_id, render_view_id); | |
Alpha Left Google
2012/11/26 22:59:59
If you access |callback| only on IO thread you don
| |
87 } | |
88 } | |
89 | |
90 void WebContentsCaptureUtil::RenderViewTracker::LookUpAndObserveWebContents( | |
91 int render_process_id, int render_view_id) { | |
92 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
93 | |
94 RenderViewHost* const rvh = | |
95 RenderViewHost::FromID(render_process_id, render_view_id); | |
96 DVLOG_IF(1, !rvh) << "RenderViewHost::FromID(" | |
97 << render_process_id << ", " << render_view_id | |
98 << ") returned NULL."; | |
99 Observe(rvh ? WebContents::FromRenderViewHost(rvh) : NULL); | |
100 DVLOG_IF(1, !web_contents()) | |
101 << "WebContents::FromRenderViewHost(" << rvh << ") returned NULL."; | |
102 | |
103 OnWebContentsChangeEvent(); | |
104 } | |
105 | |
106 void WebContentsCaptureUtil::RenderViewTracker::RenderViewReady() { | |
107 OnWebContentsChangeEvent(); | |
108 } | |
109 | |
110 void WebContentsCaptureUtil::RenderViewTracker::WebContentsDestroyed( | |
111 WebContents* web_contents) { | |
112 OnWebContentsChangeEvent(); | |
113 } | |
114 | |
115 } // namespace content | |
OLD | NEW |