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"
20 class NavigationControllerImpl
;
21 class NavigationURLLoader
;
22 class ResourceRequestBody
;
23 class SiteInstanceImpl
;
24 struct NavigationRequestInfo
;
27 // A UI thread object that owns a navigation request until it commits. It
28 // ensures the UI thread can start a navigation request in the
29 // ResourceDispatcherHost (that lives on the IO thread).
30 // TODO(clamy): Describe the interactions between the UI and IO thread during
31 // the navigation following its refactoring.
32 class CONTENT_EXPORT NavigationRequest
: public NavigationURLLoaderDelegate
{
34 // Keeps track of the various stages of a NavigationRequest.
35 enum NavigationState
{
39 // Waiting for a BeginNavigation IPC from the renderer in a
40 // browser-initiated navigation. If there is no live renderer when the
41 // request is created, this stage is skipped.
42 WAITING_FOR_RENDERER_RESPONSE
,
44 // The request was sent to the IO thread.
47 // The response started on the IO thread and is ready to be committed. This
48 // is one of the two final states for the request.
51 // The request failed on the IO thread and an error page should be
52 // displayed. This is one of the two final states for the request.
56 // Helper function to determine if the navigation request to |url| should be
57 // sent to the network stack.
58 static bool ShouldMakeNetworkRequest(const GURL
& url
);
60 // Creates a request for a browser-intiated navigation.
61 static scoped_ptr
<NavigationRequest
> CreateBrowserInitiated(
62 FrameTreeNode
* frame_tree_node
,
63 const NavigationEntryImpl
& entry
,
64 FrameMsg_Navigate_Type::Value navigation_type
,
65 base::TimeTicks navigation_start
,
66 NavigationControllerImpl
* controller
);
68 // Creates a request for a renderer-intiated navigation.
69 // Note: |body| is sent to the IO thread when calling BeginNavigation, and
70 // should no longer be manipulated afterwards on the UI thread.
71 static scoped_ptr
<NavigationRequest
> CreateRendererInitiated(
72 FrameTreeNode
* frame_tree_node
,
73 const CommonNavigationParams
& common_params
,
74 const BeginNavigationParams
& begin_params
,
75 scoped_refptr
<ResourceRequestBody
> body
,
76 int current_history_list_offset
,
77 int current_history_list_length
);
79 ~NavigationRequest() override
;
81 // Called on the UI thread by the Navigator to start the navigation. Returns
82 // whether a request was made on the IO thread.
83 // TODO(clamy): see if ResourceRequestBody could be un-refcounted to avoid
84 // threading subtleties.
85 bool BeginNavigation();
87 const CommonNavigationParams
& common_params() const { return common_params_
; }
89 const BeginNavigationParams
& begin_params() const { return begin_params_
; }
91 const RequestNavigationParams
& request_params() const {
92 return request_params_
;
95 NavigationURLLoader
* loader_for_testing() const { return loader_
.get(); }
97 NavigationState
state() const { return state_
; }
99 SiteInstanceImpl
* source_site_instance() const {
100 return source_site_instance_
.get();
103 SiteInstanceImpl
* dest_site_instance() const {
104 return dest_site_instance_
.get();
107 NavigationEntryImpl::RestoreType
restore_type() const {
108 return restore_type_
;
111 bool is_view_source() const { return is_view_source_
; };
113 int bindings() const { return bindings_
; };
115 bool browser_initiated() const { return browser_initiated_
; }
117 void SetWaitingForRendererResponse() {
118 DCHECK(state_
== NOT_STARTED
);
119 state_
= WAITING_FOR_RENDERER_RESPONSE
;
123 NavigationRequest(FrameTreeNode
* frame_tree_node
,
124 const CommonNavigationParams
& common_params
,
125 const BeginNavigationParams
& begin_params
,
126 const RequestNavigationParams
& request_params
,
127 scoped_refptr
<ResourceRequestBody
> body
,
128 bool browser_initiated
,
129 const NavigationEntryImpl
* navitation_entry
);
131 // NavigationURLLoaderDelegate implementation.
132 void OnRequestRedirected(
133 const net::RedirectInfo
& redirect_info
,
134 const scoped_refptr
<ResourceResponse
>& response
) override
;
135 void OnResponseStarted(const scoped_refptr
<ResourceResponse
>& response
,
136 scoped_ptr
<StreamHandle
> body
) override
;
137 void OnRequestFailed(int net_error
) override
;
138 void OnRequestStarted(base::TimeTicks timestamp
) override
;
140 FrameTreeNode
* frame_tree_node_
;
142 // Initialized on creation of the NavigationRequest. Sent to the renderer when
143 // the navigation is ready to commit.
144 // Note: When the navigation is ready to commit, the url in |common_params|
145 // will be set to the final navigation url, obtained after following all
147 CommonNavigationParams common_params_
;
148 const BeginNavigationParams begin_params_
;
149 const RequestNavigationParams request_params_
;
150 const bool browser_initiated_
;
152 NavigationState state_
;
155 // The parameters to send to the IO thread. |loader_| takes ownership of
156 // |info_| after calling BeginNavigation.
157 scoped_ptr
<NavigationRequestInfo
> info_
;
159 scoped_ptr
<NavigationURLLoader
> loader_
;
161 // These next items are used in browser-initiated navigations to store
162 // information from the NavigationEntryImpl that is required after request
164 scoped_refptr
<SiteInstanceImpl
> source_site_instance_
;
165 scoped_refptr
<SiteInstanceImpl
> dest_site_instance_
;
166 NavigationEntryImpl::RestoreType restore_type_
;
167 bool is_view_source_
;
170 DISALLOW_COPY_AND_ASSIGN(NavigationRequest
);
173 } // namespace content
175 #endif // CONTENT_BROWSER_FRAME_HOST_NAVIGATION_REQUEST_H_