1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  * vim: set ts=8 sts=2 et sw=2 tw=80:
3  * This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "NodeUbiReporting.h"
8 #include "js/UbiNodeUtils.h"
9 #include "nsWindowSizes.h"
10 
11 using JS::ubi::EdgeRange;
12 using JS::ubi::SimpleEdgeRange;
13 
14 const char16_t JS::ubi::Concrete<mozilla::dom::Document>::concreteTypeName[] =
15     u"Document";
16 const char16_t JS::ubi::Concrete<nsIContent>::concreteTypeName[] =
17     u"nsIContent";
18 const char16_t JS::ubi::Concrete<mozilla::dom::Attr>::concreteTypeName[] =
19     u"Attr";
20 
construct(void * storage,nsINode * ptr)21 void JS::ubi::Concrete<nsINode>::construct(void* storage, nsINode* ptr) {
22   // nsINode is abstract, and all of its inherited instances have
23   // an overridden function with instructions to construct ubi::Nodes.
24   // We actually want to call that function and construct from those instances.
25   ptr->ConstructUbiNode(storage);
26 }
27 
edges(JSContext * cx,bool wantNames) const28 js::UniquePtr<EdgeRange> JS::ubi::Concrete<nsINode>::edges(
29     JSContext* cx, bool wantNames) const {
30   AutoSuppressGCAnalysis suppress;
31   auto range = js::MakeUnique<SimpleEdgeRange>();
32   if (!range) {
33     return nullptr;
34   }
35   if (get().GetParent()) {
36     char16_t* edgeName = nullptr;
37     if (wantNames) {
38       edgeName = NS_xstrdup(u"Parent Node");
39     }
40     if (!range->addEdge(JS::ubi::Edge(edgeName, get().GetParent()))) {
41       return nullptr;
42     }
43   }
44   for (auto curr = get().GetFirstChild(); curr; curr = curr->GetNextSibling()) {
45     char16_t* edgeName = nullptr;
46     if (wantNames) {
47       edgeName = NS_xstrdup(u"Child Node");
48     }
49     if (!range->addEdge(JS::ubi::Edge(edgeName, curr))) {
50       return nullptr;
51     }
52   }
53   return js::UniquePtr<EdgeRange>(range.release());
54 }
55 
size(mozilla::MallocSizeOf mallocSizeOf) const56 JS::ubi::Node::Size JS::ubi::Concrete<nsINode>::size(
57     mozilla::MallocSizeOf mallocSizeOf) const {
58   AutoSuppressGCAnalysis suppress;
59   mozilla::SizeOfState sz(mallocSizeOf);
60   nsWindowSizes wn(sz);
61   size_t n = 0;
62   get().AddSizeOfIncludingThis(wn, &n);
63   return n;
64 }
65 
descriptiveTypeName() const66 const char16_t* JS::ubi::Concrete<nsINode>::descriptiveTypeName() const {
67   return get().NodeName().get();
68 }
69 
size(mozilla::MallocSizeOf mallocSizeOf) const70 JS::ubi::Node::Size JS::ubi::Concrete<mozilla::dom::Document>::size(
71     mozilla::MallocSizeOf mallocSizeOf) const {
72   AutoSuppressGCAnalysis suppress;
73   mozilla::SizeOfState sz(mallocSizeOf);
74   nsWindowSizes wn(sz);
75   getDoc().DocAddSizeOfIncludingThis(wn);
76   return wn.getTotalSize();
77 }
78