window.mm 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2018 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. #include "components/remote_cocoa/browser/window.h"
  5. #import <Cocoa/Cocoa.h>
  6. #include <map>
  7. #include "base/check.h"
  8. #include "base/no_destructor.h"
  9. namespace remote_cocoa {
  10. namespace {
  11. using NativeWindowMap = std::map<gfx::NativeWindow, ScopedNativeWindowMapping*>;
  12. NativeWindowMap& GetMap() {
  13. static base::NoDestructor<NativeWindowMap> map;
  14. return *map.get();
  15. }
  16. ScopedNativeWindowMapping* GetMapping(gfx::NativeWindow native_window) {
  17. auto found = GetMap().find(native_window);
  18. if (found == GetMap().end())
  19. return nullptr;
  20. return found->second;
  21. }
  22. } // namespace
  23. ScopedNativeWindowMapping::ScopedNativeWindowMapping(
  24. gfx::NativeWindow native_window,
  25. ApplicationHost* app_host,
  26. NativeWidgetNSWindowBridge* in_process_window_bridge,
  27. mojom::NativeWidgetNSWindow* mojo_interface)
  28. : native_window_(native_window),
  29. application_host_(app_host),
  30. in_process_window_bridge_(in_process_window_bridge),
  31. mojo_interface_(mojo_interface) {
  32. GetMap().insert(std::make_pair(native_window, this));
  33. }
  34. ScopedNativeWindowMapping::~ScopedNativeWindowMapping() {
  35. GetMap().erase(native_window_);
  36. }
  37. bool IsWindowRemote(gfx::NativeWindow gfx_window) {
  38. auto* scoped_mapping = GetMapping(gfx_window);
  39. if (scoped_mapping)
  40. return !scoped_mapping->in_process_window_bridge();
  41. return false;
  42. }
  43. ApplicationHost* GetWindowApplicationHost(gfx::NativeWindow gfx_window) {
  44. auto* scoped_mapping = GetMapping(gfx_window);
  45. if (scoped_mapping)
  46. return scoped_mapping->application_host();
  47. return nullptr;
  48. }
  49. mojom::NativeWidgetNSWindow* GetWindowMojoInterface(
  50. gfx::NativeWindow gfx_window) {
  51. auto* scoped_mapping = GetMapping(gfx_window);
  52. if (scoped_mapping)
  53. return scoped_mapping->mojo_interface();
  54. return nullptr;
  55. }
  56. } // namespace remote_cocoa