summaryrefslogtreecommitdiff
path: root/chromium/components/remote_cocoa/browser/window.mm
blob: 11e4b8a58d9539948f5dd0f36998c8842d12e86c (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
// Copyright 2018 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/remote_cocoa/browser/window.h"

#import <Cocoa/Cocoa.h>
#include <map>

#include "base/check.h"
#include "base/no_destructor.h"

namespace remote_cocoa {
namespace {

using NativeWindowMap = std::map<gfx::NativeWindow, ScopedNativeWindowMapping*>;
NativeWindowMap& GetMap() {
  static base::NoDestructor<NativeWindowMap> map;
  return *map.get();
}

ScopedNativeWindowMapping* GetMapping(gfx::NativeWindow native_window) {
  auto found = GetMap().find(native_window);
  if (found == GetMap().end())
    return nullptr;
  return found->second;
}

}  // namespace

ScopedNativeWindowMapping::ScopedNativeWindowMapping(
    gfx::NativeWindow native_window,
    ApplicationHost* app_host,
    NativeWidgetNSWindowBridge* in_process_window_bridge,
    mojom::NativeWidgetNSWindow* mojo_interface)
    : native_window_(native_window),
      application_host_(app_host),
      in_process_window_bridge_(in_process_window_bridge),
      mojo_interface_(mojo_interface) {
  GetMap().insert(std::make_pair(native_window, this));
}

ScopedNativeWindowMapping::~ScopedNativeWindowMapping() {
  GetMap().erase(native_window_);
}

bool IsWindowRemote(gfx::NativeWindow gfx_window) {
  auto* scoped_mapping = GetMapping(gfx_window);
  if (scoped_mapping)
    return !scoped_mapping->in_process_window_bridge();
  return false;
}

ApplicationHost* GetWindowApplicationHost(gfx::NativeWindow gfx_window) {
  auto* scoped_mapping = GetMapping(gfx_window);
  if (scoped_mapping)
    return scoped_mapping->application_host();
  return nullptr;
}

mojom::NativeWidgetNSWindow* GetWindowMojoInterface(
    gfx::NativeWindow gfx_window) {
  auto* scoped_mapping = GetMapping(gfx_window);
  if (scoped_mapping)
    return scoped_mapping->mojo_interface();
  return nullptr;
}

NSWindow* CreateInProcessTransparentClone(gfx::NativeWindow remote_window) {
  DCHECK(IsWindowRemote(remote_window));
  NSWindow* window = [[NSWindow alloc]
      initWithContentRect:[remote_window.GetNativeNSWindow() frame]
                styleMask:NSWindowStyleMaskBorderless
                  backing:NSBackingStoreBuffered
                    defer:NO];
  [window setAlphaValue:0];
  [window makeKeyAndOrderFront:nil];
  [window setLevel:NSModalPanelWindowLevel];
  return window;
}

}  // namespace remote_cocoa