nsUrlClassifierPrefixSet.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //* -*- Mode: C++; tab-width: 8; 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/. */
  5. #ifndef nsUrlClassifierPrefixSet_h_
  6. #define nsUrlClassifierPrefixSet_h_
  7. #include "nsISupportsUtils.h"
  8. #include "nsID.h"
  9. #include "nsIFile.h"
  10. #include "nsIMemoryReporter.h"
  11. #include "nsIMutableArray.h"
  12. #include "nsIFileStreams.h"
  13. #include "nsIUrlClassifierPrefixSet.h"
  14. #include "nsTArray.h"
  15. #include "nsToolkitCompsCID.h"
  16. #include "mozilla/FileUtils.h"
  17. #include "mozilla/MemoryReporting.h"
  18. #include "mozilla/Mutex.h"
  19. namespace mozilla {
  20. namespace safebrowsing {
  21. class VariableLengthPrefixSet;
  22. } // namespace safebrowsing
  23. } // namespace mozilla
  24. class nsUrlClassifierPrefixSet final
  25. : public nsIUrlClassifierPrefixSet
  26. , public nsIMemoryReporter
  27. {
  28. public:
  29. nsUrlClassifierPrefixSet();
  30. NS_IMETHOD Init(const nsACString& aName) override;
  31. NS_IMETHOD SetPrefixes(const uint32_t* aArray, uint32_t aLength) override;
  32. NS_IMETHOD GetPrefixes(uint32_t* aCount, uint32_t** aPrefixes) override;
  33. NS_IMETHOD Contains(uint32_t aPrefix, bool* aFound) override;
  34. NS_IMETHOD IsEmpty(bool* aEmpty) override;
  35. NS_IMETHOD LoadFromFile(nsIFile* aFile) override;
  36. NS_IMETHOD StoreToFile(nsIFile* aFile) override;
  37. nsresult GetPrefixesNative(FallibleTArray<uint32_t>& outArray);
  38. size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf);
  39. NS_DECL_THREADSAFE_ISUPPORTS
  40. NS_DECL_NSIMEMORYREPORTER
  41. friend class mozilla::safebrowsing::VariableLengthPrefixSet;
  42. private:
  43. virtual ~nsUrlClassifierPrefixSet();
  44. static const uint32_t MAX_BUFFER_SIZE = 64 * 1024;
  45. static const uint32_t DELTAS_LIMIT = 120;
  46. static const uint32_t MAX_INDEX_DIFF = (1 << 16);
  47. static const uint32_t PREFIXSET_VERSION_MAGIC = 1;
  48. nsresult MakePrefixSet(const uint32_t* aArray, uint32_t aLength);
  49. uint32_t BinSearch(uint32_t start, uint32_t end, uint32_t target);
  50. uint32_t CalculatePreallocateSize();
  51. nsresult WritePrefixes(nsIOutputStream* out);
  52. nsresult LoadPrefixes(nsIInputStream* in);
  53. // Lock to prevent races between the url-classifier thread (which does most
  54. // of the operations) and the main thread (which does memory reporting).
  55. // It should be held for all operations between Init() and destruction that
  56. // touch this class's data members.
  57. mozilla::Mutex mLock;
  58. // list of fully stored prefixes, that also form the
  59. // start of a run of deltas in mIndexDeltas.
  60. nsTArray<uint32_t> mIndexPrefixes;
  61. // array containing arrays of deltas from indices.
  62. // Index to the place that matches the closest lower
  63. // prefix from mIndexPrefix. Then every "delta" corresponds
  64. // to a prefix in the PrefixSet.
  65. nsTArray<nsTArray<uint16_t> > mIndexDeltas;
  66. // how many prefixes we have.
  67. uint32_t mTotalPrefixes;
  68. nsCString mMemoryReportPath;
  69. };
  70. #endif