Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/toolkit/components/autocomplete/nsAutoCompleteController.h
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
#ifndef __nsAutoCompleteController__
6
#define __nsAutoCompleteController__
7
8
#include "nsIAutoCompleteController.h"
9
10
#include "nsCOMPtr.h"
11
#include "nsIAutoCompleteInput.h"
12
#include "nsIAutoCompletePopup.h"
13
#include "nsIAutoCompleteResult.h"
14
#include "nsIAutoCompleteSearch.h"
15
#include "nsINamed.h"
16
#include "nsString.h"
17
#include "nsITimer.h"
18
#include "nsTArray.h"
19
#include "nsCOMArray.h"
20
#include "nsCycleCollectionParticipant.h"
21
22
class nsAutoCompleteController final : public nsIAutoCompleteController,
23
                                       public nsIAutoCompleteObserver,
24
                                       public nsITimerCallback,
25
                                       public nsINamed
26
{
27
public:
28
  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
29
  NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(nsAutoCompleteController,
30
                                           nsIAutoCompleteController)
31
  NS_DECL_NSIAUTOCOMPLETECONTROLLER
32
  NS_DECL_NSIAUTOCOMPLETEOBSERVER
33
  NS_DECL_NSITIMERCALLBACK
34
  NS_DECL_NSINAMED
35
36
  nsAutoCompleteController();
37
38
protected:
39
  virtual ~nsAutoCompleteController();
40
41
  /**
42
   * SetValueOfInputTo() sets value of mInput to aValue and notifies the input
43
   * of setting reason.
44
   */
45
  void SetValueOfInputTo(const nsString& aValue, uint16_t aReason);
46
47
  /**
48
   * SetSearchStringInternal() sets both mSearchString and mSetValue to
49
   * aSearchString.
50
   */
51
  void SetSearchStringInternal(const nsAString& aSearchString)
52
0
  {
53
0
    mSearchString = mSetValue = aSearchString;
54
0
  }
55
56
  nsresult OpenPopup();
57
  nsresult ClosePopup();
58
59
  nsresult StartSearch(uint16_t aSearchType);
60
61
  nsresult BeforeSearches();
62
  nsresult StartSearches();
63
  void AfterSearches();
64
  nsresult ClearSearchTimer();
65
  void MaybeCompletePlaceholder();
66
67
  nsresult ProcessResult(int32_t aSearchIndex, nsIAutoCompleteResult *aResult);
68
  nsresult PostSearchCleanup();
69
70
  nsresult EnterMatch(bool aIsPopupSelection,
71
                      mozilla::dom::Event* aEvent);
72
  nsresult RevertTextValue();
73
74
  nsresult CompleteDefaultIndex(int32_t aResultIndex);
75
  nsresult CompleteValue(nsString &aValue);
76
77
  nsresult GetResultAt(int32_t aIndex, nsIAutoCompleteResult** aResult,
78
                       int32_t* aMatchIndex);
79
  nsresult GetResultValueAt(int32_t aIndex, bool aGetFinalValue,
80
                            nsAString & _retval);
81
  nsresult GetResultLabelAt(int32_t aIndex, nsAString & _retval);
82
private:
83
  nsresult GetResultValueLabelAt(int32_t aIndex, bool aGetFinalValue,
84
                                 bool aGetValue, nsAString & _retval);
85
86
  /**
87
   * Gets and validates the defaultComplete result and the relative
88
   * defaultIndex value.
89
   *
90
   * @param aResultIndex
91
   *        Index of the defaultComplete result to be used.  Pass -1 to search
92
   *        for the first result providing a valid defaultIndex.
93
   * @param _result
94
   *        The found result.
95
   * @param _defaultIndex
96
   *        The defaultIndex relative to _result.
97
   */
98
  nsresult GetDefaultCompleteResult(int32_t aResultIndex,
99
                                    nsIAutoCompleteResult** _result,
100
                                    int32_t* _defaultIndex);
101
102
  /**
103
   * Gets the defaultComplete value to be suggested to the user.
104
   *
105
   * @param aResultIndex
106
   *        Index of the defaultComplete result to be used.
107
   * @param aPreserveCasing
108
   *        Whether user casing should be preserved.
109
   * @param _retval
110
   *        The value to be completed.
111
   */
112
  nsresult GetDefaultCompleteValue(int32_t aResultIndex, bool aPreserveCasing,
113
                                   nsAString &_retval);
114
115
  /**
116
   * Gets the defaultComplete value to be used when the user confirms the
117
   * current match.
118
   * The value is returned only if it case-insensitively matches the current
119
   * input text, otherwise the method returns NS_ERROR_FAILURE.
120
   * This happens because we don't want to replace text if the user backspaces
121
   * just before Enter.
122
   *
123
   * @param _retval
124
   *        The value to be completed.
125
   */
126
  nsresult GetFinalDefaultCompleteValue(nsAString &_retval);
127
128
  nsresult ClearResults(bool aIsSearching = false);
129
130
  nsresult MatchIndexToSearch(int32_t aMatchIndex,
131
                              int32_t *aSearchIndex, int32_t *aItemIndex);
132
133
  // members //////////////////////////////////////////
134
135
  nsCOMPtr<nsIAutoCompleteInput> mInput;
136
137
  nsCOMArray<nsIAutoCompleteSearch> mSearches;
138
  // This is used as a sparse array, always use SafeObjectAt to access it.
139
  nsCOMArray<nsIAutoCompleteResult> mResults;
140
  // Temporarily keeps the results alive while invoking startSearch() for each
141
  // search.  This is needed to allow the searches to reuse the previous result,
142
  // since otherwise the first search clears mResults.
143
  nsCOMArray<nsIAutoCompleteResult> mResultCache;
144
145
  nsCOMPtr<nsITimer> mTimer;
146
147
  // mSearchString stores value which is the original value of the input or
148
  // typed by the user.  When user is choosing an item from the popup, this
149
  // is NOT modified by the item because this is used for reverting the input
150
  // value when user cancels choosing an item from the popup.
151
  // This should be set through only SetSearchStringInternal().
152
  nsString mSearchString;
153
  nsString mPlaceholderCompletionString;
154
  // mSetValue stores value which is expected in the input.  So, if input's
155
  // value and mSetValue are different, it means somebody has changed the
156
  // value like JS of the web content.
157
  // This is set only by SetValueOfInputTo() or when modifying mSearchString
158
  // through SetSearchStringInternal().
159
  nsString mSetValue;
160
  bool mDefaultIndexCompleted;
161
  bool mPopupClosedByCompositionStart;
162
163
  // Whether autofill is allowed for the next search. May be retrieved by the
164
  // search through the "prohibit-autofill" searchParam.
165
  bool mProhibitAutoFill;
166
167
  // Indicates whether the user cleared the autofilled part, returning to the
168
  // originally entered search string.
169
  bool mUserClearedAutoFill;
170
171
  // Indicates whether clearing the autofilled string should issue a new search.
172
  bool mClearingAutoFillSearchesAgain;
173
174
  enum CompositionState {
175
    eCompositionState_None,
176
    eCompositionState_Composing,
177
    eCompositionState_Committing
178
  };
179
  CompositionState mCompositionState;
180
  uint16_t mSearchStatus;
181
  uint32_t mMatchCount;
182
  uint32_t mSearchesOngoing;
183
  uint32_t mSearchesFailed;
184
  uint32_t mImmediateSearchesCount;
185
  // The index of the match on the popup that was selected using the keyboard,
186
  // if the completeselectedindex attribute is set.
187
  // This is used to distinguish that selection (which would have been put in
188
  // the input on being selected) from a moused-over selectedIndex value. This
189
  // distinction is used to prevent mouse moves from inadvertently changing
190
  // what happens once the user hits Enter on the keyboard.
191
  // See bug 1043584 for more details.
192
  int32_t  mCompletedSelectionIndex;
193
};
194
195
#endif /* __nsAutoCompleteController__ */