summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/loader/frame_or_imported_document.cc
blob: 12b944fac4a881cb19a68562cfe3a8699c7699aa (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
// Copyright 2019 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/loader/frame_or_imported_document.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_imports_controller.h"
#include "third_party/blink/renderer/core/loader/document_loader.h"
#include "third_party/blink/renderer/core/loader/frame_loader.h"

namespace blink {

LocalFrame& FrameOrImportedDocument::GetFrame() const {
  if (document_loader_) {
    LocalFrame* frame = document_loader_->GetFrame();
    DCHECK(frame);
    return *frame;
  }

  // HTML imports case
  DCHECK(document_);
  // It's guaranteed that imports_controller is not nullptr since:
  // - only ClearImportsController() clears it
  // - ClearImportsController() also calls ResourceFethcer::ClearContext().
  HTMLImportsController* imports_controller = document_->ImportsController();
  DCHECK(imports_controller);

  // It's guaranteed that Master() is not yet Shutdown()-ed since when Master()
  // is Shutdown()-ed:
  // - Master()'s HTMLImportsController is disposed.
  // - All the HTMLImportLoader instances of the HTMLImportsController are
  //   disposed.
  // - ClearImportsController() is called on the Document.
  // HTMLImportsController is created only when the master Document's
  // GetFrame() doesn't return nullptr, this is guaranteed to be not nullptr
  // here.
  LocalFrame* frame = imports_controller->Master()->GetFrame();
  DCHECK(frame);
  return *frame;
}

DocumentLoader& FrameOrImportedDocument::GetMasterDocumentLoader() const {
  if (document_loader_)
    return *document_loader_;

  // HTML imports case
  // GetDocumentLoader() here always returns a non-nullptr value that is the
  // DocumentLoader for |document_| because:
  // - A Document is created with a LocalFrame only after the
  //   DocumentLoader is committed
  // - When another DocumentLoader is committed, the FrameLoader
  //   Shutdown()-s |document_|.
  auto* loader = GetFrame().Loader().GetDocumentLoader();
  DCHECK(loader);
  return *loader;
}

void FrameOrImportedDocument::UpdateDocument(Document& document) {
  DCHECK(document_loader_);
  document_ = document;
}

void FrameOrImportedDocument::Trace(Visitor* visitor) {
  visitor->Trace(document_loader_);
  visitor->Trace(document_);
}

}  // namespace blink