|
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 |
|
9 import org.chromium.sdk.Browser; |
|
10 import org.chromium.sdk.BrowserTab; |
|
11 import org.chromium.sdk.DebugEventListener; |
|
12 import org.chromium.sdk.TabDebugEventListener; |
|
13 import org.chromium.sdk.internal.tools.ToolHandler; |
|
14 import org.chromium.sdk.internal.tools.ToolName; |
|
15 import org.chromium.sdk.internal.tools.ToolOutput; |
|
16 import org.chromium.sdk.internal.tools.v8.ChromeDevToolSessionManager; |
|
17 import org.chromium.sdk.internal.tools.v8.ChromeDevToolSessionManager.AttachmentFailureException; |
|
18 import org.chromium.sdk.internal.transport.Connection; |
|
19 import org.chromium.sdk.internal.transport.Message; |
|
20 |
|
21 /** |
|
22 * A default, thread-safe implementation of the BrowserTab interface. |
|
23 */ |
|
24 public class BrowserTabImpl extends JavascriptVmImpl implements BrowserTab { |
|
25 |
|
26 /** Tab ID as reported by the DevTools server. */ |
|
27 private final int tabId; |
|
28 |
|
29 /** The primary tab URL. */ |
|
30 private final String url; |
|
31 |
|
32 private final SessionManager.Ticket<BrowserImpl.Session> connectionTicket; |
|
33 |
|
34 private final ChromeDevToolSessionManager devToolSessionManager; |
|
35 |
|
36 /** The listener to report debug events to. */ |
|
37 private DebugEventListener debugEventListener = null; |
|
38 |
|
39 /** The listener to report browser-related debug events to. */ |
|
40 private TabDebugEventListener tabDebugEventListener = null; |
|
41 |
|
42 public BrowserTabImpl(int tabId, String url, Connection connection, |
|
43 SessionManager.Ticket<BrowserImpl.Session> ticket) throws IOException { |
|
44 this.tabId = tabId; |
|
45 this.url = url; |
|
46 this.connectionTicket = ticket; |
|
47 String tabIdString = String.valueOf(tabId); |
|
48 ChromeDevToolOutput chromeDevToolOutput = new ChromeDevToolOutput(tabIdString, connection); |
|
49 this.devToolSessionManager = new ChromeDevToolSessionManager(this, chromeDevToolOutput); |
|
50 |
|
51 ToolHandler toolHandler = devToolSessionManager.getToolHandler(); |
|
52 // After this statement we are responsible for dismissing our ticket (we do it via eos message). |
|
53 getBrowserConnectionSession().registerTab(tabId, toolHandler, |
|
54 this.devToolSessionManager.getDebugSession()); |
|
55 } |
|
56 |
|
57 public String getUrl() { |
|
58 return url; |
|
59 } |
|
60 |
|
61 public int getId() { |
|
62 return tabId; |
|
63 } |
|
64 |
|
65 @Override |
|
66 public DebugSession getDebugSession() { |
|
67 return devToolSessionManager.getDebugSession(); |
|
68 } |
|
69 |
|
70 public synchronized TabDebugEventListener getTabDebugEventListener() { |
|
71 return tabDebugEventListener; |
|
72 } |
|
73 |
|
74 public Browser getBrowser() { |
|
75 return getBrowserConnectionSession().getBrowser(); |
|
76 } |
|
77 |
|
78 public BrowserImpl.Session getBrowserConnectionSession() { |
|
79 return connectionTicket.getSession(); |
|
80 } |
|
81 |
|
82 synchronized void attach(TabDebugEventListener listener) throws IOException { |
|
83 this.tabDebugEventListener = listener; |
|
84 this.debugEventListener = listener.getDebugEventListener(); |
|
85 |
|
86 boolean normalExit = false; |
|
87 try { |
|
88 Result result; |
|
89 try { |
|
90 result = devToolSessionManager.attachToTab(); |
|
91 } catch (AttachmentFailureException e) { |
|
92 throw new IOException(e); |
|
93 } |
|
94 if (Result.OK != result) { |
|
95 throw new IOException("Failed to attach with result: " + result); |
|
96 } |
|
97 normalExit = true; |
|
98 } finally { |
|
99 if (!normalExit) { |
|
100 devToolSessionManager.cutTheLineMyself(); |
|
101 } |
|
102 } |
|
103 } |
|
104 |
|
105 public boolean detach() { |
|
106 Result result = devToolSessionManager.detachFromTab(); |
|
107 return Result.OK == result; |
|
108 } |
|
109 |
|
110 public boolean isAttached() { |
|
111 return devToolSessionManager.isAttachedForUi(); |
|
112 } |
|
113 |
|
114 public void sessionTerminated() { |
|
115 //browserSession.sessionTerminated(this.tabId); |
|
116 } |
|
117 |
|
118 public ToolHandler getV8ToolHandler() { |
|
119 return devToolSessionManager.getToolHandler(); |
|
120 } |
|
121 |
|
122 public ChromeDevToolSessionManager getSessionManager() { |
|
123 return devToolSessionManager; |
|
124 } |
|
125 |
|
126 public void handleEosFromToolService() { |
|
127 getBrowserConnectionSession().unregisterTab(tabId); |
|
128 connectionTicket.dismiss(); |
|
129 } |
|
130 |
|
131 private static class ChromeDevToolOutput implements ToolOutput { |
|
132 private final String destination; |
|
133 private final Connection connection; |
|
134 |
|
135 ChromeDevToolOutput(String destination, Connection connection) { |
|
136 this.destination = destination; |
|
137 this.connection = connection; |
|
138 } |
|
139 |
|
140 |
|
141 public void send(String content) { |
|
142 Message message = |
|
143 MessageFactory.createMessage(ToolName.V8_DEBUGGER.value, destination, content); |
|
144 connection.send(message); |
|
145 } |
|
146 } |
|
147 |
|
148 public DebugEventListener getDebugEventListener() { |
|
149 return debugEventListener; |
|
150 } |
|
151 } |