IndexedDBFactory now ForceCloses databases.
[chromium-blink-merge.git] / content / browser / media / android / media_drm_credential_manager.cc
blobe3e7afb8b879cdb9b46594ea486238cf96723aba
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 using base::android::ScopedJavaGlobalRef;
19 namespace {
21 void MediaDrmCredentialManagerCallback(
22 const ScopedJavaGlobalRef<jobject>& j_media_drm_credential_manager_callback,
23 bool succeeded) {
24 JNIEnv* env = base::android::AttachCurrentThread();
25 content::Java_MediaDrmCredentialManagerCallback_onCredentialResetFinished(
26 env, j_media_drm_credential_manager_callback.obj(), succeeded);
29 } // namespace
31 namespace content {
33 // TODO(qinmin): Move the UUID definition to some common places.
34 static const uint8 kWidevineUuid[16] = {
35 0xED, 0xEF, 0x8B, 0xA9, 0x79, 0xD6, 0x4A, 0xCE,
36 0xA3, 0xC8, 0x27, 0xDC, 0xD5, 0x1D, 0x21, 0xED };
38 MediaDrmCredentialManager::MediaDrmCredentialManager() {};
40 MediaDrmCredentialManager::~MediaDrmCredentialManager() {};
42 // static
43 MediaDrmCredentialManager* MediaDrmCredentialManager::GetInstance() {
44 return Singleton<MediaDrmCredentialManager>::get();
47 void MediaDrmCredentialManager::ResetCredentials(
48 const ResetCredentialsCB& reset_credentials_cb) {
49 // Ignore reset request if one is already in progress.
50 if (!reset_credentials_cb_.is_null())
51 return;
53 reset_credentials_cb_ = reset_credentials_cb;
55 // First reset the L3 credential.
56 if (!ResetCredentialsInternal(media::MediaDrmBridge::SECURITY_LEVEL_3)) {
57 // TODO(qinmin): We should post a task instead.
58 base::ResetAndReturn(&reset_credentials_cb_).Run(false);
62 // static
63 void ResetCredentials(
64 JNIEnv* env,
65 jclass clazz,
66 jobject j_media_drm_credential_manager_callback) {
67 MediaDrmCredentialManager* media_drm_credential_manager =
68 MediaDrmCredentialManager::GetInstance();
70 ScopedJavaGlobalRef<jobject> j_scoped_media_drm_credential_manager_callback;
71 j_scoped_media_drm_credential_manager_callback.Reset(
72 env, j_media_drm_credential_manager_callback);
74 MediaDrmCredentialManager::ResetCredentialsCB callback_runner =
75 base::Bind(&MediaDrmCredentialManagerCallback,
76 j_scoped_media_drm_credential_manager_callback);
78 media_drm_credential_manager->ResetCredentials(callback_runner);
81 void MediaDrmCredentialManager::OnResetCredentialsCompleted(
82 SecurityLevel security_level, bool success) {
83 if (security_level == media::MediaDrmBridge::SECURITY_LEVEL_3 && success) {
84 if (ResetCredentialsInternal(media::MediaDrmBridge::SECURITY_LEVEL_1))
85 return;
86 success = false;
89 base::ResetAndReturn(&reset_credentials_cb_).Run(success);
90 media_drm_bridge_.reset();
93 bool MediaDrmCredentialManager::ResetCredentialsInternal(
94 SecurityLevel security_level) {
95 std::vector<uint8> uuid(kWidevineUuid, kWidevineUuid + 16);
96 media_drm_bridge_ = media::MediaDrmBridge::Create(0, uuid, GURL(), NULL);
97 if (!media_drm_bridge_)
98 return false;
100 ResetCredentialsCB reset_credentials_cb =
101 base::Bind(&MediaDrmCredentialManager::OnResetCredentialsCompleted,
102 base::Unretained(this), security_level);
104 if (!media_drm_bridge_->SetSecurityLevel(security_level)) {
105 // No need to reset credentials for unsupported |security_level|.
106 base::MessageLoopProxy::current()->PostTask(
107 FROM_HERE, base::Bind(reset_credentials_cb, true));
108 return true;
111 media_drm_bridge_->ResetDeviceCredentials(reset_credentials_cb);
112 return true;
115 // static
116 bool MediaDrmCredentialManager::RegisterMediaDrmCredentialManager(JNIEnv* env) {
117 return RegisterNativesImpl(env);
120 } // namespace content