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/accessibility/ax_active_popup.h"
6 
7 namespace ui {
8 // Represents a global storage for the view accessibility for an
9 // autofill popup. It is a singleton wrapper around the ax unique id of the
10 // autofill popup. This singleton is used for communicating the live status of
11 // the autofill popup between web contents and views.
12 // The assumption here is that only one autofill popup can exist at a time.
13 static base::NoDestructor<base::Optional<int32_t>> g_active_popup_ax_unique_id;
14 
GetActivePopupAxUniqueId()15 base::Optional<int32_t> GetActivePopupAxUniqueId() {
16   return *g_active_popup_ax_unique_id;
17 }
18 
SetActivePopupAxUniqueId(base::Optional<int32_t> ax_unique_id)19 void SetActivePopupAxUniqueId(base::Optional<int32_t> ax_unique_id) {
20   // When an instance of autofill popup hides, the caller of popup hide should
21   // make sure g_active_popup_ax_unique_id is cleared. The assumption is that
22   // there can only be one active autofill popup existing at a time. If on
23   // popup showing, we encounter g_active_popup_ax_unique_id is already set,
24   // this would indicate two autofill popups are showing at the same time or
25   // previous on popup hide call did not clear the variable, so we should fail
26   // DCHECK here.
27   DCHECK(!GetActivePopupAxUniqueId());
28 
29   *g_active_popup_ax_unique_id = ax_unique_id;
30 }
31 
ClearActivePopupAxUniqueId()32 void ClearActivePopupAxUniqueId() {
33   *g_active_popup_ax_unique_id = base::nullopt;
34 }
35 
36 }  // namespace ui
37