Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / media / mojo / services / mojo_cdm.h
blob96b4532ba1e0955990bd787e655634979d21d84f
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 MEDIA_MOJO_SERVICES_MOJO_CDM_H_
6 #define MEDIA_MOJO_SERVICES_MOJO_CDM_H_
8 #include <vector>
10 #include "base/macros.h"
11 #include "base/memory/weak_ptr.h"
12 #include "media/base/cdm_context.h"
13 #include "media/base/cdm_initialized_promise.h"
14 #include "media/base/media_keys.h"
15 #include "media/mojo/interfaces/content_decryption_module.mojom.h"
16 #include "media/mojo/services/mojo_type_trait.h"
17 #include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h"
19 namespace mojo {
20 class ServiceProvider;
23 namespace media {
25 // A MediaKeys that proxies to a interfaces::ContentDecryptionModule. That
26 // interfaces::ContentDecryptionModule proxies back to the MojoCdm via the
27 // interfaces::ContentDecryptionModuleClient interface.
28 class MojoCdm : public MediaKeys,
29 public CdmContext,
30 public interfaces::ContentDecryptionModuleClient {
31 public:
32 static void Create(
33 const std::string& key_system,
34 const GURL& security_origin,
35 const media::CdmConfig& cdm_config,
36 interfaces::ContentDecryptionModulePtr remote_cdm,
37 const media::SessionMessageCB& session_message_cb,
38 const media::SessionClosedCB& session_closed_cb,
39 const media::LegacySessionErrorCB& legacy_session_error_cb,
40 const media::SessionKeysChangeCB& session_keys_change_cb,
41 const media::SessionExpirationUpdateCB& session_expiration_update_cb,
42 const media::CdmCreatedCB& cdm_created_cb);
44 ~MojoCdm() final;
46 // MediaKeys implementation.
47 void SetServerCertificate(const std::vector<uint8_t>& certificate,
48 scoped_ptr<SimpleCdmPromise> promise) final;
49 void CreateSessionAndGenerateRequest(
50 SessionType session_type,
51 EmeInitDataType init_data_type,
52 const std::vector<uint8_t>& init_data,
53 scoped_ptr<NewSessionCdmPromise> promise) final;
54 void LoadSession(SessionType session_type,
55 const std::string& session_id,
56 scoped_ptr<NewSessionCdmPromise> promise) final;
57 void UpdateSession(const std::string& session_id,
58 const std::vector<uint8_t>& response,
59 scoped_ptr<SimpleCdmPromise> promise) final;
60 void CloseSession(const std::string& session_id,
61 scoped_ptr<SimpleCdmPromise> promise) final;
62 void RemoveSession(const std::string& session_id,
63 scoped_ptr<SimpleCdmPromise> promise) final;
64 CdmContext* GetCdmContext() final;
66 // CdmContext implementation.
67 media::Decryptor* GetDecryptor() final;
68 int GetCdmId() const final;
70 private:
71 MojoCdm(interfaces::ContentDecryptionModulePtr remote_cdm,
72 const SessionMessageCB& session_message_cb,
73 const SessionClosedCB& session_closed_cb,
74 const LegacySessionErrorCB& legacy_session_error_cb,
75 const SessionKeysChangeCB& session_keys_change_cb,
76 const SessionExpirationUpdateCB& session_expiration_update_cb);
78 void InitializeCdm(const std::string& key_system,
79 const GURL& security_origin,
80 const media::CdmConfig& cdm_config,
81 scoped_ptr<CdmInitializedPromise> promise);
83 // interfaces::ContentDecryptionModuleClient implementation.
84 void OnSessionMessage(const mojo::String& session_id,
85 interfaces::CdmMessageType message_type,
86 mojo::Array<uint8_t> message,
87 const mojo::String& legacy_destination_url) final;
88 void OnSessionClosed(const mojo::String& session_id) final;
89 void OnLegacySessionError(const mojo::String& session_id,
90 interfaces::CdmException exception,
91 uint32_t system_code,
92 const mojo::String& error_message) final;
93 void OnSessionKeysChange(
94 const mojo::String& session_id,
95 bool has_additional_usable_key,
96 mojo::Array<interfaces::CdmKeyInformationPtr> keys_info) final;
97 void OnSessionExpirationUpdate(const mojo::String& session_id,
98 double new_expiry_time_sec) final;
100 // Callbacks to handle CDM promises.
101 // We have to inline this method, since MS VS 2013 compiler fails to compile
102 // it when this method is not inlined. It fails with error C2244
103 // "unable to match function definition to an existing declaration".
104 template <typename... T>
105 void OnPromiseResult(scoped_ptr<CdmPromiseTemplate<T...>> promise,
106 interfaces::CdmPromiseResultPtr result,
107 typename MojoTypeTrait<T>::MojoType... args) {
108 if (result->success)
109 promise->resolve(args.template To<T>()...); // See ISO C++03 14.2/4.
110 else
111 RejectPromise(promise.Pass(), result.Pass());
114 static int next_cdm_id_;
116 interfaces::ContentDecryptionModulePtr remote_cdm_;
117 mojo::Binding<ContentDecryptionModuleClient> binding_;
118 int cdm_id_;
120 // Callbacks for firing session events.
121 SessionMessageCB session_message_cb_;
122 SessionClosedCB session_closed_cb_;
123 LegacySessionErrorCB legacy_session_error_cb_;
124 SessionKeysChangeCB session_keys_change_cb_;
125 SessionExpirationUpdateCB session_expiration_update_cb_;
127 base::WeakPtrFactory<MojoCdm> weak_factory_;
129 DISALLOW_COPY_AND_ASSIGN(MojoCdm);
132 } // namespace media
134 #endif // MEDIA_MOJO_SERVICES_MOJO_CDM_H_