1 // Copyright 2013 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 #include "content/browser/media/android/media_drm_credential_manager.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/scoped_java_ref.h"
10 #include "base/callback_helpers.h"
11 #include "base/location.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "jni/MediaDrmCredentialManager_jni.h"
15 #include "media/base/android/media_drm_bridge.h"
18 #include "widevine_cdm_version.h" // In SHARED_INTERMEDIATE_DIR.
20 using base::android::ScopedJavaGlobalRef
;
24 void MediaDrmCredentialManagerCallback(
25 const ScopedJavaGlobalRef
<jobject
>& j_media_drm_credential_manager_callback
,
27 JNIEnv
* env
= base::android::AttachCurrentThread();
28 content::Java_MediaDrmCredentialManagerCallback_onCredentialResetFinished(
29 env
, j_media_drm_credential_manager_callback
.obj(), succeeded
);
36 MediaDrmCredentialManager::MediaDrmCredentialManager() {};
38 MediaDrmCredentialManager::~MediaDrmCredentialManager() {};
41 MediaDrmCredentialManager
* MediaDrmCredentialManager::GetInstance() {
42 return Singleton
<MediaDrmCredentialManager
>::get();
45 void MediaDrmCredentialManager::ResetCredentials(
46 const ResetCredentialsCB
& reset_credentials_cb
) {
47 // Ignore reset request if one is already in progress.
48 if (!reset_credentials_cb_
.is_null())
51 reset_credentials_cb_
= reset_credentials_cb
;
53 // First reset the L3 credential.
54 if (!ResetCredentialsInternal(media::MediaDrmBridge::SECURITY_LEVEL_3
)) {
55 // TODO(qinmin): We should post a task instead.
56 base::ResetAndReturn(&reset_credentials_cb_
).Run(false);
61 void ResetCredentials(
64 jobject j_media_drm_credential_manager_callback
) {
65 MediaDrmCredentialManager
* media_drm_credential_manager
=
66 MediaDrmCredentialManager::GetInstance();
68 ScopedJavaGlobalRef
<jobject
> j_scoped_media_drm_credential_manager_callback
;
69 j_scoped_media_drm_credential_manager_callback
.Reset(
70 env
, j_media_drm_credential_manager_callback
);
72 MediaDrmCredentialManager::ResetCredentialsCB callback_runner
=
73 base::Bind(&MediaDrmCredentialManagerCallback
,
74 j_scoped_media_drm_credential_manager_callback
);
76 media_drm_credential_manager
->ResetCredentials(callback_runner
);
79 void MediaDrmCredentialManager::OnResetCredentialsCompleted(
80 SecurityLevel security_level
, bool success
) {
81 if (security_level
== media::MediaDrmBridge::SECURITY_LEVEL_3
&& success
) {
82 if (ResetCredentialsInternal(media::MediaDrmBridge::SECURITY_LEVEL_1
))
87 base::ResetAndReturn(&reset_credentials_cb_
).Run(success
);
88 media_drm_bridge_
.reset();
91 // TODO(ddorwin): The key system should be passed in. http://crbug.com/459400
92 bool MediaDrmCredentialManager::ResetCredentialsInternal(
93 SecurityLevel security_level
) {
95 media::MediaDrmBridge::CreateWithoutSessionSupport(kWidevineKeySystem
);
96 if (!media_drm_bridge_
)
99 ResetCredentialsCB reset_credentials_cb
=
100 base::Bind(&MediaDrmCredentialManager::OnResetCredentialsCompleted
,
101 base::Unretained(this), security_level
);
103 if (!media_drm_bridge_
->SetSecurityLevel(security_level
)) {
104 // No need to reset credentials for unsupported |security_level|.
105 base::ThreadTaskRunnerHandle::Get()->PostTask(
106 FROM_HERE
, base::Bind(reset_credentials_cb
, true));
110 media_drm_bridge_
->ResetDeviceCredentials(reset_credentials_cb
);
115 bool MediaDrmCredentialManager::RegisterMediaDrmCredentialManager(JNIEnv
* env
) {
116 return RegisterNativesImpl(env
);
119 } // namespace content