Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / media / base / cdm_promise.h
blob93515477dade35482f4e346d1d8a8b0a17fd8a68
1 // Copyright 2014 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.
5 #ifndef MEDIA_BASE_CDM_PROMISE_H_
6 #define MEDIA_BASE_CDM_PROMISE_H_
8 #include <string>
10 #include "base/basictypes.h"
11 #include "base/logging.h"
12 #include "media/base/media_export.h"
13 #include "media/base/media_keys.h"
15 namespace media {
17 // Interface for promises being resolved/rejected in response to various
18 // session actions. These may be called synchronously or asynchronously.
19 // The promise must be resolved or rejected exactly once. It is expected that
20 // the caller free the promise once it is resolved/rejected.
22 // These classes are almost generic, except for the parameters to reject(). If
23 // a generic class for promises is available, this could be changed to use the
24 // generic class as long as the parameters to reject() can be set appropriately.
26 // The base class only has a reject() method and GetResolveParameterType() that
27 // indicates the type of CdmPromiseTemplate. CdmPromiseTemplate<T> adds the
28 // resolve(T) method that is dependent on the type of promise. This base class
29 // is specified so that the promises can be easily saved before passing across
30 // the pepper interface.
31 class MEDIA_EXPORT CdmPromise {
32 public:
33 enum ResolveParameterType {
34 VOID_TYPE,
35 STRING_TYPE,
36 KEY_IDS_VECTOR_TYPE
39 CdmPromise();
40 virtual ~CdmPromise();
42 // Used to indicate that the operation failed. |exception_code| must be
43 // specified. |system_code| is a Key System-specific value for the error
44 // that occurred, or 0 if there is no associated status code or such status
45 // codes are not supported by the Key System. |error_message| is optional.
46 virtual void reject(MediaKeys::Exception exception_code,
47 uint32 system_code,
48 const std::string& error_message) = 0;
50 // Used to determine the template type of CdmPromiseTemplate<T> so that
51 // saved CdmPromise objects can be cast to the correct templated version.
52 virtual ResolveParameterType GetResolveParameterType() const = 0;
54 private:
55 DISALLOW_COPY_AND_ASSIGN(CdmPromise);
58 // For some reason the Windows compiler is not happy with the implementation
59 // of CdmPromiseTemplate being in the .cc file, so moving it here.
60 template <typename... T>
61 struct CdmPromiseTraits {};
63 template <>
64 struct CdmPromiseTraits<> {
65 static const CdmPromise::ResolveParameterType kType = CdmPromise::VOID_TYPE;
68 template <>
69 struct CdmPromiseTraits<std::string> {
70 static const CdmPromise::ResolveParameterType kType = CdmPromise::STRING_TYPE;
73 // This class adds the resolve(T) method. This class is still an interface, and
74 // is used as the type of promise that gets passed around.
75 template <typename... T>
76 class MEDIA_EXPORT CdmPromiseTemplate : public CdmPromise {
77 public:
78 CdmPromiseTemplate() : is_settled_(false) {}
80 virtual ~CdmPromiseTemplate() { DCHECK(is_settled_); }
82 virtual void resolve(const T&... result) = 0;
84 // CdmPromise implementation.
85 virtual void reject(MediaKeys::Exception exception_code,
86 uint32 system_code,
87 const std::string& error_message) = 0;
89 ResolveParameterType GetResolveParameterType() const override {
90 return CdmPromiseTraits<T...>::kType;
93 protected:
94 // All implementations must call this method in resolve() and reject() methods
95 // to indicate that the promise has been settled.
96 void MarkPromiseSettled() {
97 // Promise can only be settled once.
98 DCHECK(!is_settled_);
99 is_settled_ = true;
102 private:
103 // Keep track of whether the promise has been resolved or rejected yet.
104 bool is_settled_;
106 DISALLOW_COPY_AND_ASSIGN(CdmPromiseTemplate);
109 } // namespace media
111 #endif // MEDIA_BASE_CDM_PROMISE_H_