1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/ui/views/auto_keep_alive.h"
6
7#include "chrome/browser/lifetime/application_lifetime.h"
8#include "chrome/browser/ui/host_desktop.h"
9#include "ui/aura/window.h"
10#include "ui/views/view_constants_aura.h"
11
12AutoKeepAlive::AutoKeepAlive(gfx::NativeWindow window)
13    : keep_alive_available_(false) {
14  // In case of aura we want default to be keep alive not available for ash
15  // because ash has keep alive set and we don't want additional keep alive
16  // count. If there is a |window|, use its root window's kDesktopRootWindow
17  // to test whether we are on desktop. Otherwise, use GetActiveDesktop().
18  if (window) {
19    gfx::NativeWindow native_window = window->GetRootWindow();
20    if (native_window->GetProperty(views::kDesktopRootWindow))
21      keep_alive_available_ = true;
22  } else if (chrome::GetActiveDesktop() != chrome::HOST_DESKTOP_TYPE_ASH) {
23    keep_alive_available_ = true;
24  }
25
26  if (keep_alive_available_)
27    chrome::IncrementKeepAliveCount();
28}
29
30AutoKeepAlive::~AutoKeepAlive() {
31  if (keep_alive_available_)
32    chrome::DecrementKeepAliveCount();
33}
34