frame_tree.h revision f2477e01787aa58f445919b809d89e252beef54f
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_H_
6#define CONTENT_BROWSER_FRAME_HOST_FRAME_TREE_H_
7
8#include <string>
9
10#include "base/callback.h"
11#include "base/memory/scoped_ptr.h"
12#include "content/browser/frame_host/frame_tree_node.h"
13#include "content/common/content_export.h"
14
15namespace content {
16
17class FrameTreeNode;
18class Navigator;
19class RenderProcessHost;
20class RenderViewHostDelegate;
21class RenderViewHostImpl;
22class RenderFrameHostManager;
23class RenderWidgetHostDelegate;
24
25// Represents the frame tree for a page. With the exception of the main frame,
26// all FrameTreeNodes will be created/deleted in response to frame attach and
27// detach events in the DOM.
28//
29// The main frame's FrameTreeNode is special in that it is reused. This allows
30// it to serve as an anchor for state that needs to persist across top-level
31// page navigations.
32//
33// TODO(ajwong): Move NavigationController ownership to the main frame
34// FrameTreeNode. Possibly expose access to it from here.
35//
36// TODO(ajwong): Currently this class only contains FrameTreeNodes for
37// subframes if the --site-per-process flag is enabled.
38//
39// This object is only used on the UI thread.
40class CONTENT_EXPORT FrameTree {
41 public:
42  // Each FrameTreeNode will default to using the given |navigator| for
43  // navigation tasks in the frame.
44  // A set of delegates are remembered here so that we can create
45  // RenderFrameHostManagers.
46  // TODO(creis): This set of delegates will change as we move things to
47  // Navigator.
48  FrameTree(Navigator* navigator,
49            RenderViewHostDelegate* render_view_delegate,
50            RenderWidgetHostDelegate* render_widget_delegate,
51            RenderFrameHostManager::Delegate* manager_delegate);
52  ~FrameTree();
53
54  // Returns the FrameTreeNode with the given |frame_tree_node_id|.
55  FrameTreeNode* FindByID(int64 frame_tree_node_id);
56
57  // Executes |on_node| on each node in the frame tree.  If |on_node| returns
58  // false, terminates the iteration immediately. Returning false is useful
59  // if |on_node| is just doing a search over the tree.
60  void ForEach(const base::Callback<bool(FrameTreeNode*)>& on_node) const;
61
62  // After the FrameTree is created, or after SwapMainFrame() has been called,
63  // the root node does not yet have a frame id. This is allocated by the
64  // renderer and is published to the browser process on the first navigation
65  // after a swap. These two functions are used to set the root node's frame
66  // id.
67  //
68  // TODO(ajwong): Remove these once RenderFrameHost's routing id replaces
69  // frame_id.
70  bool IsFirstNavigationAfterSwap() const;
71  void OnFirstNavigationAfterSwap(int main_frame_id);
72
73  // Frame tree manipulation routines.
74  // TODO(creis): These should take in RenderFrameHost routing IDs.
75  void AddFrame(int render_frame_host_id,
76                int64 parent_frame_tree_node_id,
77                int64 frame_id,
78                const std::string& frame_name);
79  void RemoveFrame(int64 parent_frame_id, int64 frame_id);
80  void SetFrameUrl(int64 frame_id, const GURL& url);
81
82  // Resets the FrameTree and changes RenderFrameHost for the main frame.
83  // This destroys most of the frame tree but retains the root node so that
84  // navigation state may be kept on it between process swaps. Used to
85  // support bookkeeping for top-level navigations.
86  //
87  // If |main_frame| is NULL, reset tree to initially constructed state.
88  //
89  // TODO(ajwong): This function should not be given a |main_frame|. This is
90  // required currently because the RenderViewHost owns its main frame. When
91  // that relation is fixed, the FrameTree should be responsible for
92  // created/destroying the main frame on the swap.
93  void SwapMainFrame(RenderFrameHostImpl* main_frame);
94
95  // Convenience accessor for the main frame's RenderFrameHostImpl.
96  RenderFrameHostImpl* GetMainFrame() const;
97
98  // Allows a client to listen for frame removal.  The listener should expect
99  // to receive the RenderViewHostImpl containing the frame and the renderer-
100  // specific frame ID of the removed frame.
101  // TODO(creis): These parameters will later change to be the RenderFrameHost.
102  void SetFrameRemoveListener(
103      const base::Callback<void(RenderViewHostImpl*, int64)>& on_frame_removed);
104
105  FrameTreeNode* root() const { return root_.get(); }
106
107 private:
108  // Returns the FrameTreeNode with the given renderer-specific |frame_id|.
109  // For internal use only.
110  // TODO(creis): Replace this with a version that takes in a routing ID.
111  FrameTreeNode* FindByFrameID(int64 frame_id);
112
113  scoped_ptr<FrameTreeNode> CreateNode(int64 frame_id,
114                                       const std::string& frame_name,
115                                       int render_frame_host_id,
116                                       Navigator* navigator,
117                                       RenderProcessHost* render_process_host);
118
119  // These delegates are installed into all the RenderViewHosts and
120  // RenderFrameHosts that we create.
121  RenderViewHostDelegate* render_view_delegate_;
122  RenderWidgetHostDelegate* render_widget_delegate_;
123  RenderFrameHostManager::Delegate* manager_delegate_;
124
125  scoped_ptr<FrameTreeNode> root_;
126
127  base::Callback<void(RenderViewHostImpl*, int64)> on_frame_removed_;
128
129  DISALLOW_COPY_AND_ASSIGN(FrameTree);
130};
131
132}  // namespace content
133
134#endif  // CONTENT_BROWSER_FRAME_HOST_FRAME_TREE_H_
135