summaryrefslogtreecommitdiffstats
path: root/chrome/browser/safe_browsing/prefix_set.h
diff options
context:
space:
mode:
authorshess@chromium.org <shess@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-03-18 20:41:06 +0000
committershess@chromium.org <shess@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-03-18 20:41:06 +0000
commit2b7dea8f59e2517df1cd8bfba3dd90ce260dafc3 (patch)
tree90ae9fe84421fc07b14a3bec97c7edf178592824 /chrome/browser/safe_browsing/prefix_set.h
parent139c51038197965b0e010a1b4496829a6828310f (diff)
downloadchromium_src-2b7dea8f59e2517df1cd8bfba3dd90ce260dafc3.zip
chromium_src-2b7dea8f59e2517df1cd8bfba3dd90ce260dafc3.tar.gz
chromium_src-2b7dea8f59e2517df1cd8bfba3dd90ce260dafc3.tar.bz2
Incremental PrefixSet builder for Safe Browsing store.
Incremental update of the database file implies incrementally building the PrefixSet. Refactor the code to that end, taking advantage of the change to get rid of the copy converting from SBAddPrefix to SBPrefix. Change SBAddPrefix ordering to prefix-primary to remove the need to re-sort things and get some bake time on the reordering. BUG=351448 Review URL: https://codereview.chromium.org/200633002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@257735 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/safe_browsing/prefix_set.h')
-rw-r--r--chrome/browser/safe_browsing/prefix_set.h43
1 files changed, 41 insertions, 2 deletions
diff --git a/chrome/browser/safe_browsing/prefix_set.h b/chrome/browser/safe_browsing/prefix_set.h
index 997d6f3..7d02473 100644
--- a/chrome/browser/safe_browsing/prefix_set.h
+++ b/chrome/browser/safe_browsing/prefix_set.h
@@ -51,6 +51,7 @@
#include <vector>
+#include "base/memory/scoped_ptr.h"
#include "chrome/browser/safe_browsing/safe_browsing_util.h"
namespace base {
@@ -61,14 +62,13 @@ namespace safe_browsing {
class PrefixSet {
public:
- explicit PrefixSet(const std::vector<SBPrefix>& sorted_prefixes);
~PrefixSet();
// |true| if |prefix| was in |prefixes| passed to the constructor.
bool Exists(SBPrefix prefix) const;
// Persist the set on disk.
- static PrefixSet* LoadFile(const base::FilePath& filter_name);
+ static scoped_ptr<PrefixSet> LoadFile(const base::FilePath& filter_name);
bool WriteFile(const base::FilePath& filter_name) const;
// Regenerate the vector of prefixes passed to the constructor into
@@ -76,6 +76,8 @@ class PrefixSet {
void GetPrefixes(std::vector<SBPrefix>* prefixes) const;
private:
+ friend class PrefixSetBuilder;
+
// Maximum number of consecutive deltas to encode before generating
// a new index entry. This helps keep the worst-case performance
// for |Exists()| under control.
@@ -86,6 +88,14 @@ class PrefixSet {
typedef std::vector<IndexPair> IndexVector;
static bool PrefixLess(const IndexPair& a, const IndexPair& b);
+ // Helper to let |PrefixSetBuilder| add a run of data. |index_prefix| is
+ // added to |index_|, with the other elements added into |deltas_|.
+ void AddRun(SBPrefix index_prefix,
+ const uint16* run_begin, const uint16* run_end);
+
+ // Used by |PrefixSetBuilder|.
+ PrefixSet();
+
// Helper for |LoadFile()|. Steals the contents of |index| and
// |deltas| using |swap()|.
PrefixSet(IndexVector* index, std::vector<uint16>* deltas);
@@ -104,6 +114,35 @@ class PrefixSet {
DISALLOW_COPY_AND_ASSIGN(PrefixSet);
};
+// Helper to incrementally build a PrefixSet from a stream of sorted prefixes.
+class PrefixSetBuilder {
+ public:
+ PrefixSetBuilder();
+ ~PrefixSetBuilder();
+
+ // Helper for unit tests and format conversion.
+ explicit PrefixSetBuilder(const std::vector<SBPrefix>& prefixes);
+
+ // Add a prefix to the set. Prefixes must arrive in ascending order.
+ // Duplicate prefixes are dropped.
+ void AddPrefix(SBPrefix prefix);
+
+ // Flush any buffered prefixes, and return the final PrefixSet instance.
+ // Any call other than the destructor is illegal after this call.
+ scoped_ptr<PrefixSet> GetPrefixSet();
+
+ private:
+ // Encode a run of deltas for |AddRun()|. The run is broken by a too-large
+ // delta, or kMaxRun, whichever comes first.
+ void EmitRun();
+
+ // Buffers prefixes until enough are avaliable to emit a run.
+ std::vector<SBPrefix> buffer_;
+
+ // The PrefixSet being built.
+ scoped_ptr<PrefixSet> prefix_set_;
+};
+
} // namespace safe_browsing
#endif // CHROME_BROWSER_SAFE_BROWSING_PREFIX_SET_H_