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

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 // This is the background window. It can access the necessary APIs to get
6 // at the console messages. It can only communicate with the content
7 // window through message passing.
8 //
9 // There is no way to query the console messages, as such, but we can
10 // set up a handler that is called when there are console messages. This
11 // will be called with any console messages already present, so it can be set
12 // up after the fact. However, if there are no messages it won't be called.
13 // To handle the end of the messages (or no messages) we have to use a timer
14 // that can fire when the handler has been idle for a while.
15
16 var version = "1.0";
17 var messages = []; // An array that we can put messages in.
18 var debuggeeId; // An object that identifies the browser tab we are talking to.
19 var callback; // For passing back the response to the content window.
20 var timer; // To time out if no messages are available.
21
22 function allDone() {
23 callback(messages); // Send back the messages we obtained.
Emily Fortuna 2012/10/09 18:15:22 do we want to do the google style javascript comme
24 // Turn off console listening and debugging.
25 chrome.debugger.sendCommand(debuggeeId, "Console.disable", {}, function() { }) ;
Emily Fortuna 2012/10/09 18:15:22 80 char
gram 2012/10/09 18:57:14 Done.
26 chrome.debugger.detach(debuggeeId, function() {});
27 }
28
29 function onEvent(debuggeeId, method, params) {
30 clearTimeout(timer); // Reset the timeout.
31 var tabId = debuggeeId.tabId;
32 if (method == "Console.messageAdded") {
33 var msg = params.message;
34 // More fields are available if we want them later. See
35 // https://developers.google.com/chrome-developer-tools/docs/\
36 // protocol/1.0/console#type-ConsoleMessage
ahe 2012/10/09 06:01:04 I don't think you need to wrap URIs.
gram 2012/10/09 18:57:14 Done.
37 messages.push({"source":msg.url, "line": msg.line,
38 "category":msg.source, "message":msg.text });
39 }
40 timer = setTimeout(allDone, 1000);
41 }
42
43 // Set up the general handler for debug events.
44 chrome.debugger.onEvent.addListener(onEvent);
45
46 // Handle requests sent by the content script.
47 function onRequest(request, sender, sendResponse) {
48 if (request.command == "getMessages") {
49 callback = sendResponse; // Save the callback for later.
50 // Get the window and tab we are talking to.
51 chrome.windows.getCurrent(function(win) {
52 chrome.tabs.getSelected(win.id, function(tab) {
53 debuggeeId = {tabId:tab.id};
54 // Attach the debugger to the tab.
55 chrome.debugger.attach(debuggeeId, version, function() {
56 if (chrome.extension.lastError) {
57 // Attach failed; send an empty response.
58 callback([]);
59 } else {
60 // Turn on console message event handling.
61 chrome.debugger.sendCommand(debuggeeId, "Console.enable", {},
62 function() {});
63 timer = setTimeout(allDone, 1000);
64 }
65 });
66 });
67 });
68 }
69 }
70
71 // Listen for the content script to send a message to the background page.
72 chrome.extension.onRequest.addListener(onRequest);
73
Emily Fortuna 2012/10/09 18:15:22 nits: extra newlines at the bottoms of this file a
gram 2012/10/09 18:57:14 Done.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698