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 #include "content/browser/loader/resource_request_info_impl.h"
6
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"
13
14 namespace content {
15
16 // ----------------------------------------------------------------------------
17 // ResourceRequestInfo
18
19 // static
ForRequest(const net::URLRequest * request)20 const ResourceRequestInfo* ResourceRequestInfo::ForRequest(
21 const net::URLRequest* request) {
22 return ResourceRequestInfoImpl::ForRequest(request);
23 }
24
25 // static
AllocateForTesting(net::URLRequest * request,ResourceType::Type resource_type,ResourceContext * context,int render_process_id,int render_view_id,bool is_async)26 void ResourceRequestInfo::AllocateForTesting(
27 net::URLRequest* request,
28 ResourceType::Type resource_type,
29 ResourceContext* context,
30 int render_process_id,
31 int render_view_id,
32 bool is_async) {
33 ResourceRequestInfoImpl* info =
34 new ResourceRequestInfoImpl(
35 PROCESS_TYPE_RENDERER, // process_type
36 render_process_id, // child_id
37 render_view_id, // route_id
38 0, // origin_pid
39 0, // request_id
40 MSG_ROUTING_NONE, // render_frame_id
41 resource_type == ResourceType::MAIN_FRAME, // is_main_frame
42 0, // frame_id
43 false, // parent_is_main_frame
44 0, // parent_frame_id
45 resource_type, // resource_type
46 PAGE_TRANSITION_LINK, // transition_type
47 false, // should_replace_current_entry
48 false, // is_download
49 false, // is_stream
50 true, // allow_download
51 false, // has_user_gesture
52 blink::WebReferrerPolicyDefault, // referrer_policy
53 context, // context
54 base::WeakPtr<ResourceMessageFilter>(), // filter
55 is_async); // is_async
56 info->AssociateWithRequest(request);
57 }
58
59 // static
GetRenderViewForRequest(const net::URLRequest * request,int * render_process_id,int * render_view_id)60 bool ResourceRequestInfo::GetRenderViewForRequest(
61 const net::URLRequest* request,
62 int* render_process_id,
63 int* render_view_id) {
64 URLRequestUserData* user_data = static_cast<URLRequestUserData*>(
65 request->GetUserData(URLRequestUserData::kUserDataKey));
66 if (!user_data)
67 return false;
68 *render_process_id = user_data->render_process_id();
69 *render_view_id = user_data->render_view_id();
70 return true;
71 }
72
73 // ----------------------------------------------------------------------------
74 // ResourceRequestInfoImpl
75
76 // static
ForRequest(net::URLRequest * request)77 ResourceRequestInfoImpl* ResourceRequestInfoImpl::ForRequest(
78 net::URLRequest* request) {
79 return static_cast<ResourceRequestInfoImpl*>(request->GetUserData(NULL));
80 }
81
82 // static
ForRequest(const net::URLRequest * request)83 const ResourceRequestInfoImpl* ResourceRequestInfoImpl::ForRequest(
84 const net::URLRequest* request) {
85 return ForRequest(const_cast<net::URLRequest*>(request));
86 }
87
ResourceRequestInfoImpl(int process_type,int child_id,int route_id,int origin_pid,int request_id,int render_frame_id,bool is_main_frame,int64 frame_id,bool parent_is_main_frame,int64 parent_frame_id,ResourceType::Type resource_type,PageTransition transition_type,bool should_replace_current_entry,bool is_download,bool is_stream,bool allow_download,bool has_user_gesture,blink::WebReferrerPolicy referrer_policy,ResourceContext * context,base::WeakPtr<ResourceMessageFilter> filter,bool is_async)88 ResourceRequestInfoImpl::ResourceRequestInfoImpl(
89 int process_type,
90 int child_id,
91 int route_id,
92 int origin_pid,
93 int request_id,
94 int render_frame_id,
95 bool is_main_frame,
96 int64 frame_id,
97 bool parent_is_main_frame,
98 int64 parent_frame_id,
99 ResourceType::Type resource_type,
100 PageTransition transition_type,
101 bool should_replace_current_entry,
102 bool is_download,
103 bool is_stream,
104 bool allow_download,
105 bool has_user_gesture,
106 blink::WebReferrerPolicy referrer_policy,
107 ResourceContext* context,
108 base::WeakPtr<ResourceMessageFilter> filter,
109 bool is_async)
110 : cross_site_handler_(NULL),
111 detachable_handler_(NULL),
112 process_type_(process_type),
113 child_id_(child_id),
114 route_id_(route_id),
115 origin_pid_(origin_pid),
116 request_id_(request_id),
117 render_frame_id_(render_frame_id),
118 is_main_frame_(is_main_frame),
119 frame_id_(frame_id),
120 parent_is_main_frame_(parent_is_main_frame),
121 parent_frame_id_(parent_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),
130 memory_cost_(0),
131 referrer_policy_(referrer_policy),
132 context_(context),
133 filter_(filter),
134 is_async_(is_async) {
135 }
136
~ResourceRequestInfoImpl()137 ResourceRequestInfoImpl::~ResourceRequestInfoImpl() {
138 }
139
GetContext() const140 ResourceContext* ResourceRequestInfoImpl::GetContext() const {
141 return context_;
142 }
143
GetChildID() const144 int ResourceRequestInfoImpl::GetChildID() const {
145 return child_id_;
146 }
147
GetRouteID() const148 int ResourceRequestInfoImpl::GetRouteID() const {
149 return route_id_;
150 }
151
GetOriginPID() const152 int ResourceRequestInfoImpl::GetOriginPID() const {
153 return origin_pid_;
154 }
155
GetRequestID() const156 int ResourceRequestInfoImpl::GetRequestID() const {
157 return request_id_;
158 }
159
GetRenderFrameID() const160 int ResourceRequestInfoImpl::GetRenderFrameID() const {
161 return render_frame_id_;
162 }
163
IsMainFrame() const164 bool ResourceRequestInfoImpl::IsMainFrame() const {
165 return is_main_frame_;
166 }
167
GetFrameID() const168 int64 ResourceRequestInfoImpl::GetFrameID() const {
169 return frame_id_;
170 }
171
ParentIsMainFrame() const172 bool ResourceRequestInfoImpl::ParentIsMainFrame() const {
173 return parent_is_main_frame_;
174 }
175
GetParentFrameID() const176 int64 ResourceRequestInfoImpl::GetParentFrameID() const {
177 return parent_frame_id_;
178 }
179
GetResourceType() const180 ResourceType::Type ResourceRequestInfoImpl::GetResourceType() const {
181 return resource_type_;
182 }
183
GetReferrerPolicy() const184 blink::WebReferrerPolicy ResourceRequestInfoImpl::GetReferrerPolicy() const {
185 return referrer_policy_;
186 }
187
GetPageTransition() const188 PageTransition ResourceRequestInfoImpl::GetPageTransition() const {
189 return transition_type_;
190 }
191
HasUserGesture() const192 bool ResourceRequestInfoImpl::HasUserGesture() const {
193 return has_user_gesture_;
194 }
195
WasIgnoredByHandler() const196 bool ResourceRequestInfoImpl::WasIgnoredByHandler() const {
197 return was_ignored_by_handler_;
198 }
199
GetAssociatedRenderView(int * render_process_id,int * render_view_id) const200 bool ResourceRequestInfoImpl::GetAssociatedRenderView(
201 int* render_process_id,
202 int* render_view_id) const {
203 // If the request is from the worker process, find a content that owns the
204 // worker.
205 if (process_type_ == PROCESS_TYPE_WORKER) {
206 // Need to display some related UI for this network request - pick an
207 // arbitrary parent to do so.
208 if (!WorkerServiceImpl::GetInstance()->GetRendererForWorker(
209 child_id_, render_process_id, render_view_id)) {
210 *render_process_id = -1;
211 *render_view_id = -1;
212 return false;
213 }
214 } else if (process_type_ == PROCESS_TYPE_PLUGIN) {
215 *render_process_id = origin_pid_;
216 *render_view_id = route_id_;
217 } else {
218 *render_process_id = child_id_;
219 *render_view_id = route_id_;
220 }
221 return true;
222 }
223
IsAsync() const224 bool ResourceRequestInfoImpl::IsAsync() const {
225 return is_async_;
226 }
227
IsDownload() const228 bool ResourceRequestInfoImpl::IsDownload() const {
229 return is_download_;
230 }
231
AssociateWithRequest(net::URLRequest * request)232 void ResourceRequestInfoImpl::AssociateWithRequest(net::URLRequest* request) {
233 request->SetUserData(NULL, this);
234 int render_process_id;
235 int render_view_id;
236 if (GetAssociatedRenderView(&render_process_id, &render_view_id)) {
237 request->SetUserData(
238 URLRequestUserData::kUserDataKey,
239 new URLRequestUserData(render_process_id, render_view_id));
240 }
241 }
242
GetGlobalRequestID() const243 GlobalRequestID ResourceRequestInfoImpl::GetGlobalRequestID() const {
244 return GlobalRequestID(child_id_, request_id_);
245 }
246
GetGlobalRoutingID() const247 GlobalRoutingID ResourceRequestInfoImpl::GetGlobalRoutingID() const {
248 return GlobalRoutingID(child_id_, route_id_);
249 }
250
UpdateForTransfer(int child_id,int route_id,int origin_pid,int request_id,int64 frame_id,int64 parent_frame_id,base::WeakPtr<ResourceMessageFilter> filter)251 void ResourceRequestInfoImpl::UpdateForTransfer(
252 int child_id,
253 int route_id,
254 int origin_pid,
255 int request_id,
256 int64 frame_id,
257 int64 parent_frame_id,
258 base::WeakPtr<ResourceMessageFilter> filter) {
259 child_id_ = child_id;
260 route_id_ = route_id;
261 origin_pid_ = origin_pid;
262 request_id_ = request_id;
263 frame_id_ = frame_id;
264 filter_ = filter;
265 }
266
267 } // namespace content
268