| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef MEDIA_BASE_CDM_PROMISE_H_ | 5 #ifndef MEDIA_BASE_CDM_PROMISE_H_ |
| 6 #define MEDIA_BASE_CDM_PROMISE_H_ | 6 #define MEDIA_BASE_CDM_PROMISE_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/callback.h" | |
| 12 #include "media/base/media_export.h" | 11 #include "media/base/media_export.h" |
| 13 #include "media/base/media_keys.h" | 12 #include "media/base/media_keys.h" |
| 14 | 13 |
| 15 namespace media { | 14 namespace media { |
| 16 | 15 |
| 17 // Interface for promises being resolved/rejected in response to various | 16 // Interface for promises being resolved/rejected in response to various |
| 18 // session actions. These may be called synchronously or asynchronously. | 17 // session actions. These may be called synchronously or asynchronously. |
| 19 // The promise must be resolved or rejected exactly once. It is expected that | 18 // The promise must be resolved or rejected exactly once. It is expected that |
| 20 // the caller free the promise once it is resolved/rejected. | 19 // the caller free the promise once it is resolved/rejected. |
| 21 // | 20 |
| 22 // This is only the base class, as parameter to resolve() varies. | 21 // These classes are almost generic, except for the parameters to reject(). If |
| 22 // a generic class for promises is available, this could be changed to use the |
| 23 // generic class as long as the parameters to reject() can be set appropriately. |
| 24 |
| 25 // The base class only has a reject() method and GetResolveParameterType() that |
| 26 // indicates the type of CdmPromiseTemplate. CdmPromiseTemplate<T> adds the |
| 27 // resolve(T) method that is dependent on the type of promise. This base class |
| 28 // is specified so that the promises can be easily saved before passing across |
| 29 // the pepper interface. |
| 23 class MEDIA_EXPORT CdmPromise { | 30 class MEDIA_EXPORT CdmPromise { |
| 24 public: | 31 public: |
| 25 // A superset of media::MediaKeys::Exception for UMA reporting. | |
| 26 enum ResultCodeForUMA { | |
| 27 SUCCESS, | |
| 28 NOT_SUPPORTED_ERROR, | |
| 29 INVALID_STATE_ERROR, | |
| 30 INVALID_ACCESS_ERROR, | |
| 31 QUOTA_EXCEEDED_ERROR, | |
| 32 UNKNOWN_ERROR, | |
| 33 CLIENT_ERROR, | |
| 34 OUTPUT_ERROR, | |
| 35 NUM_RESULT_CODES | |
| 36 }; | |
| 37 | |
| 38 enum ResolveParameterType { | 32 enum ResolveParameterType { |
| 39 VOID_TYPE, | 33 VOID_TYPE, |
| 40 STRING_TYPE, | 34 STRING_TYPE, |
| 41 KEY_IDS_VECTOR_TYPE | 35 KEY_IDS_VECTOR_TYPE |
| 42 }; | 36 }; |
| 43 | 37 |
| 44 typedef base::Callback<void(MediaKeys::Exception exception_code, | 38 CdmPromise(); |
| 45 uint32 system_code, | |
| 46 const std::string& error_message)> | |
| 47 PromiseRejectedCB; | |
| 48 | |
| 49 virtual ~CdmPromise(); | 39 virtual ~CdmPromise(); |
| 50 | 40 |
| 51 // Used to indicate that the operation failed. |exception_code| must be | 41 // Used to indicate that the operation failed. |exception_code| must be |
| 52 // specified. |system_code| is a Key System-specific value for the error | 42 // 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 | 43 // 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. | 44 // codes are not supported by the Key System. |error_message| is optional. |
| 55 virtual void reject(MediaKeys::Exception exception_code, | 45 virtual void reject(MediaKeys::Exception exception_code, |
| 56 uint32 system_code, | 46 uint32 system_code, |
| 57 const std::string& error_message); | 47 const std::string& error_message) = 0; |
| 58 | 48 |
| 59 ResolveParameterType GetResolveParameterType() const { | 49 // Used to determine the template type of CdmPromiseTemplate<T> so that |
| 60 return parameter_type_; | 50 // saved CdmPromise objects can be cast to the correct templated version. |
| 61 } | 51 virtual ResolveParameterType GetResolveParameterType() const = 0; |
| 62 | 52 |
| 63 protected: | 53 private: |
| 64 explicit CdmPromise(ResolveParameterType parameter_type); | |
| 65 CdmPromise(ResolveParameterType parameter_type, PromiseRejectedCB reject_cb); | |
| 66 | |
| 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); | |
| 73 | |
| 74 // Called by all resolve()/reject() methods to report the UMA result if | |
| 75 // applicable, and update |is_pending_|. | |
| 76 void ReportResultToUMA(ResultCodeForUMA result); | |
| 77 | |
| 78 const ResolveParameterType parameter_type_; | |
| 79 PromiseRejectedCB reject_cb_; | |
| 80 | |
| 81 // Keep track of whether the promise hasn't been resolved or rejected yet. | |
| 82 bool is_pending_; | |
| 83 | |
| 84 // UMA name to report result to. | |
| 85 std::string uma_name_; | |
| 86 | |
| 87 DISALLOW_COPY_AND_ASSIGN(CdmPromise); | 54 DISALLOW_COPY_AND_ASSIGN(CdmPromise); |
| 88 }; | 55 }; |
| 89 | 56 |
| 90 template <typename T> | 57 // This class adds the resolve(T) method. This class is still an interface, and |
| 58 // is used as the type of promise that gets passed around. |
| 59 template <typename... T> |
| 91 class MEDIA_EXPORT CdmPromiseTemplate : public CdmPromise { | 60 class MEDIA_EXPORT CdmPromiseTemplate : public CdmPromise { |
| 92 public: | 61 public: |
| 93 CdmPromiseTemplate(base::Callback<void(const T&)> resolve_cb, | 62 CdmPromiseTemplate(); |
| 94 PromiseRejectedCB rejected_cb); | 63 virtual ~CdmPromiseTemplate(); |
| 95 CdmPromiseTemplate(base::Callback<void(const T&)> resolve_cb, | 64 |
| 96 PromiseRejectedCB rejected_cb, | 65 virtual void resolve(const T&... result) = 0; |
| 97 const std::string& uma_name); | 66 |
| 98 virtual void resolve(const T& result); | 67 // CdmPromise implementation. |
| 68 virtual void reject(MediaKeys::Exception exception_code, |
| 69 uint32 system_code, |
| 70 const std::string& error_message) = 0; |
| 71 virtual ResolveParameterType GetResolveParameterType() const OVERRIDE; |
| 99 | 72 |
| 100 protected: | 73 protected: |
| 101 // Allow subclasses to completely override the implementation. | 74 // All implementations must call this method in resolve() and reject() methods |
| 102 CdmPromiseTemplate(); | 75 // to indicate that the promise has been settled. |
| 76 void MarkPromiseSettled(); |
| 103 | 77 |
| 104 private: | 78 private: |
| 105 base::Callback<void(const T&)> resolve_cb_; | 79 // Keep track of whether the promise has been resolved or rejected yet. |
| 80 bool is_settled_; |
| 106 | 81 |
| 107 DISALLOW_COPY_AND_ASSIGN(CdmPromiseTemplate); | 82 DISALLOW_COPY_AND_ASSIGN(CdmPromiseTemplate); |
| 108 }; | 83 }; |
| 109 | |
| 110 // Specialization for no parameter to resolve(). | |
| 111 template <> | |
| 112 class MEDIA_EXPORT CdmPromiseTemplate<void> : public CdmPromise { | |
| 113 public: | |
| 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(); | |
| 120 | |
| 121 protected: | |
| 122 // Allow subclasses to completely override the implementation. | |
| 123 CdmPromiseTemplate(); | |
| 124 | |
| 125 private: | |
| 126 base::Callback<void(void)> resolve_cb_; | |
| 127 | |
| 128 DISALLOW_COPY_AND_ASSIGN(CdmPromiseTemplate); | |
| 129 }; | |
| 130 | 84 |
| 131 } // namespace media | 85 } // namespace media |
| 132 | 86 |
| 133 #endif // MEDIA_BASE_CDM_PROMISE_H_ | 87 #endif // MEDIA_BASE_CDM_PROMISE_H_ |
| OLD | NEW |