summaryrefslogtreecommitdiffstats
path: root/components/mus/public/cpp/lib/scoped_view_ptr.cc
blob: 79401fa223d3b8bd1510988076cc9af0941e34d8 (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
// Copyright 2015 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 "components/mus/public/cpp/scoped_view_ptr.h"

#include "components/mus/public/cpp/view.h"
#include "components/mus/public/cpp/view_observer.h"
#include "components/mus/public/cpp/view_tree_connection.h"

namespace mojo {

ScopedViewPtr::ScopedViewPtr(View* view) : view_(view) {
  view_->AddObserver(this);
}

ScopedViewPtr::~ScopedViewPtr() {
  if (view_)
    DeleteViewOrViewManager(view_);
  DetachFromView();
}

// static
void ScopedViewPtr::DeleteViewOrViewManager(View* view) {
  if (view->connection()->GetRoot() == view)
    delete view->connection();
  else
    view->Destroy();
}

void ScopedViewPtr::DetachFromView() {
  if (!view_)
    return;

  view_->RemoveObserver(this);
  view_ = nullptr;
}

void ScopedViewPtr::OnViewDestroying(View* view) {
  DCHECK_EQ(view_, view);
  DetachFromView();
}

}  // namespace mojo