Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / content / browser / media / android / media_drm_credential_manager.cc
blobe419b04f4344869aebcc1ed4dc8eb8ac55532c68
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"
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"
17 #include "widevine_cdm_version.h" // In SHARED_INTERMEDIATE_DIR.
19 using base::android::ScopedJavaGlobalRef;
21 namespace {
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);
31 } // namespace
33 namespace content {
35 MediaDrmCredentialManager::MediaDrmCredentialManager() {};
37 MediaDrmCredentialManager::~MediaDrmCredentialManager() {};
39 // static
40 MediaDrmCredentialManager* MediaDrmCredentialManager::GetInstance() {
41 return Singleton<MediaDrmCredentialManager>::get();
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;
50 reset_credentials_cb_ = reset_credentials_cb;
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);
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();
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);
71 MediaDrmCredentialManager::ResetCredentialsCB callback_runner =
72 base::Bind(&MediaDrmCredentialManagerCallback,
73 j_scoped_media_drm_credential_manager_callback);
75 media_drm_credential_manager->ResetCredentials(callback_runner);
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;
86 base::ResetAndReturn(&reset_credentials_cb_).Run(success);
87 media_drm_bridge_.reset();
90 // TODO(ddorwin): The key system should be passed in. http://crbug.com/459400
91 bool MediaDrmCredentialManager::ResetCredentialsInternal(
92 SecurityLevel security_level) {
93 media_drm_bridge_ =
94 media::MediaDrmBridge::CreateWithoutSessionSupport(kWidevineKeySystem);
95 if (!media_drm_bridge_)
96 return false;
98 ResetCredentialsCB reset_credentials_cb =
99 base::Bind(&MediaDrmCredentialManager::OnResetCredentialsCompleted,
100 base::Unretained(this), security_level);
102 if (!media_drm_bridge_->SetSecurityLevel(security_level)) {
103 // No need to reset credentials for unsupported |security_level|.
104 base::MessageLoopProxy::current()->PostTask(
105 FROM_HERE, base::Bind(reset_credentials_cb, true));
106 return true;
109 media_drm_bridge_->ResetDeviceCredentials(reset_credentials_cb);
110 return true;
113 // static
114 bool MediaDrmCredentialManager::RegisterMediaDrmCredentialManager(JNIEnv* env) {
115 return RegisterNativesImpl(env);
118 } // namespace content