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
3 // found in the LICENSE file.
5 #ifndef CONTENT_BROWSER_MEDIA_CDM_BROWSER_CDM_MANAGER_H_
6 #define CONTENT_BROWSER_MEDIA_CDM_BROWSER_CDM_MANAGER_H_
12 #include "base/basictypes.h"
13 #include "base/callback.h"
14 #include "base/containers/scoped_ptr_hash_map.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "content/common/content_export.h"
18 #include "content/common/media/cdm_messages.h"
19 #include "content/common/media/cdm_messages_enums.h"
20 #include "content/public/browser/browser_message_filter.h"
21 #include "content/public/common/permission_status.mojom.h"
22 #include "ipc/ipc_message.h"
23 #include "media/base/cdm_promise.h"
24 #include "media/base/eme_constants.h"
25 #include "media/base/media_keys.h"
28 struct CdmHostMsg_CreateSessionAndGenerateRequest_Params
;
36 // This class manages all CDM objects. It receives control operations from the
37 // the render process, and forwards them to corresponding CDM object. Callbacks
38 // from CDM objects are converted to IPCs and then sent to the render process.
39 class CONTENT_EXPORT BrowserCdmManager
: public BrowserMessageFilter
{
41 // Returns the BrowserCdmManager associated with the |render_process_id|.
42 // Returns NULL if no BrowserCdmManager is associated.
43 static BrowserCdmManager
* FromProcess(int render_process_id
);
45 // Constructs the BrowserCdmManager for |render_process_id| which runs on
47 // If |task_runner| is not NULL, all CDM messages are posted to it. Otherwise,
48 // all messages are posted to the browser UI thread.
49 BrowserCdmManager(int render_process_id
,
50 const scoped_refptr
<base::TaskRunner
>& task_runner
);
52 // BrowserMessageFilter implementations.
53 void OnDestruct() const override
;
54 base::TaskRunner
* OverrideTaskRunnerForMessage(
55 const IPC::Message
& message
) override
;
56 bool OnMessageReceived(const IPC::Message
& message
) override
;
58 media::BrowserCdm
* GetCdm(int render_frame_id
, int cdm_id
) const;
60 // Notifies that the render frame has been deleted so that all CDMs belongs
61 // to this render frame needs to be destroyed as well. This is needed because
62 // in some cases (e.g. fast termination of the renderer), the message to
63 // destroy the CDM will not be received.
64 void RenderFrameDeleted(int render_frame_id
);
67 void ResolvePromise(int render_frame_id
, int cdm_id
, uint32_t promise_id
);
68 void ResolvePromiseWithSession(int render_frame_id
,
71 const std::string
& session_id
);
72 void RejectPromise(int render_frame_id
,
75 media::MediaKeys::Exception exception
,
77 const std::string
& error_message
);
80 friend class base::RefCountedThreadSafe
<BrowserCdmManager
>;
81 friend class base::DeleteHelper
<BrowserCdmManager
>;
82 ~BrowserCdmManager() override
;
86 void OnSessionMessage(int render_frame_id
,
88 const std::string
& session_id
,
89 media::MediaKeys::MessageType message_type
,
90 const std::vector
<uint8
>& message
,
91 const GURL
& legacy_destination_url
);
92 void OnSessionClosed(int render_frame_id
,
94 const std::string
& session_id
);
95 void OnLegacySessionError(int render_frame_id
,
97 const std::string
& session_id
,
98 media::MediaKeys::Exception exception_code
,
100 const std::string
& error_message
);
101 void OnSessionKeysChange(int render_frame_id
,
103 const std::string
& session_id
,
104 bool has_additional_usable_key
,
105 media::CdmKeysInfo keys_info
);
106 void OnSessionExpirationUpdate(int render_frame_id
,
108 const std::string
& session_id
,
109 const base::Time
& new_expiry_time
);
112 void OnInitializeCdm(int render_frame_id
,
115 const CdmHostMsg_InitializeCdm_Params
& params
);
116 void OnSetServerCertificate(int render_frame_id
,
119 const std::vector
<uint8_t>& certificate
);
120 void OnCreateSessionAndGenerateRequest(
121 const CdmHostMsg_CreateSessionAndGenerateRequest_Params
& params
);
126 media::MediaKeys::SessionType session_type
,
127 const std::string
& session_id
);
128 void OnUpdateSession(int render_frame_id
,
131 const std::string
& session_id
,
132 const std::vector
<uint8
>& response
);
133 void OnCloseSession(int render_frame_id
,
136 const std::string
& session_id
);
137 void OnRemoveSession(int render_frame_id
,
140 const std::string
& session_id
);
141 void OnDestroyCdm(int render_frame_id
, int cdm_id
);
143 // Adds a new CDM identified by |cdm_id| for the given |key_system| and
144 // |security_origin|.
145 void AddCdm(int render_frame_id
,
148 const std::string
& key_system
,
149 const GURL
& security_origin
,
150 bool use_hw_secure_codecs
);
152 // Removes all CDMs associated with |render_frame_id|.
153 void RemoveAllCdmForFrame(int render_frame_id
);
155 // Removes the CDM with the specified id.
156 void RemoveCdm(uint64 id
);
158 using PermissionStatusCB
= base::Callback
<void(bool)>;
160 // Checks protected media identifier permission for the given
161 // |render_frame_id| and |cdm_id|.
162 void CheckPermissionStatus(int render_frame_id
,
164 const PermissionStatusCB
& permission_status_cb
);
166 // Checks permission status on Browser UI thread. Runs |permission_status_cb|
167 // on the |task_runner_| with the permission status.
168 void CheckPermissionStatusOnUIThread(
170 const GURL
& security_origin
,
171 const PermissionStatusCB
& permission_status_cb
);
173 // Calls CreateSessionAndGenerateRequest() on the CDM if
174 // |permission_was_allowed| is true. Otherwise rejects the |promise|.
175 void CreateSessionAndGenerateRequestIfPermitted(
178 media::MediaKeys::SessionType session_type
,
179 media::EmeInitDataType init_data_type
,
180 const std::vector
<uint8
>& init_data
,
181 scoped_ptr
<media::NewSessionCdmPromise
> promise
,
182 bool permission_was_allowed
);
184 // Calls LoadSession() on the CDM if |permission_was_allowed| is true.
185 // Otherwise rejects |promise|.
186 void LoadSessionIfPermitted(int render_frame_id
,
188 media::MediaKeys::SessionType session_type
,
189 const std::string
& session_id
,
190 scoped_ptr
<media::NewSessionCdmPromise
> promise
,
191 bool permission_was_allowed
);
193 const int render_process_id_
;
195 // TaskRunner to dispatch all CDM messages to. If it's NULL, all messages are
196 // dispatched to the browser UI thread.
197 scoped_refptr
<base::TaskRunner
> task_runner_
;
199 // The key in the following maps is a combination of |render_frame_id| and
202 // Map of managed BrowserCdms.
203 typedef base::ScopedPtrHashMap
<uint64
, scoped_ptr
<media::BrowserCdm
>> CdmMap
;
206 // Map of CDM's security origin.
207 std::map
<uint64
, GURL
> cdm_security_origin_map_
;
209 base::WeakPtrFactory
<BrowserCdmManager
> weak_ptr_factory_
;
211 DISALLOW_COPY_AND_ASSIGN(BrowserCdmManager
);
214 } // namespace content
216 #endif // CONTENT_BROWSER_MEDIA_CDM_BROWSER_CDM_MANAGER_H_