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/browser/worker_host/worker_service_impl.h"
10 #include "content/common/net/url_request_user_data.h"
11 #include "content/public/browser/global_request_id.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(
27 net::URLRequest
* request
,
28 ResourceType::Type resource_type
,
29 ResourceContext
* context
,
30 int render_process_id
,
34 ResourceRequestInfoImpl
* info
=
35 new ResourceRequestInfoImpl(
36 PROCESS_TYPE_RENDERER
, // process_type
37 render_process_id
, // child_id
38 render_view_id
, // route_id
41 render_frame_id
, // render_frame_id
42 resource_type
== ResourceType::MAIN_FRAME
, // is_main_frame
43 false, // parent_is_main_frame
44 0, // parent_render_frame_id
45 resource_type
, // resource_type
46 PAGE_TRANSITION_LINK
, // transition_type
47 false, // should_replace_current_entry
50 true, // allow_download
51 false, // has_user_gesture
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::Type resource_type
,
100 PageTransition transition_type
,
101 bool should_replace_current_entry
,
105 bool has_user_gesture
,
106 blink::WebReferrerPolicy referrer_policy
,
107 blink::WebPageVisibilityState visibility_state
,
108 ResourceContext
* context
,
109 base::WeakPtr
<ResourceMessageFilter
> filter
,
111 : cross_site_handler_(NULL
),
112 detachable_handler_(NULL
),
113 process_type_(process_type
),
116 origin_pid_(origin_pid
),
117 request_id_(request_id
),
118 render_frame_id_(render_frame_id
),
119 is_main_frame_(is_main_frame
),
120 parent_is_main_frame_(parent_is_main_frame
),
121 parent_render_frame_id_(parent_render_frame_id
),
122 should_replace_current_entry_(should_replace_current_entry
),
123 is_download_(is_download
),
124 is_stream_(is_stream
),
125 allow_download_(allow_download
),
126 has_user_gesture_(has_user_gesture
),
127 was_ignored_by_handler_(false),
128 resource_type_(resource_type
),
129 transition_type_(transition_type
),
131 referrer_policy_(referrer_policy
),
132 visibility_state_(visibility_state
),
135 is_async_(is_async
) {
138 ResourceRequestInfoImpl::~ResourceRequestInfoImpl() {
141 ResourceContext
* ResourceRequestInfoImpl::GetContext() const {
145 int ResourceRequestInfoImpl::GetChildID() const {
149 int ResourceRequestInfoImpl::GetRouteID() const {
153 int ResourceRequestInfoImpl::GetOriginPID() const {
157 int ResourceRequestInfoImpl::GetRequestID() const {
161 int ResourceRequestInfoImpl::GetRenderFrameID() const {
162 return render_frame_id_
;
165 bool ResourceRequestInfoImpl::IsMainFrame() const {
166 return is_main_frame_
;
169 bool ResourceRequestInfoImpl::ParentIsMainFrame() const {
170 return parent_is_main_frame_
;
173 int ResourceRequestInfoImpl::GetParentRenderFrameID() const {
174 return parent_render_frame_id_
;
177 ResourceType::Type
ResourceRequestInfoImpl::GetResourceType() const {
178 return resource_type_
;
181 int ResourceRequestInfoImpl::GetProcessType() const {
182 return process_type_
;
185 blink::WebReferrerPolicy
ResourceRequestInfoImpl::GetReferrerPolicy() const {
186 return referrer_policy_
;
189 blink::WebPageVisibilityState
190 ResourceRequestInfoImpl::GetVisibilityState() const {
191 return visibility_state_
;
194 PageTransition
ResourceRequestInfoImpl::GetPageTransition() const {
195 return transition_type_
;
198 bool ResourceRequestInfoImpl::HasUserGesture() const {
199 return has_user_gesture_
;
202 bool ResourceRequestInfoImpl::WasIgnoredByHandler() const {
203 return was_ignored_by_handler_
;
206 bool ResourceRequestInfoImpl::GetAssociatedRenderFrame(
207 int* render_process_id
,
208 int* render_frame_id
) const {
209 // If the request is from the worker process, find a content that owns the
211 if (process_type_
== PROCESS_TYPE_WORKER
) {
212 // Need to display some related UI for this network request - pick an
213 // arbitrary parent to do so.
214 if (!WorkerServiceImpl::GetInstance()->GetRendererForWorker(
215 child_id_
, render_process_id
, render_frame_id
)) {
216 *render_process_id
= -1;
217 *render_frame_id
= -1;
220 } else if (process_type_
== PROCESS_TYPE_PLUGIN
) {
221 *render_process_id
= origin_pid_
;
222 *render_frame_id
= render_frame_id_
;
224 *render_process_id
= child_id_
;
225 *render_frame_id
= render_frame_id_
;
230 bool ResourceRequestInfoImpl::IsAsync() const {
234 bool ResourceRequestInfoImpl::IsDownload() const {
238 void ResourceRequestInfoImpl::AssociateWithRequest(net::URLRequest
* request
) {
239 request
->SetUserData(NULL
, this);
240 int render_process_id
;
242 if (GetAssociatedRenderFrame(&render_process_id
, &render_frame_id
)) {
243 request
->SetUserData(
244 URLRequestUserData::kUserDataKey
,
245 new URLRequestUserData(render_process_id
, render_frame_id
));
249 GlobalRequestID
ResourceRequestInfoImpl::GetGlobalRequestID() const {
250 return GlobalRequestID(child_id_
, request_id_
);
253 GlobalRoutingID
ResourceRequestInfoImpl::GetGlobalRoutingID() const {
254 return GlobalRoutingID(child_id_
, route_id_
);
257 void ResourceRequestInfoImpl::UpdateForTransfer(
262 int parent_render_frame_id
,
263 base::WeakPtr
<ResourceMessageFilter
> filter
) {
264 child_id_
= child_id
;
265 route_id_
= route_id
;
266 origin_pid_
= origin_pid
;
267 request_id_
= request_id
;
268 parent_render_frame_id_
= parent_render_frame_id
;
272 } // namespace content