1 // Copyright 2013 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_FRAME_TREE_NODE_H_
6 #define CONTENT_BROWSER_FRAME_HOST_FRAME_TREE_NODE_H_
10 #include "base/basictypes.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/scoped_vector.h"
14 #include "content/browser/frame_host/render_frame_host_impl.h"
15 #include "content/browser/frame_host/render_frame_host_manager.h"
16 #include "content/common/content_export.h"
17 #include "content/common/frame_replication_state.h"
19 #include "url/origin.h"
24 class NavigationRequest
;
26 class RenderFrameHostImpl
;
28 // When a page contains iframes, its renderer process maintains a tree structure
29 // of those frames. We are mirroring this tree in the browser process. This
30 // class represents a node in this tree and is a wrapper for all objects that
31 // are frame-specific (as opposed to page-specific).
32 class CONTENT_EXPORT FrameTreeNode
{
36 // Invoked when a FrameTreeNode is being destroyed.
37 virtual void OnFrameTreeNodeDestroyed(FrameTreeNode
* node
) {}
39 virtual ~Observer() {}
42 static const int kFrameTreeNodeInvalidID
= -1;
44 // Returns the FrameTreeNode with the given global |frame_tree_node_id|,
45 // regardless of which FrameTree it is in.
46 static FrameTreeNode
* GloballyFindByID(int frame_tree_node_id
);
48 FrameTreeNode(FrameTree
* frame_tree
,
50 RenderFrameHostDelegate
* render_frame_delegate
,
51 RenderViewHostDelegate
* render_view_delegate
,
52 RenderWidgetHostDelegate
* render_widget_delegate
,
53 RenderFrameHostManager::Delegate
* manager_delegate
,
54 blink::WebTreeScopeType scope
,
55 const std::string
& name
,
56 blink::WebSandboxFlags sandbox_flags
);
60 void AddObserver(Observer
* observer
);
61 void RemoveObserver(Observer
* observer
);
63 bool IsMainFrame() const;
65 void AddChild(scoped_ptr
<FrameTreeNode
> child
,
67 int frame_routing_id
);
68 void RemoveChild(FrameTreeNode
* child
);
70 // Clears process specific-state in this node to prepare for a new process.
71 void ResetForNewProcess();
73 FrameTree
* frame_tree() const {
77 Navigator
* navigator() {
78 return navigator_
.get();
81 RenderFrameHostManager
* render_manager() {
82 return &render_manager_
;
85 int frame_tree_node_id() const {
86 return frame_tree_node_id_
;
89 const std::string
& frame_name() const {
90 return replication_state_
.name
;
93 size_t child_count() const {
94 return children_
.size();
97 FrameTreeNode
* parent() const { return parent_
; }
99 FrameTreeNode
* opener() const { return opener_
; }
101 // Assigns a new opener for this node and, if |opener| is non-null, registers
102 // an observer that will clear this node's opener if |opener| is ever
104 void SetOpener(FrameTreeNode
* opener
);
106 FrameTreeNode
* child_at(size_t index
) const {
107 return children_
[index
];
110 const GURL
& current_url() const {
114 void set_current_url(const GURL
& url
) {
118 // Set the current origin and notify proxies about the update.
119 void SetCurrentOrigin(const url::Origin
& origin
);
121 // Set the current name and notify proxies about the update.
122 void SetFrameName(const std::string
& name
);
124 blink::WebSandboxFlags
effective_sandbox_flags() {
125 return effective_sandbox_flags_
;
128 void set_sandbox_flags(blink::WebSandboxFlags sandbox_flags
) {
129 replication_state_
.sandbox_flags
= sandbox_flags
;
132 // Transfer any pending sandbox flags into |effective_sandbox_flags_|, and
133 // return true if the sandbox flags were changed.
134 bool CommitPendingSandboxFlags();
136 bool HasSameOrigin(const FrameTreeNode
& node
) const {
137 return replication_state_
.origin
.IsSameAs(node
.replication_state_
.origin
);
140 const FrameReplicationState
& current_replication_state() const {
141 return replication_state_
;
144 RenderFrameHostImpl
* current_frame_host() const {
145 return render_manager_
.current_frame_host();
148 bool IsDescendantOf(FrameTreeNode
* other
) const;
150 // Return the node immediately preceding this node in its parent's
151 // |children_|, or nullptr if there is no such node.
152 FrameTreeNode
* PreviousSibling() const;
154 // Returns true if this node is in a loading state.
155 bool IsLoading() const;
157 // Returns this node's loading progress.
158 double loading_progress() const { return loading_progress_
; }
160 NavigationRequest
* navigation_request() { return navigation_request_
.get(); }
163 // Takes ownership of |navigation_request| and makes it the current
164 // NavigationRequest of this frame. This corresponds to the start of a new
165 // navigation. If there was an ongoing navigation request before calling this
166 // function, it is canceled. |navigation_request| should not be null.
167 void CreatedNavigationRequest(
168 scoped_ptr
<NavigationRequest
> navigation_request
);
171 // Resets the current navigation request. |is_commit| is true if the reset is
172 // due to the commit of the navigation.
173 void ResetNavigationRequest(bool is_commit
);
175 // Returns true if this node is in a state where the loading progress is being
177 bool has_started_loading() const;
179 // Resets this node's loading progress.
180 void reset_loading_progress();
182 // A RenderFrameHost in this node started loading.
183 // |to_different_document| will be true unless the load is a fragment
184 // navigation, or triggered by history.pushState/replaceState.
185 void DidStartLoading(bool to_different_document
);
187 // A RenderFrameHost in this node stopped loading.
188 void DidStopLoading();
190 // The load progress for a RenderFrameHost in this node was updated to
191 // |load_progress|. This will notify the FrameTree which will in turn notify
193 void DidChangeLoadProgress(double load_progress
);
196 class OpenerDestroyedObserver
;
198 void set_parent(FrameTreeNode
* parent
) { parent_
= parent
; }
200 // The next available browser-global FrameTreeNode ID.
201 static int next_frame_tree_node_id_
;
203 // The FrameTree that owns us.
204 FrameTree
* frame_tree_
; // not owned.
206 // The Navigator object responsible for managing navigations at this node
207 // of the frame tree.
208 scoped_refptr
<Navigator
> navigator_
;
210 // Manages creation and swapping of RenderFrameHosts for this frame. This
211 // must be declared before |children_| so that it gets deleted after them.
212 // That's currently necessary so that RenderFrameHostImpl's destructor can
214 RenderFrameHostManager render_manager_
;
216 // A browser-global identifier for the frame in the page, which stays stable
217 // even if the frame does a cross-process navigation.
218 const int frame_tree_node_id_
;
220 // The parent node of this frame. NULL if this node is the root or if it has
221 // not yet been attached to the frame tree.
222 FrameTreeNode
* parent_
;
224 // The frame that opened this frame, if any. Will be set to null if the
225 // opener is closed, or if this frame disowns its opener by setting its
226 // window.opener to null.
227 FrameTreeNode
* opener_
;
229 // An observer that clears this node's |opener_| if the opener is destroyed.
230 // This observer is added to the |opener_|'s observer list when the |opener_|
231 // is set to a non-null node, and it is removed from that list when |opener_|
232 // changes or when this node is destroyed. It is also cleared if |opener_|
234 scoped_ptr
<OpenerDestroyedObserver
> opener_observer_
;
236 // The immediate children of this specific frame.
237 ScopedVector
<FrameTreeNode
> children_
;
239 // Track the current frame's last committed URL, so we can estimate the
240 // process impact of out-of-process iframes.
241 // TODO(creis): Remove this when we can store subframe URLs in the
242 // NavigationController.
245 // Track information that needs to be replicated to processes that have
246 // proxies for this frame.
247 FrameReplicationState replication_state_
;
249 // Track the effective sandbox flags for this frame. When a parent frame
250 // dynamically updates sandbox flags for a child frame, the child's updated
251 // sandbox flags are stored in replication_state_.sandbox_flags. However, the
252 // update only takes effect on the next frame navigation, so the effective
253 // sandbox flags are tracked separately here. When enforcing sandbox flags
254 // directives in the browser process, |effective_sandbox_flags_| should be
255 // used. |effective_sandbox_flags_| is updated with any pending sandbox
256 // flags when a navigation for this frame commits.
257 blink::WebSandboxFlags effective_sandbox_flags_
;
259 // Used to track this node's loading progress (from 0 to 1).
260 double loading_progress_
;
263 // Owns an ongoing NavigationRequest until it is ready to commit. It will then
264 // be reset and a RenderFrameHost will be responsible for the navigation.
265 scoped_ptr
<NavigationRequest
> navigation_request_
;
267 // List of objects observing this FrameTreeNode.
268 base::ObserverList
<Observer
> observers_
;
270 DISALLOW_COPY_AND_ASSIGN(FrameTreeNode
);
273 } // namespace content
275 #endif // CONTENT_BROWSER_FRAME_HOST_FRAME_TREE_NODE_H_