VariableLengthPrefixSet.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 VariableLengthPrefixSet_h
  6. #define VariableLengthPrefixSet_h
  7. #include "nsISupports.h"
  8. #include "nsIMemoryReporter.h"
  9. #include "Entries.h"
  10. #include "nsTArray.h"
  11. #include "mozilla/MemoryReporting.h"
  12. #include "mozilla/Mutex.h"
  13. class nsUrlClassifierPrefixSet;
  14. namespace mozilla {
  15. namespace safebrowsing {
  16. class VariableLengthPrefixSet final
  17. : public nsIMemoryReporter
  18. {
  19. public:
  20. VariableLengthPrefixSet();
  21. NS_IMETHOD Init(const nsACString& aName);
  22. NS_IMETHOD SetPrefixes(const mozilla::safebrowsing::PrefixStringMap& aPrefixMap);
  23. NS_IMETHOD GetPrefixes(mozilla::safebrowsing::PrefixStringMap& aPrefixMap);
  24. NS_IMETHOD Matches(const nsACString& aFullHash, uint32_t* aLength);
  25. NS_IMETHOD IsEmpty(bool* aEmpty);
  26. NS_IMETHOD LoadFromFile(nsIFile* aFile);
  27. NS_IMETHOD StoreToFile(nsIFile* aFile);
  28. size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf);
  29. NS_DECL_THREADSAFE_ISUPPORTS
  30. NS_DECL_NSIMEMORYREPORTER
  31. private:
  32. virtual ~VariableLengthPrefixSet();
  33. static const uint32_t MAX_BUFFER_SIZE = 64 * 1024;
  34. static const uint32_t PREFIXSET_VERSION_MAGIC = 1;
  35. bool BinarySearch(const nsACString& aFullHash,
  36. const nsACString& aPrefixes,
  37. uint32_t aPrefixSize);
  38. uint32_t CalculatePreallocateSize();
  39. nsresult WritePrefixes(nsIOutputStream* out);
  40. nsresult LoadPrefixes(nsIInputStream* in);
  41. // Lock to prevent races between the url-classifier thread (which does most
  42. // of the operations) and the main thread (which does memory reporting).
  43. // It should be held for all operations between Init() and destruction that
  44. // touch this class's data members.
  45. mozilla::Mutex mLock;
  46. RefPtr<nsUrlClassifierPrefixSet> mFixedPrefixSet;
  47. mozilla::safebrowsing::PrefixStringMap mVLPrefixSet;
  48. nsCString mMemoryReportPath;
  49. };
  50. } // namespace safebrowsing
  51. } // namespace mozilla
  52. #endif