Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(265)

Side by Side Diff: core/fpdfdoc/cpdf_formfield.cpp

Issue 2510223002: Make CPDF_Dictionary use unique pointers. (Closed)
Patch Set: Plug leaks Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 PDFium Authors. All rights reserved. 1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 6
7 #include "core/fpdfdoc/cpdf_formfield.h" 7 #include "core/fpdfdoc/cpdf_formfield.h"
8 8
9 #include <set> 9 #include <set>
10 10
11 #include "core/fpdfapi/parser/cfdf_document.h" 11 #include "core/fpdfapi/parser/cfdf_document.h"
12 #include "core/fpdfapi/parser/cpdf_array.h" 12 #include "core/fpdfapi/parser/cpdf_array.h"
13 #include "core/fpdfapi/parser/cpdf_document.h" 13 #include "core/fpdfapi/parser/cpdf_document.h"
14 #include "core/fpdfapi/parser/cpdf_name.h"
14 #include "core/fpdfapi/parser/cpdf_number.h" 15 #include "core/fpdfapi/parser/cpdf_number.h"
15 #include "core/fpdfapi/parser/cpdf_simple_parser.h" 16 #include "core/fpdfapi/parser/cpdf_simple_parser.h"
16 #include "core/fpdfapi/parser/cpdf_string.h" 17 #include "core/fpdfapi/parser/cpdf_string.h"
17 #include "core/fpdfapi/parser/fpdf_parser_decode.h" 18 #include "core/fpdfapi/parser/fpdf_parser_decode.h"
18 #include "core/fpdfdoc/cpdf_formcontrol.h" 19 #include "core/fpdfdoc/cpdf_formcontrol.h"
19 #include "core/fpdfdoc/cpdf_interform.h" 20 #include "core/fpdfdoc/cpdf_interform.h"
20 #include "core/fpdfdoc/cpvt_generateap.h" 21 #include "core/fpdfdoc/cpvt_generateap.h"
21 #include "third_party/base/stl_util.h" 22 #include "third_party/base/stl_util.h"
22 23
23 namespace { 24 namespace {
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 return false; 217 return false;
217 218
218 if (bNotify && !NotifyBeforeValueChange(csDValue)) 219 if (bNotify && !NotifyBeforeValueChange(csDValue))
219 return false; 220 return false;
220 221
221 if (pDV) { 222 if (pDV) {
222 std::unique_ptr<CPDF_Object> pClone = pDV->Clone(); 223 std::unique_ptr<CPDF_Object> pClone = pDV->Clone();
223 if (!pClone) 224 if (!pClone)
224 return false; 225 return false;
225 226
226 m_pDict->SetFor("V", pClone.release()); 227 m_pDict->SetFor("V", std::move(pClone));
227 if (pRV) { 228 if (pRV)
228 std::unique_ptr<CPDF_Object> pCloneR = pDV->Clone(); 229 m_pDict->SetFor("RV", pDV->Clone());
229 m_pDict->SetFor("RV", pCloneR.release());
230 }
231 } else { 230 } else {
232 m_pDict->RemoveFor("V"); 231 m_pDict->RemoveFor("V");
233 m_pDict->RemoveFor("RV"); 232 m_pDict->RemoveFor("RV");
234 } 233 }
235 if (bNotify) 234 if (bNotify)
236 NotifyAfterValueChange(); 235 NotifyAfterValueChange();
237 break; 236 break;
238 } 237 }
239 } 238 }
240 return true; 239 return true;
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
354 return true; 353 return true;
355 } 354 }
356 case File: 355 case File:
357 case RichText: 356 case RichText:
358 case Text: 357 case Text:
359 case ComboBox: { 358 case ComboBox: {
360 CFX_WideString csValue = value; 359 CFX_WideString csValue = value;
361 if (bNotify && !NotifyBeforeValueChange(csValue)) 360 if (bNotify && !NotifyBeforeValueChange(csValue))
362 return false; 361 return false;
363 362
363 CFX_ByteString key(bDefault ? "DV" : "V");
364 int iIndex = FindOptionValue(csValue); 364 int iIndex = FindOptionValue(csValue);
365 if (iIndex < 0) { 365 if (iIndex < 0) {
366 CFX_ByteString bsEncodeText = PDF_EncodeText(csValue); 366 CFX_ByteString bsEncodeText = PDF_EncodeText(csValue);
367 m_pDict->SetStringFor(bDefault ? "DV" : "V", bsEncodeText); 367 m_pDict->SetNewFor<CPDF_String>(key, bsEncodeText, false);
368 if (m_Type == RichText && !bDefault) 368 if (m_Type == RichText && !bDefault)
369 m_pDict->SetStringFor("RV", bsEncodeText); 369 m_pDict->SetNewFor<CPDF_String>("RV", bsEncodeText, false);
370 m_pDict->RemoveFor("I"); 370 m_pDict->RemoveFor("I");
371 } else { 371 } else {
372 m_pDict->SetStringFor(bDefault ? "DV" : "V", PDF_EncodeText(csValue)); 372 m_pDict->SetNewFor<CPDF_String>(key, PDF_EncodeText(csValue), false);
373 if (!bDefault) { 373 if (!bDefault) {
374 ClearSelection(); 374 ClearSelection();
375 SetItemSelection(iIndex, true); 375 SetItemSelection(iIndex, true);
376 } 376 }
377 } 377 }
378 if (bNotify) 378 if (bNotify)
379 NotifyAfterValueChange(); 379 NotifyAfterValueChange();
380 break; 380 break;
381 } 381 }
382 case ListBox: { 382 case ListBox: {
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
542 return false; 542 return false;
543 543
544 CFX_WideString opt_value = GetOptionValue(index); 544 CFX_WideString opt_value = GetOptionValue(index);
545 if (bNotify && !NotifyListOrComboBoxBeforeChange(opt_value)) 545 if (bNotify && !NotifyListOrComboBoxBeforeChange(opt_value))
546 return false; 546 return false;
547 547
548 if (bSelected) { 548 if (bSelected) {
549 if (GetType() == ListBox) { 549 if (GetType() == ListBox) {
550 SelectOption(index, true); 550 SelectOption(index, true);
551 if (!(m_Flags & kFormListMultiSelect)) { 551 if (!(m_Flags & kFormListMultiSelect)) {
552 m_pDict->SetStringFor("V", PDF_EncodeText(opt_value)); 552 m_pDict->SetNewFor<CPDF_String>("V", PDF_EncodeText(opt_value), false);
553 } else { 553 } else {
554 CPDF_Array* pArray = new CPDF_Array; 554 CPDF_Array* pArray = m_pDict->SetNewFor<CPDF_Array>("V");
555 for (int i = 0; i < CountOptions(); i++) { 555 for (int i = 0; i < CountOptions(); i++) {
556 if (i == index || IsItemSelected(i)) { 556 if (i == index || IsItemSelected(i)) {
557 opt_value = GetOptionValue(i); 557 opt_value = GetOptionValue(i);
558 pArray->AddNew<CPDF_String>(PDF_EncodeText(opt_value), false); 558 pArray->AddNew<CPDF_String>(PDF_EncodeText(opt_value), false);
559 } 559 }
560 } 560 }
561 m_pDict->SetFor("V", pArray);
562 } 561 }
563 } else { 562 } else {
564 m_pDict->SetStringFor("V", PDF_EncodeText(opt_value)); 563 m_pDict->SetNewFor<CPDF_String>("V", PDF_EncodeText(opt_value), false);
565 CPDF_Array* pI = new CPDF_Array; 564 CPDF_Array* pI = m_pDict->SetNewFor<CPDF_Array>("I");
566 pI->AddNew<CPDF_Number>(index); 565 pI->AddNew<CPDF_Number>(index);
567 m_pDict->SetFor("I", pI);
568 } 566 }
569 } else { 567 } else {
570 CPDF_Object* pValue = FPDF_GetFieldAttr(m_pDict, "V"); 568 CPDF_Object* pValue = FPDF_GetFieldAttr(m_pDict, "V");
571 if (pValue) { 569 if (pValue) {
572 if (GetType() == ListBox) { 570 if (GetType() == ListBox) {
573 SelectOption(index, false); 571 SelectOption(index, false);
574 if (pValue->IsString()) { 572 if (pValue->IsString()) {
575 if (pValue->GetUnicodeText() == opt_value) 573 if (pValue->GetUnicodeText() == opt_value)
576 m_pDict->RemoveFor("V"); 574 m_pDict->RemoveFor("V");
577 } else if (pValue->IsArray()) { 575 } else if (pValue->IsArray()) {
578 std::unique_ptr<CPDF_Array> pArray(new CPDF_Array); 576 std::unique_ptr<CPDF_Array> pArray(new CPDF_Array);
579 for (int i = 0; i < CountOptions(); i++) { 577 for (int i = 0; i < CountOptions(); i++) {
580 if (i != index && IsItemSelected(i)) { 578 if (i != index && IsItemSelected(i)) {
581 opt_value = GetOptionValue(i); 579 opt_value = GetOptionValue(i);
582 pArray->AddNew<CPDF_String>(PDF_EncodeText(opt_value), false); 580 pArray->AddNew<CPDF_String>(PDF_EncodeText(opt_value), false);
583 } 581 }
584 } 582 }
585 if (pArray->GetCount() > 0) 583 if (pArray->GetCount() > 0)
586 m_pDict->SetFor("V", pArray.release()); // std::move someday 584 m_pDict->SetFor("V", std::move(pArray));
587 } 585 }
588 } else { 586 } else {
589 m_pDict->RemoveFor("V"); 587 m_pDict->RemoveFor("V");
590 m_pDict->RemoveFor("I"); 588 m_pDict->RemoveFor("I");
591 } 589 }
592 } 590 }
593 } 591 }
594 if (bNotify) 592 if (bNotify)
595 NotifyListOrComboBoxAfterChange(); 593 NotifyListOrComboBoxAfterChange();
596 return true; 594 return true;
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
668 int index, 666 int index,
669 bool bNotify) { 667 bool bNotify) {
670 if (csOptLabel.IsEmpty()) 668 if (csOptLabel.IsEmpty())
671 return -1; 669 return -1;
672 670
673 if (bNotify && !NotifyListOrComboBoxBeforeChange(csOptLabel)) 671 if (bNotify && !NotifyListOrComboBoxBeforeChange(csOptLabel))
674 return -1; 672 return -1;
675 673
676 CFX_ByteString csStr = 674 CFX_ByteString csStr =
677 PDF_EncodeText(csOptLabel.c_str(), csOptLabel.GetLength()); 675 PDF_EncodeText(csOptLabel.c_str(), csOptLabel.GetLength());
678 CPDF_Object* pValue = FPDF_GetFieldAttr(m_pDict, "Opt"); 676 CPDF_Array* pOpt = ToArray(FPDF_GetFieldAttr(m_pDict, "Opt"));
679 CPDF_Array* pOpt = ToArray(pValue); 677 if (!pOpt)
680 if (!pOpt) { 678 pOpt = m_pDict->SetNewFor<CPDF_Array>("Opt");
681 pOpt = new CPDF_Array;
682 m_pDict->SetFor("Opt", pOpt);
683 }
684 679
685 int iCount = pdfium::base::checked_cast<int>(pOpt->GetCount()); 680 int iCount = pdfium::base::checked_cast<int>(pOpt->GetCount());
686 if (index >= iCount) { 681 if (index >= iCount) {
687 pOpt->AddNew<CPDF_String>(csStr, false); 682 pOpt->AddNew<CPDF_String>(csStr, false);
688 index = iCount; 683 index = iCount;
689 } else { 684 } else {
690 pOpt->InsertNewAt<CPDF_String>(index, csStr, false); 685 pOpt->InsertNewAt<CPDF_String>(index, csStr, false);
691 } 686 }
692 687
693 if (bNotify) 688 if (bNotify)
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
748 if (i == iControlIndex) 743 if (i == iControlIndex)
749 pCtrl->CheckControl(bChecked); 744 pCtrl->CheckControl(bChecked);
750 else if (bChecked) 745 else if (bChecked)
751 pCtrl->CheckControl(false); 746 pCtrl->CheckControl(false);
752 } 747 }
753 } 748 }
754 749
755 CPDF_Object* pOpt = FPDF_GetFieldAttr(m_pDict, "Opt"); 750 CPDF_Object* pOpt = FPDF_GetFieldAttr(m_pDict, "Opt");
756 if (!ToArray(pOpt)) { 751 if (!ToArray(pOpt)) {
757 if (bChecked) { 752 if (bChecked) {
758 m_pDict->SetNameFor("V", csBExport); 753 m_pDict->SetNewFor<CPDF_Name>("V", csBExport);
759 } else { 754 } else {
760 CFX_ByteString csV; 755 CFX_ByteString csV;
761 CPDF_Object* pV = FPDF_GetFieldAttr(m_pDict, "V"); 756 CPDF_Object* pV = FPDF_GetFieldAttr(m_pDict, "V");
762 if (pV) 757 if (pV)
763 csV = pV->GetString(); 758 csV = pV->GetString();
764 if (csV == csBExport) 759 if (csV == csBExport)
765 m_pDict->SetNameFor("V", "Off"); 760 m_pDict->SetNewFor<CPDF_Name>("V", "Off");
766 } 761 }
767 } else if (bChecked) { 762 } else if (bChecked) {
768 CFX_ByteString csIndex; 763 CFX_ByteString csIndex;
769 csIndex.Format("%d", iControlIndex); 764 csIndex.Format("%d", iControlIndex);
770 m_pDict->SetNameFor("V", csIndex); 765 m_pDict->SetNewFor<CPDF_Name>("V", csIndex);
771 } 766 }
772 if (bNotify && m_pForm->m_pFormNotify) 767 if (bNotify && m_pForm->m_pFormNotify)
773 m_pForm->m_pFormNotify->AfterCheckedStatusChange(this); 768 m_pForm->m_pFormNotify->AfterCheckedStatusChange(this);
774 return true; 769 return true;
775 } 770 }
776 771
777 CFX_WideString CPDF_FormField::GetCheckValue(bool bDefault) const { 772 CFX_WideString CPDF_FormField::GetCheckValue(bool bDefault) const {
778 ASSERT(GetType() == CheckBox || GetType() == RadioButton); 773 ASSERT(GetType() == CheckBox || GetType() == RadioButton);
779 CFX_WideString csExport = L"Off"; 774 CFX_WideString csExport = L"Off";
780 int iCount = CountControls(); 775 int iCount = CountControls();
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
841 } 836 }
842 return false; 837 return false;
843 } 838 }
844 839
845 bool CPDF_FormField::SelectOption(int iOptIndex, bool bSelected, bool bNotify) { 840 bool CPDF_FormField::SelectOption(int iOptIndex, bool bSelected, bool bNotify) {
846 CPDF_Array* pArray = m_pDict->GetArrayFor("I"); 841 CPDF_Array* pArray = m_pDict->GetArrayFor("I");
847 if (!pArray) { 842 if (!pArray) {
848 if (!bSelected) 843 if (!bSelected)
849 return true; 844 return true;
850 845
851 pArray = new CPDF_Array; 846 pArray = m_pDict->SetNewFor<CPDF_Array>("I");
852 m_pDict->SetFor("I", pArray);
853 } 847 }
854 848
855 bool bReturn = false; 849 bool bReturn = false;
856 for (size_t i = 0; i < pArray->GetCount(); i++) { 850 for (size_t i = 0; i < pArray->GetCount(); i++) {
857 int iFind = pArray->GetIntegerAt(i); 851 int iFind = pArray->GetIntegerAt(i);
858 if (iFind == iOptIndex) { 852 if (iFind == iOptIndex) {
859 if (bSelected) 853 if (bSelected)
860 return true; 854 return true;
861 855
862 if (bNotify && m_pForm->m_pFormNotify) { 856 if (bNotify && m_pForm->m_pFormNotify) {
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
988 case ListBox: 982 case ListBox:
989 NotifyAfterSelectionChange(); 983 NotifyAfterSelectionChange();
990 break; 984 break;
991 case ComboBox: 985 case ComboBox:
992 NotifyAfterValueChange(); 986 NotifyAfterValueChange();
993 break; 987 break;
994 default: 988 default:
995 break; 989 break;
996 } 990 }
997 } 991 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698