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