Home | History | Annotate | Download | only in media
      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/renderer/media/webcontentdecryptionmodule_impl.h"
      6 
      7 #include <map>
      8 #include <vector>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/bind.h"
     12 #include "base/logging.h"
     13 #include "base/strings/string_util.h"
     14 #include "base/strings/utf_string_conversions.h"
     15 #include "content/renderer/media/cdm_session_adapter.h"
     16 #include "content/renderer/media/crypto/key_systems.h"
     17 #include "content/renderer/media/webcontentdecryptionmodulesession_impl.h"
     18 #include "media/base/media_keys.h"
     19 #include "third_party/WebKit/public/platform/WebString.h"
     20 #include "third_party/WebKit/public/web/WebSecurityOrigin.h"
     21 #include "url/gurl.h"
     22 
     23 #if defined(ENABLE_PEPPER_CDMS)
     24 #include "content/renderer/media/crypto/pepper_cdm_wrapper_impl.h"
     25 #endif
     26 
     27 namespace content {
     28 
     29 WebContentDecryptionModuleImpl* WebContentDecryptionModuleImpl::Create(
     30 #if defined(ENABLE_PEPPER_CDMS)
     31     blink::WebLocalFrame* frame,
     32 #elif defined(ENABLE_BROWSER_CDMS)
     33     RendererCdmManager* manager,
     34 #endif
     35     const blink::WebSecurityOrigin& security_origin,
     36     const base::string16& key_system) {
     37 #if defined(ENABLE_PEPPER_CDMS)
     38   DCHECK(frame);
     39 #elif defined(ENABLE_BROWSER_CDMS)
     40   DCHECK(manager);
     41 #endif
     42   DCHECK(!security_origin.isNull());
     43   DCHECK(!key_system.empty());
     44 
     45   // TODO(ddorwin): Guard against this in supported types check and remove this.
     46   // Chromium only supports ASCII key systems.
     47   if (!base::IsStringASCII(key_system)) {
     48     NOTREACHED();
     49     return NULL;
     50   }
     51 
     52   std::string key_system_ascii = base::UTF16ToASCII(key_system);
     53   if (!IsConcreteSupportedKeySystem(key_system_ascii))
     54     return NULL;
     55 
     56   // If unique security origin, don't try to create the CDM.
     57   if (security_origin.isUnique() || security_origin.toString() == "null") {
     58     DLOG(ERROR) << "CDM use not allowed for unique security origin.";
     59     return NULL;
     60   }
     61 
     62   scoped_refptr<CdmSessionAdapter> adapter(new CdmSessionAdapter());
     63   GURL security_origin_as_gurl(security_origin.toString());
     64 
     65   if (!adapter->Initialize(
     66 #if defined(ENABLE_PEPPER_CDMS)
     67           base::Bind(&PepperCdmWrapperImpl::Create, frame),
     68 #elif defined(ENABLE_BROWSER_CDMS)
     69           manager,
     70 #endif
     71           key_system_ascii,
     72           security_origin_as_gurl)) {
     73     return NULL;
     74   }
     75 
     76   return new WebContentDecryptionModuleImpl(adapter);
     77 }
     78 
     79 WebContentDecryptionModuleImpl::WebContentDecryptionModuleImpl(
     80     scoped_refptr<CdmSessionAdapter> adapter)
     81     : adapter_(adapter) {}
     82 
     83 WebContentDecryptionModuleImpl::~WebContentDecryptionModuleImpl() {
     84 }
     85 
     86 // The caller owns the created session.
     87 blink::WebContentDecryptionModuleSession*
     88 WebContentDecryptionModuleImpl::createSession(
     89     blink::WebContentDecryptionModuleSession::Client* client) {
     90   return adapter_->CreateSession(client);
     91 }
     92 
     93 media::Decryptor* WebContentDecryptionModuleImpl::GetDecryptor() {
     94   return adapter_->GetDecryptor();
     95 }
     96 
     97 #if defined(ENABLE_BROWSER_CDMS)
     98 int WebContentDecryptionModuleImpl::GetCdmId() const {
     99   return adapter_->GetCdmId();
    100 }
    101 #endif  // defined(ENABLE_BROWSER_CDMS)
    102 
    103 }  // namespace content
    104