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_enums.h"
19 #include "content/public/browser/browser_message_filter.h"
20 #include "content/public/common/permission_status.mojom.h"
21 #include "ipc/ipc_message.h"
22 #include "media/base/cdm_promise.h"
23 #include "media/base/eme_constants.h"
24 #include "media/base/media_keys.h"
33 // This class manages all CDM objects. It receives control operations from the
34 // the render process, and forwards them to corresponding CDM object. Callbacks
35 // from CDM objects are converted to IPCs and then sent to the render process.
36 class CONTENT_EXPORT BrowserCdmManager
: public BrowserMessageFilter
{
38 // Returns the BrowserCdmManager associated with the |render_process_id|.
39 // Returns NULL if no BrowserCdmManager is associated.
40 static BrowserCdmManager
* FromProcess(int render_process_id
);
42 // Constructs the BrowserCdmManager for |render_process_id| which runs on
44 // If |task_runner| is not NULL, all CDM messages are posted to it. Otherwise,
45 // all messages are posted to the browser UI thread.
46 BrowserCdmManager(int render_process_id
,
47 const scoped_refptr
<base::TaskRunner
>& task_runner
);
49 // BrowserMessageFilter implementations.
50 void OnDestruct() const override
;
51 base::TaskRunner
* OverrideTaskRunnerForMessage(
52 const IPC::Message
& message
) override
;
53 bool OnMessageReceived(const IPC::Message
& message
) override
;
55 media::BrowserCdm
* GetCdm(int render_frame_id
, int cdm_id
) const;
57 // Notifies that the render frame has been deleted so that all CDMs belongs
58 // to this render frame needs to be destroyed as well. This is needed because
59 // in some cases (e.g. fast termination of the renderer), the message to
60 // destroy the CDM will not be received.
61 void RenderFrameDeleted(int render_frame_id
);
64 void ResolvePromise(int render_frame_id
, int cdm_id
, uint32_t promise_id
);
65 void ResolvePromiseWithSession(int render_frame_id
,
68 const std::string
& session_id
);
69 void RejectPromise(int render_frame_id
,
72 media::MediaKeys::Exception exception
,
74 const std::string
& error_message
);
77 friend class base::RefCountedThreadSafe
<BrowserCdmManager
>;
78 friend class base::DeleteHelper
<BrowserCdmManager
>;
79 ~BrowserCdmManager() override
;
83 void OnSessionMessage(int render_frame_id
,
85 const std::string
& session_id
,
86 media::MediaKeys::MessageType message_type
,
87 const std::vector
<uint8
>& message
,
88 const GURL
& legacy_destination_url
);
89 void OnSessionClosed(int render_frame_id
,
91 const std::string
& session_id
);
92 void OnLegacySessionError(int render_frame_id
,
94 const std::string
& session_id
,
95 media::MediaKeys::Exception exception_code
,
97 const std::string
& error_message
);
98 void OnSessionKeysChange(int render_frame_id
,
100 const std::string
& session_id
,
101 bool has_additional_usable_key
,
102 media::CdmKeysInfo keys_info
);
103 void OnSessionExpirationUpdate(int render_frame_id
,
105 const std::string
& session_id
,
106 const base::Time
& new_expiry_time
);
109 void OnInitializeCdm(int render_frame_id
,
111 const std::string
& key_system
,
112 const GURL
& frame_url
);
113 void OnSetServerCertificate(int render_frame_id
,
116 const std::vector
<uint8_t>& certificate
);
117 void OnCreateSessionAndGenerateRequest(
121 CdmHostMsg_CreateSession_InitDataType init_data_type
,
122 const std::vector
<uint8
>& init_data
);
123 void OnUpdateSession(int render_frame_id
,
126 const std::string
& session_id
,
127 const std::vector
<uint8
>& response
);
128 void OnCloseSession(int render_frame_id
,
131 const std::string
& session_id
);
132 void OnDestroyCdm(int render_frame_id
, int cdm_id
);
134 // Adds a new CDM identified by |cdm_id| for the given |key_system| and
135 // |security_origin|.
136 void AddCdm(int render_frame_id
,
138 const std::string
& key_system
,
139 const GURL
& security_origin
);
141 // Removes all CDMs associated with |render_frame_id|.
142 void RemoveAllCdmForFrame(int render_frame_id
);
144 // Removes the CDM with the specified id.
145 void RemoveCdm(uint64 id
);
147 using PermissionStatusCB
= base::Callback
<void(bool)>;
149 // Checks protected media identifier permission for the given
150 // |render_frame_id| and |cdm_id|.
151 void CheckPermissionStatus(int render_frame_id
,
153 const PermissionStatusCB
& permission_status_cb
);
155 // Checks permission status on Browser UI thread. Runs |permission_status_cb|
156 // on the |task_runner_| with the permission status.
157 void CheckPermissionStatusOnUIThread(
159 const GURL
& security_origin
,
160 const PermissionStatusCB
& permission_status_cb
);
162 // Calls CreateSessionAndGenerateRequest() on the CDM if
163 // |permission_was_allowed| is true. Otherwise rejects the |promise|.
164 void CreateSessionAndGenerateRequestIfPermitted(
167 media::EmeInitDataType init_data_type
,
168 const std::vector
<uint8
>& init_data
,
169 scoped_ptr
<media::NewSessionCdmPromise
> promise
,
170 bool permission_was_allowed
);
172 const int render_process_id_
;
174 // TaskRunner to dispatch all CDM messages to. If it's NULL, all messages are
175 // dispatched to the browser UI thread.
176 scoped_refptr
<base::TaskRunner
> task_runner_
;
178 // The key in the following maps is a combination of |render_frame_id| and
181 // Map of managed BrowserCdms.
182 typedef base::ScopedPtrHashMap
<uint64
, scoped_ptr
<media::BrowserCdm
>> CdmMap
;
185 // Map of CDM's security origin.
186 std::map
<uint64
, GURL
> cdm_security_origin_map_
;
188 base::WeakPtrFactory
<BrowserCdmManager
> weak_ptr_factory_
;
190 DISALLOW_COPY_AND_ASSIGN(BrowserCdmManager
);
193 } // namespace content
195 #endif // CONTENT_BROWSER_MEDIA_CDM_BROWSER_CDM_MANAGER_H_