chromium/media/base/cdm_promise.h

// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MEDIA_BASE_CDM_PROMISE_H_
#define MEDIA_BASE_CDM_PROMISE_H_

#include <stdint.h>

#include <string>

#include "base/check.h"
#include "base/logging.h"
#include "media/base/cdm_key_information.h"
#include "media/base/media_export.h"

namespace media {

// Interface for promises being resolved/rejected in response to various
// session actions. These may be called synchronously or asynchronously.
// The promise must be resolved or rejected exactly once. It is expected that
// the caller free the promise once it is resolved/rejected.

// These classes are almost generic, except for the parameters to reject(). If
// a generic class for promises is available, this could be changed to use the
// generic class as long as the parameters to reject() can be set appropriately.

// The base class only has a reject() method and GetResolveParameterType() that
// indicates the type of CdmPromiseTemplate. CdmPromiseTemplate<T> adds the
// resolve(T) method that is dependent on the type of promise. This base class
// is specified so that the promises can be easily saved before passing across
// IPC.
class MEDIA_EXPORT CdmPromise {};

template <typename... T>
struct CdmPromiseTraits {};

template <>
struct MEDIA_EXPORT CdmPromiseTraits<> {};

template <>
struct MEDIA_EXPORT CdmPromiseTraits<int> {};

template <>
struct MEDIA_EXPORT CdmPromiseTraits<std::string> {};

template <>
struct MEDIA_EXPORT CdmPromiseTraits<CdmKeyInformation::KeyStatus> {};

// This class adds the resolve(T) method. This class is still an interface, and
// is used as the type of promise that gets passed around.
template <typename... T>
class CdmPromiseTemplate : public CdmPromise {};

// Explicitly defining all variants of GetResolveParameterType().
// Without this component builds on Windows fail due to versions of the same
// method being generated in multiple DLLs.
template <>
MEDIA_EXPORT CdmPromise::ResolveParameterType
CdmPromiseTemplate<>::GetResolveParameterType() const;

template <>
MEDIA_EXPORT CdmPromise::ResolveParameterType
CdmPromiseTemplate<int>::GetResolveParameterType() const;

template <>
MEDIA_EXPORT CdmPromise::ResolveParameterType
CdmPromiseTemplate<std::string>::GetResolveParameterType() const;

template <>
MEDIA_EXPORT CdmPromise::ResolveParameterType CdmPromiseTemplate<
    CdmKeyInformation::KeyStatus>::GetResolveParameterType() const;

// A dummy CdmPromise that does nothing. Used for APIs requiring a CdmPromise
// while the result will be ignored.
template <typename... T>
class MEDIA_EXPORT DoNothingCdmPromise : public CdmPromiseTemplate<T...> {};

}  // namespace media

#endif  // MEDIA_BASE_CDM_PROMISE_H_