1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_ANDROID_CUSTOMTABS_DETACHED_RESOURCE_REQUEST_H_
#define CHROME_BROWSER_ANDROID_CUSTOMTABS_DETACHED_RESOURCE_REQUEST_H_
#include <memory>
#include <string>
#include <vector>
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/time/time.h"
#include "net/url_request/referrer_policy.h"
#include "services/network/public/mojom/url_response_head.mojom-forward.h"
#include "url/gurl.h"
namespace content {
class BrowserContext;
} // namespace content
namespace net {
struct RedirectInfo;
} // namespace net
namespace network {
class SimpleURLLoader;
} // namespace network
namespace customtabs {
// Detached resource request, that is a resource request initiated from the
// browser process, and which starts detached from any client.
//
// It is intended to provide "detached" request capabilities from the browser
// process, that is like <a ping> or <link rel="prefetch">.
//
// This is a UI thread class.
class DetachedResourceRequest {
public:
static constexpr int kMaxResponseSize = 500 * 1024;
// The motivation of the resource request, used for histograms reporting.
// GENERATED_JAVA_ENUM_PACKAGE: org.chromium.chrome.browser.customtabs
// GENERATED_JAVA_CLASS_NAME_OVERRIDE: DetachedResourceRequestMotivation
enum class Motivation { kParallelRequest, kResourcePrefetch };
using OnResultCallback = base::OnceCallback<void(int net_error)>;
DetachedResourceRequest(const DetachedResourceRequest&) = delete;
DetachedResourceRequest& operator=(const DetachedResourceRequest&) = delete;
~DetachedResourceRequest();
// Creates a detached request to a `url`, with a given initiating URL,
// `site_for_referrer`. Called on the UI thread.
// Optional `cb` to get notified about the fetch result.
static void CreateAndStart(content::BrowserContext* browser_context,
const GURL& url,
const GURL& site_for_referrer,
net::ReferrerPolicy referer_policy,
Motivation motivation,
const std::string& package_name,
OnResultCallback cb = base::DoNothing());
private:
DetachedResourceRequest(const GURL& url,
const GURL& site_for_referrer,
net::ReferrerPolicy referer_policy,
Motivation motivation,
const std::string& package_name,
OnResultCallback cb);
static void Start(std::unique_ptr<DetachedResourceRequest> request,
content::BrowserContext* browser_context);
void OnRedirectCallback(const GURL& url_before_redirect,
const net::RedirectInfo& redirect_info,
const network::mojom::URLResponseHead& response_head,
std::vector<std::string>* to_be_removed_headers);
void OnResponseCallback(std::unique_ptr<std::string> response_body);
const GURL url_;
const GURL site_for_referrer_;
base::TimeTicks start_time_;
Motivation motivation_;
OnResultCallback cb_;
std::unique_ptr<network::SimpleURLLoader> url_loader_;
int redirects_;
bool is_from_aga_;
};
} // namespace customtabs
#endif // CHROME_BROWSER_ANDROID_CUSTOMTABS_DETACHED_RESOURCE_REQUEST_H_
|