[LayoutNG] Change NGFragment to accept a reference instead of a pointer.

This also scopes down any NGFragment related classes to the minimum which
is needed.

Changes a few other related pieces of code to use const references.

Bug: 635619
Cq-Include-Trybots: master.tryserver.chromium.linux:linux_layout_tests_layout_ng
Change-Id: I1ed13ce254aef4723c393877b5190295dcd12146
Reviewed-on: https://chromium-review.googlesource.com/646752
Commit-Queue: Ian Kilpatrick <ikilpatrick@chromium.org>
Reviewed-by: Koji Ishii <kojii@chromium.org>
Cr-Commit-Position: refs/heads/master@{#499295}
diff --git a/third_party/WebKit/Source/core/layout/ng/inline/ng_inline_layout_algorithm.cc b/third_party/WebKit/Source/core/layout/ng/inline/ng_inline_layout_algorithm.cc
index 22fb69e..8295db8 100644
--- a/third_party/WebKit/Source/core/layout/ng/inline/ng_inline_layout_algorithm.cc
+++ b/third_party/WebKit/Source/core/layout/ng/inline/ng_inline_layout_algorithm.cc
@@ -296,8 +296,7 @@
 
   NGBoxFragment fragment(
       ConstraintSpace().WritingMode(),
-      ToNGPhysicalBoxFragment(
-          item_result->layout_result->PhysicalFragment().Get()));
+      ToNGPhysicalBoxFragment(*item_result->layout_result->PhysicalFragment()));
   NGLineHeightMetrics metrics = fragment.BaselineMetrics(
       {line_info.UseFirstLineStyle()
            ? NGBaselineAlgorithmType::kAtomicInlineForFirstLine
diff --git a/third_party/WebKit/Source/core/layout/ng/inline/ng_inline_node.cc b/third_party/WebKit/Source/core/layout/ng/inline/ng_inline_node.cc
index 6a9e5269..016efbf 100644
--- a/third_party/WebKit/Source/core/layout/ng/inline/ng_inline_node.cc
+++ b/third_party/WebKit/Source/core/layout/ng/inline/ng_inline_node.cc
@@ -65,18 +65,17 @@
                     HashMap<LineLayoutItem, FragmentPosition>* positions_out) {
   for (const auto& child : children) {
     if (child->Type() == NGPhysicalFragment::kFragmentText) {
-      const auto* physical_fragment = ToNGPhysicalTextFragment(child.Get());
-      const NGInlineItem& item =
-          items[physical_fragment->ItemIndexDeprecated()];
+      const auto& physical_fragment = ToNGPhysicalTextFragment(*child);
+      const NGInlineItem& item = items[physical_fragment.ItemIndexDeprecated()];
       BidiRun* run;
       if (item.Type() == NGInlineItem::kText ||
           item.Type() == NGInlineItem::kControl) {
         LayoutObject* layout_object = item.GetLayoutObject();
         DCHECK(layout_object->IsText());
         unsigned text_offset =
-            text_offsets[physical_fragment->ItemIndexDeprecated()];
-        run = new BidiRun(physical_fragment->StartOffset() - text_offset,
-                          physical_fragment->EndOffset() - text_offset,
+            text_offsets[physical_fragment.ItemIndexDeprecated()];
+        run = new BidiRun(physical_fragment.StartOffset() - text_offset,
+                          physical_fragment.EndOffset() - text_offset,
                           item.BidiLevel(), LineLayoutItem(layout_object));
         layout_object->ClearNeedsLayout();
       } else if (item.Type() == NGInlineItem::kAtomicInline) {
@@ -88,27 +87,26 @@
         continue;
       }
       bidi_runs->AddRun(run);
-      NGTextFragment fragment(constraint_space.WritingMode(),
-                              physical_fragment);
+      NGFragment fragment(constraint_space.WritingMode(), physical_fragment);
       // Store text fragments in a vector in the same order as BidiRunList.
       // One LayoutText may produce multiple text fragments that they can't
       // be set to a map.
       positions_for_bidi_runs_out->push_back(FragmentPosition{
           fragment.Offset() + parent_offset, fragment.InlineSize(),
-          physical_fragment->EndEffect()});
+          physical_fragment.EndEffect()});
     } else {
       DCHECK_EQ(child->Type(), NGPhysicalFragment::kFragmentBox);
-      NGPhysicalBoxFragment* physical_fragment =
-          ToNGPhysicalBoxFragment(child.Get());
-      NGBoxFragment fragment(constraint_space.WritingMode(), physical_fragment);
+      const auto& physical_fragment = ToNGPhysicalBoxFragment(*child);
+
+      NGFragment fragment(constraint_space.WritingMode(), physical_fragment);
       NGLogicalOffset child_offset = fragment.Offset() + parent_offset;
-      if (physical_fragment->Children().size()) {
-        CreateBidiRuns(bidi_runs, physical_fragment->Children(),
+      if (physical_fragment.Children().size()) {
+        CreateBidiRuns(bidi_runs, physical_fragment.Children(),
                        constraint_space, child_offset, items, text_offsets,
                        positions_for_bidi_runs_out, positions_out);
       } else {
         // An empty inline needs a BidiRun for itself.
-        LayoutObject* layout_object = physical_fragment->GetLayoutObject();
+        LayoutObject* layout_object = physical_fragment.GetLayoutObject();
         BidiRun* run = new BidiRun(0, 1, 0, LineLayoutItem(layout_object));
         bidi_runs->AddRun(run);
       }
@@ -640,13 +638,12 @@
     if (!container_child.Get()->IsLineBox())
       continue;
 
-    NGPhysicalLineBoxFragment* physical_line_box =
-        ToNGPhysicalLineBoxFragment(container_child.Get());
-    NGLineBoxFragment line_box(constraint_space.WritingMode(),
-                               physical_line_box);
+    const auto& physical_line_box =
+        ToNGPhysicalLineBoxFragment(*container_child);
+    NGFragment line_box(constraint_space.WritingMode(), physical_line_box);
 
     // Create a BidiRunList for this line.
-    CreateBidiRuns(&bidi_runs, physical_line_box->Children(), constraint_space,
+    CreateBidiRuns(&bidi_runs, physical_line_box.Children(), constraint_space,
                    {line_box.InlineOffset(), LayoutUnit(0)}, items,
                    text_offsets, &positions_for_bidi_runs, &positions);
     // TODO(kojii): bidi needs to find the logical last run.
@@ -678,7 +675,7 @@
     root_line_box->SetLogicalWidth(line_box.InlineSize());
     LayoutUnit line_top = line_box.BlockOffset() + border_padding.block_start;
     NGLineHeightMetrics line_metrics(Style(), baseline_type);
-    const NGLineHeightMetrics& max_with_leading = physical_line_box->Metrics();
+    const NGLineHeightMetrics& max_with_leading = physical_line_box.Metrics();
     LayoutUnit baseline = line_top + max_with_leading.ascent;
     root_line_box->SetLogicalTop(baseline - line_metrics.ascent);
     root_line_box->SetLineTopBottomPositions(
diff --git a/third_party/WebKit/Source/core/layout/ng/inline/ng_line_box_fragment.h b/third_party/WebKit/Source/core/layout/ng/inline/ng_line_box_fragment.h
index e0f1f8d8..1e33e57 100644
--- a/third_party/WebKit/Source/core/layout/ng/inline/ng_line_box_fragment.h
+++ b/third_party/WebKit/Source/core/layout/ng/inline/ng_line_box_fragment.h
@@ -15,7 +15,7 @@
 class CORE_EXPORT NGLineBoxFragment final : public NGFragment {
  public:
   NGLineBoxFragment(NGWritingMode writing_mode,
-                    const NGPhysicalLineBoxFragment* physical_fragment)
+                    const NGPhysicalLineBoxFragment& physical_fragment)
       : NGFragment(writing_mode, physical_fragment) {}
 };
 
diff --git a/third_party/WebKit/Source/core/layout/ng/inline/ng_line_breaker.cc b/third_party/WebKit/Source/core/layout/ng/inline/ng_line_breaker.cc
index 7276455..20f0bac 100644
--- a/third_party/WebKit/Source/core/layout/ng/inline/ng_line_breaker.cc
+++ b/third_party/WebKit/Source/core/layout/ng/inline/ng_line_breaker.cc
@@ -458,10 +458,10 @@
           .ToConstraintSpace(FromPlatformWritingMode(style.GetWritingMode()));
   item_result->layout_result = node.Layout(*constraint_space);
 
+  DCHECK(item_result->layout_result->PhysicalFragment());
   item_result->inline_size =
-      NGBoxFragment(constraint_space_.WritingMode(),
-                    ToNGPhysicalBoxFragment(
-                        item_result->layout_result->PhysicalFragment().Get()))
+      NGFragment(constraint_space_.WritingMode(),
+                 *item_result->layout_result->PhysicalFragment())
           .InlineSize();
 
   item_result->margins =
diff --git a/third_party/WebKit/Source/core/layout/ng/inline/ng_text_fragment.h b/third_party/WebKit/Source/core/layout/ng/inline/ng_text_fragment.h
index 9ee213f..f8cb9a4 100644
--- a/third_party/WebKit/Source/core/layout/ng/inline/ng_text_fragment.h
+++ b/third_party/WebKit/Source/core/layout/ng/inline/ng_text_fragment.h
@@ -14,7 +14,7 @@
 class CORE_EXPORT NGTextFragment final : public NGFragment {
  public:
   NGTextFragment(NGWritingMode writing_mode,
-                 const NGPhysicalTextFragment* physical_text_fragment)
+                 const NGPhysicalTextFragment& physical_text_fragment)
       : NGFragment(writing_mode, physical_text_fragment) {}
 };
 
diff --git a/third_party/WebKit/Source/core/layout/ng/ng_block_layout_algorithm.cc b/third_party/WebKit/Source/core/layout/ng/ng_block_layout_algorithm.cc
index 057137c..502dceb 100644
--- a/third_party/WebKit/Source/core/layout/ng/ng_block_layout_algorithm.cc
+++ b/third_party/WebKit/Source/core/layout/ng/ng_block_layout_algorithm.cc
@@ -76,7 +76,7 @@
   // assume that its in the same writing mode as its parent, as a different
   // writing mode child will be caught by the CreatesNewFormattingContext check.
   NGFragment fragment(FromPlatformWritingMode(child.Style().GetWritingMode()),
-                      layout_result.PhysicalFragment().Get());
+                      *layout_result.PhysicalFragment());
   DCHECK_EQ(LayoutUnit(), fragment.BlockSize());
 #endif
 
@@ -660,11 +660,10 @@
   }
 
   // We must have an actual fragment at this stage.
-  DCHECK(layout_result->PhysicalFragment().Get());
+  DCHECK(layout_result->PhysicalFragment());
 
-  NGBoxFragment fragment(
-      ConstraintSpace().WritingMode(),
-      ToNGPhysicalBoxFragment(layout_result->PhysicalFragment().Get()));
+  NGFragment fragment(ConstraintSpace().WritingMode(),
+                      *layout_result->PhysicalFragment());
 
   NGLogicalOffset logical_offset =
       CalculateLogicalOffset(fragment, child_data.margins, child_bfc_offset);
@@ -802,9 +801,9 @@
     WTF::Optional<NGBfcOffset>* child_bfc_offset) {
   const EClear child_clear = child.Style().Clear();
 
-  NGBoxFragment fragment(
-      ConstraintSpace().WritingMode(),
-      ToNGPhysicalBoxFragment(layout_result.PhysicalFragment().Get()));
+  DCHECK(layout_result.PhysicalFragment());
+  NGFragment fragment(ConstraintSpace().WritingMode(),
+                      *layout_result.PhysicalFragment());
 
   LayoutUnit child_bfc_offset_estimate =
       child_data.bfc_offset_estimate.block_offset;
diff --git a/third_party/WebKit/Source/core/layout/ng/ng_block_node.cc b/third_party/WebKit/Source/core/layout/ng/ng_block_node.cc
index 71accc5f..38d74b7a 100644
--- a/third_party/WebKit/Source/core/layout/ng/ng_block_node.cc
+++ b/third_party/WebKit/Source/core/layout/ng/ng_block_node.cc
@@ -49,7 +49,7 @@
 void UpdateLegacyMultiColumnFlowThread(
     LayoutBox* layout_box,
     const NGConstraintSpace& constraint_space,
-    const NGPhysicalBoxFragment* fragment) {
+    const NGPhysicalBoxFragment& fragment) {
   LayoutBlockFlow* multicol = ToLayoutBlockFlow(layout_box);
   LayoutMultiColumnFlowThread* flow_thread = multicol->MultiColumnFlowThread();
   if (!flow_thread)
@@ -60,15 +60,14 @@
   LayoutUnit flow_end;
 
   // Stitch the columns together.
-  for (const RefPtr<NGPhysicalFragment> child : fragment->Children()) {
-    NGBoxFragment child_fragment(writing_mode,
-                                 ToNGPhysicalBoxFragment(child.Get()));
+  for (const RefPtr<NGPhysicalFragment> child : fragment.Children()) {
+    NGFragment child_fragment(writing_mode, *child);
     flow_end += child_fragment.BlockSize();
     column_inline_size = child_fragment.InlineSize();
   }
 
   if (LayoutMultiColumnSet* column_set = flow_thread->FirstMultiColumnSet()) {
-    NGBoxFragment logical_fragment(writing_mode, fragment);
+    NGFragment logical_fragment(writing_mode, fragment);
     column_set->SetLogicalWidth(logical_fragment.InlineSize());
     column_set->SetLogicalHeight(logical_fragment.BlockSize());
     column_set->EndFlow(flow_end);
@@ -93,7 +92,7 @@
   const auto* break_token = ToNGBlockBreakToken(fragment.BreakToken());
   if (!break_token)
     return true;
-  NGBoxFragment logical_fragment(constraint_space.WritingMode(), &fragment);
+  NGFragment logical_fragment(constraint_space.WritingMode(), fragment);
   return break_token->UsedBlockSize() <= logical_fragment.BlockSize();
 }
 
@@ -131,7 +130,7 @@
 
   if (layout_result->Status() == NGLayoutResult::kSuccess &&
       layout_result->UnpositionedFloats().IsEmpty())
-    CopyFragmentDataToLayoutBox(constraint_space, layout_result.Get());
+    CopyFragmentDataToLayoutBox(constraint_space, *layout_result);
 
   return layout_result;
 }
@@ -169,10 +168,9 @@
 
   // Have to synthesize this value.
   RefPtr<NGLayoutResult> layout_result = Layout(*constraint_space);
-  NGPhysicalFragment* physical_fragment =
-      layout_result->PhysicalFragment().Get();
-  NGBoxFragment min_fragment(FromPlatformWritingMode(Style().GetWritingMode()),
-                             ToNGPhysicalBoxFragment(physical_fragment));
+  NGBoxFragment min_fragment(
+      FromPlatformWritingMode(Style().GetWritingMode()),
+      ToNGPhysicalBoxFragment(*layout_result->PhysicalFragment()));
   sizes.min_size = min_fragment.OverflowSize().inline_size;
 
   // Now, redo with infinite space for max_content
@@ -186,9 +184,9 @@
           .ToConstraintSpace(FromPlatformWritingMode(Style().GetWritingMode()));
 
   layout_result = Layout(*constraint_space);
-  physical_fragment = layout_result->PhysicalFragment().Get();
-  NGBoxFragment max_fragment(FromPlatformWritingMode(Style().GetWritingMode()),
-                             ToNGPhysicalBoxFragment(physical_fragment));
+  NGBoxFragment max_fragment(
+      FromPlatformWritingMode(Style().GetWritingMode()),
+      ToNGPhysicalBoxFragment(*layout_result->PhysicalFragment()));
   sizes.max_size = max_fragment.OverflowSize().inline_size;
   return sizes;
 }
@@ -226,9 +224,11 @@
 
 void NGBlockNode::CopyFragmentDataToLayoutBox(
     const NGConstraintSpace& constraint_space,
-    NGLayoutResult* layout_result) {
-  const NGPhysicalBoxFragment* physical_fragment =
-      ToNGPhysicalBoxFragment(layout_result->PhysicalFragment().Get());
+    const NGLayoutResult& layout_result) {
+  DCHECK(layout_result.PhysicalFragment());
+  const NGPhysicalBoxFragment& physical_fragment =
+      ToNGPhysicalBoxFragment(*layout_result.PhysicalFragment());
+
   if (box_->Style()->SpecifiesColumns()) {
     UpdateLegacyMultiColumnFlowThread(box_, constraint_space,
                                       physical_fragment);
@@ -242,7 +242,7 @@
   // legacy layout doesn't support non-uniform fragmentainer widths.
   LayoutUnit logical_height;
   LayoutUnit intrinsic_content_logical_height;
-  if (IsFirstFragment(constraint_space, *physical_fragment)) {
+  if (IsFirstFragment(constraint_space, physical_fragment)) {
     box_->SetLogicalWidth(fragment.InlineSize());
   } else {
     DCHECK_EQ(box_->LogicalWidth(), fragment.InlineSize())
@@ -255,7 +255,7 @@
   NGBoxStrut border_scrollbar_padding =
       ComputeBorders(constraint_space, Style()) +
       ComputePadding(constraint_space, Style()) + GetScrollbarSizes(box_);
-  if (IsLastFragment(*physical_fragment))
+  if (IsLastFragment(physical_fragment))
     intrinsic_content_logical_height -= border_scrollbar_padding.BlockSum();
   box_->SetLogicalHeight(logical_height);
   box_->SetIntrinsicContentLogicalHeight(intrinsic_content_logical_height);
@@ -276,7 +276,7 @@
     box_->SetMarginEnd(margins.inline_end);
   }
 
-  for (const auto& child_fragment : physical_fragment->Children()) {
+  for (const auto& child_fragment : physical_fragment.Children()) {
     DCHECK(child_fragment->IsPlaced());
 
     // At the moment "anonymous" fragments for inline layout will have the same
diff --git a/third_party/WebKit/Source/core/layout/ng/ng_block_node.h b/third_party/WebKit/Source/core/layout/ng/ng_block_node.h
index d9cebfb..97e2be63 100644
--- a/third_party/WebKit/Source/core/layout/ng/ng_block_node.h
+++ b/third_party/WebKit/Source/core/layout/ng/ng_block_node.h
@@ -59,7 +59,8 @@
  private:
   // After we run the layout algorithm, this function copies back the geometry
   // data to the layout box.
-  void CopyFragmentDataToLayoutBox(const NGConstraintSpace&, NGLayoutResult*);
+  void CopyFragmentDataToLayoutBox(const NGConstraintSpace&,
+                                   const NGLayoutResult&);
   void CopyChildFragmentPosition(
       const NGPhysicalFragment& fragment,
       const NGPhysicalOffset& additional_offset = NGPhysicalOffset());
diff --git a/third_party/WebKit/Source/core/layout/ng/ng_box_fragment.cc b/third_party/WebKit/Source/core/layout/ng/ng_box_fragment.cc
index 62c26da..b2b66ab 100644
--- a/third_party/WebKit/Source/core/layout/ng/ng_box_fragment.cc
+++ b/third_party/WebKit/Source/core/layout/ng/ng_box_fragment.cc
@@ -12,18 +12,18 @@
 namespace blink {
 
 NGLogicalSize NGBoxFragment::OverflowSize() const {
-  auto* physical_fragment = ToNGPhysicalBoxFragment(physical_fragment_);
-  return physical_fragment->OverflowSize().ConvertToLogical(WritingMode());
+  const auto& physical_fragment = ToNGPhysicalBoxFragment(physical_fragment_);
+  return physical_fragment.OverflowSize().ConvertToLogical(WritingMode());
 }
 
 NGLineHeightMetrics NGBoxFragment::BaselineMetrics(
     const NGBaselineRequest& request) const {
-  LayoutBox* layout_box = ToLayoutBox(physical_fragment_->GetLayoutObject());
+  const auto& physical_fragment = ToNGPhysicalBoxFragment(physical_fragment_);
+
+  LayoutBox* layout_box = ToLayoutBox(physical_fragment_.GetLayoutObject());
 
   // Find the baseline from the computed results.
-  const NGPhysicalBoxFragment* physical_fragment =
-      ToNGPhysicalBoxFragment(physical_fragment_);
-  if (const NGBaseline* baseline = physical_fragment->Baseline(request)) {
+  if (const NGBaseline* baseline = physical_fragment.Baseline(request)) {
     LayoutUnit ascent = baseline->offset;
     LayoutUnit descent = BlockSize() - ascent;
 
diff --git a/third_party/WebKit/Source/core/layout/ng/ng_box_fragment.h b/third_party/WebKit/Source/core/layout/ng/ng_box_fragment.h
index 496074e..776e7af 100644
--- a/third_party/WebKit/Source/core/layout/ng/ng_box_fragment.h
+++ b/third_party/WebKit/Source/core/layout/ng/ng_box_fragment.h
@@ -20,7 +20,7 @@
 class CORE_EXPORT NGBoxFragment final : public NGFragment {
  public:
   NGBoxFragment(NGWritingMode writing_mode,
-                const NGPhysicalBoxFragment* physical_fragment)
+                const NGPhysicalBoxFragment& physical_fragment)
       : NGFragment(writing_mode, physical_fragment) {}
 
   // Returns the total size, including the contents outside of the border-box.
diff --git a/third_party/WebKit/Source/core/layout/ng/ng_column_layout_algorithm.cc b/third_party/WebKit/Source/core/layout/ng/ng_column_layout_algorithm.cc
index 35de5dc..47eee44 100644
--- a/third_party/WebKit/Source/core/layout/ng/ng_column_layout_algorithm.cc
+++ b/third_party/WebKit/Source/core/layout/ng/ng_column_layout_algorithm.cc
@@ -56,7 +56,7 @@
     NGLogicalOffset logical_offset(column_inline_offset, LayoutUnit());
     container_builder_.AddChild(column, logical_offset);
 
-    NGLogicalSize size = NGBoxFragment(writing_mode, column.Get()).Size();
+    NGLogicalSize size = NGBoxFragment(writing_mode, *column).Size();
     NGLogicalOffset end_offset =
         logical_offset + NGLogicalOffset(size.inline_size, size.block_size);
 
diff --git a/third_party/WebKit/Source/core/layout/ng/ng_floats_utils.cc b/third_party/WebKit/Source/core/layout/ng/ng_floats_utils.cc
index aedbf81..f6a4d6f 100644
--- a/third_party/WebKit/Source/core/layout/ng/ng_floats_utils.cc
+++ b/third_party/WebKit/Source/core/layout/ng/ng_floats_utils.cc
@@ -154,9 +154,9 @@
   // the cached value.
   if (unpositioned_float->layout_result) {
     DCHECK(!is_same_writing_mode);
-    return NGFragment(
-               parent_space.WritingMode(),
-               unpositioned_float->layout_result->PhysicalFragment().Get())
+    DCHECK(unpositioned_float->layout_result->PhysicalFragment());
+    return NGFragment(parent_space.WritingMode(),
+                      *unpositioned_float->layout_result->PhysicalFragment())
         .InlineSize();
   }
 
@@ -183,10 +183,10 @@
   // unpositioned_float at this stage.
   unpositioned_float->layout_result = unpositioned_float->node.Layout(*space);
 
-  const NGPhysicalFragment* fragment =
-      unpositioned_float->layout_result->PhysicalFragment().Get();
+  DCHECK(unpositioned_float->layout_result->PhysicalFragment());
+  const auto& fragment = *unpositioned_float->layout_result->PhysicalFragment();
 
-  DCHECK(fragment->BreakToken()->IsFinished());
+  DCHECK(fragment.BreakToken()->IsFinished());
 
   return NGFragment(parent_space.WritingMode(), fragment).InlineSize();
 }
@@ -237,9 +237,9 @@
         *space, unpositioned_float->token.Get());
   }
 
-  NGBoxFragment float_fragment(
-      parent_space.WritingMode(),
-      ToNGPhysicalBoxFragment(layout_result.Get()->PhysicalFragment().Get()));
+  DCHECK(layout_result->PhysicalFragment());
+  NGFragment float_fragment(parent_space.WritingMode(),
+                            *layout_result->PhysicalFragment());
 
   // TODO(glebl): This should check for infinite opportunity instead.
   if (opportunity.IsEmpty()) {
diff --git a/third_party/WebKit/Source/core/layout/ng/ng_fragment.cc b/third_party/WebKit/Source/core/layout/ng/ng_fragment.cc
index 72146fd..b91affb 100644
--- a/third_party/WebKit/Source/core/layout/ng/ng_fragment.cc
+++ b/third_party/WebKit/Source/core/layout/ng/ng_fragment.cc
@@ -11,31 +11,31 @@
 
 LayoutUnit NGFragment::InlineSize() const {
   return writing_mode_ == kHorizontalTopBottom
-             ? physical_fragment_->Size().width
-             : physical_fragment_->Size().height;
+             ? physical_fragment_.Size().width
+             : physical_fragment_.Size().height;
 }
 
 LayoutUnit NGFragment::BlockSize() const {
   return writing_mode_ == kHorizontalTopBottom
-             ? physical_fragment_->Size().height
-             : physical_fragment_->Size().width;
+             ? physical_fragment_.Size().height
+             : physical_fragment_.Size().width;
 }
 
 NGLogicalSize NGFragment::Size() const {
-  return physical_fragment_->Size().ConvertToLogical(
+  return physical_fragment_.Size().ConvertToLogical(
       static_cast<NGWritingMode>(writing_mode_));
 }
 
 LayoutUnit NGFragment::InlineOffset() const {
   return writing_mode_ == kHorizontalTopBottom
-             ? physical_fragment_->Offset().left
-             : physical_fragment_->Offset().top;
+             ? physical_fragment_.Offset().left
+             : physical_fragment_.Offset().top;
 }
 
 LayoutUnit NGFragment::BlockOffset() const {
   return writing_mode_ == kHorizontalTopBottom
-             ? physical_fragment_->Offset().top
-             : physical_fragment_->Offset().left;
+             ? physical_fragment_.Offset().top
+             : physical_fragment_.Offset().left;
 }
 
 NGLogicalOffset NGFragment::Offset() const {
@@ -43,12 +43,12 @@
 }
 
 NGBorderEdges NGFragment::BorderEdges() const {
-  return NGBorderEdges::FromPhysical(physical_fragment_->BorderEdges(),
+  return NGBorderEdges::FromPhysical(physical_fragment_.BorderEdges(),
                                      WritingMode());
 }
 
 NGPhysicalFragment::NGFragmentType NGFragment::Type() const {
-  return physical_fragment_->Type();
+  return physical_fragment_.Type();
 }
 
 }  // namespace blink
diff --git a/third_party/WebKit/Source/core/layout/ng/ng_fragment.h b/third_party/WebKit/Source/core/layout/ng/ng_fragment.h
index 4db4f44..331ee3d9 100644
--- a/third_party/WebKit/Source/core/layout/ng/ng_fragment.h
+++ b/third_party/WebKit/Source/core/layout/ng/ng_fragment.h
@@ -21,7 +21,7 @@
 
  public:
   NGFragment(NGWritingMode writing_mode,
-             const NGPhysicalFragment* physical_fragment)
+             const NGPhysicalFragment& physical_fragment)
       : physical_fragment_(physical_fragment), writing_mode_(writing_mode) {}
 
   NGWritingMode WritingMode() const {
@@ -43,7 +43,7 @@
   NGPhysicalFragment::NGFragmentType Type() const;
 
  protected:
-  const NGPhysicalFragment* physical_fragment_;
+  const NGPhysicalFragment& physical_fragment_;
 
   unsigned writing_mode_ : 3;
 };
diff --git a/third_party/WebKit/Source/core/layout/ng/ng_out_of_flow_layout_part.cc b/third_party/WebKit/Source/core/layout/ng/ng_out_of_flow_layout_part.cc
index 5f23061..fb4503a3 100644
--- a/third_party/WebKit/Source/core/layout/ng/ng_out_of_flow_layout_part.cc
+++ b/third_party/WebKit/Source/core/layout/ng/ng_out_of_flow_layout_part.cc
@@ -129,9 +129,9 @@
   if (AbsoluteNeedsChildBlockSize(descendant.Style())) {
     layout_result = GenerateFragment(descendant, block_estimate, node_position);
 
-    NGBoxFragment fragment(
-        descendant_writing_mode,
-        ToNGPhysicalBoxFragment(layout_result->PhysicalFragment().Get()));
+    DCHECK(layout_result->PhysicalFragment().Get());
+    NGFragment fragment(descendant_writing_mode,
+                        *layout_result->PhysicalFragment());
 
     block_estimate = fragment.BlockSize();
   }