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