bd1f19aa25b879c45213ec4de77fc2e0abd65c45
[platform/framework/web/crosswalk.git] / src / content / browser / frame_host / frame_tree.h
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
15 namespace content {
16
17 class FrameTreeNode;
18 class Navigator;
19 class RenderFrameHostDelegate;
20 class RenderProcessHost;
21 class RenderViewHostDelegate;
22 class RenderViewHostImpl;
23 class RenderFrameHostManager;
24 class 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 // This object is only used on the UI thread.
38 class CONTENT_EXPORT FrameTree {
39  public:
40   // Each FrameTreeNode will default to using the given |navigator| for
41   // navigation tasks in the frame.
42   // A set of delegates are remembered here so that we can create
43   // RenderFrameHostManagers.
44   // TODO(creis): This set of delegates will change as we move things to
45   // Navigator.
46   FrameTree(Navigator* navigator,
47             RenderFrameHostDelegate* render_frame_delegate,
48             RenderViewHostDelegate* render_view_delegate,
49             RenderWidgetHostDelegate* render_widget_delegate,
50             RenderFrameHostManager::Delegate* manager_delegate);
51   ~FrameTree();
52
53   // Returns the FrameTreeNode with the given |frame_tree_node_id|.
54   static FrameTreeNode* GloballyFindByID(int64 frame_tree_node_id);
55
56   FrameTreeNode* root() const { return root_.get(); }
57
58   // Returns the FrameTreeNode with the given |frame_tree_node_id| if it is part
59   // of this FrameTree.
60   FrameTreeNode* FindByID(int64 frame_tree_node_id);
61
62   // Returns the FrameTreeNode with the given renderer-specific |routing_id|.
63   FrameTreeNode* FindByRoutingID(int routing_id, int process_id);
64
65   // Executes |on_node| on each node in the frame tree.  If |on_node| returns
66   // false, terminates the iteration immediately. Returning false is useful
67   // if |on_node| is just doing a search over the tree.  The iteration proceeds
68   // top-down and visits a node before adding its children to the queue, making
69   // it safe to remove children during the callback.
70   void ForEach(const base::Callback<bool(FrameTreeNode*)>& on_node) const;
71
72   // Frame tree manipulation routines.
73   RenderFrameHostImpl* AddFrame(FrameTreeNode* parent,
74                                 int new_routing_id,
75                                 const std::string& frame_name);
76   void RemoveFrame(FrameTreeNode* child);
77
78   // This method walks the entire frame tree and creates a RenderFrameProxyHost
79   // for the given |site_instance| in each node except the |source| one --
80   // the source will have a RenderFrameHost. It assumes that no frame tree
81   // nodes already have RenderFrameProxyHost for the given |site_instance|.
82   void CreateProxiesForSiteInstance(
83       FrameTreeNode* source,
84       SiteInstance* site_instance);
85
86   // Clears process specific-state after a main frame process swap.
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   // TODO(creis): Look into how we can remove the need for this method.
91   void ResetForMainFrameSwap();
92
93   // Update the frame tree after a process exits.  Any nodes currently using the
94   // given |render_view_host| will lose all their children.
95   // TODO(creis): This should take a RenderProcessHost once RenderFrameHost
96   // knows its process.  Until then, we would just be asking the RenderViewHost
97   // for its process, so we'll skip that step.
98   void RenderProcessGone(RenderViewHost* render_view_host);
99
100   // Convenience accessor for the main frame's RenderFrameHostImpl.
101   RenderFrameHostImpl* GetMainFrame() const;
102
103   // Returns the focused frame.
104   FrameTreeNode* GetFocusedFrame();
105
106   // Sets the focused frame.
107   void SetFocusedFrame(FrameTreeNode* node);
108
109   // Allows a client to listen for frame removal.  The listener should expect
110   // to receive the RenderViewHostImpl containing the frame and the renderer-
111   // specific frame routing ID of the removed frame.
112   void SetFrameRemoveListener(
113       const base::Callback<void(RenderFrameHost*)>& on_frame_removed);
114
115   // Creates a RenderViewHost for a new RenderFrameHost in the given
116   // |site_instance|.  The RenderViewHost will have its Shutdown method called
117   // when all of the RenderFrameHosts using it are deleted.
118   RenderViewHostImpl* CreateRenderViewHost(SiteInstance* site_instance,
119                                            int routing_id,
120                                            int main_frame_routing_id,
121                                            bool swapped_out,
122                                            bool hidden);
123
124   // Returns the existing RenderViewHost for a new RenderFrameHost.
125   // There should always be such a RenderViewHost, because the main frame
126   // RenderFrameHost for each SiteInstance should be created before subframes.
127   RenderViewHostImpl* GetRenderViewHost(SiteInstance* site_instance);
128
129   // Keeps track of which RenderFrameHosts are using each RenderViewHost.  When
130   // the number drops to zero, we call Shutdown on the RenderViewHost.
131   void RegisterRenderFrameHost(RenderFrameHostImpl* render_frame_host);
132   void UnregisterRenderFrameHost(RenderFrameHostImpl* render_frame_host);
133
134  private:
135   typedef base::hash_map<int, RenderViewHostImpl*> RenderViewHostMap;
136   typedef std::multimap<int, RenderViewHostImpl*> RenderViewHostMultiMap;
137
138   // These delegates are installed into all the RenderViewHosts and
139   // RenderFrameHosts that we create.
140   RenderFrameHostDelegate* render_frame_delegate_;
141   RenderViewHostDelegate* render_view_delegate_;
142   RenderWidgetHostDelegate* render_widget_delegate_;
143   RenderFrameHostManager::Delegate* manager_delegate_;
144
145   // Map of SiteInstance ID to a RenderViewHost.  This allows us to look up the
146   // RenderViewHost for a given SiteInstance when creating RenderFrameHosts.
147   // Combined with the refcount on RenderViewHost, this allows us to call
148   // Shutdown on the RenderViewHost and remove it from the map when no more
149   // RenderFrameHosts are using it.
150   //
151   // Must be declared before |root_| so that it is deleted afterward.  Otherwise
152   // the map will be cleared before we delete the RenderFrameHosts in the tree.
153   RenderViewHostMap render_view_host_map_;
154
155   // Map of SiteInstance ID to RenderViewHosts that are pending shutdown. The
156   // renderers of these RVH are currently executing the unload event in
157   // background. When the SwapOutACK is received, they will be deleted. In the
158   // meantime, they are kept in this map, as they should not be reused (part of
159   // their state is already gone away).
160   RenderViewHostMultiMap render_view_host_pending_shutdown_map_;
161
162   scoped_ptr<FrameTreeNode> root_;
163
164   int64 focused_frame_tree_node_id_;
165
166   base::Callback<void(RenderFrameHost*)> on_frame_removed_;
167
168   DISALLOW_COPY_AND_ASSIGN(FrameTree);
169 };
170
171 }  // namespace content
172
173 #endif  // CONTENT_BROWSER_FRAME_HOST_FRAME_TREE_H_