fa64ee7c238538f164afd688feaf4f007b4f1e3a
[platform/framework/web/crosswalk.git] / src / content / renderer / media / webcontentdecryptionmodule_impl.cc
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 blink {
28 class WebFrame;
29 }
30
31 namespace content {
32
33 WebContentDecryptionModuleImpl* WebContentDecryptionModuleImpl::Create(
34     blink::WebLocalFrame* frame,
35     const blink::WebSecurityOrigin& security_origin,
36     const base::string16& key_system) {
37   DCHECK(frame);
38   DCHECK(!security_origin.isNull());
39   DCHECK(!key_system.empty());
40
41   // TODO(ddorwin): Guard against this in supported types check and remove this.
42   // Chromium only supports ASCII key systems.
43   if (!base::IsStringASCII(key_system)) {
44     NOTREACHED();
45     return NULL;
46   }
47
48   std::string key_system_ascii = base::UTF16ToASCII(key_system);
49   if (!IsConcreteSupportedKeySystem(key_system_ascii))
50     return NULL;
51
52   // If unique security origin, don't try to create the CDM.
53   if (security_origin.isUnique() || security_origin.toString() == "null") {
54     DLOG(ERROR) << "CDM use not allowed for unique security origin.";
55     return NULL;
56   }
57
58   scoped_refptr<CdmSessionAdapter> adapter(new CdmSessionAdapter());
59   GURL security_origin_as_gurl(security_origin.toString());
60
61   if (!adapter->Initialize(
62 #if defined(ENABLE_PEPPER_CDMS)
63           base::Bind(&PepperCdmWrapperImpl::Create, frame),
64 #endif
65           key_system_ascii,
66           security_origin_as_gurl)) {
67     return NULL;
68   }
69
70   return new WebContentDecryptionModuleImpl(adapter);
71 }
72
73 WebContentDecryptionModuleImpl::WebContentDecryptionModuleImpl(
74     scoped_refptr<CdmSessionAdapter> adapter)
75     : adapter_(adapter) {}
76
77 WebContentDecryptionModuleImpl::~WebContentDecryptionModuleImpl() {
78 }
79
80 // The caller owns the created session.
81 blink::WebContentDecryptionModuleSession*
82 WebContentDecryptionModuleImpl::createSession(
83     blink::WebContentDecryptionModuleSession::Client* client) {
84   return adapter_->CreateSession(client);
85 }
86
87 media::Decryptor* WebContentDecryptionModuleImpl::GetDecryptor() {
88   return adapter_->GetDecryptor();
89 }
90
91 #if defined(OS_ANDROID)
92 int WebContentDecryptionModuleImpl::GetCdmId() const {
93   return adapter_->GetCdmId();
94 }
95 #endif  // defined(OS_ANDROID)
96
97 }  // namespace content