Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(516)

Side by Side Diff: media/base/cdm_promise.h

Issue 604283003: Refactor CdmPromise and related classes (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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" 11 #include "base/callback.h"
12 #include "media/base/media_export.h" 12 #include "media/base/media_export.h"
13 #include "media/base/media_keys.h" 13 #include "media/base/media_keys.h"
14 14
xhwang 2014/09/29 18:32:59 As we are one of the few pioneers using Promise in
jrummell 2014/09/29 22:08:39 Done.
15 namespace media { 15 namespace media {
16 16
17 // Interface for promises being resolved/rejected in response to various 17 // Interface for promises being resolved/rejected in response to various
18 // session actions. These may be called synchronously or asynchronously. 18 // session actions. These may be called synchronously or asynchronously.
19 // The promise must be resolved or rejected exactly once. It is expected that 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. 20 // the caller free the promise once it is resolved/rejected.
xhwang 2014/09/29 18:32:59 This doc needs to be updated. For example, we don'
jrummell 2014/09/29 22:08:39 Done.
21 // 21 //
22 // This is only the base class, as parameter to resolve() varies. 22 // This is only the base class, as parameter to resolve() varies.
23 class MEDIA_EXPORT CdmPromise { 23 class MEDIA_EXPORT CdmPromise {
24 public: 24 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 { 25 enum ResolveParameterType {
39 VOID_TYPE, 26 VOID_TYPE,
40 STRING_TYPE, 27 STRING_TYPE,
41 KEY_IDS_VECTOR_TYPE 28 KEY_IDS_VECTOR_TYPE
42 }; 29 };
43 30
44 typedef base::Callback<void(MediaKeys::Exception exception_code,
45 uint32 system_code,
46 const std::string& error_message)>
47 PromiseRejectedCB;
48
49 virtual ~CdmPromise(); 31 virtual ~CdmPromise();
50 32
51 // Used to indicate that the operation failed. |exception_code| must be 33 // Used to indicate that the operation failed. |exception_code| must be
52 // specified. |system_code| is a Key System-specific value for the error 34 // 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 35 // 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. 36 // codes are not supported by the Key System. |error_message| is optional.
55 virtual void reject(MediaKeys::Exception exception_code, 37 virtual void reject(MediaKeys::Exception exception_code,
56 uint32 system_code, 38 uint32 system_code,
57 const std::string& error_message); 39 const std::string& error_message) = 0;
58 40
59 ResolveParameterType GetResolveParameterType() const { 41 virtual ResolveParameterType GetResolveParameterType() const = 0;
xhwang 2014/09/29 18:32:59 Since this is an interface, it'd be nice to provid
jrummell 2014/09/29 22:08:39 Done.
60 return parameter_type_;
61 }
62 42
63 protected: 43 protected:
64 explicit CdmPromise(ResolveParameterType parameter_type); 44 CdmPromise();
xhwang 2014/09/29 18:32:59 nit: Since this class is an interface, nobody can
jrummell 2014/09/29 22:08:39 Done.
65 CdmPromise(ResolveParameterType parameter_type, PromiseRejectedCB reject_cb);
66 45
67 // If constructed with a |uma_name| (which must be the name of a 46 private:
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); 47 DISALLOW_COPY_AND_ASSIGN(CdmPromise);
88 }; 48 };
89 49
90 template <typename T> 50 template <typename T>
91 class MEDIA_EXPORT CdmPromiseTemplate : public CdmPromise { 51 class MEDIA_EXPORT CdmPromiseTemplate : public CdmPromise {
xhwang 2014/09/29 18:32:58 Comment about why we need this. Also, mention that
jrummell 2014/09/29 22:08:39 Done.
92 public: 52 public:
93 CdmPromiseTemplate(base::Callback<void(const T&)> resolve_cb, 53 virtual ~CdmPromiseTemplate();
94 PromiseRejectedCB rejected_cb); 54
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); 55 virtual void resolve(const T& result);
ddorwin 2014/09/26 22:40:00 It doesn't appear that CdmPromiseTemplate<foo> is
xhwang 2014/09/29 18:32:59 +1 Also you can put reject(...) = 0 here so it's
jrummell 2014/09/29 22:08:39 Done.
jrummell 2014/09/29 22:08:39 True. But then we'd have to clone CdmPromiseTraits
56 virtual ResolveParameterType GetResolveParameterType() const OVERRIDE;
xhwang 2014/09/29 18:32:59 // CdmPromise implementation.
jrummell 2014/09/29 22:08:39 Done.
99 57
100 protected: 58 protected:
101 // Allow subclasses to completely override the implementation.
102 CdmPromiseTemplate(); 59 CdmPromiseTemplate();
xhwang 2014/09/29 18:32:59 ditto about protected ctor since this class is als
jrummell 2014/09/29 22:08:39 Done.
103 60
104 private: 61 private:
105 base::Callback<void(const T&)> resolve_cb_;
106
107 DISALLOW_COPY_AND_ASSIGN(CdmPromiseTemplate); 62 DISALLOW_COPY_AND_ASSIGN(CdmPromiseTemplate);
108 }; 63 };
109 64
110 // Specialization for no parameter to resolve(). 65 // Specialization for no parameter to resolve().
xhwang 2014/09/29 18:32:59 FYI, now "Variadic Templates" (a c++11 feature) is
111 template <> 66 template <>
112 class MEDIA_EXPORT CdmPromiseTemplate<void> : public CdmPromise { 67 class MEDIA_EXPORT CdmPromiseTemplate<void> : public CdmPromise {
113 public: 68 public:
114 CdmPromiseTemplate(base::Callback<void(void)> resolve_cb, 69 virtual ~CdmPromiseTemplate();
115 PromiseRejectedCB rejected_cb); 70
116 CdmPromiseTemplate(base::Callback<void(void)> resolve_cb,
117 PromiseRejectedCB rejected_cb,
118 const std::string& uma_name);
119 virtual void resolve(); 71 virtual void resolve();
xhwang 2014/09/29 18:32:59 ditto, pure virtual.
jrummell 2014/09/29 22:08:39 Done.
72 virtual ResolveParameterType GetResolveParameterType() const OVERRIDE;
120 73
121 protected: 74 protected:
122 // Allow subclasses to completely override the implementation.
123 CdmPromiseTemplate(); 75 CdmPromiseTemplate();
124 76
125 private: 77 private:
126 base::Callback<void(void)> resolve_cb_;
127
128 DISALLOW_COPY_AND_ASSIGN(CdmPromiseTemplate); 78 DISALLOW_COPY_AND_ASSIGN(CdmPromiseTemplate);
129 }; 79 };
130 80
81 typedef base::Callback<void(MediaKeys::Exception exception_code,
82 uint32 system_code,
83 const std::string& error_message)>
84 PromiseRejectedCB;
85
86 template <typename T>
87 class MEDIA_EXPORT CdmCallbackPromise : public CdmPromiseTemplate<T> {
xhwang 2014/09/29 18:32:59 Does it make sense to put CdmCallbackPromise in it
jrummell 2014/09/29 22:08:39 Done.
88 public:
89 CdmCallbackPromise(base::Callback<void(const T&)> resolve_cb,
90 PromiseRejectedCB reject_cb);
91 virtual ~CdmCallbackPromise();
92
93 virtual void resolve(const T& result) OVERRIDE;
xhwang 2014/09/29 18:32:59 // CdmPromiseTemplate<T> implementation.
jrummell 2014/09/29 22:08:39 Done.
94 virtual void reject(MediaKeys::Exception exception_code,
95 uint32 system_code,
96 const std::string& error_message) OVERRIDE;
97
98 private:
99 base::Callback<void(const T&)> resolve_cb_;
100 PromiseRejectedCB reject_cb_;
101
102 // Keep track of whether the promise hasn't been resolved or rejected yet.
103 bool is_pending_;
ddorwin 2014/09/26 22:40:00 Is there a reason that is_pending_ can't be a memb
jrummell 2014/09/29 22:08:39 It seems strange to have it defined on the base cl
104
105 DISALLOW_COPY_AND_ASSIGN(CdmCallbackPromise);
106 };
107
108 // Specialization for no parameter to resolve().
109 template <>
110 class MEDIA_EXPORT CdmCallbackPromise<void> : public CdmPromiseTemplate<void> {
111 public:
112 CdmCallbackPromise(base::Callback<void(void)> resolve_cb,
113 PromiseRejectedCB reject_cb);
114 virtual ~CdmCallbackPromise();
115
116 virtual void resolve() OVERRIDE;
xhwang 2014/09/29 18:32:59 // CdmPromiseTemplate<void> implementation.
jrummell 2014/09/29 22:08:39 Done.
117 virtual void reject(MediaKeys::Exception exception_code,
118 uint32 system_code,
119 const std::string& error_message) OVERRIDE;
120
121 private:
122 base::Callback<void(void)> resolve_cb_;
123 PromiseRejectedCB reject_cb_;
124
125 // Keep track of whether the promise hasn't been resolved or rejected yet.
126 bool is_pending_;
127
128 DISALLOW_COPY_AND_ASSIGN(CdmCallbackPromise);
129 };
130
131 } // namespace media 131 } // namespace media
132 132
133 #endif // MEDIA_BASE_CDM_PROMISE_H_ 133 #endif // MEDIA_BASE_CDM_PROMISE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698