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.
5 #include "content/browser/loader/resource_request_info_impl.h"
7 #include "content/browser/loader/global_routing_id.h"
8 #include "content/browser/loader/resource_message_filter.h"
9 #include "content/common/net/url_request_user_data.h"
10 #include "content/public/browser/global_request_id.h"
11 #include "content/public/common/process_type.h"
12 #include "net/url_request/url_request.h"
16 // ----------------------------------------------------------------------------
17 // ResourceRequestInfo
20 const ResourceRequestInfo
* ResourceRequestInfo::ForRequest(
21 const net::URLRequest
* request
) {
22 return ResourceRequestInfoImpl::ForRequest(request
);
26 void ResourceRequestInfo::AllocateForTesting(net::URLRequest
* request
,
27 ResourceType resource_type
,
28 ResourceContext
* context
,
29 int render_process_id
,
33 bool parent_is_main_frame
,
36 // Make sure both |is_main_frame| and |parent_is_main_frame| aren't set at the
38 DCHECK(!(is_main_frame
&& parent_is_main_frame
));
40 // Make sure RESOURCE_TYPE_MAIN_FRAME is declared as being fetched as part of
42 DCHECK(resource_type
!= RESOURCE_TYPE_MAIN_FRAME
|| is_main_frame
);
44 ResourceRequestInfoImpl
* info
=
45 new ResourceRequestInfoImpl(
46 PROCESS_TYPE_RENDERER
, // process_type
47 render_process_id
, // child_id
48 render_view_id
, // route_id
51 render_frame_id
, // render_frame_id
52 is_main_frame
, // is_main_frame
53 parent_is_main_frame
, // parent_is_main_frame
54 0, // parent_render_frame_id
55 resource_type
, // resource_type
56 ui::PAGE_TRANSITION_LINK
, // transition_type
57 false, // should_replace_current_entry
60 allow_download
, // allow_download
61 false, // has_user_gesture
62 false, // enable load timing
63 false, // enable upload progress
64 false, // do_not_prompt_for_login
65 blink::WebReferrerPolicyDefault
, // referrer_policy
66 blink::WebPageVisibilityStateVisible
, // visibility_state
68 base::WeakPtr
<ResourceMessageFilter
>(), // filter
69 is_async
); // is_async
70 info
->AssociateWithRequest(request
);
74 bool ResourceRequestInfo::GetRenderFrameForRequest(
75 const net::URLRequest
* request
,
76 int* render_process_id
,
77 int* render_frame_id
) {
78 URLRequestUserData
* user_data
= static_cast<URLRequestUserData
*>(
79 request
->GetUserData(URLRequestUserData::kUserDataKey
));
82 *render_process_id
= user_data
->render_process_id();
83 *render_frame_id
= user_data
->render_frame_id();
87 // ----------------------------------------------------------------------------
88 // ResourceRequestInfoImpl
91 ResourceRequestInfoImpl
* ResourceRequestInfoImpl::ForRequest(
92 net::URLRequest
* request
) {
93 return static_cast<ResourceRequestInfoImpl
*>(request
->GetUserData(NULL
));
97 const ResourceRequestInfoImpl
* ResourceRequestInfoImpl::ForRequest(
98 const net::URLRequest
* request
) {
99 return ForRequest(const_cast<net::URLRequest
*>(request
));
102 ResourceRequestInfoImpl::ResourceRequestInfoImpl(
110 bool parent_is_main_frame
,
111 int parent_render_frame_id
,
112 ResourceType resource_type
,
113 ui::PageTransition transition_type
,
114 bool should_replace_current_entry
,
118 bool has_user_gesture
,
119 bool enable_load_timing
,
120 bool enable_upload_progress
,
121 bool do_not_prompt_for_login
,
122 blink::WebReferrerPolicy referrer_policy
,
123 blink::WebPageVisibilityState visibility_state
,
124 ResourceContext
* context
,
125 base::WeakPtr
<ResourceMessageFilter
> filter
,
127 : cross_site_handler_(NULL
),
128 detachable_handler_(NULL
),
129 process_type_(process_type
),
132 origin_pid_(origin_pid
),
133 request_id_(request_id
),
134 render_frame_id_(render_frame_id
),
135 is_main_frame_(is_main_frame
),
136 parent_is_main_frame_(parent_is_main_frame
),
137 parent_render_frame_id_(parent_render_frame_id
),
138 should_replace_current_entry_(should_replace_current_entry
),
139 is_download_(is_download
),
140 is_stream_(is_stream
),
141 allow_download_(allow_download
),
142 has_user_gesture_(has_user_gesture
),
143 enable_load_timing_(enable_load_timing
),
144 enable_upload_progress_(enable_upload_progress
),
145 do_not_prompt_for_login_(do_not_prompt_for_login
),
146 was_ignored_by_handler_(false),
147 counted_as_in_flight_request_(false),
148 resource_type_(resource_type
),
149 transition_type_(transition_type
),
151 referrer_policy_(referrer_policy
),
152 visibility_state_(visibility_state
),
155 is_async_(is_async
) {
158 ResourceRequestInfoImpl::~ResourceRequestInfoImpl() {
161 ResourceContext
* ResourceRequestInfoImpl::GetContext() const {
165 int ResourceRequestInfoImpl::GetChildID() const {
169 int ResourceRequestInfoImpl::GetRouteID() const {
173 int ResourceRequestInfoImpl::GetOriginPID() const {
177 int ResourceRequestInfoImpl::GetRequestID() const {
181 int ResourceRequestInfoImpl::GetRenderFrameID() const {
182 return render_frame_id_
;
185 bool ResourceRequestInfoImpl::IsMainFrame() const {
186 return is_main_frame_
;
189 bool ResourceRequestInfoImpl::ParentIsMainFrame() const {
190 return parent_is_main_frame_
;
193 int ResourceRequestInfoImpl::GetParentRenderFrameID() const {
194 return parent_render_frame_id_
;
197 ResourceType
ResourceRequestInfoImpl::GetResourceType() const {
198 return resource_type_
;
201 int ResourceRequestInfoImpl::GetProcessType() const {
202 return process_type_
;
205 blink::WebReferrerPolicy
ResourceRequestInfoImpl::GetReferrerPolicy() const {
206 return referrer_policy_
;
209 blink::WebPageVisibilityState
210 ResourceRequestInfoImpl::GetVisibilityState() const {
211 return visibility_state_
;
214 ui::PageTransition
ResourceRequestInfoImpl::GetPageTransition() const {
215 return transition_type_
;
218 bool ResourceRequestInfoImpl::HasUserGesture() const {
219 return has_user_gesture_
;
222 bool ResourceRequestInfoImpl::WasIgnoredByHandler() const {
223 return was_ignored_by_handler_
;
226 bool ResourceRequestInfoImpl::GetAssociatedRenderFrame(
227 int* render_process_id
,
228 int* render_frame_id
) const {
229 if (process_type_
== PROCESS_TYPE_PLUGIN
) {
230 *render_process_id
= origin_pid_
;
231 *render_frame_id
= render_frame_id_
;
233 *render_process_id
= child_id_
;
234 *render_frame_id
= render_frame_id_
;
239 bool ResourceRequestInfoImpl::IsAsync() const {
243 bool ResourceRequestInfoImpl::IsDownload() const {
247 void ResourceRequestInfoImpl::AssociateWithRequest(net::URLRequest
* request
) {
248 request
->SetUserData(NULL
, this);
249 int render_process_id
;
251 if (GetAssociatedRenderFrame(&render_process_id
, &render_frame_id
)) {
252 request
->SetUserData(
253 URLRequestUserData::kUserDataKey
,
254 new URLRequestUserData(render_process_id
, render_frame_id
));
258 GlobalRequestID
ResourceRequestInfoImpl::GetGlobalRequestID() const {
259 return GlobalRequestID(child_id_
, request_id_
);
262 GlobalRoutingID
ResourceRequestInfoImpl::GetGlobalRoutingID() const {
263 return GlobalRoutingID(child_id_
, route_id_
);
266 void ResourceRequestInfoImpl::UpdateForTransfer(
271 int parent_render_frame_id
,
272 base::WeakPtr
<ResourceMessageFilter
> filter
) {
273 child_id_
= child_id
;
274 route_id_
= route_id
;
275 origin_pid_
= origin_pid
;
276 request_id_
= request_id
;
277 parent_render_frame_id_
= parent_render_frame_id
;
281 } // namespace content