Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / renderer / media / webcontentdecryptionmodulesession_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/webcontentdecryptionmodulesession_impl.h"
6
7 #include "base/callback_helpers.h"
8 #include "base/logging.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "content/renderer/media/cdm_session_adapter.h"
12 #include "third_party/WebKit/public/platform/WebURL.h"
13
14 namespace content {
15
16 WebContentDecryptionModuleSessionImpl::WebContentDecryptionModuleSessionImpl(
17     uint32 session_id,
18     Client* client,
19     const scoped_refptr<CdmSessionAdapter>& adapter)
20     : adapter_(adapter),
21       client_(client),
22       session_id_(session_id) {
23 }
24
25 WebContentDecryptionModuleSessionImpl::
26     ~WebContentDecryptionModuleSessionImpl() {
27   adapter_->RemoveSession(session_id_);
28 }
29
30 blink::WebString WebContentDecryptionModuleSessionImpl::sessionId() const {
31   return web_session_id_;
32 }
33
34 void WebContentDecryptionModuleSessionImpl::initializeNewSession(
35     const blink::WebString& mime_type,
36     const uint8* init_data, size_t init_data_length) {
37   // TODO(ddorwin): Guard against this in supported types check and remove this.
38   // Chromium only supports ASCII MIME types.
39   if (!base::IsStringASCII(mime_type)) {
40     NOTREACHED();
41     OnSessionError(media::MediaKeys::kUnknownError, 0);
42     return;
43   }
44
45   adapter_->InitializeNewSession(
46       session_id_, base::UTF16ToASCII(mime_type), init_data, init_data_length);
47 }
48
49 void WebContentDecryptionModuleSessionImpl::update(const uint8* response,
50                                                    size_t response_length) {
51   DCHECK(response);
52   adapter_->UpdateSession(session_id_, response, response_length);
53 }
54
55 void WebContentDecryptionModuleSessionImpl::release() {
56   adapter_->ReleaseSession(session_id_);
57 }
58
59 void WebContentDecryptionModuleSessionImpl::OnSessionCreated(
60     const std::string& web_session_id) {
61   // Due to heartbeat messages, OnSessionCreated() can get called multiple
62   // times.
63   // TODO(jrummell): Once all CDMs are updated to support reference ids,
64   // OnSessionCreated() should only be called once, and the second check can be
65   // removed.
66   blink::WebString id = blink::WebString::fromUTF8(web_session_id);
67   DCHECK(web_session_id_.isEmpty() || web_session_id_ == id)
68       << "Session ID may not be changed once set.";
69   web_session_id_ = id;
70 }
71
72 void WebContentDecryptionModuleSessionImpl::OnSessionMessage(
73     const std::vector<uint8>& message,
74     const std::string& destination_url) {
75   client_->message(message.empty() ? NULL : &message[0],
76                    message.size(),
77                    GURL(destination_url));
78 }
79
80 void WebContentDecryptionModuleSessionImpl::OnSessionReady() {
81   client_->ready();
82 }
83
84 void WebContentDecryptionModuleSessionImpl::OnSessionClosed() {
85   client_->close();
86 }
87
88 void WebContentDecryptionModuleSessionImpl::OnSessionError(
89     media::MediaKeys::KeyError error_code,
90     uint32 system_code) {
91   client_->error(static_cast<Client::MediaKeyErrorCode>(error_code),
92                  system_code);
93 }
94
95 }  // namespace content