summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/html/imports/html_import_tree_root.cc
blob: be259e347cbf3b3a575e67f56e6aa7ab894fcddb (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
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
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/core/html/imports/html_import_tree_root.h"

#include "third_party/blink/renderer/core/css/style_engine.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/html/imports/html_import_child.h"

namespace blink {

HTMLImportTreeRoot* HTMLImportTreeRoot::Create(Document* document) {
  return new HTMLImportTreeRoot(document);
}

HTMLImportTreeRoot::HTMLImportTreeRoot(Document* document)
    : HTMLImport(HTMLImport::kSync),
      document_(document),
      recalc_timer_(
          document->GetFrame()->GetTaskRunner(TaskType::kInternalDefault),
          this,
          &HTMLImportTreeRoot::RecalcTimerFired) {
  ScheduleRecalcState();  // This recomputes initial state.
}

HTMLImportTreeRoot::~HTMLImportTreeRoot() = default;

void HTMLImportTreeRoot::Dispose() {
  for (const auto& import_child : imports_)
    import_child->Dispose();
  imports_.clear();
  document_ = nullptr;
  recalc_timer_.Stop();
}

Document* HTMLImportTreeRoot::GetDocument() const {
  return document_;
}

bool HTMLImportTreeRoot::HasFinishedLoading() const {
  return !document_->Parsing() &&
         document_->GetStyleEngine().HaveScriptBlockingStylesheetsLoaded();
}

void HTMLImportTreeRoot::StateWillChange() {
  ScheduleRecalcState();
}

void HTMLImportTreeRoot::StateDidChange() {
  HTMLImport::StateDidChange();

  if (GetState().IsReady())
    document_->CheckCompleted();
}

void HTMLImportTreeRoot::ScheduleRecalcState() {
  DCHECK(document_);
  if (recalc_timer_.IsActive() || !document_->IsActive())
    return;
  recalc_timer_.StartOneShot(TimeDelta(), FROM_HERE);
}

HTMLImportChild* HTMLImportTreeRoot::Add(HTMLImportChild* child) {
  imports_.push_back(child);
  return imports_.back().Get();
}

HTMLImportChild* HTMLImportTreeRoot::Find(const KURL& url) const {
  for (const auto& candidate : imports_) {
    if (EqualIgnoringFragmentIdentifier(candidate->Url(), url))
      return candidate;
  }

  return nullptr;
}

void HTMLImportTreeRoot::RecalcTimerFired(TimerBase*) {
  DCHECK(document_);
  HTMLImport::RecalcTreeState(this);
}

void HTMLImportTreeRoot::Trace(blink::Visitor* visitor) {
  visitor->Trace(document_);
  visitor->Trace(imports_);
  HTMLImport::Trace(visitor);
}

}  // namespace blink