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_SERVICE_H_
6 #define MEDIA_MOJO_SERVICES_MOJO_CDM_SERVICE_H_
8 #include "base/callback.h"
9 #include "base/macros.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "media/base/media_keys.h"
13 #include "media/mojo/interfaces/content_decryption_module.mojom.h"
14 #include "media/mojo/services/mojo_cdm_promise.h"
15 #include "media/mojo/services/mojo_cdm_service_context.h"
16 #include "mojo/application/public/interfaces/service_provider.mojom.h"
17 #include "mojo/public/cpp/bindings/strong_binding.h"
23 // A interfaces::ContentDecryptionModule implementation backed by a
25 class MojoCdmService
: public interfaces::ContentDecryptionModule
{
27 // Constructs a MojoCdmService and strongly binds it to the |request|.
29 base::WeakPtr
<MojoCdmServiceContext
> context
,
30 mojo::ServiceProvider
* service_provider
,
31 CdmFactory
* cdm_factory
,
32 mojo::InterfaceRequest
<interfaces::ContentDecryptionModule
> request
);
34 ~MojoCdmService() final
;
36 // interfaces::ContentDecryptionModule implementation.
37 void SetClient(interfaces::ContentDecryptionModuleClientPtr client
) final
;
38 void Initialize(const mojo::String
& key_system
,
39 const mojo::String
& security_origin
,
40 interfaces::CdmConfigPtr cdm_config
,
42 const mojo::Callback
<void(interfaces::CdmPromiseResultPtr
)>&
44 void SetServerCertificate(
45 mojo::Array
<uint8_t> certificate_data
,
46 const mojo::Callback
<void(interfaces::CdmPromiseResultPtr
)>& callback
)
48 void CreateSessionAndGenerateRequest(
49 interfaces::ContentDecryptionModule::SessionType session_type
,
50 interfaces::ContentDecryptionModule::InitDataType init_data_type
,
51 mojo::Array
<uint8_t> init_data
,
52 const mojo::Callback
<void(interfaces::CdmPromiseResultPtr
, mojo::String
)>&
55 interfaces::ContentDecryptionModule::SessionType session_type
,
56 const mojo::String
& session_id
,
57 const mojo::Callback
<void(interfaces::CdmPromiseResultPtr
, mojo::String
)>&
60 const mojo::String
& session_id
,
61 mojo::Array
<uint8_t> response
,
62 const mojo::Callback
<void(interfaces::CdmPromiseResultPtr
)>& callback
)
64 void CloseSession(const mojo::String
& session_id
,
65 const mojo::Callback
<void(interfaces::CdmPromiseResultPtr
)>&
68 const mojo::String
& session_id
,
69 const mojo::Callback
<void(interfaces::CdmPromiseResultPtr
)>& callback
)
72 mojo::InterfaceRequest
<interfaces::Decryptor
> decryptor
) final
;
74 // Get CdmContext to be used by the media pipeline.
75 CdmContext
* GetCdmContext();
78 // Callback for CdmFactory::Create().
79 void OnCdmCreated(int cdm_id
,
80 scoped_ptr
<MojoCdmPromise
<>> promise
,
81 scoped_ptr
<MediaKeys
> cdm
,
82 const std::string
& error_message
);
84 // Callbacks for firing session events.
85 void OnSessionMessage(const std::string
& session_id
,
86 MediaKeys::MessageType message_type
,
87 const std::vector
<uint8_t>& message
,
88 const GURL
& legacy_destination_url
);
89 void OnSessionKeysChange(const std::string
& session_id
,
90 bool has_additional_usable_key
,
91 CdmKeysInfo keys_info
);
92 void OnSessionExpirationUpdate(const std::string
& session_id
,
93 const base::Time
& new_expiry_time
);
94 void OnSessionClosed(const std::string
& session_id
);
95 void OnLegacySessionError(const std::string
& session_id
,
96 MediaKeys::Exception exception
,
98 const std::string
& error_message
);
100 mojo::StrongBinding
<interfaces::ContentDecryptionModule
> binding_
;
101 base::WeakPtr
<MojoCdmServiceContext
> context_
;
103 mojo::ServiceProvider
* service_provider_
;
104 CdmFactory
* cdm_factory_
;
105 scoped_ptr
<MediaKeys
> cdm_
;
107 // Set to a valid CDM ID if the |cdm_| is successfully created.
110 interfaces::ContentDecryptionModuleClientPtr client_
;
112 base::WeakPtr
<MojoCdmService
> weak_this_
;
113 base::WeakPtrFactory
<MojoCdmService
> weak_factory_
;
115 DISALLOW_COPY_AND_ASSIGN(MojoCdmService
);
120 #endif // MEDIA_MOJO_SERVICES_MOJO_CDM_SERVICE_H_