services\network\public\cpp\simple_url_loader.h
SimpleURLLoader(const SimpleURLLoader&) = delete;
SimpleURLLoader& operator=(const SimpleURLLoader&) = delete;
virtual ~SimpleURLLoader();
// Starts the request using |url_loader_factory|. The SimpleURLLoader will
// accumulate all downloaded data in an in-memory string of bounded size. If
// |max_body_size| is exceeded, the request will fail with
// net::ERR_INSUFFICIENT_RESOURCES. |max_body_size| must be no greater than
// |kMaxBoundedStringDownloadSize|. For anything larger, it's recommended to
// either save to a temp file, or consume the data as it is received.
//
// Whether the request succeeds or fails, the URLLoaderFactory pipe is closed,
// or the body exceeds |max_body_size|, |body_as_string_callback| will be
// invoked on completion. Deleting the SimpleURLLoader before the callback is
// invoked will result in cancelling the request, and the callback will not be
// called.
virtual void DownloadToString(mojom::URLLoaderFactory* url_loader_factory,
BodyAsStringCallback body_as_string_callback,
size_t max_body_size) = 0;
=================================================================
第一种 以下载文件为例:
注意 mojom::URLLoaderFactory* url_loader_factory 是根据profile绑定的。
#include "net/base/load_flags.h"
#include "net/http/http_status_code.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"
#include "url/gurl.h"
void OnDownloadedToFile(base::FilePath path){
int rep_code = 0;
if (url_loader_->NetError() == net::OK && url_loader_->ResponseInfo() &&
url_loader_->ResponseInfo()->headers)
rep_code = url_loader_->ResponseInfo()->headers->response_code();
LOG(ERROR) << "rep_code=" << rep_code;
if (url_loader_->NetError() == net::OK && net::HTTP_OK == rep_code) {
//
}
}
auto request = std::make_unique<network::ResourceRequest>();
request->url = GURL("www.test.com");
request->method = "GET";
request->redirect_mode = network::mojom::RedirectMode::kError;
request->referrer_policy = net::ReferrerPolicy::NEVER_CLEAR;
request->referrer = request->url.DeprecatedGetOriginAsURL();
request->site_for_cookies =
net::SiteForCookies::FromUrl(request->url.DeprecatedGetOriginAsURL());
request->load_flags = net::LOAD_IGNORE_CN_AND_DATE_CERT_ERRORS;
std::unique_ptr<network::SimpleURLLoader> url_loader_;
url_loader_ = network::SimpleURLLoader::Create(std::move(request),
MISSING_TRAFFIC_ANNOTATION);
url_loader_->SetAllowHttpErrorResults(true);
url_loader_->DownloadToFile(
profile_->GetURLLoaderFactory().get(),
base::BindOnce(&OnDownloadedToFile),
path);
==============================================================
第二种网络请求是和profile无关的:
chrome\browser\net\system_network_context_manager.h
摘自chrome\browser\ash\app_mode\web_app\web_kiosk_app_data.cc 里面的例子:
net::NetworkTrafficAnnotationTag traffic_annotation =
net::DefineNetworkTrafficAnnotation("kiosk_app_icon", R"(
semantics {
sender: "Kiosk App Icon Downloader"
description:
"The actual meta-data of the kiosk apps that is used to "
"display the application info can only be obtained upon "
"the installation of the app itself. Before this happens, "
"we are using default placeholder icon for the app. To "
"overcome this issue, the URL with the icon file is being "
"sent from the device management server. Chromium will "
"download the image located at this url."
trigger:
"User clicks on the menu button with the list of kiosk apps"
data: "None"
destination: WEBSITE
}
policy {
cookies_allowed: NO
setting: "This feature cannot be disabled in settings."
policy_exception_justification:
"No content is being uploaded or saved; this request merely "
"downloads a publicly available PNG file."
})"); //参数可选
auto resource_request = std::make_unique<network::ResourceRequest>();
resource_request->url = icon_url_;
simple_loader_ = network::SimpleURLLoader::Create(
std::move(resource_request), traffic_annotation);
simple_loader_->SetRetryOptions(
/* max_retries=*/3,
network::SimpleURLLoader::RETRY_ON_5XX |
network::SimpleURLLoader::RETRY_ON_NETWORK_CHANGE);
SystemNetworkContextManager* system_network_context_manager =
g_browser_process->system_network_context_manager();
network::mojom::URLLoaderFactory* loader_factory =
system_network_context_manager->GetURLLoaderFactory();
simple_loader_->DownloadToString(
loader_factory,
base::BindOnce(
[](base::WeakPtr<WebKioskAppData> client,
std::unique_ptr<std::string> response_body) {
if (!client)
return;
client->icon_fetcher_->OnSimpleLoaderComplete(
std::move(response_body));
},
client_),
kMaxIconFileSize);
10-07
9万+