Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "IMETextTxn.h" |
michael@0 | 7 | #include "mozilla/DebugOnly.h" // for DebugOnly |
michael@0 | 8 | #include "mozilla/mozalloc.h" // for operator new |
michael@0 | 9 | #include "mozilla/TextEvents.h" // for TextRangeStyle |
michael@0 | 10 | #include "nsAString.h" // for nsAString_internal::Length, etc |
michael@0 | 11 | #include "nsAutoPtr.h" // for nsRefPtr |
michael@0 | 12 | #include "nsDebug.h" // for NS_ASSERTION, etc |
michael@0 | 13 | #include "nsError.h" // for NS_SUCCEEDED, NS_FAILED, etc |
michael@0 | 14 | #include "nsIDOMCharacterData.h" // for nsIDOMCharacterData |
michael@0 | 15 | #include "nsIDOMRange.h" // for nsRange::SetEnd, etc |
michael@0 | 16 | #include "nsIContent.h" // for nsIContent |
michael@0 | 17 | #include "nsIEditor.h" // for nsIEditor |
michael@0 | 18 | #include "nsIPresShell.h" // for SelectionType |
michael@0 | 19 | #include "nsISelection.h" // for nsISelection |
michael@0 | 20 | #include "nsISelectionController.h" // for nsISelectionController, etc |
michael@0 | 21 | #include "nsISelectionPrivate.h" // for nsISelectionPrivate |
michael@0 | 22 | #include "nsISupportsImpl.h" // for nsRange::AddRef, etc |
michael@0 | 23 | #include "nsISupportsUtils.h" // for NS_ADDREF_THIS, NS_RELEASE |
michael@0 | 24 | #include "nsITransaction.h" // for nsITransaction |
michael@0 | 25 | #include "nsRange.h" // for nsRange |
michael@0 | 26 | #include "nsString.h" // for nsString |
michael@0 | 27 | |
michael@0 | 28 | using namespace mozilla; |
michael@0 | 29 | |
michael@0 | 30 | // #define DEBUG_IMETXN |
michael@0 | 31 | |
michael@0 | 32 | IMETextTxn::IMETextTxn() |
michael@0 | 33 | : EditTxn() |
michael@0 | 34 | { |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | NS_IMPL_CYCLE_COLLECTION_INHERITED(IMETextTxn, EditTxn, |
michael@0 | 38 | mElement) |
michael@0 | 39 | // mRangeList can't lead to cycles |
michael@0 | 40 | |
michael@0 | 41 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(IMETextTxn) |
michael@0 | 42 | if (aIID.Equals(IMETextTxn::GetCID())) { |
michael@0 | 43 | *aInstancePtr = (void*)(IMETextTxn*)this; |
michael@0 | 44 | NS_ADDREF_THIS(); |
michael@0 | 45 | return NS_OK; |
michael@0 | 46 | } else |
michael@0 | 47 | NS_INTERFACE_MAP_END_INHERITING(EditTxn) |
michael@0 | 48 | |
michael@0 | 49 | NS_IMETHODIMP IMETextTxn::Init(nsIDOMCharacterData *aElement, |
michael@0 | 50 | uint32_t aOffset, |
michael@0 | 51 | uint32_t aReplaceLength, |
michael@0 | 52 | TextRangeArray *aTextRangeArray, |
michael@0 | 53 | const nsAString &aStringToInsert, |
michael@0 | 54 | nsIEditor *aEditor) |
michael@0 | 55 | { |
michael@0 | 56 | NS_ENSURE_ARG_POINTER(aElement); |
michael@0 | 57 | mElement = aElement; |
michael@0 | 58 | mOffset = aOffset; |
michael@0 | 59 | mReplaceLength = aReplaceLength; |
michael@0 | 60 | mStringToInsert = aStringToInsert; |
michael@0 | 61 | mEditor = aEditor; |
michael@0 | 62 | mRanges = aTextRangeArray; |
michael@0 | 63 | mFixed = false; |
michael@0 | 64 | return NS_OK; |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | NS_IMETHODIMP IMETextTxn::DoTransaction(void) |
michael@0 | 68 | { |
michael@0 | 69 | |
michael@0 | 70 | #ifdef DEBUG_IMETXN |
michael@0 | 71 | printf("Do IME Text element = %p replace = %d len = %d\n", mElement.get(), mReplaceLength, mStringToInsert.Length()); |
michael@0 | 72 | #endif |
michael@0 | 73 | |
michael@0 | 74 | nsCOMPtr<nsISelectionController> selCon; |
michael@0 | 75 | mEditor->GetSelectionController(getter_AddRefs(selCon)); |
michael@0 | 76 | NS_ENSURE_TRUE(selCon, NS_ERROR_NOT_INITIALIZED); |
michael@0 | 77 | |
michael@0 | 78 | // advance caret: This requires the presentation shell to get the selection. |
michael@0 | 79 | nsresult result; |
michael@0 | 80 | if (mReplaceLength == 0) { |
michael@0 | 81 | result = mElement->InsertData(mOffset, mStringToInsert); |
michael@0 | 82 | } else { |
michael@0 | 83 | result = mElement->ReplaceData(mOffset, mReplaceLength, mStringToInsert); |
michael@0 | 84 | } |
michael@0 | 85 | if (NS_SUCCEEDED(result)) { |
michael@0 | 86 | result = SetSelectionForRanges(); |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | return result; |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | NS_IMETHODIMP IMETextTxn::UndoTransaction(void) |
michael@0 | 93 | { |
michael@0 | 94 | #ifdef DEBUG_IMETXN |
michael@0 | 95 | printf("Undo IME Text element = %p\n", mElement.get()); |
michael@0 | 96 | #endif |
michael@0 | 97 | |
michael@0 | 98 | nsCOMPtr<nsISelectionController> selCon; |
michael@0 | 99 | mEditor->GetSelectionController(getter_AddRefs(selCon)); |
michael@0 | 100 | NS_ENSURE_TRUE(selCon, NS_ERROR_NOT_INITIALIZED); |
michael@0 | 101 | |
michael@0 | 102 | nsresult result = mElement->DeleteData(mOffset, mStringToInsert.Length()); |
michael@0 | 103 | if (NS_SUCCEEDED(result)) |
michael@0 | 104 | { // set the selection to the insertion point where the string was removed |
michael@0 | 105 | nsCOMPtr<nsISelection> selection; |
michael@0 | 106 | result = selCon->GetSelection(nsISelectionController::SELECTION_NORMAL, getter_AddRefs(selection)); |
michael@0 | 107 | if (NS_SUCCEEDED(result) && selection) { |
michael@0 | 108 | result = selection->Collapse(mElement, mOffset); |
michael@0 | 109 | NS_ASSERTION((NS_SUCCEEDED(result)), "selection could not be collapsed after undo of IME insert."); |
michael@0 | 110 | } |
michael@0 | 111 | } |
michael@0 | 112 | return result; |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | NS_IMETHODIMP IMETextTxn::Merge(nsITransaction *aTransaction, bool *aDidMerge) |
michael@0 | 116 | { |
michael@0 | 117 | NS_ASSERTION(aDidMerge, "illegal vaule- null ptr- aDidMerge"); |
michael@0 | 118 | NS_ASSERTION(aTransaction, "illegal vaule- null ptr- aTransaction"); |
michael@0 | 119 | NS_ENSURE_TRUE(aDidMerge && aTransaction, NS_ERROR_NULL_POINTER); |
michael@0 | 120 | |
michael@0 | 121 | #ifdef DEBUG_IMETXN |
michael@0 | 122 | printf("Merge IME Text element = %p\n", mElement.get()); |
michael@0 | 123 | #endif |
michael@0 | 124 | |
michael@0 | 125 | // |
michael@0 | 126 | // check to make sure we aren't fixed, if we are then nothing get's absorbed |
michael@0 | 127 | // |
michael@0 | 128 | if (mFixed) { |
michael@0 | 129 | *aDidMerge = false; |
michael@0 | 130 | return NS_OK; |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | // |
michael@0 | 134 | // if aTransaction is another IMETextTxn then absorb it |
michael@0 | 135 | // |
michael@0 | 136 | IMETextTxn* otherTxn = nullptr; |
michael@0 | 137 | nsresult result = aTransaction->QueryInterface(IMETextTxn::GetCID(),(void**)&otherTxn); |
michael@0 | 138 | if (otherTxn && NS_SUCCEEDED(result)) |
michael@0 | 139 | { |
michael@0 | 140 | // |
michael@0 | 141 | // we absorb the next IME transaction by adopting its insert string as our own |
michael@0 | 142 | // |
michael@0 | 143 | mStringToInsert = otherTxn->mStringToInsert; |
michael@0 | 144 | mRanges = otherTxn->mRanges; |
michael@0 | 145 | *aDidMerge = true; |
michael@0 | 146 | #ifdef DEBUG_IMETXN |
michael@0 | 147 | printf("IMETextTxn assimilated IMETextTxn:%p\n", aTransaction); |
michael@0 | 148 | #endif |
michael@0 | 149 | NS_RELEASE(otherTxn); |
michael@0 | 150 | return NS_OK; |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | *aDidMerge = false; |
michael@0 | 154 | return NS_OK; |
michael@0 | 155 | } |
michael@0 | 156 | |
michael@0 | 157 | NS_IMETHODIMP IMETextTxn::MarkFixed(void) |
michael@0 | 158 | { |
michael@0 | 159 | mFixed = true; |
michael@0 | 160 | return NS_OK; |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | NS_IMETHODIMP IMETextTxn::GetTxnDescription(nsAString& aString) |
michael@0 | 164 | { |
michael@0 | 165 | aString.AssignLiteral("IMETextTxn: "); |
michael@0 | 166 | aString += mStringToInsert; |
michael@0 | 167 | return NS_OK; |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | /* ============ protected methods ================== */ |
michael@0 | 171 | static SelectionType |
michael@0 | 172 | ToSelectionType(uint32_t aTextRangeType) |
michael@0 | 173 | { |
michael@0 | 174 | switch(aTextRangeType) { |
michael@0 | 175 | case NS_TEXTRANGE_RAWINPUT: |
michael@0 | 176 | return nsISelectionController::SELECTION_IME_RAWINPUT; |
michael@0 | 177 | case NS_TEXTRANGE_SELECTEDRAWTEXT: |
michael@0 | 178 | return nsISelectionController::SELECTION_IME_SELECTEDRAWTEXT; |
michael@0 | 179 | case NS_TEXTRANGE_CONVERTEDTEXT: |
michael@0 | 180 | return nsISelectionController::SELECTION_IME_CONVERTEDTEXT; |
michael@0 | 181 | case NS_TEXTRANGE_SELECTEDCONVERTEDTEXT: |
michael@0 | 182 | return nsISelectionController::SELECTION_IME_SELECTEDCONVERTEDTEXT; |
michael@0 | 183 | default: |
michael@0 | 184 | MOZ_CRASH("Selection type is invalid"); |
michael@0 | 185 | return nsISelectionController::SELECTION_NORMAL; |
michael@0 | 186 | } |
michael@0 | 187 | } |
michael@0 | 188 | |
michael@0 | 189 | nsresult |
michael@0 | 190 | IMETextTxn::SetSelectionForRanges() |
michael@0 | 191 | { |
michael@0 | 192 | nsCOMPtr<nsISelectionController> selCon; |
michael@0 | 193 | mEditor->GetSelectionController(getter_AddRefs(selCon)); |
michael@0 | 194 | NS_ENSURE_TRUE(selCon, NS_ERROR_NOT_INITIALIZED); |
michael@0 | 195 | |
michael@0 | 196 | nsCOMPtr<nsISelection> selection; |
michael@0 | 197 | nsresult rv = |
michael@0 | 198 | selCon->GetSelection(nsISelectionController::SELECTION_NORMAL, |
michael@0 | 199 | getter_AddRefs(selection)); |
michael@0 | 200 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 201 | |
michael@0 | 202 | nsCOMPtr<nsISelectionPrivate> selPriv(do_QueryInterface(selection)); |
michael@0 | 203 | rv = selPriv->StartBatchChanges(); |
michael@0 | 204 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 205 | |
michael@0 | 206 | // First, remove all selections of IME composition. |
michael@0 | 207 | static const SelectionType kIMESelections[] = { |
michael@0 | 208 | nsISelectionController::SELECTION_IME_RAWINPUT, |
michael@0 | 209 | nsISelectionController::SELECTION_IME_SELECTEDRAWTEXT, |
michael@0 | 210 | nsISelectionController::SELECTION_IME_CONVERTEDTEXT, |
michael@0 | 211 | nsISelectionController::SELECTION_IME_SELECTEDCONVERTEDTEXT |
michael@0 | 212 | }; |
michael@0 | 213 | for (uint32_t i = 0; i < ArrayLength(kIMESelections); ++i) { |
michael@0 | 214 | nsCOMPtr<nsISelection> selectionOfIME; |
michael@0 | 215 | if (NS_FAILED(selCon->GetSelection(kIMESelections[i], |
michael@0 | 216 | getter_AddRefs(selectionOfIME)))) { |
michael@0 | 217 | continue; |
michael@0 | 218 | } |
michael@0 | 219 | DebugOnly<nsresult> rv = selectionOfIME->RemoveAllRanges(); |
michael@0 | 220 | NS_ASSERTION(NS_SUCCEEDED(rv), |
michael@0 | 221 | "Failed to remove all ranges of IME selection"); |
michael@0 | 222 | } |
michael@0 | 223 | |
michael@0 | 224 | // Set caret position and selection of IME composition with TextRangeArray. |
michael@0 | 225 | bool setCaret = false; |
michael@0 | 226 | uint32_t countOfRanges = mRanges ? mRanges->Length() : 0; |
michael@0 | 227 | for (uint32_t i = 0; i < countOfRanges; ++i) { |
michael@0 | 228 | const TextRange& textRange = mRanges->ElementAt(i); |
michael@0 | 229 | |
michael@0 | 230 | // Caret needs special handling since its length may be 0 and if it's not |
michael@0 | 231 | // specified explicitly, we need to handle it ourselves later. |
michael@0 | 232 | if (textRange.mRangeType == NS_TEXTRANGE_CARETPOSITION) { |
michael@0 | 233 | NS_ASSERTION(!setCaret, "The ranges already has caret position"); |
michael@0 | 234 | NS_ASSERTION(!textRange.Length(), "nsEditor doesn't support wide caret"); |
michael@0 | 235 | // NOTE: If the caret position is larger than max length of the editor |
michael@0 | 236 | // content, this may fail. |
michael@0 | 237 | rv = selection->Collapse(mElement, mOffset + textRange.mStartOffset); |
michael@0 | 238 | setCaret = setCaret || NS_SUCCEEDED(rv); |
michael@0 | 239 | NS_ASSERTION(setCaret, "Failed to collapse normal selection"); |
michael@0 | 240 | continue; |
michael@0 | 241 | } |
michael@0 | 242 | |
michael@0 | 243 | // If the clause length is 0, it's should be a bug. |
michael@0 | 244 | if (!textRange.Length()) { |
michael@0 | 245 | NS_WARNING("Any clauses must not be empty"); |
michael@0 | 246 | continue; |
michael@0 | 247 | } |
michael@0 | 248 | |
michael@0 | 249 | nsRefPtr<nsRange> clauseRange; |
michael@0 | 250 | rv = nsRange::CreateRange(mElement, mOffset + textRange.mStartOffset, |
michael@0 | 251 | mElement, mOffset + textRange.mEndOffset, |
michael@0 | 252 | getter_AddRefs(clauseRange)); |
michael@0 | 253 | if (NS_FAILED(rv)) { |
michael@0 | 254 | NS_WARNING("Failed to create a DOM range for a clause of composition"); |
michael@0 | 255 | break; |
michael@0 | 256 | } |
michael@0 | 257 | |
michael@0 | 258 | // Set the range of the clause to selection. |
michael@0 | 259 | nsCOMPtr<nsISelection> selectionOfIME; |
michael@0 | 260 | rv = selCon->GetSelection(ToSelectionType(textRange.mRangeType), |
michael@0 | 261 | getter_AddRefs(selectionOfIME)); |
michael@0 | 262 | if (NS_FAILED(rv)) { |
michael@0 | 263 | NS_WARNING("Failed to get IME selection"); |
michael@0 | 264 | break; |
michael@0 | 265 | } |
michael@0 | 266 | |
michael@0 | 267 | rv = selectionOfIME->AddRange(clauseRange); |
michael@0 | 268 | if (NS_FAILED(rv)) { |
michael@0 | 269 | NS_WARNING("Failed to add selection range for a clause of composition"); |
michael@0 | 270 | break; |
michael@0 | 271 | } |
michael@0 | 272 | |
michael@0 | 273 | // Set the style of the clause. |
michael@0 | 274 | nsCOMPtr<nsISelectionPrivate> selectionOfIMEPriv = |
michael@0 | 275 | do_QueryInterface(selectionOfIME); |
michael@0 | 276 | if (!selectionOfIMEPriv) { |
michael@0 | 277 | NS_WARNING("Failed to get nsISelectionPrivate interface from selection"); |
michael@0 | 278 | continue; // Since this is additional feature, we can continue this job. |
michael@0 | 279 | } |
michael@0 | 280 | rv = selectionOfIMEPriv->SetTextRangeStyle(clauseRange, |
michael@0 | 281 | textRange.mRangeStyle); |
michael@0 | 282 | if (NS_FAILED(rv)) { |
michael@0 | 283 | NS_WARNING("Failed to set selection style"); |
michael@0 | 284 | break; // but this is unexpected... |
michael@0 | 285 | } |
michael@0 | 286 | } |
michael@0 | 287 | |
michael@0 | 288 | // If the ranges doesn't include explicit caret position, let's set the |
michael@0 | 289 | // caret to the end of composition string. |
michael@0 | 290 | if (!setCaret) { |
michael@0 | 291 | rv = selection->Collapse(mElement, mOffset + mStringToInsert.Length()); |
michael@0 | 292 | NS_ASSERTION(NS_SUCCEEDED(rv), |
michael@0 | 293 | "Failed to set caret at the end of composition string"); |
michael@0 | 294 | } |
michael@0 | 295 | |
michael@0 | 296 | rv = selPriv->EndBatchChanges(); |
michael@0 | 297 | NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to end batch changes"); |
michael@0 | 298 | |
michael@0 | 299 | return rv; |
michael@0 | 300 | } |
michael@0 | 301 |