• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/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"
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 resource_type,ResourceContext * context,int render_process_id,int render_view_id,int render_frame_id,bool is_async)26 void ResourceRequestInfo::AllocateForTesting(net::URLRequest* request,
27                                              ResourceType resource_type,
28                                              ResourceContext* context,
29                                              int render_process_id,
30                                              int render_view_id,
31                                              int render_frame_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           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           ui::PAGE_TRANSITION_LINK,          // transition_type
46           false,                             // should_replace_current_entry
47           false,                             // is_download
48           false,                             // is_stream
49           true,                              // allow_download
50           false,                             // has_user_gesture
51           false,                             // enable load timing
52           blink::WebReferrerPolicyDefault,   // referrer_policy
53           blink::WebPageVisibilityStateVisible,  // visibility_state
54           context,                           // context
55           base::WeakPtr<ResourceMessageFilter>(),  // filter
56           is_async);                         // is_async
57   info->AssociateWithRequest(request);
58 }
59 
60 // static
GetRenderFrameForRequest(const net::URLRequest * request,int * render_process_id,int * render_frame_id)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));
67   if (!user_data)
68     return false;
69   *render_process_id = user_data->render_process_id();
70   *render_frame_id = user_data->render_frame_id();
71   return true;
72 }
73 
74 // ----------------------------------------------------------------------------
75 // ResourceRequestInfoImpl
76 
77 // static
ForRequest(net::URLRequest * request)78 ResourceRequestInfoImpl* ResourceRequestInfoImpl::ForRequest(
79     net::URLRequest* request) {
80   return static_cast<ResourceRequestInfoImpl*>(request->GetUserData(NULL));
81 }
82 
83 // static
ForRequest(const net::URLRequest * request)84 const ResourceRequestInfoImpl* ResourceRequestInfoImpl::ForRequest(
85     const net::URLRequest* request) {
86   return ForRequest(const_cast<net::URLRequest*>(request));
87 }
88 
ResourceRequestInfoImpl(int process_type,int child_id,int route_id,int origin_pid,int request_id,int render_frame_id,bool is_main_frame,bool parent_is_main_frame,int parent_render_frame_id,ResourceType resource_type,ui::PageTransition transition_type,bool should_replace_current_entry,bool is_download,bool is_stream,bool allow_download,bool has_user_gesture,bool enable_load_timing,blink::WebReferrerPolicy referrer_policy,blink::WebPageVisibilityState visibility_state,ResourceContext * context,base::WeakPtr<ResourceMessageFilter> filter,bool is_async)89 ResourceRequestInfoImpl::ResourceRequestInfoImpl(
90     int process_type,
91     int child_id,
92     int route_id,
93     int origin_pid,
94     int request_id,
95     int render_frame_id,
96     bool is_main_frame,
97     bool parent_is_main_frame,
98     int parent_render_frame_id,
99     ResourceType resource_type,
100     ui::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     bool enable_load_timing,
107     blink::WebReferrerPolicy referrer_policy,
108     blink::WebPageVisibilityState visibility_state,
109     ResourceContext* context,
110     base::WeakPtr<ResourceMessageFilter> filter,
111     bool is_async)
112     : cross_site_handler_(NULL),
113       detachable_handler_(NULL),
114       process_type_(process_type),
115       child_id_(child_id),
116       route_id_(route_id),
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),
132       memory_cost_(0),
133       referrer_policy_(referrer_policy),
134       visibility_state_(visibility_state),
135       context_(context),
136       filter_(filter),
137       is_async_(is_async) {
138 }
139 
~ResourceRequestInfoImpl()140 ResourceRequestInfoImpl::~ResourceRequestInfoImpl() {
141 }
142 
GetContext() const143 ResourceContext* ResourceRequestInfoImpl::GetContext() const {
144   return context_;
145 }
146 
GetChildID() const147 int ResourceRequestInfoImpl::GetChildID() const {
148   return child_id_;
149 }
150 
GetRouteID() const151 int ResourceRequestInfoImpl::GetRouteID() const {
152   return route_id_;
153 }
154 
GetOriginPID() const155 int ResourceRequestInfoImpl::GetOriginPID() const {
156   return origin_pid_;
157 }
158 
GetRequestID() const159 int ResourceRequestInfoImpl::GetRequestID() const {
160   return request_id_;
161 }
162 
GetRenderFrameID() const163 int ResourceRequestInfoImpl::GetRenderFrameID() const {
164   return render_frame_id_;
165 }
166 
IsMainFrame() const167 bool ResourceRequestInfoImpl::IsMainFrame() const {
168   return is_main_frame_;
169 }
170 
ParentIsMainFrame() const171 bool ResourceRequestInfoImpl::ParentIsMainFrame() const {
172   return parent_is_main_frame_;
173 }
174 
GetParentRenderFrameID() const175 int ResourceRequestInfoImpl::GetParentRenderFrameID() const {
176   return parent_render_frame_id_;
177 }
178 
GetResourceType() const179 ResourceType ResourceRequestInfoImpl::GetResourceType() const {
180   return resource_type_;
181 }
182 
GetProcessType() const183 int ResourceRequestInfoImpl::GetProcessType() const {
184   return process_type_;
185 }
186 
GetReferrerPolicy() const187 blink::WebReferrerPolicy ResourceRequestInfoImpl::GetReferrerPolicy() const {
188   return referrer_policy_;
189 }
190 
191 blink::WebPageVisibilityState
GetVisibilityState() const192 ResourceRequestInfoImpl::GetVisibilityState() const {
193   return visibility_state_;
194 }
195 
GetPageTransition() const196 ui::PageTransition ResourceRequestInfoImpl::GetPageTransition() const {
197   return transition_type_;
198 }
199 
HasUserGesture() const200 bool ResourceRequestInfoImpl::HasUserGesture() const {
201   return has_user_gesture_;
202 }
203 
WasIgnoredByHandler() const204 bool ResourceRequestInfoImpl::WasIgnoredByHandler() const {
205   return was_ignored_by_handler_;
206 }
207 
GetAssociatedRenderFrame(int * render_process_id,int * render_frame_id) const208 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_;
214   } else {
215     *render_process_id = child_id_;
216     *render_frame_id = render_frame_id_;
217   }
218   return true;
219 }
220 
IsAsync() const221 bool ResourceRequestInfoImpl::IsAsync() const {
222   return is_async_;
223 }
224 
IsDownload() const225 bool ResourceRequestInfoImpl::IsDownload() const {
226   return is_download_;
227 }
228 
AssociateWithRequest(net::URLRequest * request)229 void ResourceRequestInfoImpl::AssociateWithRequest(net::URLRequest* request) {
230   request->SetUserData(NULL, this);
231   int render_process_id;
232   int render_frame_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));
237   }
238 }
239 
GetGlobalRequestID() const240 GlobalRequestID ResourceRequestInfoImpl::GetGlobalRequestID() const {
241   return GlobalRequestID(child_id_, request_id_);
242 }
243 
GetGlobalRoutingID() const244 GlobalRoutingID ResourceRequestInfoImpl::GetGlobalRoutingID() const {
245   return GlobalRoutingID(child_id_, route_id_);
246 }
247 
UpdateForTransfer(int child_id,int route_id,int origin_pid,int request_id,int parent_render_frame_id,base::WeakPtr<ResourceMessageFilter> filter)248 void ResourceRequestInfoImpl::UpdateForTransfer(
249     int child_id,
250     int route_id,
251     int origin_pid,
252     int request_id,
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;
260   filter_ = filter;
261 }
262 
263 }  // namespace content
264