Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/toolkit/components/autocomplete/nsAutoCompleteSimpleResult.cpp
Line
Count
Source (jump to first uncovered line)
1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
 * License, v. 2.0. If a copy of the MPL was not distributed with this
3
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
#include "nsAutoCompleteSimpleResult.h"
6
7
#define CHECK_MATCH_INDEX(_index, _insert)                                     \
8
0
  if (_index < 0 ||                                                            \
9
0
      static_cast<MatchesArray::size_type>(_index) > mMatches.Length() ||      \
10
0
      (!_insert && static_cast<MatchesArray::size_type>(_index) == mMatches.Length())) { \
11
0
    MOZ_ASSERT(false, "Trying to use an invalid index on mMatches");           \
12
0
    return NS_ERROR_ILLEGAL_VALUE;                                             \
13
0
  }                                                                            \
14
15
NS_IMPL_ISUPPORTS(nsAutoCompleteSimpleResult,
16
                  nsIAutoCompleteResult,
17
                  nsIAutoCompleteSimpleResult)
18
19
struct AutoCompleteSimpleResultMatch
20
{
21
  AutoCompleteSimpleResultMatch(const nsAString& aValue,
22
                                const nsAString& aComment,
23
                                const nsAString& aImage,
24
                                const nsAString& aStyle,
25
                                const nsAString& aFinalCompleteValue,
26
                                const nsAString& aLabel)
27
    : mValue(aValue)
28
    , mComment(aComment)
29
    , mImage(aImage)
30
    , mStyle(aStyle)
31
    , mFinalCompleteValue(aFinalCompleteValue)
32
    , mLabel(aLabel)
33
0
  {
34
0
  }
35
36
  nsString mValue;
37
  nsString mComment;
38
  nsString mImage;
39
  nsString mStyle;
40
  nsString mFinalCompleteValue;
41
  nsString mLabel;
42
};
43
44
nsAutoCompleteSimpleResult::nsAutoCompleteSimpleResult() :
45
  mDefaultIndex(-1),
46
  mSearchResult(RESULT_NOMATCH)
