1 // Copyright 2014 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 #ifndef CONTENT_BROWSER_FRAME_HOST_NAVIGATION_REQUEST_H_
6 #define CONTENT_BROWSER_FRAME_HOST_NAVIGATION_REQUEST_H_
8 #include "base/basictypes.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "content/browser/frame_host/navigation_entry_impl.h"
12 #include "content/browser/loader/navigation_url_loader_delegate.h"
13 #include "content/common/content_export.h"
14 #include "content/common/frame_message_enums.h"
15 #include "content/common/navigation_params.h"
19 class FrameNavigationEntry
;
21 class NavigationControllerImpl
;
22 class NavigationHandleImpl
;
23 class NavigationURLLoader
;
24 class NavigatorDelegate
;
25 class ResourceRequestBody
;
26 class SiteInstanceImpl
;
27 struct NavigationRequestInfo
;
30 // A UI thread object that owns a navigation request until it commits. It
31 // ensures the UI thread can start a navigation request in the
32 // ResourceDispatcherHost (that lives on the IO thread).
33 // TODO(clamy): Describe the interactions between the UI and IO thread during
34 // the navigation following its refactoring.
35 class CONTENT_EXPORT NavigationRequest
: public NavigationURLLoaderDelegate
{
37 // Keeps track of the various stages of a NavigationRequest.
38 enum NavigationState
{
42 // Waiting for a BeginNavigation IPC from the renderer in a
43 // browser-initiated navigation. If there is no live renderer when the
44 // request is created, this stage is skipped.
45 WAITING_FOR_RENDERER_RESPONSE
,
47 // The request was sent to the IO thread.
50 // The response started on the IO thread and is ready to be committed. This
51 // is one of the two final states for the request.
54 // The request failed on the IO thread and an error page should be
55 // displayed. This is one of the two final states for the request.
59 // Creates a request for a browser-intiated navigation.
60 static scoped_ptr
<NavigationRequest
> CreateBrowserInitiated(
61 FrameTreeNode
* frame_tree_node
,
63 const Referrer
& dest_referrer
,
64 const FrameNavigationEntry
& frame_entry
,
65 const NavigationEntryImpl
& entry
,
66 FrameMsg_Navigate_Type::Value navigation_type
,
67 bool is_same_document_history_load
,
68 base::TimeTicks navigation_start
,
69 NavigationControllerImpl
* controller
);
71 // Creates a request for a renderer-intiated navigation.
72 // Note: |body| is sent to the IO thread when calling BeginNavigation, and
73 // should no longer be manipulated afterwards on the UI thread.
74 static scoped_ptr
<NavigationRequest
> CreateRendererInitiated(
75 FrameTreeNode
* frame_tree_node
,
76 const CommonNavigationParams
& common_params
,
77 const BeginNavigationParams
& begin_params
,
78 scoped_refptr
<ResourceRequestBody
> body
,
79 int current_history_list_offset
,
80 int current_history_list_length
);
82 ~NavigationRequest() override
;
84 // Called on the UI thread by the Navigator to start the navigation. Returns
85 // whether a request was made on the IO thread.
86 // TODO(clamy): see if ResourceRequestBody could be un-refcounted to avoid
87 // threading subtleties.
88 bool BeginNavigation();
90 const CommonNavigationParams
& common_params() const { return common_params_
; }
92 const BeginNavigationParams
& begin_params() const { return begin_params_
; }
94 const RequestNavigationParams
& request_params() const {
95 return request_params_
;
98 NavigationURLLoader
* loader_for_testing() const { return loader_
.get(); }
100 NavigationState
state() const { return state_
; }
102 SiteInstanceImpl
* source_site_instance() const {
103 return source_site_instance_
.get();
106 SiteInstanceImpl
* dest_site_instance() const {
107 return dest_site_instance_
.get();
110 NavigationEntryImpl::RestoreType
restore_type() const {
111 return restore_type_
;
114 bool is_view_source() const { return is_view_source_
; };
116 int bindings() const { return bindings_
; };
118 bool browser_initiated() const { return browser_initiated_
; }
120 void SetWaitingForRendererResponse() {
121 DCHECK(state_
== NOT_STARTED
);
122 state_
= WAITING_FOR_RENDERER_RESPONSE
;
125 NavigationHandleImpl
* navigation_handle() const {
126 return navigation_handle_
.get();
129 // Creates a NavigationHandle. This should be called after any previous
130 // NavigationRequest for the FrameTreeNode has been destroyed.
131 void CreateNavigationHandle(NavigatorDelegate
* delegate
);
133 // Transfers the ownership of the NavigationHandle to |render_frame_host|.
134 // This should be called when the navigation is ready to commit, because the
135 // NavigationHandle outlives the NavigationRequest. The NavigationHandle's
136 // lifetime is the entire navigation, while the NavigationRequest is
137 // destroyed when a navigation is ready for commit.
138 void TransferNavigationHandleOwnership(
139 RenderFrameHostImpl
* render_frame_host
);
142 NavigationRequest(FrameTreeNode
* frame_tree_node
,
143 const CommonNavigationParams
& common_params
,
144 const BeginNavigationParams
& begin_params
,
145 const RequestNavigationParams
& request_params
,
146 scoped_refptr
<ResourceRequestBody
> body
,
147 bool browser_initiated
,
148 const FrameNavigationEntry
* frame_navigation_entry
,
149 const NavigationEntryImpl
* navitation_entry
);
151 // NavigationURLLoaderDelegate implementation.
152 void OnRequestRedirected(
153 const net::RedirectInfo
& redirect_info
,
154 const scoped_refptr
<ResourceResponse
>& response
) override
;
155 void OnResponseStarted(const scoped_refptr
<ResourceResponse
>& response
,
156 scoped_ptr
<StreamHandle
> body
) override
;
157 void OnRequestFailed(bool has_stale_copy_in_cache
, int net_error
) override
;
158 void OnRequestStarted(base::TimeTicks timestamp
) override
;
160 FrameTreeNode
* frame_tree_node_
;
162 // Initialized on creation of the NavigationRequest. Sent to the renderer when
163 // the navigation is ready to commit.
164 // Note: When the navigation is ready to commit, the url in |common_params|
165 // will be set to the final navigation url, obtained after following all
167 // Note: |common_params_| and |begin_params_| are not const as they can be
168 // modified during redirects.
169 CommonNavigationParams common_params_
;
170 BeginNavigationParams begin_params_
;
171 const RequestNavigationParams request_params_
;
172 const bool browser_initiated_
;
174 NavigationState state_
;
177 // The parameters to send to the IO thread. |loader_| takes ownership of
178 // |info_| after calling BeginNavigation.
179 scoped_ptr
<NavigationRequestInfo
> info_
;
181 scoped_ptr
<NavigationURLLoader
> loader_
;
183 // These next items are used in browser-initiated navigations to store
184 // information from the NavigationEntryImpl that is required after request
186 scoped_refptr
<SiteInstanceImpl
> source_site_instance_
;
187 scoped_refptr
<SiteInstanceImpl
> dest_site_instance_
;
188 NavigationEntryImpl::RestoreType restore_type_
;
189 bool is_view_source_
;
192 scoped_ptr
<NavigationHandleImpl
> navigation_handle_
;
194 DISALLOW_COPY_AND_ASSIGN(NavigationRequest
);
197 } // namespace content
199 #endif // CONTENT_BROWSER_FRAME_HOST_NAVIGATION_REQUEST_H_