Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(902)

Side by Side Diff: plugins/org.chromium.sdk.wipbackend.wk118685/src/org/chromium/sdk/internal/wip/WipBackendImpl.java

Issue 11829027: drop old backends (Closed) Base URL: https://chromedevtools.googlecode.com/svn/trunk
Patch Set: Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 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.sdk.internal.wip;
6
7 import java.io.IOException;
8 import java.io.OutputStream;
9 import java.net.InetSocketAddress;
10 import java.net.URI;
11 import java.util.AbstractList;
12 import java.util.List;
13
14 import org.chromium.sdk.ConnectionLogger;
15 import org.chromium.sdk.TabDebugEventListener;
16 import org.chromium.sdk.internal.protocolparser.JsonProtocolParseException;
17 import org.chromium.sdk.internal.transport.SocketWrapper;
18 import org.chromium.sdk.internal.transport.SocketWrapper.LoggableInputStream;
19 import org.chromium.sdk.internal.transport.SocketWrapper.LoggableOutputStream;
20 import org.chromium.sdk.internal.websocket.HandshakeUtil;
21 import org.chromium.sdk.internal.websocket.Hybi00WsConnection;
22 import org.chromium.sdk.internal.websocket.Hybi17WsConnection;
23 import org.chromium.sdk.internal.websocket.WsConnection;
24 import org.chromium.sdk.internal.wip.protocol.WipParserAccess;
25 import org.chromium.sdk.internal.wip.protocol.input.WipTabList;
26 import org.chromium.sdk.internal.wip.protocol.input.WipTabList.TabDescription;
27 import org.chromium.sdk.wip.WipBrowser.WipTabConnector;
28 import org.chromium.sdk.wip.WipBrowserFactory.LoggerFactory;
29 import org.chromium.sdk.wip.WipBrowserTab;
30 import org.json.simple.parser.JSONParser;
31 import org.json.simple.parser.ParseException;
32
33 public class WipBackendImpl extends WipBackendBase {
34 private static final int DEFAULT_CONNECTION_TIMEOUT_MS = 1000;
35
36 private static final boolean USE_OLD_WEBSOCKET = false;
37
38 private static final String ID = "WK@118685";
39 private static final String DESCRIPTION =
40 "Google Chrome/Chromium: 21.0.1180.*\n" +
41 "WebKit revision: 118685\n";
42
43 public WipBackendImpl() {
44 super(ID, DESCRIPTION);
45 }
46
47 @Override
48 public List<? extends WipTabConnector> getTabs(final WipBrowserImpl browserImp l)
49 throws IOException {
50 InetSocketAddress socketAddress = browserImpl.getSocketAddress();
51
52 String content = readHttpResponseContent(socketAddress, "/json",
53 browserImpl.getConnectionLoggerFactory());
54
55 final List<WipTabList.TabDescription> list = parseJsonReponse(content);
56
57 return new AbstractList<WipTabConnector>() {
58 @Override
59 public WipTabConnector get(int index) {
60 return new TabConnectorImpl(list.get(index), browserImpl);
61 }
62
63 @Override
64 public int size() {
65 return list.size();
66 }
67 };
68 }
69
70 private class TabConnectorImpl implements WipTabConnector {
71 private final TabDescription description;
72 private final WipBrowserImpl browserImpl;
73
74 private TabConnectorImpl(TabDescription description, WipBrowserImpl browserI mpl) {
75 this.description = description;
76 this.browserImpl = browserImpl;
77 }
78
79 @Override
80 public boolean isAlreadyAttached() {
81 return description.webSocketDebuggerUrl() == null;
82 }
83
84 @Override
85 public String getUrl() {
86 return description.url();
87 }
88
89 @Override
90 public String getTitle() {
91 return description.title();
92 }
93
94 @Override
95 public WipBrowserTab attach(TabDebugEventListener listener) throws IOExcepti on {
96 LoggerFactory connectionLoggerFactory = browserImpl.getConnectionLoggerFac tory();
97 ConnectionLogger connectionLogger;
98 if (connectionLoggerFactory == null) {
99 connectionLogger = null;
100 } else {
101 connectionLogger = connectionLoggerFactory.newTabConnectionLogger();
102 }
103 String webSocketDebuggerUrl = description.webSocketDebuggerUrl();
104
105 if (webSocketDebuggerUrl == null) {
106 throw new IOException("Tab is already attached");
107 }
108
109 URI uri = URI.create(webSocketDebuggerUrl);
110 WsConnection socket;
111 if (USE_OLD_WEBSOCKET) {
112 socket = Hybi00WsConnection.connect(browserImpl.getSocketAddress(),
113 DEFAULT_CONNECTION_TIMEOUT_MS, uri.getPath(), "empty origin", connec tionLogger);
114 } else {
115 socket = Hybi17WsConnection.connect(browserImpl.getSocketAddress(),
116 DEFAULT_CONNECTION_TIMEOUT_MS, uri.getPath(),
117 Hybi17WsConnection.MaskStrategy.TRANSPARENT_MASK, connectionLogger);
118 }
119
120
121 return new WipTabImpl(socket, browserImpl, listener, description.url());
122 }
123 }
124
125 private String readHttpResponseContent(InetSocketAddress socketAddress, String resource,
126 LoggerFactory loggerFactory) throws IOException {
127 ConnectionLogger browserConnectionLogger = loggerFactory.newBrowserConnectio nLogger();
128 final SocketWrapper socketWrapper = new SocketWrapper(
129 socketAddress, DEFAULT_CONNECTION_TIMEOUT_MS, browserConnectionLogger,
130 HandshakeUtil.ASCII_CHARSET);
131 try {
132 if (browserConnectionLogger != null) {
133 browserConnectionLogger.start();
134 browserConnectionLogger.setConnectionCloser(new ConnectionLogger.Connect ionCloser() {
135 @Override
136 public void closeConnection() {
137 socketWrapper.getShutdownRelay().sendSignal(null, new Exception("UI close request"));
138 }
139 });
140 }
141 LoggableOutputStream output = socketWrapper.getLoggableOutput();
142 writeHttpLine(output, "GET " + resource + " HTTP/1.1");
143 writeHttpLine(output, "User-Agent: ChromeDevTools for Java SDK");
144 writeHttpLine(output, "Host: " + socketAddress.getHostName() + ":" +
145 socketAddress.getPort());
146 writeHttpLine(output, "");
147 output.getOutputStream().flush();
148
149 LoggableInputStream input = socketWrapper.getLoggableInput();
150
151 HandshakeUtil.HttpResponse httpResponse =
152 HandshakeUtil.readHttpResponse(HandshakeUtil.createLineReader(input.ge tInputStream()));
153
154 if (httpResponse.getCode() != 200) {
155 throw new IOException("Unrecognized respose: " + httpResponse.getCode() + " " +
156 httpResponse.getReasonPhrase());
157 }
158
159 String lengthStr = httpResponse.getFields().get("content-length");
160 if (lengthStr == null) {
161 throw new IOException("Unrecognizable respose: no content-length");
162 }
163 int length;
164 try {
165 length = Integer.parseInt(lengthStr.trim());
166 } catch (NumberFormatException e) {
167 throw new IOException("Unrecognizable respose: incorrect content-length" );
168 }
169 byte[] responseBytes = new byte[length];
170 {
171 int readSoFar = 0;
172 while (readSoFar < length) {
173 int res = input.getInputStream().read(responseBytes, readSoFar, length - readSoFar);
174 if (res == -1) {
175 throw new IOException("Unexpected EOS");
176 }
177 readSoFar += res;
178 }
179 }
180 return new String(responseBytes, HandshakeUtil.UTF_8_CHARSET);
181 } finally {
182 if (browserConnectionLogger != null) {
183 browserConnectionLogger.handleEos();
184 }
185 socketWrapper.getShutdownRelay().sendSignal(null, null);
186 }
187 }
188
189 private static void writeHttpLine(LoggableOutputStream output, String line) th rows IOException {
190 OutputStream stream = output.getOutputStream();
191 stream.write(line.getBytes(HandshakeUtil.ASCII_CHARSET));
192 stream.write(0xD);
193 stream.write(0xA);
194 }
195
196 private static List<WipTabList.TabDescription> parseJsonReponse(String content )
197 throws IOException {
198 Object jsonValue;
199 try {
200 jsonValue = new JSONParser().parse(content);
201 } catch (ParseException e) {
202 throw new IOException("Failed to parse a JSON tab list response", e);
203 }
204
205 try {
206 WipTabList tabList = WipParserAccess.get().parseTabList(jsonValue);
207 return tabList.asTabList();
208 } catch (JsonProtocolParseException e) {
209 throw new IOException(
210 "Failed to parse tab list response (on protocol level)", e);
211 }
212 }
213 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698