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 NavigationURLLoader
;
23 class ResourceRequestBody
;
24 class SiteInstanceImpl
;
25 struct NavigationRequestInfo
;
28 // A UI thread object that owns a navigation request until it commits. It
29 // ensures the UI thread can start a navigation request in the
30 // ResourceDispatcherHost (that lives on the IO thread).
31 // TODO(clamy): Describe the interactions between the UI and IO thread during
32 // the navigation following its refactoring.
33 class CONTENT_EXPORT NavigationRequest
: public NavigationURLLoaderDelegate
{
35 // Keeps track of the various stages of a NavigationRequest.
36 enum NavigationState
{
40 // Waiting for a BeginNavigation IPC from the renderer in a
41 // browser-initiated navigation. If there is no live renderer when the
42 // request is created, this stage is skipped.
43 WAITING_FOR_RENDERER_RESPONSE
,
45 // The request was sent to the IO thread.
48 // The response started on the IO thread and is ready to be committed. This
49 // is one of the two final states for the request.
52 // The request failed on the IO thread and an error page should be
53 // displayed. This is one of the two final states for the request.
57 // Creates a request for a browser-intiated navigation.
58 static scoped_ptr
<NavigationRequest
> CreateBrowserInitiated(
59 FrameTreeNode
* frame_tree_node
,
61 const Referrer
& dest_referrer
,
62 const FrameNavigationEntry
& frame_entry
,
63 const NavigationEntryImpl
& entry
,
64 FrameMsg_Navigate_Type::Value navigation_type
,
65 bool is_same_document_history_load
,
66 base::TimeTicks navigation_start
,
67 NavigationControllerImpl
* controller
);
69 // Creates a request for a renderer-intiated navigation.
70 // Note: |body| is sent to the IO thread when calling BeginNavigation, and
71 // should no longer be manipulated afterwards on the UI thread.
72 static scoped_ptr
<NavigationRequest
> CreateRendererInitiated(
73 FrameTreeNode
* frame_tree_node
,
74 const CommonNavigationParams
& common_params
,
75 const BeginNavigationParams
& begin_params
,
76 scoped_refptr
<ResourceRequestBody
> body
,
77 int current_history_list_offset
,
78 int current_history_list_length
);
80 ~NavigationRequest() override
;
82 // Called on the UI thread by the Navigator to start the navigation. Returns
83 // whether a request was made on the IO thread.
84 // TODO(clamy): see if ResourceRequestBody could be un-refcounted to avoid
85 // threading subtleties.
86 bool BeginNavigation();
88 const CommonNavigationParams
& common_params() const { return common_params_
; }
90 const BeginNavigationParams
& begin_params() const { return begin_params_
; }
92 const RequestNavigationParams
& request_params() const {
93 return request_params_
;
96 NavigationURLLoader
* loader_for_testing() const { return loader_
.get(); }
98 NavigationState
state() const { return state_
; }
100 SiteInstanceImpl
* source_site_instance() const {
101 return source_site_instance_
.get();
104 SiteInstanceImpl
* dest_site_instance() const {
105 return dest_site_instance_
.get();
108 NavigationEntryImpl::RestoreType
restore_type() const {
109 return restore_type_
;
112 bool is_view_source() const { return is_view_source_
; };
114 int bindings() const { return bindings_
; };
116 bool browser_initiated() const { return browser_initiated_
; }
118 void SetWaitingForRendererResponse() {
119 DCHECK(state_
== NOT_STARTED
);
120 state_
= WAITING_FOR_RENDERER_RESPONSE
;
124 NavigationRequest(FrameTreeNode
* frame_tree_node
,
125 const CommonNavigationParams
& common_params
,
126 const BeginNavigationParams
& begin_params
,
127 const RequestNavigationParams
& request_params
,
128 scoped_refptr
<ResourceRequestBody
> body
,
129 bool browser_initiated
,
130 const FrameNavigationEntry
* frame_navigation_entry
,
131 const NavigationEntryImpl
* navitation_entry
);
133 // NavigationURLLoaderDelegate implementation.
134 void OnRequestRedirected(
135 const net::RedirectInfo
& redirect_info
,
136 const scoped_refptr
<ResourceResponse
>& response
) override
;
137 void OnResponseStarted(const scoped_refptr
<ResourceResponse
>& response
,
138 scoped_ptr
<StreamHandle
> body
) override
;
139 void OnRequestFailed(bool has_stale_copy_in_cache
, int net_error
) override
;
140 void OnRequestStarted(base::TimeTicks timestamp
) override
;
142 FrameTreeNode
* frame_tree_node_
;
144 // Initialized on creation of the NavigationRequest. Sent to the renderer when
145 // the navigation is ready to commit.
146 // Note: When the navigation is ready to commit, the url in |common_params|
147 // will be set to the final navigation url, obtained after following all
149 CommonNavigationParams common_params_
;
150 const BeginNavigationParams begin_params_
;
151 const RequestNavigationParams request_params_
;
152 const bool browser_initiated_
;
154 NavigationState state_
;
157 // The parameters to send to the IO thread. |loader_| takes ownership of
158 // |info_| after calling BeginNavigation.
159 scoped_ptr
<NavigationRequestInfo
> info_
;
161 scoped_ptr
<NavigationURLLoader
> loader_
;
163 // These next items are used in browser-initiated navigations to store
164 // information from the NavigationEntryImpl that is required after request
166 scoped_refptr
<SiteInstanceImpl
> source_site_instance_
;
167 scoped_refptr
<SiteInstanceImpl
> dest_site_instance_
;
168 NavigationEntryImpl::RestoreType restore_type_
;
169 bool is_view_source_
;
172 DISALLOW_COPY_AND_ASSIGN(NavigationRequest
);
175 } // namespace content
177 #endif // CONTENT_BROWSER_FRAME_HOST_NAVIGATION_REQUEST_H_