Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / components / devtools_bridge / android / java / src / org / chromium / components / devtools_bridge / commands / ParamDefinitions.java
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 package org.chromium.components.devtools_bridge.commands;
6
7 import android.util.JsonReader;
8 import android.util.JsonWriter;
9
10 import org.chromium.components.devtools_bridge.RTCConfiguration;
11
12 import java.io.IOException;
13 import java.io.StringReader;
14 import java.io.StringWriter;
15 import java.util.ArrayList;
16 import java.util.List;
17
18 /**
19  * Hepler class with a collection of parameter definitions.
20  */
21 final class ParamDefinitions {
22     public static ParamDefinition<String> newStringParam(String name) {
23         return new ParamDefinition<String>(name) {
24             @Override
25             protected String fromString(String value) {
26                 return value;
27             }
28
29             @Override
30             protected String toString(String value) {
31                 return value;
32             }
33         };
34     }
35
36     public static ParamDefinition<List<String>> newStringListParam(String name) {
37         return new ParamDefinition<List<String>>(name) {
38             @Override
39             protected List<String> fromString(String value) throws CommandFormatException {
40                 try {
41                     return new ParamReader(value)
42                             .readStringList()
43                             .close()
44                             .stringListResult;
45                 } catch (Exception e) {
46                     throw newException(this, "Expected JSON-serialized string list", e);
47                 }
48             }
49
50             @Override
51             protected String toString(List<String> value) {
52                 try {
53                     return new ParamWriter().write(value).close().toString();
54                 } catch (IOException e) {
55                     throw new RuntimeException(e);
56                 }
57             }
58         };
59     }
60
61     public static ParamDefinition<RTCConfiguration> newConfigParam(String name) {
62         return new ParamDefinition<RTCConfiguration>(name) {
63             @Override
64             protected RTCConfiguration fromString(String value) throws CommandFormatException {
65                 try {
66                     return new ParamReader(value)
67                         .readConfig()
68                         .close()
69                         .configResult;
70                 } catch (Exception e) {
71                     throw newException(this, "Expected WebRTC configuration", e);
72                 }
73             }
74
75             @Override
76             protected String toString(RTCConfiguration value) {
77                 try {
78                     return new ParamWriter().write(value).close().toString();
79                 } catch (IOException e) {
80                     throw new RuntimeException(e);
81                 }
82             }
83         };
84     }
85
86     private static CommandFormatException newException(
87             ParamDefinition<?> param, String message, Exception cause) {
88         return new CommandFormatException(
89             "Exception in parameter " + param.name() + ": " + message, cause);
90     }
91
92     private static class ParamReader {
93         private final JsonReader mReader;
94
95         public List<String> stringListResult;
96         public RTCConfiguration configResult;
97
98         public ParamReader(String source) {
99             mReader = new JsonReader(new StringReader(source));
100         }
101
102         public ParamReader readStringList() throws IOException {
103             stringListResult = new ArrayList<String>();
104             mReader.beginArray();
105             while (mReader.hasNext()) {
106                 stringListResult.add(mReader.nextString());
107             }
108             mReader.endArray();
109             return this;
110         }
111
112         public ParamReader readConfig() throws IOException {
113             RTCConfiguration.Builder builder = new RTCConfiguration.Builder();
114             mReader.beginObject();
115             while (mReader.hasNext()) {
116                 String name = mReader.nextName();
117                 if ("iceServers".equals(name)) {
118                     readIceServerList(builder);
119                 } else {
120                     mReader.skipValue();
121                 }
122             }
123             configResult = builder.build();
124             return this;
125         }
126
127         public ParamReader close() throws IOException {
128             mReader.close();
129             return this;
130         }
131
132         private void readIceServerList(RTCConfiguration.Builder builder) throws IOException {
133             mReader.beginArray();
134             while (mReader.hasNext()) {
135                 readIceServer(builder);
136             }
137             mReader.endArray();
138         }
139
140         private void readIceServer(RTCConfiguration.Builder builder) throws IOException {
141             String uri = null;
142             String username = "";
143             String credential = "";
144             mReader.beginObject();
145             while (mReader.hasNext()) {
146                 String name = mReader.nextName();
147                 if ("uri".equals(name)) {
148                     uri = mReader.nextString();
149                 } else if ("username".equals(name)) {
150                     username = mReader.nextString();
151                 } else if ("credential".equals(name)) {
152                     credential = mReader.nextString();
153                 } else {
154                     mReader.skipValue();
155                 }
156             }
157             mReader.endObject();
158             if (uri != null) {
159                 builder.addIceServer(uri, username, credential);
160             }
161         }
162     }
163
164     private static class ParamWriter {
165         private final StringWriter mStringWriter = new StringWriter();
166         private final JsonWriter mWriter = new JsonWriter(mStringWriter);
167
168         public ParamWriter write(List<String> value) throws IOException {
169             mWriter.beginArray();
170             for (String item : value) {
171                 mWriter.value(item);
172             }
173             mWriter.endArray();
174             return this;
175         }
176
177         public ParamWriter write(RTCConfiguration config) throws IOException {
178             mWriter.beginObject();
179             mWriter.name("iceServers");
180             mWriter.beginArray();
181             for (RTCConfiguration.IceServer server : config.iceServers) {
182                 mWriter.beginObject();
183                 mWriter.name("uri").value(server.uri);
184                 mWriter.name("username").value(server.username);
185                 mWriter.name("credential").value(server.credential);
186                 mWriter.endObject();
187             }
188             mWriter.endArray();
189             mWriter.endObject();
190             return this;
191         }
192
193         public ParamWriter close() throws IOException {
194             mWriter.close();
195             return this;
196         }
197
198         @Override
199         public String toString() {
200             return mStringWriter.toString();
201         }
202     }
203 }