Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / media / cdm / browser_cdm_manager.h
blob629a49d84eff165ab6565d9de5f7334524144747
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_
8 #include <map>
9 #include <string>
10 #include <vector>
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"
26 #include "url/gurl.h"
28 struct CdmHostMsg_CreateSessionAndGenerateRequest_Params;
30 namespace media {
31 class BrowserCdm;
34 namespace content {
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 {
40 public:
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
46 // |task_runner|.
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);
66 // Promise handlers.
67 void ResolvePromise(int render_frame_id, int cdm_id, uint32_t promise_id);
68 void ResolvePromiseWithSession(int render_frame_id,
69 int cdm_id,
70 uint32_t promise_id,
71 const std::string& session_id);
72 void RejectPromise(int render_frame_id,
73 int cdm_id,
74 uint32_t promise_id,
75 media::MediaKeys::Exception exception,
76 uint32_t system_code,
77 const std::string& error_message);
79 protected:
80 friend class base::RefCountedThreadSafe<BrowserCdmManager>;
81 friend class base::DeleteHelper<BrowserCdmManager>;
82 ~BrowserCdmManager() override;
84 private:
85 // CDM callbacks.
86 void OnSessionMessage(int render_frame_id,
87 int cdm_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,
93 int cdm_id,
94 const std::string& session_id);
95 void OnLegacySessionError(int render_frame_id,
96 int cdm_id,
97 const std::string& session_id,
98 media::MediaKeys::Exception exception_code,
99 uint32_t system_code,
100 const std::string& error_message);
101 void OnSessionKeysChange(int render_frame_id,
102 int cdm_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,
107 int cdm_id,
108 const std::string& session_id,
109 const base::Time& new_expiry_time);
111 // Message handlers.
112 void OnInitializeCdm(int render_frame_id,
113 int cdm_id,
114 uint32_t promise_id,
115 const CdmHostMsg_InitializeCdm_Params& params);
116 void OnSetServerCertificate(int render_frame_id,
117 int cdm_id,
118 uint32_t promise_id,
119 const std::vector<uint8_t>& certificate);
120 void OnCreateSessionAndGenerateRequest(
121 const CdmHostMsg_CreateSessionAndGenerateRequest_Params& params);
122 void OnLoadSession(
123 int render_frame_id,
124 int cdm_id,
125 uint32_t promise_id,
126 media::MediaKeys::SessionType session_type,
127 const std::string& session_id);
128 void OnUpdateSession(int render_frame_id,
129 int cdm_id,
130 uint32_t promise_id,
131 const std::string& session_id,
132 const std::vector<uint8>& response);
133 void OnCloseSession(int render_frame_id,
134 int cdm_id,
135 uint32_t promise_id,
136 const std::string& session_id);
137 void OnRemoveSession(int render_frame_id,
138 int cdm_id,
139 uint32_t promise_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,
146 int cdm_id,
147 uint32_t promise_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,
163 int cdm_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(
169 int render_frame_id,
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(
176 int render_frame_id,
177 int cdm_id,
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,
187 int cdm_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
200 // |cdm_id|.
202 // Map of managed BrowserCdms.
203 typedef base::ScopedPtrHashMap<uint64, scoped_ptr<media::BrowserCdm>> CdmMap;
204 CdmMap cdm_map_;
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_