Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / frame_host / frame_tree_node.h
blob2d033a65db6f56f0f7ad88b946186f2f8457c79c
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_
8 #include <string>
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"
18 #include "url/gurl.h"
19 #include "url/origin.h"
21 namespace content {
23 class FrameTree;
24 class NavigationRequest;
25 class Navigator;
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 {
33 public:
34 class Observer {
35 public:
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,
49 Navigator* navigator,
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);
58 ~FrameTreeNode();
60 void AddObserver(Observer* observer);
61 void RemoveObserver(Observer* observer);
63 bool IsMainFrame() const;
65 void AddChild(scoped_ptr<FrameTreeNode> child,
66 int process_id,
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 {
74 return frame_tree_;
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
103 // destroyed.
104 void SetOpener(FrameTreeNode* opener);
106 FrameTreeNode* child_at(size_t index) const {
107 return children_[index];
110 const GURL& current_url() const {
111 return current_url_;
114 // Sets the last committed URL for this frame and updates
115 // has_committed_real_load accordingly.
116 void SetCurrentURL(const GURL& url);
118 // Returns true iff SetCurrentURL has been called with a non-blank URL.
119 bool has_committed_real_load() const {
120 return has_committed_real_load_;
123 // Set the current origin and notify proxies about the update.
124 void SetCurrentOrigin(const url::Origin& origin);
126 // Set the current name and notify proxies about the update.
127 void SetFrameName(const std::string& name);
129 blink::WebSandboxFlags effective_sandbox_flags() {
130 return effective_sandbox_flags_;
133 void set_sandbox_flags(blink::WebSandboxFlags sandbox_flags) {
134 replication_state_.sandbox_flags = sandbox_flags;
137 // Transfer any pending sandbox flags into |effective_sandbox_flags_|, and
138 // return true if the sandbox flags were changed.
139 bool CommitPendingSandboxFlags();
141 bool HasSameOrigin(const FrameTreeNode& node) const {
142 return replication_state_.origin.IsSameOriginWith(
143 node.replication_state_.origin);
146 const FrameReplicationState& current_replication_state() const {
147 return replication_state_;
150 RenderFrameHostImpl* current_frame_host() const {
151 return render_manager_.current_frame_host();
154 bool IsDescendantOf(FrameTreeNode* other) const;
156 // Return the node immediately preceding this node in its parent's
157 // |children_|, or nullptr if there is no such node.
158 FrameTreeNode* PreviousSibling() const;
160 // Returns true if this node is in a loading state.
161 bool IsLoading() const;
163 // Returns this node's loading progress.
164 double loading_progress() const { return loading_progress_; }
166 NavigationRequest* navigation_request() { return navigation_request_.get(); }
168 // PlzNavigate
169 // Takes ownership of |navigation_request| and makes it the current
170 // NavigationRequest of this frame. This corresponds to the start of a new
171 // navigation. If there was an ongoing navigation request before calling this
172 // function, it is canceled. |navigation_request| should not be null.
173 void CreatedNavigationRequest(
174 scoped_ptr<NavigationRequest> navigation_request);
176 // PlzNavigate
177 // Resets the current navigation request. |is_commit| is true if the reset is
178 // due to the commit of the navigation.
179 void ResetNavigationRequest(bool is_commit);
181 // Returns true if this node is in a state where the loading progress is being
182 // tracked.
183 bool has_started_loading() const;
185 // Resets this node's loading progress.
186 void reset_loading_progress();
188 // A RenderFrameHost in this node started loading.
189 // |to_different_document| will be true unless the load is a fragment
190 // navigation, or triggered by history.pushState/replaceState.
191 void DidStartLoading(bool to_different_document);
193 // A RenderFrameHost in this node stopped loading.
194 void DidStopLoading();
196 // The load progress for a RenderFrameHost in this node was updated to
197 // |load_progress|. This will notify the FrameTree which will in turn notify
198 // the WebContents.
199 void DidChangeLoadProgress(double load_progress);
201 // Called when the user directed the page to stop loading. Stops all loads
202 // happening in the FrameTreeNode. This method should be used with
203 // FrameTree::ForEach to stop all loads in the entire FrameTree.
204 bool StopLoading();
206 // Returns the time this frame was last focused.
207 base::TimeTicks last_focus_time() const { return last_focus_time_; }
208 void set_last_focus_time(const base::TimeTicks& last_focus_time) {
209 last_focus_time_ = last_focus_time;
212 private:
213 class OpenerDestroyedObserver;
215 void set_parent(FrameTreeNode* parent) { parent_ = parent; }
217 // The next available browser-global FrameTreeNode ID.
218 static int next_frame_tree_node_id_;
220 // The FrameTree that owns us.
221 FrameTree* frame_tree_; // not owned.
223 // The Navigator object responsible for managing navigations at this node
224 // of the frame tree.
225 scoped_refptr<Navigator> navigator_;
227 // Manages creation and swapping of RenderFrameHosts for this frame. This
228 // must be declared before |children_| so that it gets deleted after them.
229 // That's currently necessary so that RenderFrameHostImpl's destructor can
230 // call GetProcess.
231 RenderFrameHostManager render_manager_;
233 // A browser-global identifier for the frame in the page, which stays stable
234 // even if the frame does a cross-process navigation.
235 const int frame_tree_node_id_;
237 // The parent node of this frame. NULL if this node is the root or if it has
238 // not yet been attached to the frame tree.
239 FrameTreeNode* parent_;
241 // The frame that opened this frame, if any. Will be set to null if the
242 // opener is closed, or if this frame disowns its opener by setting its
243 // window.opener to null.
244 FrameTreeNode* opener_;
246 // An observer that clears this node's |opener_| if the opener is destroyed.
247 // This observer is added to the |opener_|'s observer list when the |opener_|
248 // is set to a non-null node, and it is removed from that list when |opener_|
249 // changes or when this node is destroyed. It is also cleared if |opener_|
250 // is disowned.
251 scoped_ptr<OpenerDestroyedObserver> opener_observer_;
253 // The immediate children of this specific frame.
254 ScopedVector<FrameTreeNode> children_;
256 // Track the current frame's last committed URL.
257 // TODO(creis): Consider storing a reference to the last committed
258 // FrameNavigationEntry here once those are created in all modes.
259 GURL current_url_;
261 // Whether this frame has committed any real load, replacing its initial
262 // about:blank page.
263 bool has_committed_real_load_;
265 // Track information that needs to be replicated to processes that have
266 // proxies for this frame.
267 FrameReplicationState replication_state_;
269 // Track the effective sandbox flags for this frame. When a parent frame
270 // dynamically updates sandbox flags for a child frame, the child's updated
271 // sandbox flags are stored in replication_state_.sandbox_flags. However, the
272 // update only takes effect on the next frame navigation, so the effective
273 // sandbox flags are tracked separately here. When enforcing sandbox flags
274 // directives in the browser process, |effective_sandbox_flags_| should be
275 // used. |effective_sandbox_flags_| is updated with any pending sandbox
276 // flags when a navigation for this frame commits.
277 blink::WebSandboxFlags effective_sandbox_flags_;
279 // Used to track this node's loading progress (from 0 to 1).
280 double loading_progress_;
282 // PlzNavigate
283 // Owns an ongoing NavigationRequest until it is ready to commit. It will then
284 // be reset and a RenderFrameHost will be responsible for the navigation.
285 scoped_ptr<NavigationRequest> navigation_request_;
287 // List of objects observing this FrameTreeNode.
288 base::ObserverList<Observer> observers_;
290 base::TimeTicks last_focus_time_;
292 DISALLOW_COPY_AND_ASSIGN(FrameTreeNode);
295 } // namespace content
297 #endif // CONTENT_BROWSER_FRAME_HOST_FRAME_TREE_NODE_H_