OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/cdm/browser_cdm_manager.h" | 5 #include "content/browser/media/cdm/browser_cdm_manager.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/task_runner.h" | 12 #include "base/task_runner.h" |
13 #include "content/public/browser/browser_context.h" | 13 #include "content/public/browser/browser_context.h" |
14 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
15 #include "content/public/browser/content_browser_client.h" | 15 #include "content/public/browser/content_browser_client.h" |
16 #include "content/public/browser/permission_manager.h" | 16 #include "content/public/browser/permission_manager.h" |
17 #include "content/public/browser/permission_type.h" | 17 #include "content/public/browser/permission_type.h" |
18 #include "content/public/browser/render_frame_host.h" | 18 #include "content/public/browser/render_frame_host.h" |
19 #include "content/public/browser/render_process_host.h" | 19 #include "content/public/browser/render_process_host.h" |
20 #include "content/public/browser/render_view_host.h" | 20 #include "content/public/browser/render_process_host_observer.h" |
21 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
22 #include "media/base/browser_cdm.h" | 22 #include "media/base/browser_cdm.h" |
23 #include "media/base/browser_cdm_factory.h" | 23 #include "media/base/browser_cdm_factory.h" |
24 #include "media/base/cdm_promise.h" | 24 #include "media/base/cdm_promise.h" |
25 #include "media/base/limits.h" | 25 #include "media/base/limits.h" |
26 | 26 |
27 #if defined(OS_ANDROID) | 27 #if defined(OS_ANDROID) |
28 #include "content/public/common/renderer_preferences.h" | 28 #include "content/public/common/renderer_preferences.h" |
29 #endif | 29 #endif |
30 | 30 |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
98 template <> | 98 template <> |
99 void CdmPromiseInternal<std::string>::resolve(const std::string& session_id) { | 99 void CdmPromiseInternal<std::string>::resolve(const std::string& session_id) { |
100 MarkPromiseSettled(); | 100 MarkPromiseSettled(); |
101 manager_->ResolvePromiseWithSession(render_frame_id_, cdm_id_, promise_id_, | 101 manager_->ResolvePromiseWithSession(render_frame_id_, cdm_id_, promise_id_, |
102 session_id); | 102 session_id); |
103 } | 103 } |
104 | 104 |
105 typedef CdmPromiseInternal<> SimplePromise; | 105 typedef CdmPromiseInternal<> SimplePromise; |
106 typedef CdmPromiseInternal<std::string> NewSessionPromise; | 106 typedef CdmPromiseInternal<std::string> NewSessionPromise; |
107 | 107 |
108 } // namespace | |
109 | |
110 // Render process ID to BrowserCdmManager map. | 108 // Render process ID to BrowserCdmManager map. |
111 typedef std::map<int, BrowserCdmManager*> BrowserCdmManagerMap; | 109 typedef std::map<int, BrowserCdmManager*> BrowserCdmManagerMap; |
112 base::LazyInstance<BrowserCdmManagerMap>::Leaky g_browser_cdm_manager_map = | 110 base::LazyInstance<BrowserCdmManagerMap>::Leaky g_browser_cdm_manager_map = |
113 LAZY_INSTANCE_INITIALIZER; | 111 LAZY_INSTANCE_INITIALIZER; |
114 | 112 |
113 // Keeps the BrowserCdmManager alive, and in the global map, for as long as the | |
114 // RenderProcessHost is connected to the child process. This class is a | |
115 // self-owned observer. | |
116 class BrowserCdmManagerProcessWatcher : public RenderProcessHostObserver { | |
117 public: | |
118 BrowserCdmManagerProcessWatcher( | |
119 int render_process_id, | |
120 const scoped_refptr<BrowserCdmManager>& manager) | |
121 : browser_cdm_manager_(manager) { | |
122 RenderProcessHost::FromID(render_process_id)->AddObserver(this); | |
123 CHECK(g_browser_cdm_manager_map.Get() | |
124 .insert(std::make_pair(render_process_id, manager.get())) | |
125 .second); | |
ddorwin
2015/07/09 17:52:22
Is this checking that manager.get() is not NULL? I
xhwang
2015/07/09 19:04:40
This is to check that the insert succeeded, meanin
| |
126 } | |
127 | |
128 // RenderProcessHostObserver: | |
129 void RenderProcessExited(RenderProcessHost* host, | |
130 base::TerminationStatus /* status */, | |
131 int /* exit_code */) override { | |
132 Destroy(host); | |
ddorwin
2015/07/09 17:52:22
nit: This looks like it destroys |host|.
It's a b
xhwang
2015/07/09 19:04:39
Hmm, all HostObservers know which Host they are ob
ncarter (slow)
2015/07/09 19:16:22
It's structured this way to allow a RPHObserver in
| |
133 } | |
134 | |
135 void RenderProcessHostDestroyed(RenderProcessHost* host) override { | |
136 Destroy(host); | |
137 } | |
138 | |
139 private: | |
140 void Destroy(RenderProcessHost* host) { | |
141 CHECK(g_browser_cdm_manager_map.Get().erase(host->GetID())); | |
142 host->RemoveObserver(this); | |
143 delete this; | |
144 } | |
145 | |
146 const scoped_refptr<BrowserCdmManager> browser_cdm_manager_; | |
147 }; | |
148 | |
149 } // namespace | |
150 | |
151 // static | |
115 BrowserCdmManager* BrowserCdmManager::FromProcess(int render_process_id) { | 152 BrowserCdmManager* BrowserCdmManager::FromProcess(int render_process_id) { |
116 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 153 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
117 | 154 auto& map = g_browser_cdm_manager_map.Get(); |
118 if (!g_browser_cdm_manager_map.Get().count(render_process_id)) | 155 auto iterator = map.find(render_process_id); |
119 return NULL; | 156 return (iterator == map.end()) ? nullptr : iterator->second; |
120 | |
121 return g_browser_cdm_manager_map.Get()[render_process_id]; | |
122 } | 157 } |
123 | 158 |
124 BrowserCdmManager::BrowserCdmManager( | 159 BrowserCdmManager::BrowserCdmManager( |
125 int render_process_id, | 160 int render_process_id, |
126 const scoped_refptr<base::TaskRunner>& task_runner) | 161 const scoped_refptr<base::TaskRunner>& task_runner) |
127 : BrowserMessageFilter(CdmMsgStart), | 162 : BrowserMessageFilter(CdmMsgStart), |
128 render_process_id_(render_process_id), | 163 render_process_id_(render_process_id), |
129 task_runner_(task_runner), | 164 task_runner_(task_runner), |
130 weak_ptr_factory_(this) { | 165 weak_ptr_factory_(this) { |
131 DVLOG(1) << __FUNCTION__ << ": " << render_process_id_; | 166 DVLOG(1) << __FUNCTION__ << ": " << render_process_id_; |
132 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 167 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
133 | 168 |
ddorwin
2015/07/09 17:52:22
Can/should we check that there is no entry for the
xhwang
2015/07/09 19:04:39
This is indirectly checked on line 123.
| |
169 new BrowserCdmManagerProcessWatcher(render_process_id, this); | |
170 | |
134 if (!task_runner_.get()) { | 171 if (!task_runner_.get()) { |
135 task_runner_ = | 172 task_runner_ = |
136 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI); | 173 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI); |
137 } | 174 } |
138 | |
139 DCHECK(!g_browser_cdm_manager_map.Get().count(render_process_id_)) | |
140 << render_process_id_; | |
141 g_browser_cdm_manager_map.Get()[render_process_id] = this; | |
142 } | 175 } |
143 | 176 |
144 BrowserCdmManager::~BrowserCdmManager() { | 177 BrowserCdmManager::~BrowserCdmManager() { |
145 DVLOG(1) << __FUNCTION__ << ": " << render_process_id_; | 178 DVLOG(1) << __FUNCTION__ << ": " << render_process_id_; |
146 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 179 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
147 DCHECK(g_browser_cdm_manager_map.Get().count(render_process_id_)); | 180 DCHECK(g_browser_cdm_manager_map.Get().count(render_process_id_) == 0); |
148 DCHECK_EQ(this, g_browser_cdm_manager_map.Get()[render_process_id_]); | |
149 | |
150 g_browser_cdm_manager_map.Get().erase(render_process_id_); | |
151 } | 181 } |
152 | 182 |
153 // Makes sure BrowserCdmManager is always deleted on the Browser UI thread. | 183 // Makes sure BrowserCdmManager is always deleted on the Browser UI thread. |
154 void BrowserCdmManager::OnDestruct() const { | 184 void BrowserCdmManager::OnDestruct() const { |
155 DVLOG(1) << __FUNCTION__ << ": " << render_process_id_; | 185 DVLOG(1) << __FUNCTION__ << ": " << render_process_id_; |
156 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 186 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
157 delete this; | 187 delete this; |
158 } else { | 188 } else { |
159 BrowserThread::DeleteSoon(BrowserThread::UI, FROM_HERE, this); | 189 BrowserThread::DeleteSoon(BrowserThread::UI, FROM_HERE, this); |
160 } | 190 } |
(...skipping 484 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
645 BrowserCdm* cdm = GetCdm(render_frame_id, cdm_id); | 675 BrowserCdm* cdm = GetCdm(render_frame_id, cdm_id); |
646 if (!cdm) { | 676 if (!cdm) { |
647 promise->reject(MediaKeys::INVALID_STATE_ERROR, 0, "CDM not found."); | 677 promise->reject(MediaKeys::INVALID_STATE_ERROR, 0, "CDM not found."); |
648 return; | 678 return; |
649 } | 679 } |
650 | 680 |
651 cdm->LoadSession(session_type, session_id, promise.Pass()); | 681 cdm->LoadSession(session_type, session_id, promise.Pass()); |
652 } | 682 } |
653 | 683 |
654 } // namespace content | 684 } // namespace content |
OLD | NEW |