1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __DRM_CONSTRAINTS_H__
18 #define __DRM_CONSTRAINTS_H__
19 
20 #include "drm_framework_common.h"
21 
22 namespace android {
23 
24 /**
25  * This is an utility class which contains the constraints information.
26  *
27  * As a result of DrmManagerClient::getConstraints(const String8*, const int)
28  * an instance of DrmConstraints would be returned.
29  *
30  */
31 class DrmConstraints {
32 public:
33     /**
34      * The following variables are replica of android.drm.DrmStore.ConstraintsColumns
35      * Any changes should also be incorporated with Java Layer as well
36      */
37     /**
38      * The max repeat count
39      */
40     static const String8 MAX_REPEAT_COUNT;
41     /**
42      * The remaining repeat count
43      */
44     static const String8 REMAINING_REPEAT_COUNT;
45 
46     /**
47      * The time before which the protected file can not be played/viewed
48      */
49     static const String8 LICENSE_START_TIME;
50 
51     /**
52      * The time after which the protected file can not be played/viewed
53      */
54     static const String8 LICENSE_EXPIRY_TIME;
55 
56     /**
57      * The available time for license
58      */
59     static const String8 LICENSE_AVAILABLE_TIME;
60 
61     /**
62      * The data stream for extended metadata
63      */
64     static const String8 EXTENDED_METADATA;
65 
66 public:
67     /**
68      * Iterator for key
69      */
70     class KeyIterator {
71         friend class DrmConstraints;
72     private:
KeyIterator(DrmConstraints * drmConstraints)73         KeyIterator(DrmConstraints* drmConstraints)
74             : mDrmConstraints(drmConstraints), mIndex(0) {}
75 
76     public:
77         KeyIterator(const KeyIterator& keyIterator);
78         KeyIterator& operator=(const KeyIterator& keyIterator);
~KeyIterator()79         virtual ~KeyIterator() {}
80 
81     public:
82         bool hasNext();
83         const String8& next();
84 
85     private:
86         DrmConstraints* mDrmConstraints;
87         unsigned int mIndex;
88     };
89 
90     /**
91      * Iterator for constraints
92      */
93     class Iterator {
94         friend class DrmConstraints;
95     private:
Iterator(DrmConstraints * drmConstraints)96         Iterator(DrmConstraints* drmConstraints)
97             : mDrmConstraints(drmConstraints), mIndex(0) {}
98 
99     public:
100         Iterator(const Iterator& iterator);
101         Iterator& operator=(const Iterator& iterator);
~Iterator()102         virtual ~Iterator() {}
103 
104     public:
105         bool hasNext();
106         String8 next();
107 
108     private:
109         DrmConstraints* mDrmConstraints;
110         unsigned int mIndex;
111     };
112 
113 public:
DrmConstraints()114     DrmConstraints() {}
~DrmConstraints()115     virtual ~DrmConstraints() {
116         DrmConstraints::KeyIterator keyIt = this->keyIterator();
117 
118         while (keyIt.hasNext()) {
119             String8 key = keyIt.next();
120                 const char* value = this->getAsByteArray(&key);
121                 if (NULL != value) {
122                     delete[] value;
123                     value = NULL;
124                 }
125         }
126         mConstraintMap.clear();
127     }
128 public:
129     /**
130      * Returns the number of constraints contained in this instance
131      *
132      * @return Number of constraints
133      */
134     int getCount(void) const;
135 
136     /**
137      * Adds constraint information as <key, value> pair to this instance
138      *
139      * @param[in] key Key to add
140      * @param[in] value Value to add
141      * @return Returns DRM_NO_ERROR for success, DRM_ERROR_UNKNOWN for failure
142      */
143     status_t put(const String8* key, const char* value);
144 
145     /**
146      * Retrieves the value of given key
147      *
148      * @param key Key whose value to be retrieved
149      * @return The value
150      */
151     String8 get(const String8& key) const;
152 
153      /**
154      * Retrieves the value as byte array of given key
155      * @param key Key whose value to be retrieved as byte array
156      * @return The byte array value
157      */
158     const char* getAsByteArray(const String8* key) const;
159 
160     /**
161      * Returns KeyIterator object to walk through the keys associated with this instance
162      *
163      * @return KeyIterator object
164      */
165     KeyIterator keyIterator();
166 
167     /**
168      * Returns Iterator object to walk through the values associated with this instance
169      *
170      * @return Iterator object
171      */
172     Iterator iterator();
173 private:
174     const char* getValue(const String8* key) const;
175 private:
176     typedef KeyedVector<String8, const char*> DrmConstraintsMap;
177     DrmConstraintsMap mConstraintMap;
178 };
179 
180 };
181 
182 #endif /* __DRM_CONSTRAINTS_H__ */
183 
184