summaryrefslogtreecommitdiffstats
path: root/media/mojo/services/mojo_cdm_promise.cc
blob: 84d2328b73313c624a0885b4b71c03459e9a5eca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/mojo/services/mojo_cdm_promise.h"

#include <string>
#include <vector>

#include "base/bind.h"
#include "base/logging.h"
#include "media/base/decryptor.h"
#include "media/base/media_keys.h"

namespace media {

static mojo::CdmPromiseResultPtr GetRejectResult(
    MediaKeys::Exception exception,
    uint32_t system_code,
    const std::string& error_message) {
  mojo::CdmPromiseResultPtr cdm_promise_result(mojo::CdmPromiseResult::New());
  cdm_promise_result->success = false;
  cdm_promise_result->exception = static_cast<mojo::CdmException>(exception);
  cdm_promise_result->system_code = system_code;
  cdm_promise_result->error_message = error_message;
  return cdm_promise_result.Pass();
}

template <typename... T>
MojoCdmPromise<T...>::MojoCdmPromise(const CallbackType& callback)
    : callback_(callback) {
  DCHECK(!callback_.is_null());
}

template <typename... T>
MojoCdmPromise<T...>::~MojoCdmPromise() {
  if (!callback_.is_null())
    DVLOG(1) << "Promise not resolved before destruction.";
}

template <typename... T>
void MojoCdmPromise<T...>::resolve(const T&... result) {
  mojo::CdmPromiseResultPtr cdm_promise_result(mojo::CdmPromiseResult::New());
  cdm_promise_result->success = true;
  callback_.Run(cdm_promise_result.Pass(),
                MojoTypeTrait<T>::MojoType::From(result)...);
  callback_.reset();
}

template <typename... T>
void MojoCdmPromise<T...>::reject(MediaKeys::Exception exception,
                                  uint32_t system_code,
                                  const std::string& error_message) {
  callback_.Run(GetRejectResult(exception, system_code, error_message),
                MojoTypeTrait<T>::DefaultValue()...);
  callback_.reset();
}

template class MojoCdmPromise<>;
template class MojoCdmPromise<std::string>;
template class MojoCdmPromise<std::vector<std::vector<uint8_t>>>;

}  // namespace media