Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / media / mojo / services / mojo_cdm.h
blobea4e8a4729edaf944130427637e901a2e8390081
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"
18 namespace mojo {
19 class ServiceProvider;
22 namespace media {
24 // A MediaKeys that proxies to a interfaces::ContentDecryptionModule. That
25 // interfaces::ContentDecryptionModule proxies back to the MojoCdm via the
26 // interfaces::ContentDecryptionModuleClient interface.
27 class MojoCdm : public MediaKeys,
28 public CdmContext,
29 public interfaces::ContentDecryptionModuleClient {
30 public:
31 static void Create(
32 const std::string& key_system,
33 const GURL& security_origin,
34 const media::CdmConfig& cdm_config,
35 interfaces::ContentDecryptionModulePtr remote_cdm,
36 const media::SessionMessageCB& session_message_cb,
37 const media::SessionClosedCB& session_closed_cb,
38 const media::LegacySessionErrorCB& legacy_session_error_cb,
39 const media::SessionKeysChangeCB& session_keys_change_cb,
40 const media::SessionExpirationUpdateCB& session_expiration_update_cb,
41 const media::CdmCreatedCB& cdm_created_cb);
43 ~MojoCdm() final;
45 // MediaKeys implementation.
46 void SetServerCertificate(const std::vector<uint8_t>& certificate,
47 scoped_ptr<SimpleCdmPromise> promise) final;
48 void CreateSessionAndGenerateRequest(
49 SessionType session_type,
50 EmeInitDataType init_data_type,
51 const std::vector<uint8_t>& init_data,
52 scoped_ptr<NewSessionCdmPromise> promise) final;
53 void LoadSession(SessionType session_type,
54 const std::string& session_id,
55 scoped_ptr<NewSessionCdmPromise> promise) final;
56 void UpdateSession(const std::string& session_id,
57 const std::vector<uint8_t>& response,
58 scoped_ptr<SimpleCdmPromise> promise) final;
59 void CloseSession(const std::string& session_id,
60 scoped_ptr<SimpleCdmPromise> promise) final;
61 void RemoveSession(const std::string& session_id,
62 scoped_ptr<SimpleCdmPromise> promise) final;
63 CdmContext* GetCdmContext() final;
65 // CdmContext implementation.
66 media::Decryptor* GetDecryptor() final;
67 int GetCdmId() const final;
69 private:
70 MojoCdm(interfaces::ContentDecryptionModulePtr remote_cdm,
71 const SessionMessageCB& session_message_cb,
72 const SessionClosedCB& session_closed_cb,
73 const LegacySessionErrorCB& legacy_session_error_cb,
74 const SessionKeysChangeCB& session_keys_change_cb,
75 const SessionExpirationUpdateCB& session_expiration_update_cb);
77 void InitializeCdm(const std::string& key_system,
78 const GURL& security_origin,
79 const media::CdmConfig& cdm_config,
80 scoped_ptr<CdmInitializedPromise> promise);
82 // interfaces::ContentDecryptionModuleClient implementation.
83 void OnSessionMessage(const mojo::String& session_id,
84 interfaces::CdmMessageType message_type,
85 mojo::Array<uint8_t> message,
86 const mojo::String& legacy_destination_url) final;
87 void OnSessionClosed(const mojo::String& session_id) final;
88 void OnLegacySessionError(const mojo::String& session_id,
89 interfaces::CdmException exception,
90 uint32_t system_code,
91 const mojo::String& error_message) final;
92 void OnSessionKeysChange(
93 const mojo::String& session_id,
94 bool has_additional_usable_key,
95 mojo::Array<interfaces::CdmKeyInformationPtr> keys_info) final;
96 void OnSessionExpirationUpdate(const mojo::String& session_id,
97 double new_expiry_time_sec) final;
99 // Callbacks to handle CDM promises.
100 // We have to inline this method, since MS VS 2013 compiler fails to compile
101 // it when this method is not inlined. It fails with error C2244
102 // "unable to match function definition to an existing declaration".
103 template <typename... T>
104 void OnPromiseResult(scoped_ptr<CdmPromiseTemplate<T...>> promise,
105 interfaces::CdmPromiseResultPtr result,
106 typename MojoTypeTrait<T>::MojoType... args) {
107 if (result->success)
108 promise->resolve(args.template To<T>()...); // See ISO C++03 14.2/4.
109 else
110 RejectPromise(promise.Pass(), result.Pass());
113 static int next_cdm_id_;
115 interfaces::ContentDecryptionModulePtr remote_cdm_;
116 mojo::Binding<ContentDecryptionModuleClient> binding_;
117 int cdm_id_;
119 // Callbacks for firing session events.
120 SessionMessageCB session_message_cb_;
121 SessionClosedCB session_closed_cb_;
122 LegacySessionErrorCB legacy_session_error_cb_;
123 SessionKeysChangeCB session_keys_change_cb_;
124 SessionExpirationUpdateCB session_expiration_update_cb_;
126 base::WeakPtrFactory<MojoCdm> weak_factory_;
128 DISALLOW_COPY_AND_ASSIGN(MojoCdm);
131 } // namespace media
133 #endif // MEDIA_MOJO_SERVICES_MOJO_CDM_H_