1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CONTENT_PUBLIC_BROWSER_RESOURCE_REQUEST_INFO_H_
6#define CONTENT_PUBLIC_BROWSER_RESOURCE_REQUEST_INFO_H_
7
8#include "base/callback_forward.h"
9#include "base/strings/string_piece.h"
10#include "content/common/content_export.h"
11#include "content/public/browser/global_request_id.h"
12#include "content/public/browser/navigation_ui_data.h"
13#include "content/public/common/previews_state.h"
14#include "content/public/common/resource_intercept_policy.h"
15#include "content/public/common/resource_type.h"
16#include "services/network/public/mojom/referrer_policy.mojom-forward.h"
17#include "third_party/blink/public/platform/resource_request_blocked_reason.h"
18#include "ui/base/page_transition_types.h"
19
20namespace net {
21class URLRequest;
22}
23
24namespace content {
25class ResourceContext;
26class WebContents;
27
28// Each URLRequest allocated by the ResourceDispatcherHost has a
29// ResourceRequestInfo instance associated with it.
30class ResourceRequestInfo {
31 public:
32 // Returns the ResourceRequestInfo associated with the given URLRequest.
33 CONTENT_EXPORT static ResourceRequestInfo* ForRequest(
34 const net::URLRequest* request);
35
36 // Allocates a new, dummy ResourceRequestInfo and associates it with the
37 // given URLRequest.
38 //
39 // The RenderView routing ID must correspond to the RenderView of the
40 // RenderFrame, both of which share the same RenderProcess. This may be a
41 // different RenderView than the WebContents' main RenderView. If the
42 // download is not associated with a frame, the IDs can be all -1.
43 //
44 // NOTE: Add more parameters if you need to initialize other fields.
45 CONTENT_EXPORT static void AllocateForTesting(
46 net::URLRequest* request,
47 ResourceType resource_type,
48 ResourceContext* context,
49 int render_process_id,
50 int render_view_id,
51 int render_frame_id,
52 bool is_main_frame,
53 ResourceInterceptPolicy resource_intercept_policy,
54 bool is_async,
55 PreviewsState previews_state,
56 std::unique_ptr<NavigationUIData> navigation_ui_data);
57
58 // Returns the associated RenderFrame for a given process. Returns false, if
59 // there is no associated RenderFrame. This method does not rely on the
60 // request being allocated by the ResourceDispatcherHost, but works for all
61 // URLRequests that are associated with a RenderFrame.
62 CONTENT_EXPORT static bool GetRenderFrameForRequest(
63 const net::URLRequest* request,
64 int* render_process_id,
65 int* render_frame_id);
66
67 // Returns true if the request originated from a Service Worker.
68 CONTENT_EXPORT static bool OriginatedFromServiceWorker(
69 const net::URLRequest* request);
70
71 // A callback that returns a pointer to a WebContents. The callback can
72 // always be used, but it may return nullptr: if the info used to
73 // instantiate the callback can no longer be used to return a WebContents,
74 // nullptr will be returned instead.
75 // The callback should only run on the UI thread and it should always be
76 // non-null.
77 using WebContentsGetter = base::Callback<WebContents*(void)>;
78
79 // A callback that returns a frame tree node id . The callback can always
80 // be used, but it may return -1 if no id is found. The callback should only
81 // run on the UI thread.
82 using FrameTreeNodeIdGetter = base::Callback<int(void)>;
83
84 // Returns a callback that returns a pointer to the WebContents this request
85 // is associated with, or nullptr if it no longer exists or the request is
86 // not associated with a WebContents. The callback should only run on the UI
87 // thread.
88 // Note: Not all resource requests will be owned by a WebContents. For
89 // example, requests made by a ServiceWorker.
90 virtual WebContentsGetter GetWebContentsGetterForRequest() = 0;
91
92 // Returns a callback that returns an int with the frame tree node id
93 // associated with this request, or -1 if it no longer exists. This
94 // callback should only be run on the UI thread.
95 // Note: Not all resource requests will be associated with a frame. For
96 // example, requests made by a ServiceWorker.
97 virtual FrameTreeNodeIdGetter GetFrameTreeNodeIdGetterForRequest() = 0;
98
99 // Returns the associated ResourceContext.
100 virtual ResourceContext* GetContext() = 0;
101
102 // The child process unique ID of the requestor.
103 // To get a WebContents, use GetWebContentsGetterForRequest instead.
104 virtual int GetChildID() = 0;
105
106 // The IPC route identifier for this request (this identifies the RenderView
107 // or like-thing in the renderer that the request gets routed to).
108 // To get a WebContents, use GetWebContentsGetterForRequest instead.
109 // Don't use this method for new code, as RenderViews are going away.
110 virtual int GetRouteID() = 0;
111
112 // The globally unique identifier for this request.
113 virtual GlobalRequestID GetGlobalRequestID() = 0;
114
115 // The child process unique ID of the originating process, if the request is
116 // was proxied through a renderer process on behalf of a pepper plugin
117 // process; -1 otherwise.
118 virtual int GetPluginChildID() = 0;
119
120 // Returns the FrameTreeNode ID for this frame. This ID is browser-global and
121 // uniquely identifies a frame that hosts content.
122 // Note: Returns -1 for all requests except PlzNavigate requests.
123 virtual int GetFrameTreeNodeId() = 0;
124
125 // The IPC route identifier of the RenderFrame.
126 // To get a WebContents, use GetWebContentsGetterForRequest instead.
127 // TODO(jam): once all navigation and resource requests are sent between
128 // frames and RenderView/RenderViewHost aren't involved we can remove this and
129 // just use GetRouteID above.
130 virtual int GetRenderFrameID() = 0;
131
132 // True if GetRenderFrameID() represents a main frame in the RenderView.
133 virtual bool IsMainFrame() = 0;
134
135 // Returns the associated resource type.
136 virtual ResourceType GetResourceType() = 0;
137
138 // Returns the associated referrer policy.
139 virtual network::mojom::ReferrerPolicy GetReferrerPolicy() = 0;
140
141 // Returns whether the frame that initiated this request is used for
142 // prerendering.
143 virtual bool IsPrerendering() = 0;
144
145 // Returns the associated page transition type.
146 virtual ui::PageTransition GetPageTransition() = 0;
147
148 // True if the request was initiated by a user action (like a tap to follow
149 // a link).
150 //
151 // Note that a false value does not mean the request was not initiated by a
152 // user gesture. Also note that the fact that a user gesture was active
153 // while the request was created does not imply that the user consciously
154 // wanted this request to happen nor is aware of it.
155 //
156 // DO NOT BASE SECURITY DECISIONS ON THIS FLAG!
157 virtual bool HasUserGesture() = 0;
158
159 // Returns false if there is NOT an associated render frame.
160 virtual bool GetAssociatedRenderFrame(int* render_process_id,
161 int* render_frame_id) = 0;
162
163 // Returns true if this is associated with an asynchronous request.
164 virtual bool IsAsync() = 0;
165
166 // Whether this is a download.
167 virtual bool IsDownload() = 0;
168
169 // Returns the current state of Previews.
170 virtual PreviewsState GetPreviewsState() = 0;
171
172 // PlzNavigate
173 // Only used for navigations. Returns opaque data set by the embedder on the
174 // UI thread at the beginning of navigation.
175 virtual NavigationUIData* GetNavigationUIData() = 0;
176
177 // Used to annotate requests blocked using net::ERR_BLOCKED_BY_CLIENT and
178 // net::ERR_BLOCKED_BY_RESPONSE errors, with a ResourceRequestBlockedReason.
179 virtual void SetResourceRequestBlockedReason(
180 blink::ResourceRequestBlockedReason) = 0;
181
182 // Returns the ResourceRequestBlockedReason for this request, else
183 // base::nullopt.
184 virtual base::Optional<blink::ResourceRequestBlockedReason>
185 GetResourceRequestBlockedReason() = 0;
186
187 // When the client of a request decides to cancel it, it may optionally
188 // provide an application-defined description of the canncellation reason.
189 // This method returns the custom reason. If no such reason has been provided,
190 // it returns an empty string.
191 virtual base::StringPiece GetCustomCancelReason() = 0;
192
193 protected:
194 virtual ~ResourceRequestInfo() {}
195};
196
197} // namespace content
198
199#endif // CONTENT_PUBLIC_BROWSER_RESOURCE_REQUEST_INFO_H_
200