Bug 1825212 [wpt PR 39266] - [@scope] Propagate proximity from SubResult, a=testonly
[gecko.git] / dom / xul / nsXULControllers.cpp
blob3e96fd6e41e37201df084abbe9c95252ae6eef5e
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 /*
8 This file provides the implementation for the XUL "controllers"
9 object.
13 #include "nsString.h"
15 #include "nsIControllers.h"
16 #include "nsXULControllers.h"
17 #include "nsIController.h"
18 #include "mozilla/RefPtr.h"
20 //----------------------------------------------------------------------
22 nsXULControllerData::nsXULControllerData(uint32_t inControllerID,
23 nsIController* inController)
24 : mControllerID(inControllerID), mController(inController) {}
26 nsresult nsXULControllerData::GetController(nsIController** outController) {
27 NS_IF_ADDREF(*outController = mController);
28 return NS_OK;
31 nsXULControllers::nsXULControllers() : mCurControllerID(0) {}
33 nsXULControllers::~nsXULControllers(void) { DeleteControllers(); }
35 void nsXULControllers::DeleteControllers() {
36 uint32_t count = mControllers.Length();
37 for (uint32_t i = 0; i < count; i++) {
38 nsXULControllerData* controllerData = mControllers.ElementAt(i);
39 delete controllerData; // releases the nsIController
42 mControllers.Clear();
45 NS_IMPL_CYCLE_COLLECTION_CLASS(nsXULControllers)
47 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsXULControllers)
48 tmp->DeleteControllers();
49 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
50 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsXULControllers)
52 uint32_t i, count = tmp->mControllers.Length();
53 for (i = 0; i < count; ++i) {
54 nsXULControllerData* controllerData = tmp->mControllers[i];
55 if (controllerData) {
56 cb.NoteXPCOMChild(controllerData->mController);
60 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
62 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsXULControllers)
63 NS_INTERFACE_MAP_ENTRY(nsIControllers)
64 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIControllers)
65 NS_INTERFACE_MAP_END
67 NS_IMPL_CYCLE_COLLECTING_ADDREF(nsXULControllers)
68 NS_IMPL_CYCLE_COLLECTING_RELEASE(nsXULControllers)
70 NS_IMETHODIMP
71 nsXULControllers::GetControllerForCommand(const char* aCommand,
72 nsIController** _retval) {
73 NS_ENSURE_ARG_POINTER(_retval);
74 *_retval = nullptr;
76 uint32_t count = mControllers.Length();
77 for (uint32_t i = 0; i < count; i++) {
78 nsXULControllerData* controllerData = mControllers.ElementAt(i);
79 if (controllerData) {
80 nsCOMPtr<nsIController> controller;
81 controllerData->GetController(getter_AddRefs(controller));
82 if (controller) {
83 bool supportsCommand;
84 controller->SupportsCommand(aCommand, &supportsCommand);
85 if (supportsCommand) {
86 controller.forget(_retval);
87 return NS_OK;
93 return NS_OK;
96 NS_IMETHODIMP
97 nsXULControllers::InsertControllerAt(uint32_t aIndex,
98 nsIController* controller) {
99 nsXULControllerData* controllerData =
100 new nsXULControllerData(++mCurControllerID, controller);
101 #ifdef DEBUG
102 nsXULControllerData** inserted =
103 #endif
104 mControllers.InsertElementAt(aIndex, controllerData);
105 NS_ASSERTION(inserted != nullptr, "Insertion of controller failed");
106 return NS_OK;
109 NS_IMETHODIMP
110 nsXULControllers::RemoveControllerAt(uint32_t aIndex, nsIController** _retval) {
111 NS_ENSURE_ARG_POINTER(_retval);
112 *_retval = nullptr;
114 nsXULControllerData* controllerData = mControllers.SafeElementAt(aIndex);
115 if (!controllerData) return NS_ERROR_FAILURE;
117 mControllers.RemoveElementAt(aIndex);
119 controllerData->GetController(_retval);
120 delete controllerData;
122 return NS_OK;
125 NS_IMETHODIMP
126 nsXULControllers::GetControllerAt(uint32_t aIndex, nsIController** _retval) {
127 NS_ENSURE_ARG_POINTER(_retval);
128 *_retval = nullptr;
130 nsXULControllerData* controllerData = mControllers.SafeElementAt(aIndex);
131 if (!controllerData) return NS_ERROR_FAILURE;
133 return controllerData->GetController(_retval); // does the addref
136 NS_IMETHODIMP
137 nsXULControllers::AppendController(nsIController* controller) {
138 // This assigns controller IDs starting at 1 so we can use 0 to test if an ID
139 // was obtained
140 nsXULControllerData* controllerData =
141 new nsXULControllerData(++mCurControllerID, controller);
143 #ifdef DEBUG
144 nsXULControllerData** appended =
145 #endif
146 mControllers.AppendElement(controllerData);
147 NS_ASSERTION(appended != nullptr, "Appending controller failed");
148 return NS_OK;
151 NS_IMETHODIMP
152 nsXULControllers::RemoveController(nsIController* controller) {
153 // first get the identity pointer
154 nsCOMPtr<nsISupports> controllerSup(do_QueryInterface(controller));
155 // then find it
156 uint32_t count = mControllers.Length();
157 for (uint32_t i = 0; i < count; i++) {
158 nsXULControllerData* controllerData = mControllers.ElementAt(i);
159 if (controllerData) {
160 nsCOMPtr<nsIController> thisController;
161 controllerData->GetController(getter_AddRefs(thisController));
162 nsCOMPtr<nsISupports> thisControllerSup(
163 do_QueryInterface(thisController)); // get identity
164 if (thisControllerSup == controllerSup) {
165 mControllers.RemoveElementAt(i);
166 delete controllerData;
167 return NS_OK;
171 return NS_ERROR_FAILURE; // right thing to return if no controller found?
174 NS_IMETHODIMP
175 nsXULControllers::GetControllerId(nsIController* controller,
176 uint32_t* _retval) {
177 NS_ENSURE_ARG_POINTER(_retval);
179 uint32_t count = mControllers.Length();
180 for (uint32_t i = 0; i < count; i++) {
181 nsXULControllerData* controllerData = mControllers.ElementAt(i);
182 if (controllerData) {
183 nsCOMPtr<nsIController> thisController;
184 controllerData->GetController(getter_AddRefs(thisController));
185 if (thisController.get() == controller) {
186 *_retval = controllerData->GetControllerID();
187 return NS_OK;
191 return NS_ERROR_FAILURE; // none found
194 NS_IMETHODIMP
195 nsXULControllers::GetControllerById(uint32_t controllerID,
196 nsIController** _retval) {
197 NS_ENSURE_ARG_POINTER(_retval);
199 uint32_t count = mControllers.Length();
200 for (uint32_t i = 0; i < count; i++) {
201 nsXULControllerData* controllerData = mControllers.ElementAt(i);
202 if (controllerData && controllerData->GetControllerID() == controllerID) {
203 return controllerData->GetController(_retval);
206 return NS_ERROR_FAILURE; // none found
209 NS_IMETHODIMP
210 nsXULControllers::GetControllerCount(uint32_t* _retval) {
211 NS_ENSURE_ARG_POINTER(_retval);
212 *_retval = mControllers.Length();
213 return NS_OK;