14f79f852d149a4a80471e823c873508ec10d8fc
[platform/framework/web/crosswalk.git] / src / media / base / cdm_promise.h
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.
4
5 #ifndef MEDIA_BASE_CDM_PROMISE_H_
6 #define MEDIA_BASE_CDM_PROMISE_H_
7
8 #include <string>
9
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "media/base/media_export.h"
13 #include "media/base/media_keys.h"
14
15 namespace media {
16
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.
21 //
22 // This is only the base class, as parameter to resolve() varies.
23 class MEDIA_EXPORT CdmPromise {
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   typedef base::Callback<void(MediaKeys::Exception exception_code,
39                               uint32 system_code,
40                               const std::string& error_message)>
41       PromiseRejectedCB;
42
43   virtual ~CdmPromise();
44
45   // Used to indicate that the operation failed. |exception_code| must be
46   // specified. |system_code| is a Key System-specific value for the error
47   // that occurred, or 0 if there is no associated status code or such status
48   // codes are not supported by the Key System. |error_message| is optional.
49   virtual void reject(MediaKeys::Exception exception_code,
50                       uint32 system_code,
51                       const std::string& error_message);
52
53  protected:
54   CdmPromise();
55   CdmPromise(PromiseRejectedCB reject_cb);
56
57   // If constructed with a |uma_name| (which must be the name of a
58   // CdmPromiseResult UMA), CdmPromise will report the promise result (success
59   // or rejection code).
60   CdmPromise(PromiseRejectedCB reject_cb, const std::string& uma_name);
61
62   PromiseRejectedCB reject_cb_;
63
64   // Keep track of whether the promise hasn't been resolved or rejected yet.
65   bool is_pending_;
66
67   // UMA to report result to.
68   std::string uma_name_;
69
70   DISALLOW_COPY_AND_ASSIGN(CdmPromise);
71 };
72
73 template <typename T>
74 class MEDIA_EXPORT CdmPromiseTemplate : public CdmPromise {
75  public:
76   CdmPromiseTemplate(base::Callback<void(const T&)> resolve_cb,
77                      PromiseRejectedCB rejected_cb);
78   CdmPromiseTemplate(base::Callback<void(const T&)> resolve_cb,
79                      PromiseRejectedCB rejected_cb,
80                      const std::string& uma_name);
81   virtual ~CdmPromiseTemplate();
82   virtual void resolve(const T& result);
83
84  protected:
85   // Allow subclasses to completely override the implementation.
86   // TODO(jrummell): Remove when derived class SessionLoadedPromise
87   // (in ppapi_decryptor.cc) is no longer needed.
88   CdmPromiseTemplate();
89
90  private:
91   base::Callback<void(const T&)> resolve_cb_;
92
93   DISALLOW_COPY_AND_ASSIGN(CdmPromiseTemplate);
94 };
95
96 // Specialization for no parameter to resolve().
97 template <>
98 class MEDIA_EXPORT CdmPromiseTemplate<void> : public CdmPromise {
99  public:
100   CdmPromiseTemplate(base::Callback<void(void)> resolve_cb,
101                      PromiseRejectedCB rejected_cb);
102   CdmPromiseTemplate(base::Callback<void(void)> resolve_cb,
103                      PromiseRejectedCB rejected_cb,
104                      const std::string& uma_name);
105   virtual ~CdmPromiseTemplate();
106   virtual void resolve();
107
108  protected:
109   // Allow subclasses to completely override the implementation.
110   CdmPromiseTemplate();
111
112  private:
113   base::Callback<void(void)> resolve_cb_;
114
115   DISALLOW_COPY_AND_ASSIGN(CdmPromiseTemplate);
116 };
117
118 }  // namespace media
119
120 #endif  // MEDIA_BASE_CDM_PROMISE_H_