summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/host_desktop.cc
blob: e2c34bba85564bde5d138583dfe0834a5f596bd4 (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
91
92
93
94
95
96
97
// Copyright 2012 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 "chrome/browser/ui/host_desktop.h"

#if defined(OS_WIN)
#include <windows.h>
#endif

#include "chrome/browser/ui/ash/ash_util.h"
#include "chrome/browser/ui/browser_list_impl.h"

#if defined(OS_WIN)
#include "ash/shell.h"
#include "ui/aura/root_window.h"
#endif

namespace chrome {

namespace {

bool g_force_ = false;
HostDesktopType g_force_type_ = HOST_DESKTOP_TYPE_COUNT;

}  // namespace

ScopedForceDesktopType::ScopedForceDesktopType(HostDesktopType type)
    : previous_type_(g_force_type_),
      previous_force_(g_force_) {
  g_force_type_ = type;
  g_force_ = true;
}

ScopedForceDesktopType::~ScopedForceDesktopType() {
  g_force_type_ = previous_type_;
  g_force_ = previous_force_;
}

HostDesktopType GetHostDesktopTypeForNativeView(gfx::NativeView native_view) {
  if (g_force_)
    return g_force_type_;
#if defined(USE_ASH)
  return IsNativeViewInAsh(native_view) ?
      HOST_DESKTOP_TYPE_ASH :
      HOST_DESKTOP_TYPE_NATIVE;
#else
  return HOST_DESKTOP_TYPE_NATIVE;
#endif
}

HostDesktopType GetHostDesktopTypeForNativeWindow(
    gfx::NativeWindow native_window) {
  if (g_force_)
    return g_force_type_;
#if defined(USE_ASH)
  return IsNativeWindowInAsh(native_window) ?
      HOST_DESKTOP_TYPE_ASH :
      HOST_DESKTOP_TYPE_NATIVE;
#else
  return HOST_DESKTOP_TYPE_NATIVE;
#endif
}

HostDesktopType GetHostDesktopTypeForBrowser(const Browser* browser) {
  if (g_force_)
    return g_force_type_;
  for (HostDesktopType type = HOST_DESKTOP_TYPE_FIRST;
      type < HOST_DESKTOP_TYPE_COUNT;
      type = static_cast<HostDesktopType>(type + 1)) {
    BrowserListImpl::const_iterator begin =
        BrowserListImpl::GetInstance(type)->begin();
    BrowserListImpl::const_iterator end =
        BrowserListImpl::GetInstance(type)->end();
    if (std::find(begin, end, browser) != end)
      return type;
  }
  return HOST_DESKTOP_TYPE_NATIVE;
}

HostDesktopType GetActiveDesktop() {
#if defined(OS_WIN) && defined(USE_ASH)
  if (ash::Shell::HasInstance()) {
    HWND active_window = GetActiveWindow();
    typedef ash::Shell::RootWindowList RootWindowList;
    RootWindowList roots(ash::Shell::GetAllRootWindows());
    for (RootWindowList::const_iterator i = roots.begin(); i != roots.end();
         ++i) {
      if ((*i)->GetAcceleratedWidget() == active_window)
        return HOST_DESKTOP_TYPE_ASH;
    }
  }
#endif
  return HOST_DESKTOP_TYPE_NATIVE;
}

}  // namespace chrome