summaryrefslogtreecommitdiff
path: root/xfa/fxfa/parser/cxfa_nodeowner.cpp
blob: 63f2f717c2bddca44ccbbf2a7f4774de8e9ddfc9 (plain)
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
// Copyright 2018 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com

#include "xfa/fxfa/parser/cxfa_nodeowner.h"

#include <utility>

#include "third_party/base/stl_util.h"
#include "xfa/fxfa/parser/cxfa_node.h"

CXFA_NodeOwner::CXFA_NodeOwner() = default;

CXFA_NodeOwner::~CXFA_NodeOwner() = default;

void CXFA_NodeOwner::ReleaseXMLNodesIfNeeded() {
  // Because we don't know what order we'll free the nodes we may end up
  // destroying the XML tree before nodes have been cleaned up that point into
  // it. This will cause the ProbeForLowSeverityLifetimeIssue to fire.
  //
  // This doesn't happen in the destructor because of the ownership semantics
  // between the CXFA_Document and CXFA_SimpleParser. It has to happen before
  // the simple parser is destroyed, but the document has to live longer then
  // the simple parser.
  for (auto& it : nodes_)
    it->ReleaseXMLNodeIfUnowned();
}

CXFA_Node* CXFA_NodeOwner::AddOwnedNode(std::unique_ptr<CXFA_Node> node) {
  if (!node)
    return nullptr;

  CXFA_Node* ret = node.get();
  nodes_.insert(std::move(node));
  return ret;
}

void CXFA_NodeOwner::FreeOwnedNode(CXFA_Node* node) {
  if (!node)
    return;

  pdfium::FakeUniquePtr<CXFA_Node> search(node);
  auto it = nodes_.find(search);
  assert(it != nodes_.end());
  nodes_.erase(it);
}