zcr_secure_output.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2019 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/zcr_secure_output.h"
  5. #include <secure-output-unstable-v1-server-protocol.h>
  6. #include "components/exo/surface.h"
  7. #include "components/exo/surface_observer.h"
  8. #include "components/exo/wayland/server_util.h"
  9. namespace exo {
  10. namespace wayland {
  11. namespace {
  12. // A property key containing a boolean set to true if a security object is
  13. // associated with surface object.
  14. DEFINE_UI_CLASS_PROPERTY_KEY(bool, kSurfaceHasSecurityKey, false)
  15. ////////////////////////////////////////////////////////////////////////////////
  16. // security_interface:
  17. // Implements the security interface to a Surface. The "only visible on secure
  18. // output"-state is set to false upon destruction. A window property will be set
  19. // during the lifetime of this class to prevent multiple instances from being
  20. // created for the same Surface.
  21. class Security : public SurfaceObserver {
  22. public:
  23. explicit Security(Surface* surface) : surface_(surface) {
  24. surface_->AddSurfaceObserver(this);
  25. surface_->SetProperty(kSurfaceHasSecurityKey, true);
  26. }
  27. Security(const Security&) = delete;
  28. Security& operator=(const Security&) = delete;
  29. ~Security() override {
  30. if (surface_) {
  31. surface_->RemoveSurfaceObserver(this);
  32. surface_->SetOnlyVisibleOnSecureOutput(false);
  33. surface_->SetProperty(kSurfaceHasSecurityKey, false);
  34. }
  35. }
  36. void OnlyVisibleOnSecureOutput() {
  37. if (surface_)
  38. surface_->SetOnlyVisibleOnSecureOutput(true);
  39. }
  40. // Overridden from SurfaceObserver:
  41. void OnSurfaceDestroying(Surface* surface) override {
  42. surface->RemoveSurfaceObserver(this);
  43. surface_ = nullptr;
  44. }
  45. private:
  46. Surface* surface_;
  47. };
  48. void security_destroy(wl_client* client, wl_resource* resource) {
  49. wl_resource_destroy(resource);
  50. }
  51. void security_only_visible_on_secure_output(wl_client* client,
  52. wl_resource* resource) {
  53. GetUserDataAs<Security>(resource)->OnlyVisibleOnSecureOutput();
  54. }
  55. const struct zcr_security_v1_interface security_implementation = {
  56. security_destroy, security_only_visible_on_secure_output};
  57. ////////////////////////////////////////////////////////////////////////////////
  58. // secure_output_interface:
  59. void secure_output_destroy(wl_client* client, wl_resource* resource) {
  60. wl_resource_destroy(resource);
  61. }
  62. void secure_output_get_security(wl_client* client,
  63. wl_resource* resource,
  64. uint32_t id,
  65. wl_resource* surface_resource) {
  66. Surface* surface = GetUserDataAs<Surface>(surface_resource);
  67. if (surface->GetProperty(kSurfaceHasSecurityKey)) {
  68. wl_resource_post_error(resource, ZCR_SECURE_OUTPUT_V1_ERROR_SECURITY_EXISTS,
  69. "a security object for that surface already exists");
  70. return;
  71. }
  72. wl_resource* security_resource =
  73. wl_resource_create(client, &zcr_security_v1_interface, 1, id);
  74. SetImplementation(security_resource, &security_implementation,
  75. std::make_unique<Security>(surface));
  76. }
  77. const struct zcr_secure_output_v1_interface secure_output_implementation = {
  78. secure_output_destroy, secure_output_get_security};
  79. } // namespace
  80. void bind_secure_output(wl_client* client,
  81. void* data,
  82. uint32_t version,
  83. uint32_t id) {
  84. wl_resource* resource =
  85. wl_resource_create(client, &zcr_secure_output_v1_interface, 1, id);
  86. wl_resource_set_implementation(resource, &secure_output_implementation, data,
  87. nullptr);
  88. }
  89. } // namespace wayland
  90. } // namespace exo