-
Notifications
You must be signed in to change notification settings - Fork 822
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolves brave/brave-browser#33662
- Loading branch information
Showing
22 changed files
with
1,376 additions
and
352 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* Copyright (c) 2023 The Brave Authors. All rights reserved. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at https://mozilla.org/MPL/2.0/. */ | ||
|
||
#include "brave/components/brave_wallet/browser/zcash/zcash_grpc_utils.h" | ||
|
||
#include <utility> | ||
|
||
#include "base/big_endian.h" | ||
#include "base/functional/callback.h" | ||
#include "base/logging.h" | ||
|
||
namespace brave_wallet { | ||
|
||
namespace { | ||
constexpr size_t kMaxMessageSizeBytes = 10000; | ||
} // namespace | ||
|
||
std::string GetPrefixedProtobuf(const std::string& serialized_proto) { | ||
char compression = 0; // 0 means no compression | ||
char buff[4]; // big-endian 4 bytes of message size | ||
base::WriteBigEndian<uint32_t>(buff, serialized_proto.size()); | ||
std::string result; | ||
result.append(&compression, 1); | ||
result.append(buff, 4); | ||
result.append(serialized_proto); | ||
return result; | ||
} | ||
|
||
// Resolves serialized protobuf from length-prefixed string | ||
absl::optional<std::string> ResolveSerializedMessage( | ||
const std::string& grpc_response_body) { | ||
if (grpc_response_body.size() < 5) { | ||
return absl::nullopt; | ||
} | ||
if (grpc_response_body[0] != 0) { | ||
// Compression is not supported yet | ||
return absl::nullopt; | ||
} | ||
uint32_t size = 0; | ||
base::ReadBigEndian( | ||
reinterpret_cast<const uint8_t*>(&(grpc_response_body[1])), &size); | ||
|
||
if (grpc_response_body.size() != size + 5) { | ||
return absl::nullopt; | ||
} | ||
|
||
return grpc_response_body.substr(5); | ||
} | ||
|
||
GRrpcMessageStreamHandler::GRrpcMessageStreamHandler() = default; | ||
GRrpcMessageStreamHandler::~GRrpcMessageStreamHandler() = default; | ||
|
||
void GRrpcMessageStreamHandler::OnDataReceived(std::string_view string_piece, | ||
base::OnceClosure resume) { | ||
data_.append(string_piece); | ||
|
||
bool should_resume = false; | ||
while (!should_resume) { | ||
if (!data_to_read_ && data_.size() > 5) { | ||
if (data_[0] != 0) { | ||
OnComplete(false); | ||
return; | ||
} | ||
uint32_t size = 0; | ||
base::ReadBigEndian(reinterpret_cast<const uint8_t*>(&(data_[1])), &size); | ||
data_to_read_ = size; | ||
if (*data_to_read_ > kMaxMessageSizeBytes) { | ||
// Too large message | ||
OnComplete(false); | ||
return; | ||
} | ||
} | ||
|
||
if (data_to_read_ && data_.size() > *data_to_read_) { | ||
if (!ProcessMessage(data_.substr(0, 5 + *data_to_read_))) { | ||
OnComplete(true); | ||
return; | ||
} | ||
|
||
data_ = data_.substr(5 + *data_to_read_); | ||
data_to_read_.reset(); | ||
} else { | ||
should_resume = true; | ||
} | ||
} | ||
std::move(resume).Run(); | ||
} | ||
|
||
void GRrpcMessageStreamHandler::OnRetry(base::OnceClosure start_retry) { | ||
if (retry_counter_++ < 5) { | ||
std::move(start_retry).Run(); | ||
} | ||
} | ||
|
||
} // namespace brave_wallet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* Copyright (c) 2023 The Brave Authors. All rights reserved. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at https://mozilla.org/MPL/2.0/. */ | ||
|
||
#ifndef BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_ZCASH_ZCASH_GRPC_UTILS_H_ | ||
#define BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_ZCASH_ZCASH_GRPC_UTILS_H_ | ||
|
||
#include <string> | ||
#include <string_view> | ||
|
||
#include "services/network/public/cpp/simple_url_loader_stream_consumer.h" | ||
#include "third_party/abseil-cpp/absl/types/optional.h" | ||
|
||
namespace brave_wallet { | ||
|
||
// Resolves serialized protobuf from length-prefixed string | ||
absl::optional<std::string> ResolveSerializedMessage( | ||
const std::string& grpc_response_body); | ||
|
||
// Prefixes provided serialized protobuf with compression byte and 4 bytes of | ||
// message size. See | ||
// https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md | ||
std::string GetPrefixedProtobuf(const std::string& serialized_proto); | ||
|
||
// Handles a stream of GRPC objects | ||
class GRrpcMessageStreamHandler | ||
: public network::SimpleURLLoaderStreamConsumer { | ||
public: | ||
GRrpcMessageStreamHandler(); | ||
~GRrpcMessageStreamHandler() override; | ||
|
||
void OnDataReceived(std::string_view string_piece, | ||
base::OnceClosure resume) override; | ||
void OnRetry(base::OnceClosure start_retry) override; | ||
|
||
private: | ||
virtual bool ProcessMessage(const std::string& message) = 0; | ||
|
||
absl::optional<size_t> data_to_read_; | ||
std::string data_; | ||
size_t retry_counter_ = 0; | ||
}; | ||
|
||
} // namespace brave_wallet | ||
|
||
#endif // BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_ZCASH_ZCASH_GRPC_UTILS_H_ |
Oops, something went wrong.