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.
5 package org.chromium.components.devtools_bridge.commands;
7 import android.util.JsonReader;
8 import android.util.JsonWriter;
10 import org.chromium.components.devtools_bridge.RTCConfiguration;
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;
19 * Hepler class with a collection of parameter definitions.
21 final class ParamDefinitions {
22 public static ParamDefinition<String> newStringParam(String name) {
23 return new ParamDefinition<String>(name) {
25 protected String fromString(String value) {
30 protected String toString(String value) {
36 public static ParamDefinition<List<String>> newStringListParam(String name) {
37 return new ParamDefinition<List<String>>(name) {
39 protected List<String> fromString(String value) throws CommandFormatException {
41 return new ParamReader(value)
45 } catch (Exception e) {
46 throw newException(this, "Expected JSON-serialized string list", e);
51 protected String toString(List<String> value) {
53 return new ParamWriter().write(value).close().toString();
54 } catch (IOException e) {
55 throw new RuntimeException(e);
61 public static ParamDefinition<RTCConfiguration> newConfigParam(String name) {
62 return new ParamDefinition<RTCConfiguration>(name) {
64 protected RTCConfiguration fromString(String value) throws CommandFormatException {
66 return new ParamReader(value)
70 } catch (Exception e) {
71 throw newException(this, "Expected WebRTC configuration", e);
76 protected String toString(RTCConfiguration value) {
78 return new ParamWriter().write(value).close().toString();
79 } catch (IOException e) {
80 throw new RuntimeException(e);
86 private static CommandFormatException newException(
87 ParamDefinition<?> param, String message, Exception cause) {
88 return new CommandFormatException(
89 "Exception in parameter " + param.name() + ": " + message, cause);
92 private static class ParamReader {
93 private final JsonReader mReader;
95 public List<String> stringListResult;
96 public RTCConfiguration configResult;
98 public ParamReader(String source) {
99 mReader = new JsonReader(new StringReader(source));
102 public ParamReader readStringList() throws IOException {
103 stringListResult = new ArrayList<String>();
104 mReader.beginArray();
105 while (mReader.hasNext()) {
106 stringListResult.add(mReader.nextString());
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);
123 configResult = builder.build();
127 public ParamReader close() throws IOException {
132 private void readIceServerList(RTCConfiguration.Builder builder) throws IOException {
133 mReader.beginArray();
134 while (mReader.hasNext()) {
135 readIceServer(builder);
140 private void readIceServer(RTCConfiguration.Builder builder) throws IOException {
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();
159 builder.addIceServer(uri, username, credential);
164 private static class ParamWriter {
165 private final StringWriter mStringWriter = new StringWriter();
166 private final JsonWriter mWriter = new JsonWriter(mStringWriter);
168 public ParamWriter write(List<String> value) throws IOException {
169 mWriter.beginArray();
170 for (String item : value) {
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);
193 public ParamWriter close() throws IOException {
199 public String toString() {
200 return mStringWriter.toString();