1/*
2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include "config.h"
32
33#include "public/platform/WebMediaConstraints.h"
34
35#include "wtf/PassRefPtr.h"
36#include "wtf/RefCounted.h"
37
38namespace blink {
39
40class WebMediaConstraintsPrivate FINAL : public RefCounted<WebMediaConstraintsPrivate> {
41public:
42    static PassRefPtr<WebMediaConstraintsPrivate> create();
43    static PassRefPtr<WebMediaConstraintsPrivate> create(const WebVector<WebMediaConstraint>& optional, const WebVector<WebMediaConstraint>& mandatory);
44
45    void getOptionalConstraints(WebVector<WebMediaConstraint>&);
46    void getMandatoryConstraints(WebVector<WebMediaConstraint>&);
47    bool getMandatoryConstraintValue(const WebString& name, WebString& value);
48    bool getOptionalConstraintValue(const WebString& name, WebString& value);
49
50private:
51    WebMediaConstraintsPrivate(const WebVector<WebMediaConstraint>& optional, const WebVector<WebMediaConstraint>& mandatory);
52
53    WebVector<WebMediaConstraint> m_optional;
54    WebVector<WebMediaConstraint> m_mandatory;
55};
56
57PassRefPtr<WebMediaConstraintsPrivate> WebMediaConstraintsPrivate::create()
58{
59    WebVector<WebMediaConstraint> optional;
60    WebVector<WebMediaConstraint> mandatory;
61    return adoptRef(new WebMediaConstraintsPrivate(optional, mandatory));
62}
63
64PassRefPtr<WebMediaConstraintsPrivate> WebMediaConstraintsPrivate::create(const WebVector<WebMediaConstraint>& optional, const WebVector<WebMediaConstraint>& mandatory)
65{
66    return adoptRef(new WebMediaConstraintsPrivate(optional, mandatory));
67}
68
69WebMediaConstraintsPrivate::WebMediaConstraintsPrivate(const WebVector<WebMediaConstraint>& optional, const WebVector<WebMediaConstraint>& mandatory)
70    : m_optional(optional)
71    , m_mandatory(mandatory)
72{
73}
74
75void WebMediaConstraintsPrivate::getOptionalConstraints(WebVector<WebMediaConstraint>& constraints)
76{
77    constraints = m_optional;
78}
79
80void WebMediaConstraintsPrivate::getMandatoryConstraints(WebVector<WebMediaConstraint>& constraints)
81{
82    constraints = m_mandatory;
83}
84
85bool WebMediaConstraintsPrivate::getMandatoryConstraintValue(const WebString& name, WebString& value)
86{
87    for (size_t i = 0; i < m_mandatory.size(); ++i) {
88        if (m_mandatory[i].m_name == name) {
89            value = m_mandatory[i].m_value;
90            return true;
91        }
92    }
93    return false;
94}
95
96bool WebMediaConstraintsPrivate::getOptionalConstraintValue(const WebString& name, WebString& value)
97{
98    for (size_t i = 0; i < m_optional.size(); ++i) {
99        if (m_optional[i].m_name == name) {
100            value = m_optional[i].m_value;
101            return true;
102        }
103    }
104    return false;
105}
106
107// WebMediaConstraints
108
109void WebMediaConstraints::assign(const WebMediaConstraints& other)
110{
111    m_private = other.m_private;
112}
113
114void WebMediaConstraints::reset()
115{
116    m_private.reset();
117}
118
119void WebMediaConstraints::getMandatoryConstraints(WebVector<WebMediaConstraint>& constraints) const
120{
121    ASSERT(!isNull());
122    m_private->getMandatoryConstraints(constraints);
123}
124
125void WebMediaConstraints::getOptionalConstraints(WebVector<WebMediaConstraint>& constraints) const
126{
127    ASSERT(!isNull());
128    m_private->getOptionalConstraints(constraints);
129}
130
131bool WebMediaConstraints::getMandatoryConstraintValue(const WebString& name, WebString& value) const
132{
133    ASSERT(!isNull());
134    return m_private->getMandatoryConstraintValue(name, value);
135}
136
137bool WebMediaConstraints::getOptionalConstraintValue(const WebString& name, WebString& value) const
138{
139    ASSERT(!isNull());
140    return m_private->getOptionalConstraintValue(name, value);
141}
142
143void WebMediaConstraints::initialize()
144{
145    ASSERT(isNull());
146    m_private = WebMediaConstraintsPrivate::create();
147}
148
149void WebMediaConstraints::initialize(const WebVector<WebMediaConstraint>& optional, const WebVector<WebMediaConstraint>& mandatory)
150{
151    ASSERT(isNull());
152    m_private = WebMediaConstraintsPrivate::create(optional, mandatory);
153}
154
155} // namespace blink
156
157