-
Notifications
You must be signed in to change notification settings - Fork 925
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce JSON P3A pings that duplicate the legacy pings.
- Loading branch information
Showing
9 changed files
with
267 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* Copyright 2021 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 http://mozilla.org/MPL/2.0/. */ | ||
|
||
#include "brave/components/p3a/brave_p3a_new_uploader.h" | ||
|
||
#include <utility> | ||
|
||
#include "base/base64.h" | ||
#include "net/base/load_flags.h" | ||
#include "services/network/public/cpp/resource_request.h" | ||
#include "services/network/public/cpp/shared_url_loader_factory.h" | ||
#include "services/network/public/cpp/simple_url_loader.h" | ||
#include "services/network/public/mojom/url_response_head.mojom.h" | ||
|
||
namespace brave { | ||
|
||
namespace { | ||
|
||
// TODO(iefremov): Provide more details for the traffic annotation. | ||
net::NetworkTrafficAnnotationTag GetNetworkTrafficAnnotation( | ||
base::StringPiece upload_type) { | ||
if (upload_type == "p3a") { | ||
return net::DefineNetworkTrafficAnnotation("metrics_report_uma", R"( | ||
semantics { | ||
sender: "Brave Privacy-Preserving Product Analytics Uploader" | ||
description: | ||
"Report of anonymized usage statistics. For more info, see " | ||
"https://brave.com/P3A" | ||
trigger: | ||
"Reports are automatically generated on startup and at intervals " | ||
"while Brave is running." | ||
data: | ||
"A protocol buffer with anonymized and encrypted usage data." | ||
destination: WEBSITE | ||
} | ||
policy { | ||
cookies_allowed: NO | ||
setting: | ||
"Users can enable or disable it in brave://settings/privacy" | ||
policy_exception_justification: | ||
"Not implemented." | ||
})"); | ||
} | ||
DCHECK_EQ(upload_type, "p2a"); | ||
return net::DefineNetworkTrafficAnnotation("metrics_report_uma", R"( | ||
semantics { | ||
sender: "Brave Privacy-Preserving Ad Analytics Uploader" | ||
description: | ||
"Report of anonymized usage statistics. For more info, see " | ||
"https://brave.com/P2A" | ||
trigger: | ||
"Reports are automatically generated on startup and at intervals " | ||
"while Brave is running." | ||
data: | ||
"A protocol buffer with anonymized and encrypted usage data." | ||
destination: WEBSITE | ||
} | ||
policy { | ||
cookies_allowed: NO | ||
setting: | ||
"Users can enable or disable it by enabling or disabling Brave rewards | ||
or ads in brave://rewards" | ||
policy_exception_justification: | ||
"Not implemented." | ||
})"); | ||
} | ||
|
||
} // namespace | ||
|
||
BraveP3ANewUploader::BraveP3ANewUploader( | ||
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory, | ||
const GURL& p3a_endpoint, | ||
const GURL& p2a_endpoint) | ||
: url_loader_factory_(url_loader_factory), | ||
p3a_endpoint_(p3a_endpoint), | ||
p2a_endpoint_(p2a_endpoint) {} | ||
|
||
BraveP3ANewUploader::~BraveP3ANewUploader() = default; | ||
|
||
void BraveP3ANewUploader::UploadLog(const std::string& compressed_log_data, | ||
const std::string& upload_type) { | ||
auto resource_request = std::make_unique<network::ResourceRequest>(); | ||
if (upload_type == "p2a") { | ||
resource_request->url = p2a_endpoint_; | ||
resource_request->headers.SetHeader("X-Brave-P2A", "?1"); | ||
} else if (upload_type == "p3a") { | ||
resource_request->url = p3a_endpoint_; | ||
resource_request->headers.SetHeader("X-Brave-P3A", "?1"); | ||
} else { | ||
NOTREACHED(); | ||
} | ||
|
||
resource_request->credentials_mode = network::mojom::CredentialsMode::kOmit; | ||
resource_request->method = "POST"; | ||
|
||
url_loader_ = network::SimpleURLLoader::Create( | ||
std::move(resource_request), | ||
GetNetworkTrafficAnnotation(upload_type)); | ||
url_loader_->AttachStringForUpload(compressed_log_data, "application/json"); | ||
|
||
url_loader_->DownloadToStringOfUnboundedSizeUntilCrashAndDie( | ||
url_loader_factory_.get(), | ||
base::BindOnce(&BraveP3ANewUploader::OnUploadComplete, | ||
base::Unretained(this))); | ||
} | ||
|
||
void BraveP3ANewUploader::OnUploadComplete( | ||
std::unique_ptr<std::string> response_body) { | ||
url_loader_.reset(); | ||
} | ||
|
||
} // namespace brave |
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,53 @@ | ||
/* Copyright 2021 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 http://mozilla.org/MPL/2.0/. */ | ||
|
||
#ifndef BRAVE_COMPONENTS_P3A_BRAVE_P3A_NEW_UPLOADER_H_ | ||
#define BRAVE_COMPONENTS_P3A_BRAVE_P3A_NEW_UPLOADER_H_ | ||
|
||
#include <memory> | ||
#include <string> | ||
|
||
#include "base/memory/ref_counted.h" | ||
#include "url/gurl.h" | ||
|
||
namespace network { | ||
class SharedURLLoaderFactory; | ||
class SimpleURLLoader; | ||
} // namespace network | ||
|
||
namespace brave { | ||
|
||
// This will replace the "normal" uploader when the server-side is ready. | ||
// The difference is only in endpoint, mime and lack of base64 encoding. | ||
// Also this one doesn't report the upload status back (since this one is for | ||
// testing the new endpoint). | ||
class BraveP3ANewUploader { | ||
public: | ||
BraveP3ANewUploader( | ||
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory, | ||
const GURL& p3a_endpoint, | ||
const GURL& p2a_endpoint); | ||
|
||
BraveP3ANewUploader(const BraveP3ANewUploader&) = delete; | ||
BraveP3ANewUploader& operator=(const BraveP3ANewUploader&) = delete; | ||
|
||
~BraveP3ANewUploader(); | ||
|
||
// From metrics::MetricsLogUploader | ||
void UploadLog(const std::string& compressed_log_data, | ||
const std::string& upload_type); | ||
|
||
void OnUploadComplete(std::unique_ptr<std::string> response_body); | ||
|
||
private: | ||
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_; | ||
const GURL p3a_endpoint_; | ||
const GURL p2a_endpoint_; | ||
std::unique_ptr<network::SimpleURLLoader> url_loader_; | ||
}; | ||
|
||
} // namespace brave | ||
|
||
#endif // BRAVE_COMPONENTS_P3A_BRAVE_P3A_NEW_UPLOADER_H_ |
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
Oops, something went wrong.