Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(511)

Side by Side Diff: media/mojo/services/mojo_cdm_service.cc

Issue 2668813002: Remove LazyInstance usage from media/ (Closed)
Patch Set: Fix presubmit comments. Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « media/midi/usb_midi_device_factory_android.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "media/mojo/services/mojo_cdm_service.h" 5 #include "media/mojo/services/mojo_cdm_service.h"
6 6
7 #include <map> 7 #include <map>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/lazy_instance.h"
12 #include "base/macros.h" 11 #include "base/macros.h"
13 #include "base/memory/ptr_util.h" 12 #include "base/memory/ptr_util.h"
14 #include "base/synchronization/lock.h" 13 #include "base/synchronization/lock.h"
15 #include "media/base/cdm_config.h" 14 #include "media/base/cdm_config.h"
16 #include "media/base/cdm_context.h" 15 #include "media/base/cdm_context.h"
17 #include "media/base/cdm_factory.h" 16 #include "media/base/cdm_factory.h"
18 #include "media/base/cdm_key_information.h" 17 #include "media/base/cdm_key_information.h"
19 #include "media/base/key_systems.h" 18 #include "media/base/key_systems.h"
20 #include "media/mojo/common/media_type_converters.h" 19 #include "media/mojo/common/media_type_converters.h"
21 #include "media/mojo/services/mojo_cdm_service_context.h" 20 #include "media/mojo/services/mojo_cdm_service_context.h"
22 #include "url/gurl.h" 21 #include "url/gurl.h"
23 22
24 namespace media { 23 namespace media {
25 24
26 namespace { 25 namespace {
27 26
28 // Manages all CDMs created by MojoCdmService. Can only have one instance per 27 // Manages all CDMs created by MojoCdmService. Can only have one instance per
29 // process so use a LazyInstance to ensure this. 28 // process, so we use a thread-safe static to achieve this.
30 class CdmManager { 29 class CdmManager {
31 public: 30 public:
32 CdmManager() {} 31 CdmManager() {}
33 ~CdmManager() {} 32 ~CdmManager() {}
34 33
35 // Returns the CDM associated with |cdm_id|. Can be called on any thread. 34 // Returns the CDM associated with |cdm_id|. Can be called on any thread.
36 scoped_refptr<ContentDecryptionModule> GetCdm(int cdm_id) { 35 scoped_refptr<ContentDecryptionModule> GetCdm(int cdm_id) {
37 base::AutoLock lock(lock_); 36 base::AutoLock lock(lock_);
38 auto iter = cdm_map_.find(cdm_id); 37 auto iter = cdm_map_.find(cdm_id);
39 return iter == cdm_map_.end() ? nullptr : iter->second; 38 return iter == cdm_map_.end() ? nullptr : iter->second;
(...skipping 15 matching lines...) Expand all
55 } 54 }
56 55
57 private: 56 private:
58 // Lock to protect |cdm_map_|. 57 // Lock to protect |cdm_map_|.
59 base::Lock lock_; 58 base::Lock lock_;
60 std::map<int, scoped_refptr<ContentDecryptionModule>> cdm_map_; 59 std::map<int, scoped_refptr<ContentDecryptionModule>> cdm_map_;
61 60
62 DISALLOW_COPY_AND_ASSIGN(CdmManager); 61 DISALLOW_COPY_AND_ASSIGN(CdmManager);
63 }; 62 };
64 63
65 base::LazyInstance<CdmManager>::Leaky g_cdm_manager = LAZY_INSTANCE_INITIALIZER; 64 CdmManager* GetManager() {
65 static CdmManager* manager = new CdmManager();
66 return manager;
67 }
66 68
67 } // namespace 69 } // namespace
68 70
69 using SimpleMojoCdmPromise = MojoCdmPromise<>; 71 using SimpleMojoCdmPromise = MojoCdmPromise<>;
70 using NewSessionMojoCdmPromise = MojoCdmPromise<std::string>; 72 using NewSessionMojoCdmPromise = MojoCdmPromise<std::string>;
71 73
72 int MojoCdmService::next_cdm_id_ = CdmContext::kInvalidCdmId + 1; 74 int MojoCdmService::next_cdm_id_ = CdmContext::kInvalidCdmId + 1;
73 75
74 // static 76 // static
75 scoped_refptr<ContentDecryptionModule> MojoCdmService::LegacyGetCdm( 77 scoped_refptr<ContentDecryptionModule> MojoCdmService::LegacyGetCdm(
76 int cdm_id) { 78 int cdm_id) {
77 DVLOG(1) << __func__ << ": " << cdm_id; 79 DVLOG(1) << __func__ << ": " << cdm_id;
78 return g_cdm_manager.Get().GetCdm(cdm_id); 80 return GetManager()->GetCdm(cdm_id);
79 } 81 }
80 82
81 MojoCdmService::MojoCdmService(base::WeakPtr<MojoCdmServiceContext> context, 83 MojoCdmService::MojoCdmService(base::WeakPtr<MojoCdmServiceContext> context,
82 CdmFactory* cdm_factory) 84 CdmFactory* cdm_factory)
83 : context_(context), 85 : context_(context),
84 cdm_factory_(cdm_factory), 86 cdm_factory_(cdm_factory),
85 cdm_id_(CdmContext::kInvalidCdmId), 87 cdm_id_(CdmContext::kInvalidCdmId),
86 weak_factory_(this) { 88 weak_factory_(this) {
87 DCHECK(context_); 89 DCHECK(context_);
88 DCHECK(cdm_factory_); 90 DCHECK(cdm_factory_);
89 } 91 }
90 92
91 MojoCdmService::~MojoCdmService() { 93 MojoCdmService::~MojoCdmService() {
92 if (cdm_id_ == CdmContext::kInvalidCdmId) 94 if (cdm_id_ == CdmContext::kInvalidCdmId)
93 return; 95 return;
94 96
95 g_cdm_manager.Get().UnregisterCdm(cdm_id_); 97 GetManager()->UnregisterCdm(cdm_id_);
96 98
97 if (context_) 99 if (context_)
98 context_->UnregisterCdm(cdm_id_); 100 context_->UnregisterCdm(cdm_id_);
99 } 101 }
100 102
101 void MojoCdmService::SetClient(mojom::ContentDecryptionModuleClientPtr client) { 103 void MojoCdmService::SetClient(mojom::ContentDecryptionModuleClientPtr client) {
102 client_ = std::move(client); 104 client_ = std::move(client);
103 } 105 }
104 106
105 void MojoCdmService::Initialize(const std::string& key_system, 107 void MojoCdmService::Initialize(const std::string& key_system,
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 cdm_promise_result->system_code = 0; 189 cdm_promise_result->system_code = 0;
188 cdm_promise_result->error_message = error_message; 190 cdm_promise_result->error_message = error_message;
189 callback.Run(std::move(cdm_promise_result), 0, nullptr); 191 callback.Run(std::move(cdm_promise_result), 0, nullptr);
190 return; 192 return;
191 } 193 }
192 194
193 cdm_ = cdm; 195 cdm_ = cdm;
194 cdm_id_ = next_cdm_id_++; 196 cdm_id_ = next_cdm_id_++;
195 197
196 context_->RegisterCdm(cdm_id_, this); 198 context_->RegisterCdm(cdm_id_, this);
197 g_cdm_manager.Get().RegisterCdm(cdm_id_, cdm); 199 GetManager()->RegisterCdm(cdm_id_, cdm);
198 200
199 // If |cdm| has a decryptor, create the MojoDecryptorService 201 // If |cdm| has a decryptor, create the MojoDecryptorService
200 // and pass the connection back to the client. 202 // and pass the connection back to the client.
201 mojom::DecryptorPtr decryptor_service; 203 mojom::DecryptorPtr decryptor_service;
202 CdmContext* const cdm_context = cdm_->GetCdmContext(); 204 CdmContext* const cdm_context = cdm_->GetCdmContext();
203 if (cdm_context && cdm_context->GetDecryptor()) { 205 if (cdm_context && cdm_context->GetDecryptor()) {
204 // MojoDecryptorService takes a reference to the CDM, but it is still owned 206 // MojoDecryptorService takes a reference to the CDM, but it is still owned
205 // by |this|. 207 // by |this|.
206 decryptor_.reset(new MojoDecryptorService( 208 decryptor_.reset(new MojoDecryptorService(
207 cdm_, MakeRequest(&decryptor_service), 209 cdm_, MakeRequest(&decryptor_service),
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
249 251
250 void MojoCdmService::OnDecryptorConnectionError() { 252 void MojoCdmService::OnDecryptorConnectionError() {
251 DVLOG(2) << __func__; 253 DVLOG(2) << __func__;
252 254
253 // MojoDecryptorService has lost connectivity to it's client, so it can be 255 // MojoDecryptorService has lost connectivity to it's client, so it can be
254 // freed. 256 // freed.
255 decryptor_.reset(); 257 decryptor_.reset();
256 } 258 }
257 259
258 } // namespace media 260 } // namespace media
OLDNEW
« no previous file with comments | « media/midi/usb_midi_device_factory_android.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698