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. 4 5 #include "content/browser/media/android/media_drm_credential_manager.h" 6 7 #include "base/android/jni_android.h" 8 #include "base/android/scoped_java_ref.h" 9 #include "base/bind.h" 10 #include "base/callback_helpers.h" 11 #include "base/location.h" 12 #include "base/message_loop/message_loop_proxy.h" 13 #include "jni/MediaDrmCredentialManager_jni.h" 14 #include "media/base/android/media_drm_bridge.h" 15 #include "url/gurl.h" 16 17 #include "widevine_cdm_version.h" // In SHARED_INTERMEDIATE_DIR. 18 19 using base::android::ScopedJavaGlobalRef; 20 21 namespace { 22 23 void MediaDrmCredentialManagerCallback( 24 const ScopedJavaGlobalRef<jobject>& j_media_drm_credential_manager_callback, 25 bool succeeded) { 26 JNIEnv* env = base::android::AttachCurrentThread(); 27 content::Java_MediaDrmCredentialManagerCallback_onCredentialResetFinished( 28 env, j_media_drm_credential_manager_callback.obj(), succeeded); 29 } 30 31 } // namespace 32 33 namespace content { 34 35 MediaDrmCredentialManager::MediaDrmCredentialManager() {}; 36 37 MediaDrmCredentialManager::~MediaDrmCredentialManager() {}; 38 39 // static 40 MediaDrmCredentialManager* MediaDrmCredentialManager::GetInstance() { 41 return Singleton<MediaDrmCredentialManager>::get(); 42 } 43 44 void MediaDrmCredentialManager::ResetCredentials( 45 const ResetCredentialsCB& reset_credentials_cb) { 46 // Ignore reset request if one is already in progress. 47 if (!reset_credentials_cb_.is_null()) 48 return; 49 50 reset_credentials_cb_ = reset_credentials_cb; 51 52 // First reset the L3 credential. 53 if (!ResetCredentialsInternal(media::MediaDrmBridge::SECURITY_LEVEL_3)) { 54 // TODO(qinmin): We should post a task instead. 55 base::ResetAndReturn(&reset_credentials_cb_).Run(false); 56 } 57 } 58 59 // static 60 void ResetCredentials( 61 JNIEnv* env, 62 jclass clazz, 63 jobject j_media_drm_credential_manager_callback) { 64 MediaDrmCredentialManager* media_drm_credential_manager = 65 MediaDrmCredentialManager::GetInstance(); 66 67 ScopedJavaGlobalRef<jobject> j_scoped_media_drm_credential_manager_callback; 68 j_scoped_media_drm_credential_manager_callback.Reset( 69 env, j_media_drm_credential_manager_callback); 70 71 MediaDrmCredentialManager::ResetCredentialsCB callback_runner = 72 base::Bind(&MediaDrmCredentialManagerCallback, 73 j_scoped_media_drm_credential_manager_callback); 74 75 media_drm_credential_manager->ResetCredentials(callback_runner); 76 } 77 78 void MediaDrmCredentialManager::OnResetCredentialsCompleted( 79 SecurityLevel security_level, bool success) { 80 if (security_level == media::MediaDrmBridge::SECURITY_LEVEL_3 && success) { 81 if (ResetCredentialsInternal(media::MediaDrmBridge::SECURITY_LEVEL_1)) 82 return; 83 success = false; 84 } 85 86 base::ResetAndReturn(&reset_credentials_cb_).Run(success); 87 media_drm_bridge_.reset(); 88 } 89 90 bool MediaDrmCredentialManager::ResetCredentialsInternal( 91 SecurityLevel security_level) { 92 media_drm_bridge_ = 93 media::MediaDrmBridge::CreateSessionless(kWidevineKeySystem); 94 if (!media_drm_bridge_) 95 return false; 96 97 ResetCredentialsCB reset_credentials_cb = 98 base::Bind(&MediaDrmCredentialManager::OnResetCredentialsCompleted, 99 base::Unretained(this), security_level); 100 101 if (!media_drm_bridge_->SetSecurityLevel(security_level)) { 102 // No need to reset credentials for unsupported |security_level|. 103 base::MessageLoopProxy::current()->PostTask( 104 FROM_HERE, base::Bind(reset_credentials_cb, true)); 105 return true; 106 } 107 108 media_drm_bridge_->ResetDeviceCredentials(reset_credentials_cb); 109 return true; 110 } 111 112 // static 113 bool MediaDrmCredentialManager::RegisterMediaDrmCredentialManager(JNIEnv* env) { 114 return RegisterNativesImpl(env); 115 } 116 117 } // namespace content 118