1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 #ifndef TelemetryCommon_h__
7 #define TelemetryCommon_h__
8
9 #include "nsTHashtable.h"
10 #include "jsapi.h"
11 #include "nsIScriptError.h"
12
13 namespace mozilla {
14 namespace Telemetry {
15 namespace Common {
16
17 template<class EntryType>
18 class AutoHashtable : public nsTHashtable<EntryType>
19 {
20 public:
21 explicit AutoHashtable(uint32_t initLength =
22 PLDHashTable::kDefaultInitialLength);
23 typedef bool (*ReflectEntryFunc)(EntryType *entry, JSContext *cx, JS::Handle<JSObject*> obj);
24 bool ReflectIntoJS(ReflectEntryFunc entryFunc, JSContext *cx, JS::Handle<JSObject*> obj);
25 };
26
27 template<class EntryType>
AutoHashtable(uint32_t initLength)28 AutoHashtable<EntryType>::AutoHashtable(uint32_t initLength)
29 : nsTHashtable<EntryType>(initLength)
30 {
31 }
32
33 /**
34 * Reflect the individual entries of table into JS, usually by defining
35 * some property and value of obj. entryFunc is called for each entry.
36 */
37 template<typename EntryType>
38 bool
ReflectIntoJS(ReflectEntryFunc entryFunc,JSContext * cx,JS::Handle<JSObject * > obj)39 AutoHashtable<EntryType>::ReflectIntoJS(ReflectEntryFunc entryFunc,
40 JSContext *cx, JS::Handle<JSObject*> obj)
41 {
42 for (auto iter = this->Iter(); !iter.Done(); iter.Next()) {
43 if (!entryFunc(iter.Get(), cx, obj)) {
44 return false;
45 }
46 }
47 return true;
48 }
49
50 bool IsExpiredVersion(const char* aExpiration);
51 bool IsInDataset(uint32_t aDataset, uint32_t aContainingDataset);
52 bool CanRecordDataset(uint32_t aDataset, bool aCanRecordBase, bool aCanRecordExtended);
53
54 /**
55 * Return the number of milliseconds since process start using monotonic
56 * timestamps (unaffected by system clock changes).
57 *
58 * @return NS_OK on success, NS_ERROR_NOT_AVAILABLE if TimeStamp doesn't have the data.
59 */
60 nsresult MsSinceProcessStart(double* aResult);
61
62 /**
63 * Dumps a log message to the Browser Console using the provided level.
64 *
65 * @param aLogLevel The level to use when displaying the message in the browser console
66 * (e.g. nsIScriptError::warningFlag, ...).
67 * @param aMsg The text message to print to the console.
68 */
69 void LogToBrowserConsole(uint32_t aLogLevel, const nsAString& aMsg);
70
71 } // namespace Common
72 } // namespace Telemetry
73 } // namespace mozilla
74
75 #endif // TelemetryCommon_h__
76