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

Side by Side Diff: tools/testing/extensions/chrome/ConsoleCollector/background.js

Issue 11092015: Chrome extension to get console messages upon test failures. Unlike the (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 2 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) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 /**
6 * This is the background window. It can access the necessary APIs to get
7 * at the console messages. It can only communicate with the content
8 * window through message passing.
9 *
10 * There is no way to query the console messages, as such, but we can
11 * set up a handler that is called when there are console messages. This
12 * will be called with any console messages already present, so it can be set
13 * up after the fact. However, if there are no messages it won't be called.
14 * To handle the end of the messages (or no messages) we have to use a
15 * sentinel message that is logged by the content page.
16 */
17 var version = "1.0";
18 var messages = []; // An array that we can put messages in.
19 var debuggeeId; // An object that identifies the browser tab we are talking to.
20 var callback; // For passing back the response to the content window.
21 var timer; // To time out if no messages are available.
22
23 /**
24 * When we have collected all the messages, we send them back to the
25 * content page via the callback, turn off message collection, and
26 * detach the debugger from the browser tab.
27 */
28 function allDone() {
29 callback(messages);
30 chrome.debugger.sendCommand(debuggeeId, "Console.clearMessages", {},
31 function() {
32 chrome.debugger.sendCommand(debuggeeId, "Console.disable", {},
33 function() {});
34 chrome.debugger.detach(debuggeeId, function() {});
35 });
36 messages = [];
37 }
38
39 /**
40 * Debugger event handler. We only care about console.messageAdded
41 * events, in which case we add a new message object with the fields
42 * we care about to our messages array.
43 */
44 function onEvent(debuggeeId, method, params) {
45 var tabId = debuggeeId.tabId;
46 if (method == "Console.messageAdded") {
47 var msg = params.message;
48 // More fields are available if we want them later. See
49 // https://developers.google.com/chrome-developer-tools/docs/protocol/1.0/co nsole#type-ConsoleMessage
50 if (msg.text == 'getMessages/end') {
51 allDone();
52 } else {
53 messages.push({"source":msg.url, "line": msg.line,
54 "category":msg.source, "message":msg.text });
55 }
56 }
57 }
58
59 /**
60 * Handle requests sent by the content script. We save the callback,
61 * get the window and tab that is currently active, attach the
62 * debugger to that tab, and then turn on console message event
63 * handling, which will result in onEvent calls for each console
64 * message, including the ones that are already present in the console.
65 */
66 function onRequest(request, sender, sendResponse) {
67 if (request.command == "getMessages") {
68 callback = sendResponse;
69 chrome.windows.getCurrent(function(win) {
70 chrome.tabs.getSelected(win.id, function(tab) {
71 debuggeeId = {tabId:tab.id};
72 chrome.debugger.attach(debuggeeId, version, function() {
73 if (chrome.extension.lastError) {
74 // Attach failed; send an empty response.
75 callback([]);
76 } else {
77 chrome.debugger.sendCommand(debuggeeId, "Console.enable", {},
78 function() {});
79 //timer = setTimeout(allDone, 1000);
80 }
81 });
82 });
83 });
84 }
85 }
86
87 // Set up the general handler for debug events.
88 chrome.debugger.onEvent.addListener(onEvent);
89 // Listen for the content script to send a message to the background page.
90 chrome.extension.onRequest.addListener(onRequest);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698