Bug 1274112 - Part 2: Use protobuf API to parse v4 update response. r=francois

MozReview-Commit-ID: 3sjR3Feq4ua
This commit is contained in:
Henry Chang
2016-08-04 18:10:12 +08:00
parent 9708a23b7f
commit f8d8d27fdb
4 changed files with 243 additions and 10 deletions

View File

@@ -8,12 +8,13 @@
#include "HashStore.h"
#include "nsICryptoHMAC.h"
#include "safebrowsing.pb.h"
namespace mozilla {
namespace safebrowsing {
/**
* Some helpers for parsing the safe
* Helpers to parse the "shavar", "digest256" and "simple" list formats.
*/
class ProtocolParser {
public:
@@ -23,7 +24,7 @@ public:
};
ProtocolParser();
~ProtocolParser();
virtual ~ProtocolParser();
nsresult Status() const { return mUpdateStatus; }
@@ -32,7 +33,11 @@ public:
void SetCurrentTable(const nsACString& aTable);
nsresult Begin();
nsresult AppendStream(const nsACString& aData);
virtual nsresult AppendStream(const nsACString& aData);
// Notify that the inbound data is ready for parsing if progressive
// parsing is not supported, for example in V4.
virtual void End();
// Forget the table updates that were created by this pass. It
// becomes the caller's responsibility to free them. This is shitty.
@@ -73,6 +78,11 @@ private:
void CleanupUpdates();
protected:
nsCString mPending;
nsresult mUpdateStatus;
private:
enum ParserState {
PROTOCOL_STATE_CONTROL,
PROTOCOL_STATE_CHUNK
@@ -100,9 +110,6 @@ private:
nsCOMPtr<nsICryptoHash> mCryptoHash;
nsresult mUpdateStatus;
nsCString mPending;
uint32_t mUpdateWait;
bool mResetRequested;
@@ -113,6 +120,29 @@ private:
TableUpdate *mTableUpdate;
};
// Helpers to parse the "proto" list format.
class ProtocolParserProtobuf final : public ProtocolParser {
public:
typedef FetchThreatListUpdatesResponse_ListUpdateResponse ListUpdateResponse;
typedef google::protobuf::RepeatedPtrField<ThreatEntrySet> ThreatEntrySetList;
public:
ProtocolParserProtobuf();
virtual nsresult AppendStream(const nsACString& aData) override;
virtual void End() override;
private:
virtual ~ProtocolParserProtobuf();
// For parsing update info.
nsresult ProcessOneResponse(const ListUpdateResponse& aResponse);
nsresult ProcessAdditionOrRemoval(const ThreatEntrySetList& aUpdate,
bool aIsAddition);
nsresult ProcessRawAddition(const ThreatEntrySet& aAddition);
nsresult ProcessRawRemoval(const ThreatEntrySet& aRemoval);
};
} // namespace safebrowsing
} // namespace mozilla