Bug 1941046 - Part 4: Send a callback request for impression and clicks of MARS Top...
[gecko.git] / toolkit / components / telemetry / other / CombinedStacks.h
blob6bd46823dbb8a37f1e024035e70fd305023b6b33
1 /* -*- Mode: C++; tab-width: 8; 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/. */
6 #ifndef CombinedStacks_h__
7 #define CombinedStacks_h__
9 #include <vector>
11 #include "ipc/IPCMessageUtils.h"
12 #include "ProcessedStack.h"
14 class JSObject;
15 struct JSContext;
17 namespace mozilla {
18 namespace Telemetry {
20 /**
21 * This class is conceptually a list of ProcessedStack objects, but it
22 * represents them more efficiently by keeping a single global list of modules.
24 class CombinedStacks {
25 public:
26 explicit CombinedStacks();
27 explicit CombinedStacks(size_t aMaxStacksCount);
29 CombinedStacks(CombinedStacks&&) = default;
30 CombinedStacks& operator=(CombinedStacks&&) = default;
32 void Swap(CombinedStacks& aOther);
34 typedef std::vector<Telemetry::ProcessedStack::Frame> Stack;
35 const Telemetry::ProcessedStack::Module& GetModule(unsigned aIndex) const;
36 size_t GetModuleCount() const;
37 const Stack& GetStack(unsigned aIndex) const;
38 size_t AddStack(const Telemetry::ProcessedStack& aStack);
39 void AddStacks(const CombinedStacks& aStacks);
40 size_t GetStackCount() const;
41 size_t SizeOfExcludingThis() const;
42 void RemoveStack(unsigned aIndex);
43 size_t GetMaxStacksCount() const;
45 /** Clears the contents of vectors and resets the index. */
46 void Clear();
48 private:
49 std::vector<Telemetry::ProcessedStack::Module> mModules;
50 // A circular buffer to hold the stacks.
51 std::vector<Stack> mStacks;
52 // The index of the next buffer element to write to in mStacks.
53 size_t mNextIndex;
54 // The maximum number of stacks to keep in the CombinedStacks object.
55 size_t mMaxStacksCount;
57 void AddFrame(
58 size_t aStackIndex, const ProcessedStack::Frame& aFrame,
59 const std::function<const ProcessedStack::Module&(int)>& aModuleGetter);
61 friend struct ::IPC::ParamTraits<CombinedStacks>;
64 /**
65 * Creates a JSON representation of given combined stacks object.
67 JSObject* CreateJSStackObject(JSContext* cx, const CombinedStacks& stacks);
69 } // namespace Telemetry
70 } // namespace mozilla
72 namespace IPC {
74 template <>
75 struct ParamTraits<mozilla::Telemetry::CombinedStacks> {
76 typedef mozilla::Telemetry::CombinedStacks paramType;
78 static void Write(MessageWriter* aWriter, const paramType& aParam) {
79 WriteParam(aWriter, aParam.mModules);
80 WriteParam(aWriter, aParam.mStacks);
81 WriteParam(aWriter, aParam.mNextIndex);
82 WriteParam(aWriter, aParam.mMaxStacksCount);
85 static bool Read(MessageReader* aReader, paramType* aResult) {
86 if (!ReadParam(aReader, &aResult->mModules)) {
87 return false;
90 if (!ReadParam(aReader, &aResult->mStacks)) {
91 return false;
94 if (!ReadParam(aReader, &aResult->mNextIndex)) {
95 return false;
98 if (!ReadParam(aReader, &aResult->mMaxStacksCount)) {
99 return false;
102 return true;
106 } // namespace IPC
108 #endif // CombinedStacks_h__