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_
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "media/base/media_export.h"
13 #include "media/base/media_keys.h"
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 // This is only the base class, as parameter to resolve() varies.
23 class MEDIA_EXPORT CdmPromise {
25 // A superset of media::MediaKeys::Exception for UMA reporting.
26 enum ResultCodeForUMA {
38 enum ResolveParameterType {
44 typedef base::Callback<void(MediaKeys::Exception exception_code,
46 const std::string& error_message)>
49 virtual ~CdmPromise();
51 // Used to indicate that the operation failed. |exception_code| must be
52 // specified. |system_code| is a Key System-specific value for the error
53 // that occurred, or 0 if there is no associated status code or such status
54 // codes are not supported by the Key System. |error_message| is optional.
55 virtual void reject(MediaKeys::Exception exception_code,
57 const std::string& error_message);
59 ResolveParameterType GetResolveParameterType() const {
60 return parameter_type_;
64 explicit CdmPromise(ResolveParameterType parameter_type);
65 CdmPromise(ResolveParameterType parameter_type, PromiseRejectedCB reject_cb);
67 // If constructed with a |uma_name| (which must be the name of a
68 // CdmPromiseResult UMA), CdmPromise will report the promise result (success
69 // or rejection code).
70 CdmPromise(ResolveParameterType parameter_type,
71 PromiseRejectedCB reject_cb,
72 const std::string& uma_name);
74 // Called by all resolve()/reject() methods to report the UMA result if
75 // applicable, and update |is_pending_|.
76 void ReportResultToUMA(ResultCodeForUMA result);
78 const ResolveParameterType parameter_type_;
79 PromiseRejectedCB reject_cb_;
81 // Keep track of whether the promise hasn't been resolved or rejected yet.
84 // UMA name to report result to.
85 std::string uma_name_;
87 DISALLOW_COPY_AND_ASSIGN(CdmPromise);
91 class MEDIA_EXPORT CdmPromiseTemplate : public CdmPromise {
93 CdmPromiseTemplate(base::Callback<void(const T&)> resolve_cb,
94 PromiseRejectedCB rejected_cb);
95 CdmPromiseTemplate(base::Callback<void(const T&)> resolve_cb,
96 PromiseRejectedCB rejected_cb,
97 const std::string& uma_name);
98 virtual void resolve(const T& result);
101 // Allow subclasses to completely override the implementation.
102 CdmPromiseTemplate();
105 base::Callback<void(const T&)> resolve_cb_;
107 DISALLOW_COPY_AND_ASSIGN(CdmPromiseTemplate);
110 // Specialization for no parameter to resolve().
112 class MEDIA_EXPORT CdmPromiseTemplate<void> : public CdmPromise {
114 CdmPromiseTemplate(base::Callback<void(void)> resolve_cb,
115 PromiseRejectedCB rejected_cb);
116 CdmPromiseTemplate(base::Callback<void(void)> resolve_cb,
117 PromiseRejectedCB rejected_cb,
118 const std::string& uma_name);
119 virtual void resolve();
122 // Allow subclasses to completely override the implementation.
123 CdmPromiseTemplate();
126 base::Callback<void(void)> resolve_cb_;
128 DISALLOW_COPY_AND_ASSIGN(CdmPromiseTemplate);
133 #endif // MEDIA_BASE_CDM_PROMISE_H_