frame_tree_node.h revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
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.
4
5#ifndef CONTENT_BROWSER_FRAME_HOST_FRAME_TREE_NODE_H_
6#define CONTENT_BROWSER_FRAME_HOST_FRAME_TREE_NODE_H_
7
8#include <string>
9
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 "url/gurl.h"
18
19namespace content {
20
21class Navigator;
22class RenderFrameHostImpl;
23
24// When a page contains iframes, its renderer process maintains a tree structure
25// of those frames. We are mirroring this tree in the browser process. This
26// class represents a node in this tree and is a wrapper for all objects that
27// are frame-specific (as opposed to page-specific).
28class CONTENT_EXPORT FrameTreeNode {
29 public:
30  static const int64 kInvalidFrameId;
31
32  FrameTreeNode(Navigator* navigator,
33                RenderFrameHostDelegate* render_frame_delegate,
34                RenderViewHostDelegate* render_view_delegate,
35                RenderWidgetHostDelegate* render_widget_delegate,
36                RenderFrameHostManager::Delegate* manager_delegate,
37                int64 frame_id,
38                const std::string& name);
39
40  ~FrameTreeNode();
41
42  void AddChild(scoped_ptr<FrameTreeNode> child);
43  void RemoveChild(FrameTreeNode* child);
44
45  // TODO(nasko): This method should be removed once RenderFrameHosts are
46  // created by RenderFrameHostManager.
47  void set_render_frame_host(
48      RenderFrameHostImpl* render_frame_host,
49      bool owns_render_frame_host) {
50    render_frame_host_ = render_frame_host;
51    owns_render_frame_host_ = owns_render_frame_host;
52  }
53
54  // Transitional API allowing the RenderFrameHost of a FrameTreeNode
55  // representing the main frame to be provided by someone else. After
56  // this is called, the FrameTreeNode no longer owns its RenderFrameHost.
57  //
58  // This should only be used for the main frame (aka root) in a frame tree.
59  //
60  // TODO(ajwong): Remove this method once the main frame RenderFrameHostImpl is
61  // no longer owned by the RenderViewHostImpl.
62  void ResetForMainFrame(RenderFrameHostImpl* new_render_frame_host);
63
64  Navigator* navigator() {
65    return navigator_.get();
66  }
67
68  RenderFrameHostManager* render_manager() {
69    return &render_manager_;
70  }
71
72  int64 frame_tree_node_id() const {
73    return frame_tree_node_id_;
74  }
75
76  // DO NOT USE.  Only used by FrameTree until we replace renderer-specific
77  // frame IDs with RenderFrameHost routing IDs.
78  void set_frame_id(int64 frame_id) {
79    DCHECK_EQ(frame_id_, kInvalidFrameId);
80    frame_id_ = frame_id;
81  }
82  int64 frame_id() const {
83    return frame_id_;
84  }
85
86  const std::string& frame_name() const {
87    return frame_name_;
88  }
89
90  size_t child_count() const {
91    return children_.size();
92  }
93
94  FrameTreeNode* child_at(size_t index) const {
95    return children_[index];
96  }
97
98  const GURL& current_url() const {
99    return current_url_;
100  }
101
102  void set_current_url(const GURL& url) {
103    current_url_ = url;
104  }
105
106  RenderFrameHostImpl* render_frame_host() const {
107    return render_frame_host_;
108  }
109
110 private:
111  // The next available browser-global FrameTreeNode ID.
112  static int64 next_frame_tree_node_id_;
113
114  // The Navigator object responsible for managing navigations at this node
115  // of the frame tree.
116  scoped_refptr<Navigator> navigator_;
117
118  // Manages creation and swapping of RenderViewHosts for this frame.  This must
119  // be declared before |children_| so that it gets deleted after them.  That's
120  // currently necessary so that RenderFrameHostImpl's destructor can call
121  // GetProcess.
122  // TODO(creis): This will eliminate the need for |render_frame_host_| below.
123  RenderFrameHostManager render_manager_;
124
125  // A browser-global identifier for the frame in the page, which stays stable
126  // even if the frame does a cross-process navigation.
127  const int64 frame_tree_node_id_;
128
129  // The renderer-specific identifier for the frame in the page.
130  // TODO(creis): Remove this in favor of the RenderFrameHost's routing ID once
131  // we create FrameTreeNodes for all frames (even without a flag), since this
132  // value can change after cross-process navigations.
133  int64 frame_id_;
134
135  // The assigned name of the frame. This name can be empty, unlike the unique
136  // name generated internally in the DOM tree.
137  std::string frame_name_;
138
139  // The immediate children of this specific frame.
140  ScopedVector<FrameTreeNode> children_;
141
142  // When ResetForMainFrame() is called, this is set to false and the
143  // |render_frame_host_| below is not deleted on destruction.
144  //
145  // For the mainframe, the FrameTree does not own the |render_frame_host_|.
146  // This is a transitional wart because RenderFrameHostManager does not yet
147  // have the bookkeeping logic to handle creating a pending RenderFrameHost
148  // along with a pending RenderViewHost. Thus, for the main frame, the
149  // RenderViewHost currently retains ownership and the FrameTreeNode should
150  // not delete it on destruction.
151  bool owns_render_frame_host_;
152
153  // The active RenderFrameHost for this frame. The FrameTreeNode does not
154  // always own this pointer.  See comments above |owns_render_frame_host_|.
155  // TODO(ajwong): Replace with RenderFrameHostManager.
156  RenderFrameHostImpl* render_frame_host_;
157
158  // Track the current frame's last committed URL, so we can estimate the
159  // process impact of out-of-process iframes.
160  // TODO(creis): Remove this when we can store subframe URLs in the
161  // NavigationController.
162  GURL current_url_;
163
164  DISALLOW_COPY_AND_ASSIGN(FrameTreeNode);
165};
166
167}  // namespace content
168
169#endif  // CONTENT_BROWSER_FRAME_HOST_FRAME_TREE_NODE_H_
170