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/webcontentdecryptionmodulesession_impl.h"
      6 
      7 #include "base/callback_helpers.h"
      8 #include "base/logging.h"
      9 #include "base/strings/string_util.h"
     10 #include "third_party/WebKit/public/platform/WebURL.h"
     11 
     12 namespace content {
     13 
     14 WebContentDecryptionModuleSessionImpl::WebContentDecryptionModuleSessionImpl(
     15     media::MediaKeys* media_keys,
     16     Client* client,
     17     const SessionClosedCB& session_closed_cb)
     18     : media_keys_(media_keys),
     19       client_(client),
     20       session_closed_cb_(session_closed_cb) {
     21   DCHECK(media_keys_);
     22   // TODO(ddorwin): Populate session_id_ from the real implementation.
     23 }
     24 
     25 WebContentDecryptionModuleSessionImpl::
     26 ~WebContentDecryptionModuleSessionImpl() {
     27 }
     28 
     29 WebKit::WebString WebContentDecryptionModuleSessionImpl::sessionId() const {
     30   return WebKit::WebString::fromUTF8(session_id_);
     31 }
     32 
     33 void WebContentDecryptionModuleSessionImpl::generateKeyRequest(
     34     const WebKit::WebString& mime_type,
     35     const uint8* init_data, size_t init_data_length) {
     36   // TODO(ddorwin): Guard against this in supported types check and remove this.
     37   // Chromium only supports ASCII MIME types.
     38   if (!IsStringASCII(mime_type)) {
     39     NOTREACHED();
     40     KeyError(media::MediaKeys::kUnknownError, 0);
     41     return;
     42   }
     43 
     44   media_keys_->GenerateKeyRequest(UTF16ToASCII(mime_type),
     45                                   init_data, init_data_length);
     46 }
     47 
     48 void WebContentDecryptionModuleSessionImpl::update(const uint8* key,
     49                                                    size_t key_length) {
     50   DCHECK(key);
     51   media_keys_->AddKey(key, key_length, NULL, 0, session_id_);
     52 }
     53 
     54 void WebContentDecryptionModuleSessionImpl::close() {
     55   media_keys_->CancelKeyRequest(session_id_);
     56 
     57   // Detach from the CDM.
     58   if (!session_closed_cb_.is_null())
     59     base::ResetAndReturn(&session_closed_cb_).Run(session_id_);
     60 }
     61 
     62 void WebContentDecryptionModuleSessionImpl::KeyAdded() {
     63   client_->keyAdded();
     64 }
     65 
     66 void WebContentDecryptionModuleSessionImpl::KeyError(
     67     media::MediaKeys::KeyError error_code,
     68     int system_code) {
     69   client_->keyError(static_cast<Client::MediaKeyErrorCode>(error_code),
     70                     system_code);
     71 }
     72 
     73 void WebContentDecryptionModuleSessionImpl::KeyMessage(
     74     const std::vector<uint8>& message,
     75     const std::string& destination_url) {
     76   client_->keyMessage(message.empty() ? NULL : &message[0],
     77                       message.size(),
     78                       GURL(destination_url));
     79 }
     80 
     81 }  // namespace content
     82