From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Cheng Zhao <zcbenz@gmail.com>
Date: Tue, 15 Nov 2022 09:38:25 +0900
Subject: Re-register status item when owner of status watcher is changed

https://chromium-review.googlesource.com/c/chromium/src/+/4022621

diff --git a/chrome/browser/ui/views/status_icons/status_icon_linux_dbus.cc b/chrome/browser/ui/views/status_icons/status_icon_linux_dbus.cc
index cba134dd8c10e9f05d4d2547fd39cc51f83ba054..797953fd2ddef1f827e504c5116a666e0d5e90fb 100644
--- a/chrome/browser/ui/views/status_icons/status_icon_linux_dbus.cc
+++ b/chrome/browser/ui/views/status_icons/status_icon_linux_dbus.cc
@@ -361,6 +361,13 @@ void StatusIconLinuxDbus::OnInitialized(bool success) {
     return;
   }
 
+  watcher_->SetNameOwnerChangedCallback(
+      base::BindRepeating(&StatusIconLinuxDbus::NameOwnerChangedReceived,
+                          weak_factory_.GetWeakPtr()));
+  RegisterStatusNotifierItem();
+}
+
+void StatusIconLinuxDbus::RegisterStatusNotifierItem() {
   dbus::MethodCall method_call(kInterfaceStatusNotifierWatcher,
                                kMethodRegisterStatusNotifierItem);
   dbus::MessageWriter writer(&method_call);
@@ -376,6 +383,14 @@ void StatusIconLinuxDbus::OnRegistered(dbus::Response* response) {
     delegate_->OnImplInitializationFailed();
 }
 
+void StatusIconLinuxDbus::NameOwnerChangedReceived(
+    const std::string& old_owner,
+    const std::string& new_owner) {
+  // Re-register the item when the StatusNotifierWatcher has a new owner.
+  if (!new_owner.empty())
+    RegisterStatusNotifierItem();
+}
+
 void StatusIconLinuxDbus::OnActivate(
     dbus::MethodCall* method_call,
     dbus::ExportedObject::ResponseSender sender) {
diff --git a/chrome/browser/ui/views/status_icons/status_icon_linux_dbus.h b/chrome/browser/ui/views/status_icons/status_icon_linux_dbus.h
index 1257135667d738f733c576d4b36a449a429b4a47..4025a7c6483b92b4462869b903228b8d630a730b 100644
--- a/chrome/browser/ui/views/status_icons/status_icon_linux_dbus.h
+++ b/chrome/browser/ui/views/status_icons/status_icon_linux_dbus.h
@@ -71,10 +71,16 @@ class StatusIconLinuxDbus : public ui::StatusIconLinux,
                   const std::string& method_name,
                   bool success);
   void OnInitialized(bool success);
+  void RegisterStatusNotifierItem();
 
   // Step 5: register the StatusNotifierItem with the StatusNotifierWatcher.
   void OnRegistered(dbus::Response* response);
 
+  // Called when the name owner of StatusNotifierWatcher has changed, which
+  // can happen when lock/unlock screen.
+  void NameOwnerChangedReceived(const std::string& old_owner,
+                                const std::string& new_owner);
+
   // DBus methods.
   // Action       -> KDE behavior:
   // Left-click   -> Activate
