server_util.cc 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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/exo/wayland/server_util.h"
  5. #include <wayland-server-core.h>
  6. #include "base/containers/flat_map.h"
  7. #include "base/time/time.h"
  8. #include "components/exo/data_offer.h"
  9. #include "ui/display/display.h"
  10. #include "ui/gfx/geometry/insets.h"
  11. #include "ui/gfx/geometry/rect.h"
  12. #include "ui/gfx/geometry/size.h"
  13. DEFINE_UI_CLASS_PROPERTY_TYPE(wl_resource*)
  14. namespace exo {
  15. namespace wayland {
  16. namespace {
  17. // A property key containing the surface resource that is associated with
  18. // window. If unset, no surface resource is associated with surface object.
  19. DEFINE_UI_CLASS_PROPERTY_KEY(wl_resource*, kSurfaceResourceKey, nullptr)
  20. // A property key containing the data offer resource that is associated with
  21. // data offer object.
  22. DEFINE_UI_CLASS_PROPERTY_KEY(wl_resource*, kDataOfferResourceKey, nullptr)
  23. // Wayland provides no convenient way to annotate a wl_display with arbitrary
  24. // data. Therefore, to map displays to their security_delegate, we maintain this
  25. // mapping.
  26. base::flat_map<wl_display*, SecurityDelegate*> g_display_security_map;
  27. } // namespace
  28. uint32_t TimeTicksToMilliseconds(base::TimeTicks ticks) {
  29. return (ticks - base::TimeTicks()).InMilliseconds();
  30. }
  31. uint32_t NowInMilliseconds() {
  32. return TimeTicksToMilliseconds(base::TimeTicks::Now());
  33. }
  34. wl_resource* GetSurfaceResource(Surface* surface) {
  35. return surface->GetProperty(kSurfaceResourceKey);
  36. }
  37. void SetSurfaceResource(Surface* surface, wl_resource* resource) {
  38. surface->SetProperty(kSurfaceResourceKey, resource);
  39. }
  40. wl_resource* GetDataOfferResource(const DataOffer* data_offer) {
  41. return data_offer->GetProperty(kDataOfferResourceKey);
  42. }
  43. void SetDataOfferResource(DataOffer* data_offer,
  44. wl_resource* data_offer_resource) {
  45. data_offer->SetProperty(kDataOfferResourceKey, data_offer_resource);
  46. }
  47. void SetSecurityDelegate(wl_display* display,
  48. SecurityDelegate* security_delegate) {
  49. auto emplace_result =
  50. g_display_security_map.emplace(display, security_delegate);
  51. DCHECK(emplace_result.second);
  52. }
  53. void RemoveSecurityDelegate(wl_display* display) {
  54. DCHECK(g_display_security_map.contains(display));
  55. g_display_security_map.erase(display);
  56. }
  57. SecurityDelegate* GetSecurityDelegate(wl_display* display) {
  58. DCHECK(g_display_security_map.contains(display));
  59. return g_display_security_map[display];
  60. }
  61. SecurityDelegate* GetSecurityDelegate(wl_client* client) {
  62. return GetSecurityDelegate(wl_client_get_display(client));
  63. }
  64. } // namespace wayland
  65. } // namespace exo