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

Side by Side Diff: plugins/org.chromium.sdk.wipbackend.wk120709/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@120709";
39 private static final String DESCRIPTION =
40 "Google Chrome/Chromium: 22.0.1188.*\n" +
41 "Chromium build: 144271\n" +
42 "WebKit revision: 120709\n";
43
44 public WipBackendImpl() {
45 super(ID, DESCRIPTION);
46 }
47
48 @Override
49 public List<? extends WipTabConnector> getTabs(final WipBrowserImpl browserImp l)
50 throws IOException {
51 InetSocketAddress socketAddress = browserImpl.getSocketAddress();
52
53 String content = readHttpResponseContent(socketAddress, "/json",
54 browserImpl.getConnectionLoggerFactory());
55
56 final List<WipTabList.TabDescription> list = parseJsonReponse(content);
57
58 return new AbstractList<WipTabConnector>() {
59 @Override
60 public WipTabConnector get(int index) {
61 return new TabConnectorImpl(list.get(index), browserImpl);
62 }
63
64 @Override
65 public int size() {
66 return list.size();
67 }
68 };
69 }
70
71 private class TabConnectorImpl implements WipTabConnector {
72 private final TabDescription description;
73 private final WipBrowserImpl browserImpl;
74
75 private TabConnectorImpl(TabDescription description, WipBrowserImpl browserI mpl) {
76 this.description = description;
77 this.browserImpl = browserImpl;
78 }
79
80 @Override
81 public boolean isAlreadyAttached() {
82 return description.webSocketDebuggerUrl() == null;
83 }
84
85 @Override
86 public String getUrl() {
87 return description.url();
88 }
89
90 @Override
91 public String getTitle() {
92 return description.title();
93 }
94
95 @Override
96 public WipBrowserTab attach(TabDebugEventListener listener) throws IOExcepti on {
97 LoggerFactory connectionLoggerFactory = browserImpl.getConnectionLoggerFac tory();
98 ConnectionLogger connectionLogger;
99 if (connectionLoggerFactory == null) {
100 connectionLogger = null;
101 } else {
102 connectionLogger = connectionLoggerFactory.newTabConnectionLogger();
103 }
104 String webSocketDebuggerUrl = description.webSocketDebuggerUrl();
105
106 if (webSocketDebuggerUrl == null) {
107 throw new IOException("Tab is already attached");
108 }
109
110 URI uri = URI.create(webSocketDebuggerUrl);
111 WsConnection socket;
112 if (USE_OLD_WEBSOCKET) {
113 socket = Hybi00WsConnection.connect(browserImpl.getSocketAddress(),
114 DEFAULT_CONNECTION_TIMEOUT_MS, uri.getPath(), "empty origin", connec tionLogger);
115 } else {
116 socket = Hybi17WsConnection.connect(browserImpl.getSocketAddress(),
117 DEFAULT_CONNECTION_TIMEOUT_MS, uri.getPath(),
118 Hybi17WsConnection.MaskStrategy.TRANSPARENT_MASK, connectionLogger);
119 }
120
121
122 return new WipTabImpl(socket, browserImpl, listener, description.url());
123 }
124 }
125
126 private String readHttpResponseContent(InetSocketAddress socketAddress, String resource,
127 LoggerFactory loggerFactory) throws IOException {
128 ConnectionLogger browserConnectionLogger = loggerFactory.newBrowserConnectio nLogger();
129 final SocketWrapper socketWrapper = new SocketWrapper(
130 socketAddress, DEFAULT_CONNECTION_TIMEOUT_MS, browserConnectionLogger,
131 HandshakeUtil.ASCII_CHARSET);
132 try {
133 if (browserConnectionLogger != null) {
134 browserConnectionLogger.start();
135 browserConnectionLogger.setConnectionCloser(new ConnectionLogger.Connect ionCloser() {
136 @Override
137 public void closeConnection() {
138 socketWrapper.getShutdownRelay().sendSignal(null, new Exception("UI close request"));
139 }
140 });
141 }
142 LoggableOutputStream output = socketWrapper.getLoggableOutput();
143 writeHttpLine(output, "GET " + resource + " HTTP/1.1");
144 writeHttpLine(output, "User-Agent: ChromeDevTools for Java SDK");
145 writeHttpLine(output, "Host: " + socketAddress.getHostName() + ":" +
146 socketAddress.getPort());
147 writeHttpLine(output, "");
148 output.getOutputStream().flush();
149
150 LoggableInputStream input = socketWrapper.getLoggableInput();
151
152 HandshakeUtil.HttpResponse httpResponse =
153 HandshakeUtil.readHttpResponse(HandshakeUtil.createLineReader(input.ge tInputStream()));
154
155 if (httpResponse.getCode() != 200) {
156 throw new IOException("Unrecognized respose: " + httpResponse.getCode() + " " +
157 httpResponse.getReasonPhrase());
158 }
159
160 String lengthStr = httpResponse.getFields().get("content-length");
161 if (lengthStr == null) {
162 throw new IOException("Unrecognizable respose: no content-length");
163 }
164 int length;
165 try {
166 length = Integer.parseInt(lengthStr.trim());
167 } catch (NumberFormatException e) {
168 throw new IOException("Unrecognizable respose: incorrect content-length" );
169 }
170 byte[] responseBytes = new byte[length];
171 {
172 int readSoFar = 0;
173 while (readSoFar < length) {
174 int res = input.getInputStream().read(responseBytes, readSoFar, length - readSoFar);
175 if (res == -1) {
176 throw new IOException("Unexpected EOS");
177 }
178 readSoFar += res;
179 }
180 }
181 return new String(responseBytes, HandshakeUtil.UTF_8_CHARSET);
182 } finally {
183 if (browserConnectionLogger != null) {
184 browserConnectionLogger.handleEos();
185 }
186 socketWrapper.getShutdownRelay().sendSignal(null, null);
187 }
188 }
189
190 private static void writeHttpLine(LoggableOutputStream output, String line) th rows IOException {
191 OutputStream stream = output.getOutputStream();
192 stream.write(line.getBytes(HandshakeUtil.ASCII_CHARSET));
193 stream.write(0xD);
194 stream.write(0xA);
195 }
196
197 private static List<WipTabList.TabDescription> parseJsonReponse(String content )
198 throws IOException {
199 Object jsonValue;
200 try {
201 jsonValue = new JSONParser().parse(content);
202 } catch (ParseException e) {
203 throw new IOException("Failed to parse a JSON tab list response", e);
204 }
205
206 try {
207 WipTabList tabList = WipParserAccess.get().parseTabList(jsonValue);
208 return tabList.asTabList();
209 } catch (JsonProtocolParseException e) {
210 throw new IOException(
211 "Failed to parse tab list response (on protocol level)", e);
212 }
213 }
214 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698