1、定义网络说明。
net::NetworkTrafficAnnotationTag traffic_annotation =
net::DefineNetworkTrafficAnnotation("webstore_data_fetcher", R"(
semantics {
sender: "Webstore Data Fetcher"
description:
"Fetches metadata about an extension from the Chrome Web Store "
"using the item JSON API. This API is being deprecated in favor of "
"a newer item snippet API. "
trigger:
"The user or another program triggers some action where Chrome "
"will show metadata about an extension. This includes extension "
"installation flows, triggering an install for a disabled "
"extension, and an extension being added to Chrome through "
"third-party sideloading. It also happens when a kiosk app account "
"whose metadata (app icon, name, required platform version) is not "
"cached locally is detected in device local accounts list. The "
"account can be set either by device policy or through extensions "
"web UI, by the device owner (user that was initially added to the "
"device; implies non managed device). The latter case is "
"deprecated and not supported on newer Chrome OS boards."
data:
"The extension id and referrer url. The referrer chain is also "
"included if the user has not opted out of SafeBrowsing."
destination: GOOGLE_OWNED_SERVICE
}
policy {
cookies_allowed: NO
setting:
"This feature cannot be disabled in settings. It will only be "
"triggered if the user uses extensions."
policy_exception_justification: "Not implemented."
})");
2、定义http head。
auto resource_request = std::make_unique<network::ResourceRequest>();
resource_request->url = GURL("http://127.0.0.1:8000");
resource_request->headers.SetHeader("liwei", "nihao");
resource_request->method = "POST";
3、新建loader。
loader_ = network::SimpleURLLoader::Create(
std::move(resource_request), traffic_annotation);
4、获取loader_factory 。
network::mojom::URLLoaderFactory* loader_factory =
g_browser_process->system_network_context_manager()
->GetURLLoaderFactory();
5、发送请求并传递数据返回处理函数。
loader_->DownloadToStringOfUnboundedSizeUntilCrashAndDie(
loader_factory, base::BindOnce(&XtRequest::OnDownloaded,
base::Unretained(this)));
6、https返回处理函数。
void XtRequest::OnDownloaded(
std::unique_ptr<std::string> response_body) {
int response_code = -1;
if (terms_of_service_loader_->ResponseInfo() &&
terms_of_service_loader_->ResponseInfo()->headers) {
response_code =
terms_of_service_loader_->ResponseInfo()->headers->response_code();
}
// TODO(b/200782732): Add metrics to record error code.
if (response_code == net::HTTP_OK) {
DVLOG(0) << "XtRequest::run************response_body" << *response_body << "\r\n";
}}