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 
5 #include "ui/ozone/platform/wayland/test/global_object.h"
6 
7 #include <algorithm>
8 
9 #include <wayland-server-core.h>
10 
11 #include "ui/ozone/platform/wayland/test/server_object.h"
12 
13 namespace wl {
14 
operator ()(wl_global * global)15 void GlobalObject::Deleter::operator()(wl_global* global) {
16   wl_global_destroy(global);
17 }
18 
GlobalObject(const wl_interface * interface,const void * implementation,uint32_t version)19 GlobalObject::GlobalObject(const wl_interface* interface,
20                            const void* implementation,
21                            uint32_t version)
22     : interface_(interface),
23       implementation_(implementation),
24       version_(version) {}
25 
~GlobalObject()26 GlobalObject::~GlobalObject() {}
27 
Initialize(wl_display * display)28 bool GlobalObject::Initialize(wl_display* display) {
29   global_.reset(wl_global_create(display, interface_, version_, this, &Bind));
30   return global_ != nullptr;
31 }
32 
DestroyGlobal()33 void GlobalObject::DestroyGlobal() {
34   global_.reset();
35 }
36 
37 // static
Bind(wl_client * client,void * data,uint32_t version,uint32_t id)38 void GlobalObject::Bind(wl_client* client,
39                         void* data,
40                         uint32_t version,
41                         uint32_t id) {
42   auto* global = static_cast<GlobalObject*>(data);
43   wl_resource* resource = wl_resource_create(
44       client, global->interface_, std::min(version, global->version_), id);
45   if (!resource) {
46     wl_client_post_no_memory(client);
47     return;
48   }
49   if (!global->resource_)
50     global->resource_ = resource;
51   wl_resource_set_implementation(resource, global->implementation_, global,
52                                  &GlobalObject::OnResourceDestroyed);
53   global->OnBind();
54 }
55 
56 // static
OnResourceDestroyed(wl_resource * resource)57 void GlobalObject::OnResourceDestroyed(wl_resource* resource) {
58   auto* global = GetUserDataAs<GlobalObject>(resource);
59   if (global->resource_ == resource)
60     global->resource_ = nullptr;
61 }
62 
63 }  // namespace wl
64