Source

jsprobes-patches / xpcom-nsIProbeService

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
# HG changeset patch
# User Steve Fink <sfink@mozilla.com>
# Date 1315439921 25200
# Node ID 485bc3c84dd3b340bade0754c33d4cc094198e5e
# Parent 6e7bf9c91bf2aa849df5c5d9b0ca02292fef23fa
[mq]: xpcom-probes

diff --git a/toolkit/components/Makefile.in b/toolkit/components/Makefile.in
--- a/toolkit/components/Makefile.in
+++ b/toolkit/components/Makefile.in
@@ -61,16 +61,17 @@ PARALLEL_DIRS += \
   filepicker \
   find \
   intl \
   microformats \
   parentalcontrols \
   passwordmgr \
   perf \
   places \
+  probes \
   prompts \
   reflect \
   startup \
   statusfilter \
   telemetry \
   typeaheadfind \
   urlformatter \
   viewconfig \
diff --git a/toolkit/components/probes/Makefile.in b/toolkit/components/probes/Makefile.in
new file mode 100644
--- /dev/null
+++ b/toolkit/components/probes/Makefile.in
@@ -0,0 +1,85 @@
+#
+# ***** BEGIN LICENSE BLOCK *****
+# Version: MPL 1.1/GPL 2.0/LGPL 2.1
+#
+# The contents of this file are subject to the Mozilla Public License Version
+# 1.1 (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+# http://www.mozilla.org/MPL/
+#
+# Software distributed under the License is distributed on an "AS IS" basis,
+# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+# for the specific language governing rights and limitations under the
+# License.
+#
+# The Original Code is mozilla.org code.
+#
+# The Initial Developer of the Original Code is
+# The Mozilla Foundation.
+# Portions created by the Initial Developer are Copyright (C) 2011
+# the Initial Developer. All Rights Reserved.
+#
+# Contributor(s):
+#  Brian J. Burg <burg@cs.washington.edu>
+#
+# Alternatively, the contents of this file may be used under the terms of
+# either the GNU General Public License Version 2 or later (the "GPL"), or
+# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+# in which case the provisions of the GPL or the LGPL are applicable instead
+# of those above. If you wish to allow use of your version of this file only
+# under the terms of either the GPL or the LGPL, and not to allow others to
+# use your version of this file under the terms of the MPL, indicate your
+# decision by deleting the provisions above and replace them with the notice
+# and other provisions required by the GPL or the LGPL. If you do not delete
+# the provisions above, a recipient may use your version of this file under
+# the terms of any one of the MPL, the GPL or the LGPL.
+#
+# ***** END LICENSE BLOCK *****
+
+DEPTH = ../../..
+topsrcdir = @top_srcdir@
+srcdir = @srcdir@
+VPATH = @srcdir@
+
+include $(DEPTH)/config/autoconf.mk
+
+MODULE = probes
+XPIDL_MODULE = probes
+MODULE_NAME = probes
+GRE_MODULE = 1
+
+LIBXUL_LIBRARY = 1
+EXPORT_LIBRARY = 1
+IS_COMPONENT = 1
+
+LIBRARY_NAME = probes
+
+EXPORTS_NAMESPACES = mozilla
+
+EXPORTS_mozilla = \
+  ProbeService.h \
+  $(NULL)
+
+XPIDLSRCS = \
+  nsIProbeService.idl \
+  $(NULL)
+
+#EXTRA_COMPONENTS = \
+#  $(NULL)
+
+CPPSRCS = \
+  ProbeService.cpp \
+  $(NULL)
+
+EXTRA_DSO_LDOPTS += \
+  $(MOZ_COMPONENT_LIBS) \
+  $(MOZ_JS_LIBS) \
+  $(NULL)
+
+#ifdef ENABLE_TESTS
+#DIRS += tests
+#endif
+
+include $(topsrcdir)/config/config.mk
+include $(topsrcdir)/config/rules.mk
+include $(topsrcdir)/ipc/chromium/chromium-config.mk
diff --git a/toolkit/components/probes/ProbeService.cpp b/toolkit/components/probes/ProbeService.cpp
new file mode 100644
--- /dev/null
+++ b/toolkit/components/probes/ProbeService.cpp
@@ -0,0 +1,438 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
+ * vim: set ts=8 sw=4 et tw=99 ft=cpp:
+ *
+ * ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is mozilla.org code.
+ *
+ * The Initial Developer of the Original Code is
+ *   the Mozilla Corporation.
+ *
+ * Contributor(s):
+ *   Brian J. Burg <burg@cs.washington.edu> (Original Author)
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either of the GNU General Public License Version 2 or later (the "GPL"),
+ * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the MPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the MPL, the GPL or the LGPL.
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+#include "ProbeService.h"
+
+#include "nsIProbeService.h"
+#include "nsIObserver.h"
+#include "nsIObserverService.h"
+#include "nsIScriptContext.h"
+#include "nsITimer.h"
+
+#include "jsprobemgr.h"
+#include "mozilla/ModuleUtils.h"
+#include "nsCOMPtr.h"
+#include "nsContentUtils.h"
+#include "nsDOMJSUtils.h"
+#include "nsInterfaceHashtable.h"
+#include "nsServiceManagerUtils.h"
+#include "nsThreadUtils.h"
+#include "prlog.h"
+
+
+#if PR_LOGGING
+PRLogModuleInfo *gProbeServiceLog = nsnull;
+#endif
+#define LOG(_args) PR_LOG(gProbeServiceLog, PR_LOG_DEBUG, _args)
+
+namespace mozilla {
+
+class ProbeServiceImpl : public nsIProbeService
+                       , public nsIObserver
+{
+    NS_DECL_ISUPPORTS
+    NS_DECL_NSIPROBESERVICE
+    NS_DECL_NSIOBSERVER
+
+public:
+    ProbeServiceImpl();
+    ~ProbeServiceImpl();
+
+    nsresult Init();
+    /* called to actually reclaim members. */
+    nsresult Cleanup();
+
+    static already_AddRefed<nsIProbeService> CreateInstance();
+    /* called by the service manager to remove refs, trigger destructor */
+    static void Shutdown();
+
+private:
+    typedef nsInterfaceHashtable<nsUint32HashKey, nsIProbeMessageListener> ListenerMap;
+    typedef nsInterfaceHashtable<nsUint32HashKey, nsIScriptContext> ContextMap;
+
+    bool mHasShutdown;
+    nsCOMPtr<nsITimer> mDrainTimer;
+    js::probes::ScriptCookie mPreviousScript;
+    ListenerMap mListeners;
+    ContextMap mListenerContexts;
+    js::probes::ProbeManager *mManager;
+
+    static ProbeServiceImpl *sProbeService;
+
+    nsresult DispatchMessage(js::probes::Message *msg);
+    nsresult DrainMessages();
+    nsresult StartDrainTimer(PRInt32 inverval);
+
+};
+
+ProbeServiceImpl* ProbeServiceImpl::sProbeService = NULL;
+
+ProbeServiceImpl::ProbeServiceImpl()
+    : mHasShutdown(false)
+    , mPreviousScript(-1)
+    , mManager(NULL)
+{
+
+#if PR_LOGGING
+    if (!gProbeServiceLog) {
+        gProbeServiceLog = PR_NewLogModule("ProbeServiceImpl");
+    }
+#endif
+
+    LOG(("ProbeServiceImpl::ProbeServiceImpl"));
+}
+
+nsresult
+ProbeServiceImpl::Init()
+{
+    NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
+
+    LOG(("ProbeServiceImpl::Init -- initializing service..."));
+
+    nsresult rv;
+    nsCOMPtr<nsIObserverService> obs =
+        do_GetService(NS_OBSERVERSERVICE_CONTRACTID, &rv);
+    NS_ENSURE_SUCCESS(rv, rv);
+
+    rv = obs->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, PR_FALSE);
+    NS_ENSURE_SUCCESS(rv, rv);
+
+    LOG(("ProbeServiceImpl -- Creating timer instance"));
+    mDrainTimer = do_CreateInstance(NS_TIMER_CONTRACTID, &rv);
+    if (NS_FAILED(rv))
+        return rv;
+
+    mListeners.Init(5);
+    mListenerContexts.Init(5);
+    mManager = js::probes::ProbeManager::create();
+    mManager->start();
+
+    LOG(("ProbeServiceImpl::Init -- initialized!"));
+
+    return NS_OK;
+}
+
+ProbeServiceImpl::~ProbeServiceImpl()
+{
+    Cleanup();
+    LOG(("ProbeServiceImpl::~ProbeServiceImpl"));
+}
+
+already_AddRefed<nsIProbeService>
+ProbeServiceImpl::CreateInstance()
+{
+    NS_ABORT_IF_FALSE(sProbeService == NULL, "ProbeServiceImpl::CreateInstance may only be called once, via GetService()");
+    sProbeService = new ProbeServiceImpl();
+    sProbeService->Init();
+    /* AddRef each for static reference, and the caller. */
+    NS_ADDREF(sProbeService);
+    NS_ADDREF(sProbeService);
+
+    LOG(("ProbeServiceImpl::CreateInstance"));
+
+    return sProbeService;
+}
+
+nsresult
+ProbeServiceImpl::Cleanup()
+{
+    LOG(("ProbeServiceImpl::Cleanup -- starting cleanup..."));
+
+    /* reference for this section: dom/src/threads/nsIDOMThreadService.cpp */
+    NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
+
+/* TODO: kill timer */
+    LOG(("ProbeServiceImpl -- Cancelling Timer"));
+    mDrainTimer->Cancel();
+/* TODO: drain any remaining Messages synchronously */
+    mListeners.Clear();
+    mListenerContexts.Clear();
+    
+    nsresult rv;
+    nsCOMPtr<nsIObserverService> obs =
+        do_GetService(NS_OBSERVERSERVICE_CONTRACTID, &rv);
+    NS_ENSURE_SUCCESS(rv, rv);
+    rv = obs->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID);
+    NS_ENSURE_SUCCESS(rv, rv);
+
+    LOG(("ProbeServiceImpl::Cleanup -- stopping ProbeManager..."));
+    /* disables probes and joins handler thread */
+    mManager->shutdown();
+    mManager = NULL;
+
+    LOG(("ProbeServiceImpl::Cleanup -- done!"));
+    return NS_OK;
+}
+
+void
+ProbeServiceImpl::Shutdown()
+{
+    LOG(("ProbeServiceImpl::Shutdown"));
+    NS_IF_RELEASE(sProbeService);
+}
+
+nsresult
+ProbeServiceImpl::DrainMessages()
+{
+    NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
+
+    LOG(("ProbeServiceImpl -- Draining messages..."));
+
+    nsresult rv;
+
+    while (mManager->hasMessages()) {
+        js::probes::Message *msg = mManager->popMessage();
+        rv = DispatchMessage(msg);
+        NS_ENSURE_SUCCESS(rv, rv);
+    }
+    /* check timer termination condition */
+    if (!mManager->hasPendingScripts()) {
+        LOG(("ProbeServiceImpl -- Cancelling Timer"));
+        mDrainTimer->Cancel();
+    }
+
+    return NS_OK;
+}
+
+nsresult
+ProbeServiceImpl::DispatchMessage(js::probes::Message *msg)
+{
+    NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
+    NS_ENSURE_ARG(msg);
+
+    LOG(("ProbeServiceImpl::DispatchMessage -- dispatching one message"));
+    using namespace js::probes;
+
+    ScriptCookie cookie = msg->getCookie();
+
+    /* lookup listener for this scriptcookie */
+    nsCOMPtr<nsIProbeMessageListener> listener = mListeners.Get(cookie);
+    nsCOMPtr<nsIScriptContext> scriptCx = mListenerContexts.Get(cookie);
+    JSContext *cx = (JSContext *) scriptCx->GetNativeContext();
+
+    /* unpack message, using cx */
+    ScriptCookie outCookie;
+    jsval outVal;
+
+    JSAutoRequest req(cx);
+    /* put the JSContext onto the context stack. */
+    nsCxPusher cxPusher;
+    /* is this right, or should it push a null context? */
+    cxPusher.Push(cx);
+
+    JS_AddValueRoot(cx, &outVal);
+    msg->unpack(cx, &outVal, &outCookie);
+
+    LOG(("ProbeServiceImpl::DispatchMessage -- calling listener"));
+    /* call nsIProbeMessageListener.onMessage(jsval) */
+    listener->OnMessage(outVal);
+
+    JS_RemoveValueRoot(cx, &outVal);
+    cxPusher.Pop();
+
+    /* if script is different from last (and not first), then
+       it's time to remove the old listener from the listener map */
+
+    if (mListeners.Count() > 1 && mPreviousScript != cookie) {
+        mListeners.Remove(mPreviousScript);
+        mListenerContexts.Remove(mPreviousScript);
+    }
+
+    mPreviousScript = cookie;
+
+    return NS_OK;
+}
+
+/* note that interval specifies milliseconds */
+nsresult
+ProbeServiceImpl::StartDrainTimer(PRInt32 interval)
+{
+    NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
+    NS_ASSERTION(mDrainTimer, "cannot use uninitialized timer");
+
+    LOG(("ProbeServiceImpl -- initialized/started timer"));
+    return mDrainTimer->Init(this, interval, nsITimer::TYPE_REPEATING_SLACK);
+}
+
+// nsIObserver
+
+NS_IMETHODIMP
+ProbeServiceImpl::Observe(nsISupports *subject, const char *topic,
+                          const PRUnichar *data)
+{
+  NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
+
+  if (strcmp(topic, NS_XPCOM_SHUTDOWN_OBSERVER_ID) == 0) {
+      Cleanup();
+      return NS_OK;
+  }
+  else if (strcmp(topic, NS_TIMER_CALLBACK_TOPIC) == 0) {
+    nsresult rv = DrainMessages();
+    if (NS_FAILED(rv)) {
+        LOG(("ProbeServiceImpl -- Cancelling Timer"));
+        mDrainTimer->Cancel();
+    }
+  }
+  return NS_OK;
+}
+
+
+/*  PRInt32 addHandler(in PRInt32 probe, in AString dataSpec, in AString handler); */
+
+NS_IMETHODIMP
+ProbeServiceImpl::AddHandler(PRInt32 aProbe, const nsAString &aDataSpec, const nsAString &aHandler, PRInt32 *_rval)
+{
+    using namespace js::probes;
+
+    LOG(("ProbeServiceImpl::AddHandler"));
+
+    NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
+    NS_ASSERTION(mManager, "Manager is missing!");
+   
+    if (aDataSpec.IsEmpty())
+        return NS_ERROR_INVALID_ARG;
+    if (aHandler.IsEmpty())
+        return NS_ERROR_INVALID_ARG;
+
+    /* get pointer to buffer contained in |code| */
+    nsAString::const_iterator specIt;
+    nsAString::const_iterator handlerIt;
+    aDataSpec.BeginReading(specIt);
+    aHandler.BeginReading(handlerIt);
+
+    ScriptDescriptor specDesc = { reinterpret_cast<const jschar *>(specIt.get()),
+                                  aDataSpec.Length(),
+                                  "dummy-spec-file", 1 };
+    ScriptDescriptor handlerDesc = { reinterpret_cast<const jschar *>(handlerIt.get()),
+                                     aHandler.Length(),
+                                     "dummy-handler-file", 1};
+    
+    HandlerCookie cookie;
+
+    if (!mManager->addProbeHandler((ProbePoint)aProbe, handlerDesc, specDesc, &cookie))
+      return NS_ERROR_FAILURE;
+
+    *_rval = cookie;
+    return NS_OK;
+}
+
+/* void removeHandler(in PRInt32 cookie); */
+
+NS_IMETHODIMP
+ProbeServiceImpl::RemoveHandler(PRInt32 cookie)
+{
+    LOG(("ProbeServiceImpl::RemoveHandler"));
+
+    NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
+    NS_ASSERTION(mManager, "Manager is missing!");
+
+    if (!mManager->removeProbeHandler(cookie))
+        return NS_ERROR_FAILURE;
+
+    return NS_OK;
+}
+
+/* void asyncQuery(in AString code, in nsIProbeMessageListener listener); */
+
+NS_IMETHODIMP
+ProbeServiceImpl::AsyncQuery(const nsAString &code, nsIProbeMessageListener *listener, JSContext *cx)
+{
+    using namespace js::probes;
+
+    LOG(("ProbeServiceImpl::AsyncQuery"));
+
+    NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
+    NS_ENSURE_ARG(listener);
+    NS_ENSURE_ARG(cx);
+
+    if (code.IsEmpty())
+        return NS_ERROR_INVALID_ARG;
+
+    // get pointer to buffer contained in |code|
+    nsAString::const_iterator codeIter;
+    code.BeginReading(codeIter);
+
+    ScriptDescriptor codeDesc = { reinterpret_cast<const jschar *>(codeIter.get()),
+                                  code.Length(),
+                                  "dummy-async-query-file", 1 };
+    ScriptCookie codeCookie;
+
+    if (!mManager->runScriptOnce(codeDesc, &codeCookie))
+        return NS_ERROR_FAILURE;
+
+    nsIScriptContext *scriptCx = GetScriptContextFromJSContext(cx);
+    if (!scriptCx)
+        return NS_ERROR_FAILURE;
+
+    mListeners.Put(codeCookie, listener);
+    mListenerContexts.Put(codeCookie, scriptCx);
+    StartDrainTimer(1000);
+
+    return NS_OK;
+}
+
+NS_IMPL_THREADSAFE_ISUPPORTS1(ProbeServiceImpl, nsIProbeService)
+NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsIProbeService, ProbeServiceImpl::CreateInstance)
+
+#define NS_PROBESERVICE_CID \
+{0x44ac0450, 0xeb4c, 0x4a4d, {0xa1, 0x9c, 0x93, 0x38, 0x91, 0x59, 0xa6, 0x27}}
+NS_DEFINE_NAMED_CID(NS_PROBESERVICE_CID);
+
+const Module::CIDEntry kProbeServiceCIDs[] = {
+  { &kNS_PROBESERVICE_CID, false, NULL, nsIProbeServiceConstructor },
+  { NULL }
+};
+
+const Module::ContractIDEntry kProbeServiceContracts[] = {
+  { "@mozilla.org/base/probes;1", &kNS_PROBESERVICE_CID },
+  { NULL }
+};
+
+const Module kProbeServiceModule = {
+  Module::kVersion,
+  kProbeServiceCIDs,
+  kProbeServiceContracts,
+  NULL,
+  NULL,
+  NULL,
+  ProbeServiceImpl::Shutdown
+};
+
+} // namespace mozilla
+
+NSMODULE_DEFN(nsProbesModule) = &mozilla::kProbeServiceModule;
diff --git a/toolkit/components/probes/ProbeService.h b/toolkit/components/probes/ProbeService.h
new file mode 100644
--- /dev/null
+++ b/toolkit/components/probes/ProbeService.h
@@ -0,0 +1,49 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
+ * vim: set ts=8 sw=4 et tw=99 ft=cpp:
+ *
+ * ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is mozilla.org code.
+ *
+ * The Initial Developer of the Original Code is
+ *   the Mozilla Corporation.
+ *
+ * Contributor(s):
+ *   Brian J. Burg <burg@cs.washington.edu> (Original Author)
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either of the GNU General Public License Version 2 or later (the "GPL"),
+ * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the MPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the MPL, the GPL or the LGPL.
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+#ifndef ProbeService_h__
+#define ProbeService_h__
+
+namespace mozilla {
+namespace Probes {
+
+/* TODO: define an enum here? Should be same as in jsprobedefs.h */
+
+} // namespace Probes
+} // namespace mozilla
+#endif // ProbeService_h__
diff --git a/toolkit/components/probes/nsIProbeService.idl b/toolkit/components/probes/nsIProbeService.idl
new file mode 100644
--- /dev/null
+++ b/toolkit/components/probes/nsIProbeService.idl
@@ -0,0 +1,109 @@
+/* -*- Mode: C++; tab-width: 50; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is mozilla.org code.
+ *
+ * The Initial Developer of the Original Code is
+ * mozilla.org
+ * Portions created by the Initial Developer are Copyright (C) 2011
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ *   Steve Fink <sfink@mozilla.com> (original author)
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either of the GNU General Public License Version 2 or later (the "GPL"),
+ * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the MPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the MPL, the GPL or the LGPL.
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+#include "nsISupports.idl"
+
+[scriptable, function, uuid(2f66e0f5-4f9d-4588-a74d-fdd80530b4f9)]
+interface nsIProbeMessageListener : nsISupports
+{
+  void onMessage(inout jsval jsonMessage);
+};
+
+[scriptable, uuid(17587ce9-c60b-4d42-a251-b15499dd6629)]
+interface nsIProbeService : nsISupports
+{
+  /* These constants should be in sync with the probes as numbered in
+   * the file js/src/jsprobes.tbl
+   */
+  const PRInt32 JS_RUNTIME_CREATE          =  1;
+  const PRInt32 JS_RUNTIME_DESTROY         =  2;
+  const PRInt32 JS_SHUTDOWN                =  3;
+  const PRInt32 JS_FUNCTION_ENTER          =  4;
+  const PRInt32 JS_FUNCTION_EXIT           =  5;
+  const PRInt32 JS_WILL_EXECUTE_SCRIPT     =  6;
+  const PRInt32 JS_DID_EXECUTE_SCRIPT      =  7;
+  const PRInt32 JS_WILL_RESIZE_HEAP        =  8;
+  const PRInt32 JS_OBJECT_CREATE           =  9;
+  const PRInt32 JS_OBJECT_RESIZE           = 10;
+  const PRInt32 JS_OBJECT_FINALIZE         = 11;
+  const PRInt32 JS_STRING_CREATE           = 12;
+  const PRInt32 JS_STRING_FINALIZE         = 13;
+  const PRInt32 JS_WILL_COMPILE_SCRIPT     = 14;
+  const PRInt32 JS_DID_COMPILE_SCRIPT      = 15;
+  const PRInt32 JS_WILL_CALL_NATIVE        = 16;
+  const PRInt32 JS_DID_CALL_NATIVE         = 17;
+  const PRInt32 JS_ACQUIRE_MEMORY          = 18;
+  const PRInt32 JS_RELEASE_MEMORY          = 19;
+  const PRInt32 COMPARTMENT_GC_DID_START   = 20;
+  const PRInt32 COMPARTMENT_GC_WILL_END    = 21;
+  const PRInt32 GLOBAL_GC_DID_START        = 22;
+  const PRInt32 GLOBAL_GC_WILL_END         = 23;
+  const PRInt32 GC_MARK_PHASE_START        = 24;
+  const PRInt32 GC_MARK_PHASE_END          = 25;
+  const PRInt32 GC_SWEEP_PHASE_START       = 26;
+  const PRInt32 GC_SWEEP_PHASE_END         = 27;
+
+  /**
+   * Add a handler that should execute whenever the specified probe is fired. 
+   * @param probe      The id of the probe to which the handler should be attached.
+   *                   the probe ids are defined as constants of this interface.
+   * @param dataSpec   A script snippet that defines the data to serialize for the
+   *                   probe handler code. See documentation for usage.
+   * @param handler    The function body of the handler to execute for this probe.
+   *                   Arguments to the function are named and described in jsprobes.tbl,
+   *                   and in the documentation. 
+   * @retval           A cookie value that can be used to reference this handler.
+   */
+  PRInt32 addHandler(in PRInt32 probe, in AString dataSpec, in AString handler);
+
+  /**
+   * Remove the specified handler from the probe it is associated with.
+   * @param cookie     A cookie referring to the handler that should be removed.
+   */
+  void removeHandler(in PRInt32 cookie);
+
+  /**
+   * Asynchronously evaluate arbitrary JavaScript in the probe handler runtime.
+   * 
+   * @param code       Script to be evaluated.
+   * @param listener   Listener to dispatch upon receiving messages via postMessage.
+   */
+  [implicit_jscontext]
+  void asyncQuery(in AString code, in nsIProbeMessageListener listener);
+};
+
diff --git a/toolkit/content/Services.jsm b/toolkit/content/Services.jsm
--- a/toolkit/content/Services.jsm
+++ b/toolkit/content/Services.jsm
@@ -70,16 +70,17 @@ let initTable = [
   ["cookies", "@mozilla.org/cookiemanager;1", "nsICookieManager2"],
   ["droppedLinkHandler", "@mozilla.org/content/dropped-link-handler;1", "nsIDroppedLinkHandler"],
   ["eTLD", "@mozilla.org/network/effective-tld-service;1", "nsIEffectiveTLDService"],
   ["io", "@mozilla.org/network/io-service;1", "nsIIOService2"],
   ["locale", "@mozilla.org/intl/nslocaleservice;1", "nsILocaleService"],
   ["logins", "@mozilla.org/login-manager;1", "nsILoginManager"],
   ["obs", "@mozilla.org/observer-service;1", "nsIObserverService"],
   ["perms", "@mozilla.org/permissionmanager;1", "nsIPermissionManager"],
+  ["probes", "@mozilla.org/base/probes;1", "nsIProbeService"],
   ["prompt", "@mozilla.org/embedcomp/prompt-service;1", "nsIPromptService"],
   ["scriptloader", "@mozilla.org/moz/jssubscript-loader;1", "mozIJSSubScriptLoader"],
   ["scriptSecurityManager", "@mozilla.org/scriptsecuritymanager;1", "nsIScriptSecurityManager"],
 #ifdef MOZ_TOOLKIT_SEARCH
   ["search", "@mozilla.org/browser/search-service;1", "nsIBrowserSearchService"],
 #endif
   ["storage", "@mozilla.org/storage/service;1", "mozIStorageService"],
   ["domStorageManager", "@mozilla.org/dom/storagemanager;1", "nsIDOMStorageManager"],
diff --git a/toolkit/library/Makefile.in b/toolkit/library/Makefile.in
--- a/toolkit/library/Makefile.in
+++ b/toolkit/library/Makefile.in
@@ -154,16 +154,17 @@ COMPONENT_LIBS += \
   commandlines \
   toolkitcomps \
   pipboot \
   pipnss \
   appcomps \
   jsreflect \
   composer \
   telemetry \
+  probes \
   jsinspector \
   jsdebugger \
   storagecomps \
   rdf \
   windowds \
   $(NULL)
 
 ifdef BUILD_CTYPES
diff --git a/toolkit/library/nsStaticXULComponents.cpp b/toolkit/library/nsStaticXULComponents.cpp
--- a/toolkit/library/nsStaticXULComponents.cpp
+++ b/toolkit/library/nsStaticXULComponents.cpp
@@ -246,16 +246,17 @@
     OSXPROXY_MODULE                          \
     WINDOWSPROXY_MODULE                      \
     JSCTYPES_MODULE                          \
     MODULE(jsreflect)                        \
     MODULE(jsperf)                           \
     MODULE(nsServicesCryptoModule)           \
     MOZ_APP_COMPONENT_MODULES                \
     MODULE(nsTelemetryModule)                \
+    MODULE(nsProbesModule)                   \
     MODULE(jsinspector)                      \
     MODULE(jsdebugger)                       \
     /* end of list */
 
 #define MODULE(_name) \
   NSMODULE_DECL(_name);
 
 XUL_MODULES