2
|
1 |
// Copyright (c) 2009 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;
|
|
6 |
|
|
7 |
import java.io.IOException;
|
|
8 |
import java.util.ArrayList;
|
|
9 |
import java.util.Collection;
|
|
10 |
import java.util.List;
|
|
11 |
import java.util.Map;
|
|
12 |
import java.util.concurrent.ConcurrentHashMap;
|
|
13 |
import java.util.concurrent.TimeoutException;
|
|
14 |
import java.util.concurrent.atomic.AtomicBoolean;
|
|
15 |
import java.util.logging.Level;
|
|
16 |
import java.util.logging.Logger;
|
|
17 |
|
|
18 |
import org.chromium.sdk.Browser;
|
|
19 |
import org.chromium.sdk.BrowserTab;
|
|
20 |
import org.chromium.sdk.TabDebugEventListener;
|
|
21 |
import org.chromium.sdk.UnsupportedVersionException;
|
|
22 |
import org.chromium.sdk.Version;
|
|
23 |
import org.chromium.sdk.internal.tools.ToolHandler;
|
|
24 |
import org.chromium.sdk.internal.tools.ToolName;
|
|
25 |
import org.chromium.sdk.internal.tools.ToolOutput;
|
|
26 |
import org.chromium.sdk.internal.tools.devtools.DevToolsServiceHandler;
|
|
27 |
import org.chromium.sdk.internal.tools.devtools.DevToolsServiceHandler.TabIdAndUrl;
|
|
28 |
import org.chromium.sdk.internal.transport.Connection;
|
|
29 |
import org.chromium.sdk.internal.transport.Message;
|
|
30 |
import org.chromium.sdk.internal.transport.Connection.NetListener;
|
|
31 |
|
|
32 |
/**
|
|
33 |
* A thread-safe implementation of the Browser interface.
|
|
34 |
*/
|
|
35 |
public class BrowserImpl implements Browser {
|
|
36 |
|
|
37 |
private static final Logger LOGGER = Logger.getLogger(BrowserImpl.class.getName());
|
|
38 |
|
|
39 |
public static final int OPERATION_TIMEOUT_MS = 3000;
|
|
40 |
|
|
41 |
/**
|
|
42 |
* The protocol version supported by this SDK implementation.
|
|
43 |
*/
|
|
44 |
public static final Version PROTOCOL_VERSION = new Version(0, 1);
|
|
45 |
|
|
46 |
private final ConnectionSessionManager sessionManager = new ConnectionSessionManager();
|
|
47 |
|
|
48 |
/** The browser connection (gets opened in session). */
|
|
49 |
private final ConnectionFactory connectionFactory;
|
|
50 |
|
|
51 |
BrowserImpl(ConnectionFactory connectionFactory) {
|
|
52 |
this.connectionFactory = connectionFactory;
|
|
53 |
}
|
|
54 |
|
|
55 |
public TabFetcher createTabFetcher() throws IOException, UnsupportedVersionException {
|
|
56 |
SessionManager.Ticket<Session> ticket = connectInternal();
|
|
57 |
return new TabFetcherImpl(ticket);
|
|
58 |
}
|
|
59 |
|
|
60 |
private SessionManager.Ticket<Session> connectInternal() throws IOException,
|
|
61 |
UnsupportedVersionException {
|
|
62 |
try {
|
|
63 |
return sessionManager.connect();
|
|
64 |
} catch (ExceptionWrapper eWrapper) {
|
|
65 |
eWrapper.rethrow();
|
|
66 |
// Not reachable.
|
|
67 |
throw new RuntimeException();
|
|
68 |
}
|
|
69 |
}
|
|
70 |
|
|
71 |
/**
|
|
72 |
* Object that lives during one connection period. Browser should be able to
|
|
73 |
* reconnect (because we want to support attach-detach-attach sequence). On
|
|
74 |
* reconnect new session should be created. Each browser tab should be linked
|
|
75 |
* to a particular session.
|
|
76 |
*/
|
|
77 |
public class Session extends SessionManager.SessionBase<Session> {
|
|
78 |
|
|
79 |
private final AtomicBoolean alreadyClosingSession = new AtomicBoolean(false);
|
|
80 |
|
|
81 |
private final CloseableMap<Integer, ToolHandler> tabId2ToolHandler = CloseableMap.newMap();
|
|
82 |
|
|
83 |
// TODO(peter.rybin): get rid of this structure (if we can get rid
|
|
84 |
// of corresponding notification)
|
|
85 |
private final Map<Integer, DebugSession> tabId2DebugSession =
|
|
86 |
new ConcurrentHashMap<Integer, DebugSession>();
|
|
87 |
|
|
88 |
/** The DevTools service handler for the browser. */
|
|
89 |
private volatile DevToolsServiceHandler devToolsHandler;
|
|
90 |
|
|
91 |
/** Open connection which is used by the session. */
|
|
92 |
private final Connection sessionConnection;
|
|
93 |
|
|
94 |
Session() throws IOException, UnsupportedVersionException {
|
|
95 |
super(sessionManager);
|
|
96 |
|
|
97 |
devToolsHandler = new DevToolsServiceHandler(devToolsToolOutput);
|
|
98 |
|
|
99 |
sessionConnection = connectionFactory.newOpenConnection(netListener);
|
|
100 |
|
|
101 |
String serverVersionString;
|
|
102 |
try {
|
|
103 |
serverVersionString = devToolsHandler.version(OPERATION_TIMEOUT_MS);
|
|
104 |
} catch (TimeoutException e) {
|
|
105 |
throw new IOException("Failed to get protocol version from remote", e);
|
|
106 |
}
|
|
107 |
if (serverVersionString == null) {
|
|
108 |
throw new UnsupportedVersionException(BrowserImpl.PROTOCOL_VERSION, null);
|
|
109 |
}
|
|
110 |
Version serverVersion = Version.parseString(serverVersionString);
|
|
111 |
if (serverVersion == null ||
|
|
112 |
serverVersion.compareTo(BrowserImpl.PROTOCOL_VERSION) < 0) {
|
|
113 |
throw new UnsupportedVersionException(BrowserImpl.PROTOCOL_VERSION, serverVersion);
|
|
114 |
}
|
|
115 |
}
|
|
116 |
|
|
117 |
@Override
|
|
118 |
protected Session getThisAsSession() {
|
|
119 |
return this;
|
|
120 |
}
|
|
121 |
|
|
122 |
@Override
|
|
123 |
protected void lastTicketDismissed() {
|
|
124 |
boolean res = alreadyClosingSession.compareAndSet(false, true);
|
|
125 |
if (!res) {
|
|
126 |
// already closing
|
|
127 |
return;
|
|
128 |
}
|
|
129 |
closeSession();
|
|
130 |
sessionConnection.close();
|
|
131 |
}
|
|
132 |
|
|
133 |
void registerTab(int destinationTabId, ToolHandler toolHandler, DebugSession debugSession)
|
|
134 |
throws IOException {
|
|
135 |
try {
|
|
136 |
tabId2ToolHandler.put(destinationTabId, toolHandler);
|
|
137 |
} catch (IllegalStateException e) {
|
|
138 |
throw new IOException("Tab id=" + destinationTabId + " cannot be attached");
|
|
139 |
}
|
|
140 |
tabId2DebugSession.put(destinationTabId, debugSession);
|
|
141 |
}
|
|
142 |
|
|
143 |
void unregisterTab(int destinationTabId) {
|
|
144 |
tabId2DebugSession.remove(destinationTabId);
|
|
145 |
tabId2ToolHandler.remove(destinationTabId);
|
|
146 |
}
|
|
147 |
|
|
148 |
private DevToolsServiceHandler getDevToolsServiceHandler() {
|
|
149 |
return devToolsHandler;
|
|
150 |
}
|
|
151 |
|
|
152 |
@Override
|
|
153 |
protected void checkHealth() {
|
|
154 |
// We do not actually interrupt here. It's more an assert for now: we throw an exception,
|
|
155 |
// if connection is unexpectedly closed.
|
|
156 |
if (sessionConnection.isConnected()) {
|
|
157 |
// All OK
|
|
158 |
return;
|
|
159 |
}
|
|
160 |
// We should not be here
|
|
161 |
LOGGER.severe("checkHealth in BrowserImpl found a consistnecy problem; " +
|
|
162 |
"current session is broken and therefore terminated");
|
|
163 |
interruptSession();
|
|
164 |
closeSession();
|
|
165 |
}
|
|
166 |
|
|
167 |
private void checkConnection() {
|
|
168 |
if (!sessionConnection.isConnected()) {
|
|
169 |
throw new IllegalStateException("connection is not started");
|
|
170 |
}
|
|
171 |
}
|
|
172 |
|
|
173 |
private final NetListener netListener = new NetListener() {
|
|
174 |
public void connectionClosed() {
|
|
175 |
devToolsHandler.onDebuggerDetached();
|
|
176 |
// Use a copy to avoid the underlying map modification in #sessionTerminated
|
|
177 |
// invoked through #onDebuggerDetached
|
|
178 |
Collection<DebugSession> copy = new ArrayList<DebugSession>(tabId2DebugSession.values());
|
|
179 |
for (DebugSession session : copy) {
|
|
180 |
session.onDebuggerDetached();
|
|
181 |
}
|
|
182 |
}
|
|
183 |
|
|
184 |
public void messageReceived(Message message) {
|
|
185 |
ToolName toolName = ToolName.forString(message.getTool());
|
|
186 |
if (toolName == null) {
|
|
187 |
LOGGER.log(Level.SEVERE, "Bad 'Tool' header received: {0}", message.getTool());
|
|
188 |
return;
|
|
189 |
}
|
|
190 |
ToolHandler handler = null;
|
|
191 |
switch (toolName) {
|
|
192 |
case DEVTOOLS_SERVICE:
|
|
193 |
handler = devToolsHandler;
|
|
194 |
break;
|
|
195 |
case V8_DEBUGGER:
|
|
196 |
handler = tabId2ToolHandler.get(Integer.valueOf(message.getDestination()));
|
|
197 |
break;
|
|
198 |
default:
|
|
199 |
LOGGER.log(Level.SEVERE, "Unregistered handler for tool: {0}", message.getTool());
|
|
200 |
return;
|
|
201 |
}
|
|
202 |
if (handler != null) {
|
|
203 |
handler.handleMessage(message);
|
|
204 |
} else {
|
|
205 |
LOGGER.log(
|
|
206 |
Level.SEVERE,
|
|
207 |
"null handler for tool: {0}, destination: {1}",
|
|
208 |
new Object[] {message.getTool(), message.getDestination()});
|
|
209 |
}
|
|
210 |
}
|
|
211 |
public void eosReceived() {
|
|
212 |
boolean res = alreadyClosingSession.compareAndSet(false, true);
|
|
213 |
if (!res) {
|
|
214 |
// already closing
|
|
215 |
return;
|
|
216 |
}
|
|
217 |
|
|
218 |
Collection<ToolHandler> allHandlers = tabId2ToolHandler.close().values();
|
|
219 |
for (ToolHandler handler : allHandlers) {
|
|
220 |
handler.handleEos();
|
|
221 |
}
|
|
222 |
|
|
223 |
devToolsHandler.handleEos();
|
|
224 |
Collection<? extends RuntimeException> problems = interruptSession();
|
|
225 |
for (RuntimeException ex : problems) {
|
|
226 |
LOGGER.log(Level.SEVERE, "Failure in closing connections", ex);
|
|
227 |
}
|
|
228 |
closeSession();
|
|
229 |
}
|
|
230 |
};
|
|
231 |
|
|
232 |
private final ToolOutput devToolsToolOutput = new ToolOutput() {
|
|
233 |
public void send(String content) {
|
|
234 |
Message message =
|
|
235 |
MessageFactory.createMessage(ToolName.DEVTOOLS_SERVICE.value, null, content);
|
|
236 |
sessionConnection.send(message);
|
|
237 |
}
|
|
238 |
};
|
|
239 |
|
|
240 |
public BrowserImpl getBrowser() {
|
|
241 |
return BrowserImpl.this;
|
|
242 |
}
|
|
243 |
}
|
|
244 |
|
|
245 |
private class TabFetcherImpl implements TabFetcher {
|
|
246 |
private final SessionManager.Ticket<Session> ticket;
|
|
247 |
|
|
248 |
TabFetcherImpl(SessionManager.Ticket<Session> ticket) {
|
|
249 |
this.ticket = ticket;
|
|
250 |
}
|
|
251 |
|
|
252 |
public List<? extends TabConnector> getTabs() {
|
|
253 |
Session session = ticket.getSession();
|
|
254 |
session.checkConnection();
|
|
255 |
List<TabIdAndUrl> entries = session.devToolsHandler.listTabs(OPERATION_TIMEOUT_MS);
|
|
256 |
List<TabConnectorImpl> tabConnectors = new ArrayList<TabConnectorImpl>(entries.size());
|
|
257 |
for (TabIdAndUrl entry : entries) {
|
|
258 |
tabConnectors.add(new TabConnectorImpl(entry.id, entry.url, ticket));
|
|
259 |
}
|
|
260 |
return tabConnectors;
|
|
261 |
}
|
|
262 |
|
|
263 |
public void dismiss() {
|
|
264 |
ticket.dismiss();
|
|
265 |
}
|
|
266 |
}
|
|
267 |
|
|
268 |
private class TabConnectorImpl implements TabConnector {
|
|
269 |
private final int tabId;
|
|
270 |
private final String url;
|
|
271 |
// Ticket that we inherit from TabFetcher.
|
|
272 |
private final SessionManager.Ticket<Session> ticket;
|
|
273 |
|
|
274 |
TabConnectorImpl(int tabId, String url, SessionManager.Ticket<Session> ticket) {
|
|
275 |
this.tabId = tabId;
|
|
276 |
this.url = url;
|
|
277 |
this.ticket = ticket;
|
|
278 |
}
|
|
279 |
|
|
280 |
public String getUrl() {
|
|
281 |
return url;
|
|
282 |
}
|
|
283 |
|
|
284 |
public boolean isAlreadyAttached() {
|
|
285 |
return ticket.getSession().tabId2ToolHandler.get(tabId) != null;
|
|
286 |
}
|
|
287 |
|
|
288 |
public BrowserTab attach(TabDebugEventListener listener) throws IOException {
|
|
289 |
SessionManager.Ticket<Session> ticket;
|
|
290 |
try {
|
|
291 |
ticket = connectInternal();
|
|
292 |
} catch (UnsupportedVersionException e) {
|
|
293 |
// This exception should have happened on tab fetcher creation.
|
|
294 |
throw new IOException("Unexpected version problem", e);
|
|
295 |
}
|
|
296 |
|
|
297 |
Session session = ticket.getSession();
|
|
298 |
|
|
299 |
BrowserTabImpl browserTab = null;
|
|
300 |
try {
|
|
301 |
browserTab = new BrowserTabImpl(tabId, url, session.sessionConnection, ticket);
|
|
302 |
} finally {
|
|
303 |
if (browserTab == null) {
|
|
304 |
ticket.dismiss();
|
|
305 |
}
|
|
306 |
}
|
|
307 |
// From now on browserTab is responsible for the ticket.
|
|
308 |
browserTab.attach(listener);
|
|
309 |
return browserTab;
|
|
310 |
}
|
|
311 |
}
|
|
312 |
|
|
313 |
/**
|
|
314 |
* With this session manager we expect all ticket owners to call dismiss in any
|
|
315 |
* circumstances.
|
|
316 |
*/
|
|
317 |
private class ConnectionSessionManager extends
|
|
318 |
SessionManager<BrowserImpl.Session, ExceptionWrapper> {
|
|
319 |
@Override
|
|
320 |
protected Session newSessionObject() throws ExceptionWrapper {
|
|
321 |
try {
|
|
322 |
return new Session();
|
|
323 |
} catch (IOException e) {
|
|
324 |
throw ExceptionWrapper.wrap(e);
|
|
325 |
} catch (UnsupportedVersionException e) {
|
|
326 |
throw ExceptionWrapper.wrap(e);
|
|
327 |
}
|
|
328 |
}
|
|
329 |
}
|
|
330 |
|
|
331 |
private static abstract class ExceptionWrapper extends Exception {
|
|
332 |
abstract void rethrow() throws IOException, UnsupportedVersionException;
|
|
333 |
|
|
334 |
static ExceptionWrapper wrap(final IOException e) {
|
|
335 |
return new ExceptionWrapper() {
|
|
336 |
@Override
|
|
337 |
void rethrow() throws IOException {
|
|
338 |
throw e;
|
|
339 |
}
|
|
340 |
};
|
|
341 |
}
|
|
342 |
|
|
343 |
static ExceptionWrapper wrap(final UnsupportedVersionException e) {
|
|
344 |
return new ExceptionWrapper() {
|
|
345 |
@Override
|
|
346 |
void rethrow() throws UnsupportedVersionException {
|
|
347 |
throw e;
|
|
348 |
}
|
|
349 |
};
|
|
350 |
}
|
|
351 |
}
|
|
352 |
|
|
353 |
public boolean isTabConnectedForTest(int tabId) {
|
|
354 |
Session session = sessionManager.getCurrentSessionForTest();
|
|
355 |
if (session == null) {
|
|
356 |
return false;
|
|
357 |
}
|
|
358 |
return session.tabId2ToolHandler.get(tabId) != null;
|
|
359 |
}
|
|
360 |
|
|
361 |
public DevToolsServiceHandler getDevToolsServiceHandlerForTests() {
|
|
362 |
return sessionManager.getCurrentSessionForTest().getDevToolsServiceHandler();
|
|
363 |
}
|
|
364 |
|
|
365 |
public boolean isConnectedForTests() {
|
|
366 |
return sessionManager.getCurrentSessionForTest() != null;
|
|
367 |
}
|
|
368 |
}
|