- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / common / extensions / docs / examples / api / debugger / pause-resume / background.js
1 // Copyright (c) 2011 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 var attachedTabs = {};
6 var version = "1.0";
7
8 chrome.debugger.onEvent.addListener(onEvent);
9 chrome.debugger.onDetach.addListener(onDetach);
10
11 chrome.browserAction.onClicked.addListener(function(tab) {
12   var tabId = tab.id;
13   var debuggeeId = {tabId:tabId};
14
15   if (attachedTabs[tabId] == "pausing")
16     return;
17
18   if (!attachedTabs[tabId])
19     chrome.debugger.attach(debuggeeId, version, onAttach.bind(null, debuggeeId));
20   else if (attachedTabs[tabId])
21     chrome.debugger.detach(debuggeeId, onDetach.bind(null, debuggeeId));
22 });
23
24 function onAttach(debuggeeId) {
25   if (chrome.runtime.lastError) {
26     alert(chrome.runtime.lastError.message);
27     return;
28   }
29
30   var tabId = debuggeeId.tabId;
31   chrome.browserAction.setIcon({tabId: tabId, path:"debuggerPausing.png"});
32   chrome.browserAction.setTitle({tabId: tabId, title:"Pausing JavaScript"});
33   attachedTabs[tabId] = "pausing";
34   chrome.debugger.sendCommand(
35       debuggeeId, "Debugger.enable", {},
36       onDebuggerEnabled.bind(null, debuggeeId));
37 }
38
39 function onDebuggerEnabled(debuggeeId) {
40   chrome.debugger.sendCommand(debuggeeId, "Debugger.pause");
41 }
42
43 function onEvent(debuggeeId, method) {
44   var tabId = debuggeeId.tabId;
45   if (method == "Debugger.paused") {
46     attachedTabs[tabId] = "paused";
47     chrome.browserAction.setIcon({tabId:tabId, path:"debuggerContinue.png"});
48     chrome.browserAction.setTitle({tabId:tabId, title:"Resume JavaScript"});
49   }
50 }
51
52 function onDetach(debuggeeId) {
53   var tabId = debuggeeId.tabId;
54   delete attachedTabs[tabId];
55   chrome.browserAction.setIcon({tabId:tabId, path:"debuggerPause.png"});
56   chrome.browserAction.setTitle({tabId:tabId, title:"Pause JavaScript"});
57 }