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 ResourceRequestInfoImpl
* info
=
34 new ResourceRequestInfoImpl(
35 PROCESS_TYPE_RENDERER
, // process_type
36 render_process_id
, // child_id
37 render_view_id
, // route_id
40 render_frame_id
, // render_frame_id
41 resource_type
== RESOURCE_TYPE_MAIN_FRAME
, // is_main_frame
42 false, // parent_is_main_frame
43 0, // parent_render_frame_id
44 resource_type
, // resource_type
45 PAGE_TRANSITION_LINK
, // transition_type
46 false, // should_replace_current_entry
49 true, // allow_download
50 false, // has_user_gesture
51 false, // enable load timing
52 blink::WebReferrerPolicyDefault
, // referrer_policy
53 blink::WebPageVisibilityStateVisible
, // visibility_state
55 base::WeakPtr
<ResourceMessageFilter
>(), // filter
56 is_async
); // is_async
57 info
->AssociateWithRequest(request
);
61 bool ResourceRequestInfo::GetRenderFrameForRequest(
62 const net::URLRequest
* request
,
63 int* render_process_id
,
64 int* render_frame_id
) {
65 URLRequestUserData
* user_data
= static_cast<URLRequestUserData
*>(
66 request
->GetUserData(URLRequestUserData::kUserDataKey
));
69 *render_process_id
= user_data
->render_process_id();
70 *render_frame_id
= user_data
->render_frame_id();
74 // ----------------------------------------------------------------------------
75 // ResourceRequestInfoImpl
78 ResourceRequestInfoImpl
* ResourceRequestInfoImpl::ForRequest(
79 net::URLRequest
* request
) {
80 return static_cast<ResourceRequestInfoImpl
*>(request
->GetUserData(NULL
));
84 const ResourceRequestInfoImpl
* ResourceRequestInfoImpl::ForRequest(
85 const net::URLRequest
* request
) {
86 return ForRequest(const_cast<net::URLRequest
*>(request
));
89 ResourceRequestInfoImpl::ResourceRequestInfoImpl(
97 bool parent_is_main_frame
,
98 int parent_render_frame_id
,
99 ResourceType resource_type
,
100 PageTransition transition_type
,
101 bool should_replace_current_entry
,
105 bool has_user_gesture
,
106 bool enable_load_timing
,
107 blink::WebReferrerPolicy referrer_policy
,
108 blink::WebPageVisibilityState visibility_state
,
109 ResourceContext
* context
,
110 base::WeakPtr
<ResourceMessageFilter
> filter
,
112 : cross_site_handler_(NULL
),
113 detachable_handler_(NULL
),
114 process_type_(process_type
),
117 origin_pid_(origin_pid
),
118 request_id_(request_id
),
119 render_frame_id_(render_frame_id
),
120 is_main_frame_(is_main_frame
),
121 parent_is_main_frame_(parent_is_main_frame
),
122 parent_render_frame_id_(parent_render_frame_id
),
123 should_replace_current_entry_(should_replace_current_entry
),
124 is_download_(is_download
),
125 is_stream_(is_stream
),
126 allow_download_(allow_download
),
127 has_user_gesture_(has_user_gesture
),
128 enable_load_timing_(enable_load_timing
),
129 was_ignored_by_handler_(false),
130 resource_type_(resource_type
),
131 transition_type_(transition_type
),
133 referrer_policy_(referrer_policy
),
134 visibility_state_(visibility_state
),
137 is_async_(is_async
) {
140 ResourceRequestInfoImpl::~ResourceRequestInfoImpl() {
143 ResourceContext
* ResourceRequestInfoImpl::GetContext() const {
147 int ResourceRequestInfoImpl::GetChildID() const {
151 int ResourceRequestInfoImpl::GetRouteID() const {
155 int ResourceRequestInfoImpl::GetOriginPID() const {
159 int ResourceRequestInfoImpl::GetRequestID() const {
163 int ResourceRequestInfoImpl::GetRenderFrameID() const {
164 return render_frame_id_
;
167 bool ResourceRequestInfoImpl::IsMainFrame() const {
168 return is_main_frame_
;
171 bool ResourceRequestInfoImpl::ParentIsMainFrame() const {
172 return parent_is_main_frame_
;
175 int ResourceRequestInfoImpl::GetParentRenderFrameID() const {
176 return parent_render_frame_id_
;
179 ResourceType
ResourceRequestInfoImpl::GetResourceType() const {
180 return resource_type_
;
183 int ResourceRequestInfoImpl::GetProcessType() const {
184 return process_type_
;
187 blink::WebReferrerPolicy
ResourceRequestInfoImpl::GetReferrerPolicy() const {
188 return referrer_policy_
;
191 blink::WebPageVisibilityState
192 ResourceRequestInfoImpl::GetVisibilityState() const {
193 return visibility_state_
;
196 PageTransition
ResourceRequestInfoImpl::GetPageTransition() const {
197 return transition_type_
;
200 bool ResourceRequestInfoImpl::HasUserGesture() const {
201 return has_user_gesture_
;
204 bool ResourceRequestInfoImpl::WasIgnoredByHandler() const {
205 return was_ignored_by_handler_
;
208 bool ResourceRequestInfoImpl::GetAssociatedRenderFrame(
209 int* render_process_id
,
210 int* render_frame_id
) const {
211 if (process_type_
== PROCESS_TYPE_PLUGIN
) {
212 *render_process_id
= origin_pid_
;
213 *render_frame_id
= render_frame_id_
;
215 *render_process_id
= child_id_
;
216 *render_frame_id
= render_frame_id_
;
221 bool ResourceRequestInfoImpl::IsAsync() const {
225 bool ResourceRequestInfoImpl::IsDownload() const {
229 void ResourceRequestInfoImpl::AssociateWithRequest(net::URLRequest
* request
) {
230 request
->SetUserData(NULL
, this);
231 int render_process_id
;
233 if (GetAssociatedRenderFrame(&render_process_id
, &render_frame_id
)) {
234 request
->SetUserData(
235 URLRequestUserData::kUserDataKey
,
236 new URLRequestUserData(render_process_id
, render_frame_id
));
240 GlobalRequestID
ResourceRequestInfoImpl::GetGlobalRequestID() const {
241 return GlobalRequestID(child_id_
, request_id_
);
244 GlobalRoutingID
ResourceRequestInfoImpl::GetGlobalRoutingID() const {
245 return GlobalRoutingID(child_id_
, route_id_
);
248 void ResourceRequestInfoImpl::UpdateForTransfer(
253 int parent_render_frame_id
,
254 base::WeakPtr
<ResourceMessageFilter
> filter
) {
255 child_id_
= child_id
;
256 route_id_
= route_id
;
257 origin_pid_
= origin_pid
;
258 request_id_
= request_id
;
259 parent_render_frame_id_
= parent_render_frame_id
;
263 } // namespace content