47
0
{
48
0
}
49
50
nsresult
51
nsAutoCompleteSimpleResult::AppendResult(nsIAutoCompleteResult* aResult)
52
0
{
53
0
  nsAutoString searchString;
54
0
  nsresult rv = aResult->GetSearchString(searchString);
55
0
  NS_ENSURE_SUCCESS(rv, rv);
56
0
  mSearchString = searchString;
57
0
58
0
  uint16_t searchResult;
59
0
  rv = aResult->GetSearchResult(&searchResult);
60
0
  NS_ENSURE_SUCCESS(rv, rv);
61
0
  mSearchResult = searchResult;
62
0
63
0
  nsAutoString errorDescription;
64
0
  if (NS_SUCCEEDED(aResult->GetErrorDescription(errorDescription)) &&
65
0
      !errorDescription.IsEmpty()) {
66
0
    mErrorDescription = errorDescription;
67
0
  }
68
0
69
0
  int32_t defaultIndex = -1;
70
0
  if (NS_SUCCEEDED(aResult->GetDefaultIndex(&defaultIndex)) &&
71
0
      defaultIndex >= 0) {
72
0
    mDefaultIndex = defaultIndex;
73
0
  }
74
0
75
0
  nsCOMPtr<nsIAutoCompleteSimpleResult> simpleResult =
76
0
    do_QueryInterface(aResult);
77
0
  if (simpleResult) {
78
0
    nsCOMPtr<nsIAutoCompleteSimpleResultListener> listener;
79
0
    if (NS_SUCCEEDED(simpleResult->GetListener(getter_AddRefs(listener))) &&
80
0
        listener) {
81
0
      listener.swap(mListener);
82
0
    }
83
0
  }
84
0
85
0
  // Copy matches.
86
0
  uint32_t matchCount = 0;
87
0
  rv = aResult->GetMatchCount(&matchCount);
88
0
  NS_ENSURE_SUCCESS(rv, rv);
89
0
  for (size_t i = 0; i < matchCount; ++i) {
90
0
    nsAutoString value, comment, image, style, finalCompleteValue, label;
91
0
92
0
    rv = aResult->GetValueAt(i, value);
93
0
    NS_ENSURE_SUCCESS(rv, rv);
94
0
    rv = aResult->GetCommentAt(i, comment);
95
0
    NS_ENSURE_SUCCESS(rv, rv);
96
0
    rv = aResult->GetImageAt(i, image);
97
0
    NS_ENSURE_SUCCESS(rv, rv);
98
0
    rv = aResult->GetStyleAt(i, style);
99
0
    NS_ENSURE_SUCCESS(rv, rv);
100
0
    rv = aResult->GetFinalCompleteValueAt(i, finalCompleteValue);
101
0
    NS_ENSURE_SUCCESS(rv, rv);
102
0
    rv = aResult->GetLabelAt(i, label);
103
0
    NS_ENSURE_SUCCESS(rv, rv);
104
0
105
0
    rv = AppendMatch(value, comment, image, style, finalCompleteValue, label);
106
0
    NS_ENSURE_SUCCESS(rv, rv);
107
0
  }
108
0
109
0
  return NS_OK;
110
0
}
111
112
// searchString
113
NS_IMETHODIMP
114
nsAutoCompleteSimpleResult::GetSearchString(nsAString &aSearchString)
115
0
{
116
0
  aSearchString = mSearchString;
117
0
  return NS_OK;
118
0
}
119
NS_IMETHODIMP
120
nsAutoCompleteSimpleResult::SetSearchString(const nsAString &aSearchString)
121
0
{
122
0
  mSearchString.Assign(aSearchString);
123
0
  return NS_OK;
124
0
}
125
126
// searchResult
127
NS_IMETHODIMP
128
nsAutoCompleteSimpleResult::GetSearchResult(uint16_t *aSearchResult)
129
0
{
130
0
  *aSearchResult = mSearchResult;
131
0
  return NS_OK;
132
0
}
133
NS_IMETHODIMP
134
nsAutoCompleteSimpleResult::SetSearchResult(uint16_t aSearchResult)
135
0
{
136
0
  mSearchResult = aSearchResult;
137
0
  return NS_OK;
138
0
}
139
140
// defaultIndex
141
NS_IMETHODIMP
142
nsAutoCompleteSimpleResult::GetDefaultIndex(int32_t *aDefaultIndex)
143
0
{
144
0
  *aDefaultIndex = mDefaultIndex;
145
0
  return NS_OK;
146
0
}
147
NS_IMETHODIMP
148
nsAutoCompleteSimpleResult::SetDefaultIndex(int32_t aDefaultIndex)
149
0
{
150
0
  mDefaultIndex = aDefaultIndex;
151
0
  return NS_OK;
152
0
}
153
154
// errorDescription
155
NS_IMETHODIMP
156
nsAutoCompleteSimpleResult::GetErrorDescription(nsAString & aErrorDescription)
157
0
{
158
0
  aErrorDescription = mErrorDescription;
159
0
  return NS_OK;
160
0
}
161
NS_IMETHODIMP
162
nsAutoCompleteSimpleResult::SetErrorDescription(
163
                                             const nsAString &aErrorDescription)
164
0
{
165
0
  mErrorDescription.Assign(aErrorDescription);
166
0
  return NS_OK;
167
0
}
168
169
NS_IMETHODIMP
170
nsAutoCompleteSimpleResult::InsertMatchAt(int32_t aIndex,
171
                                          const nsAString& aValue,
172
                                          const nsAString& aComment,
173
                                          const nsAString& aImage,
174
                                          const nsAString& aStyle,
175
                                          const nsAString& aFinalCompleteValue,
176
                                          const nsAString& aLabel)
177
0
{
178
0
  CHECK_MATCH_INDEX(aIndex, true);
179
0
180
0
  AutoCompleteSimpleResultMatch match(aValue, aComment, aImage, aStyle, aFinalCompleteValue, aLabel);
181
0
182
0
  if (!mMatches.InsertElementAt(aIndex, match)) {
183
0
    return NS_ERROR_OUT_OF_MEMORY;
184
0
  }
185
0
186
0
  return NS_OK;
187
0
}
188
189
NS_IMETHODIMP
190
nsAutoCompleteSimpleResult::AppendMatch(const nsAString& aValue,
191
                                        const nsAString& aComment,
192
                                        const nsAString& aImage,
193
                                        const nsAString& aStyle,
194
                                        const nsAString& aFinalCompleteValue,
195
                                        const nsAString& aLabel)
