server_util.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #ifndef COMPONENTS_EXO_WAYLAND_SERVER_UTIL_H_
  5. #define COMPONENTS_EXO_WAYLAND_SERVER_UTIL_H_
  6. #include <wayland-server-core.h>
  7. #include <wayland-server-protocol-core.h>
  8. #include <memory>
  9. #include "base/memory/ptr_util.h"
  10. #include "base/time/time.h"
  11. #include "components/exo/surface.h"
  12. #include "ui/base/class_property.h"
  13. struct wl_resource;
  14. namespace exo {
  15. class SecurityDelegate;
  16. class DataOffer;
  17. namespace wayland {
  18. template <class T>
  19. T* GetUserDataAs(wl_resource* resource) {
  20. return static_cast<T*>(wl_resource_get_user_data(resource));
  21. }
  22. template <class T>
  23. std::unique_ptr<T> TakeUserDataAs(wl_resource* resource) {
  24. std::unique_ptr<T> user_data = base::WrapUnique(GetUserDataAs<T>(resource));
  25. wl_resource_set_user_data(resource, nullptr);
  26. return user_data;
  27. }
  28. template <class T>
  29. void DestroyUserData(wl_resource* resource) {
  30. TakeUserDataAs<T>(resource);
  31. }
  32. template <class T>
  33. void SetImplementation(wl_resource* resource,
  34. const void* implementation,
  35. std::unique_ptr<T> user_data) {
  36. wl_resource_set_implementation(resource, implementation, user_data.release(),
  37. DestroyUserData<T>);
  38. }
  39. // Convert a timestamp to a time value that can be used when interfacing
  40. // with wayland. Note that we cast a int64_t value to uint32_t which can
  41. // potentially overflow.
  42. uint32_t TimeTicksToMilliseconds(base::TimeTicks ticks);
  43. uint32_t NowInMilliseconds();
  44. wl_resource* GetSurfaceResource(Surface* surface);
  45. void SetSurfaceResource(Surface* surface, wl_resource* resource);
  46. wl_resource* GetDataOfferResource(const DataOffer* data_offer);
  47. void SetDataOfferResource(DataOffer* data_offer,
  48. wl_resource* data_offer_resource);
  49. // Associates the given |display| with its |security_delegate|.
  50. void SetSecurityDelegate(wl_display* display,
  51. SecurityDelegate* security_delegate);
  52. // Clears the SecurityDelegate association for |display|.
  53. void RemoveSecurityDelegate(wl_display* display);
  54. // Returns the associated security_delegate for this |display|.
  55. SecurityDelegate* GetSecurityDelegate(wl_display* display);
  56. // Returns the associated security_delegate for the display this |client| is
  57. // connected to.
  58. SecurityDelegate* GetSecurityDelegate(wl_client* client);
  59. } // namespace wayland
  60. } // namespace exo
  61. #endif // COMPONENTS_EXO_WAYLAND_SERVER_UTIL_H_