This repository was archived by the owner on Jan 30, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
vm_tools: Add WeakResourcePtr type to sommelier
Sommelier uses wl_resource_add_destroy_listener in its zwp_text_input, wl_seat/wl_pointer/wl_touch support to maintain a weak pointer to wayland objects. As the logic involved is a bit tricky, this CL moves it into a new WeakResourcePtr class to make it easier for future clients to implement similar behaviour and reduce code duplication. For now we only move the text_input usage. The behaviour for the input types is a bit more complex as we need to invoke more logic when the observed object is destroyed. This will require adding a callback to the WeakResourcePtr type, and will be done in a separate CL. BUG=b:222629444 TEST=Manually test deleting surfaces associated with text_input objects and ensure no crashes Change-Id: Ie75505cdd65e3de5e1336eb840c103f7fa978e0a Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform2/+/3974507 Reviewed-by: Nic Hollingum <[email protected]> Commit-Queue: Timothy Loh <[email protected]> Reviewed-by: Lucy Qu <[email protected]> Tested-by: Timothy Loh <[email protected]>
- Loading branch information
Showing
2 changed files
with
72 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright 2022 The ChromiumOS Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef VM_TOOLS_SOMMELIER_WEAK_RESOURCE_PTR_H_ | ||
#define VM_TOOLS_SOMMELIER_WEAK_RESOURCE_PTR_H_ | ||
|
||
#include "sommelier.h" // NOLINT(build/include_directory) | ||
|
||
#include <algorithm> | ||
#include <functional> | ||
#include <assert.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
// WeakResourcePtr is a weak pointer for a proxy object (sl_host_foo). It can | ||
// be useful for objects with events like enter/leave, to keep track of an | ||
// object provided in the enter event and ensure that it is not used if the | ||
// client destroys it. | ||
template <typename SlHostType> | ||
class WeakResourcePtr { | ||
public: | ||
WeakResourcePtr() { | ||
wl_list_init(&destroy_listener_.link); | ||
destroy_listener_.notify = ResourceDestroyed; | ||
} | ||
|
||
~WeakResourcePtr() { wl_list_remove(&destroy_listener_.link); } | ||
|
||
WeakResourcePtr& operator=(SlHostType* host) { | ||
if (host == host_) | ||
return *this; | ||
|
||
Reset(); | ||
if (host) { | ||
host_ = host; | ||
wl_resource_add_destroy_listener(host_->resource, &destroy_listener_); | ||
} | ||
return *this; | ||
} | ||
|
||
operator bool() const { return host_; } | ||
SlHostType* operator->() const { return host_; } | ||
SlHostType* get() const { return host_; } | ||
|
||
void Reset() { | ||
host_ = nullptr; | ||
// Remove the listener | ||
wl_list_remove(&destroy_listener_.link); | ||
wl_list_init(&destroy_listener_.link); | ||
} | ||
|
||
private: | ||
SlHostType* host_ = nullptr; | ||
// This is always in an initialized state | ||
wl_listener destroy_listener_; | ||
|
||
static void ResourceDestroyed(wl_listener* listener, void* data) { | ||
WeakResourcePtr* ptr; | ||
ptr = wl_container_of(listener, ptr, destroy_listener_); | ||
ptr->Reset(); | ||
} | ||
}; | ||
|
||
#endif // VM_TOOLS_SOMMELIER_WEAK_RESOURCE_PTR_H_ |