196
0
{
197
0
  return InsertMatchAt(mMatches.Length(), aValue, aComment, aImage, aStyle,
198
0
                       aFinalCompleteValue, aLabel);
199
0
}
200
201
NS_IMETHODIMP
202
nsAutoCompleteSimpleResult::RemoveMatchAt(int32_t aIndex)
203
0
{
204
0
  CHECK_MATCH_INDEX(aIndex, false);
205
0
206
0
  mMatches.RemoveElementAt(aIndex);
207
0
  return NS_OK;
208
0
}
209
210
NS_IMETHODIMP
211
nsAutoCompleteSimpleResult::GetMatchCount(uint32_t *aMatchCount)
212
0
{
213
0
  *aMatchCount = mMatches.Length();
214
0
  return NS_OK;
215
0
}
216
217
NS_IMETHODIMP
218
nsAutoCompleteSimpleResult::GetValueAt(int32_t aIndex, nsAString& _retval)
219
0
{
220
0
  CHECK_MATCH_INDEX(aIndex, false);
221
0
  _retval = mMatches[aIndex].mValue;
222
0
  return NS_OK;
223
0
}
224
225
NS_IMETHODIMP
226
nsAutoCompleteSimpleResult::GetLabelAt(int32_t aIndex, nsAString& _retval)
227
0
{
228
0
  CHECK_MATCH_INDEX(aIndex, false);
229
0
  _retval = mMatches[aIndex].mLabel;
230
0
  if (_retval.IsEmpty()) {
231
0
    _retval = mMatches[aIndex].mValue;
232
0
  }
233
0
  return NS_OK;
234
0
}
235
236
NS_IMETHODIMP
237
nsAutoCompleteSimpleResult::GetCommentAt(int32_t aIndex, nsAString& _retval)
238
0
{
239
0
  CHECK_MATCH_INDEX(aIndex, false);
240
0
  _retval = mMatches[aIndex].mComment;
241
0
  return NS_OK;
242
0
}
243
244
NS_IMETHODIMP
245
nsAutoCompleteSimpleResult::GetImageAt(int32_t aIndex, nsAString& _retval)
246
0
{
247
0
  CHECK_MATCH_INDEX(aIndex, false);
248
0
  _retval = mMatches[aIndex].mImage;
249
0
  return NS_OK;
250
0
}
251
252
NS_IMETHODIMP
253
nsAutoCompleteSimpleResult::GetStyleAt(int32_t aIndex, nsAString& _retval)
254
0
{
255
0
  CHECK_MATCH_INDEX(aIndex, false);
256
0
  _retval = mMatches[aIndex].mStyle;
257
0
  return NS_OK;
258
0
}
259
260
NS_IMETHODIMP
261
nsAutoCompleteSimpleResult::GetFinalCompleteValueAt(int32_t aIndex,
262
                                                    nsAString& _retval)
263
0
{
264
0
  CHECK_MATCH_INDEX(aIndex, false);
265
0
  _retval = mMatches[aIndex].mFinalCompleteValue;
266
0
  if (_retval.IsEmpty()) {
267
0
    _retval = mMatches[aIndex].mValue;
268
0
  }
269
0
  return NS_OK;
270
0
}
271
272
NS_IMETHODIMP
273
nsAutoCompleteSimpleResult::SetListener(nsIAutoCompleteSimpleResultListener* aListener)
274
0
{
275
0
  mListener = aListener;
276
0
  return NS_OK;
277
0
}
278
279
NS_IMETHODIMP
280
nsAutoCompleteSimpleResult::GetListener(nsIAutoCompleteSimpleResultListener** aListener)
281
0
{
282
0
  nsCOMPtr<nsIAutoCompleteSimpleResultListener> listener(mListener);
283
0
  listener.forget(aListener);
284
0
  return NS_OK;
285
0
}
286
287
NS_IMETHODIMP
288
nsAutoCompleteSimpleResult::RemoveValueAt(int32_t aRowIndex,
289
                                          bool aRemoveFromDb)
290
0
{
291
0
  CHECK_MATCH_INDEX(aRowIndex, false);
292
0
293
0
  nsString value = mMatches[aRowIndex].mValue;
294
0
  mMatches.RemoveElementAt(aRowIndex);
295
0
296
0
  if (mListener) {
297
0
    mListener->OnValueRemoved(this, value, aRemoveFromDb);
298
0
  }
299
0
300
0
  return NS_OK;
301
0
}